-
Notifications
You must be signed in to change notification settings - Fork 1
/
Obj_Tracker.cpp
207 lines (181 loc) · 3.91 KB
/
Obj_Tracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;
using namespace cv;
char t1, t2='\0';
char window_name[30] = "HSV Segemtation";
Mat src;
Mat Frame;
Mat hsv2;
Mat imgthreshold;
char c='\0';
int count5=0;
int arr[10]={0,0,0,0,0,0,0,0,0,0};
ofstream fout; // /dev/ttyACM2
int flag=0;
Point2f centre(-1,-1);
void movement(int nx, int ny);
int main()
{
int maxH, minH;
int maxS, minS;
int maxV, minV;
int count = 0;
VideoCapture cam(0);
cam.set(CV_CAP_PROP_FRAME_WIDTH,200);
cam.set(CV_CAP_PROP_FRAME_HEIGHT,200);
char key;
minH = 0;
maxH = 25;
minS = 72;
maxS = 250;
minV = 180;
maxV = 255;
Mat imgTmp;
cam.read(imgTmp);
Mat imgCircles = Mat::zeros( imgTmp.size(), CV_8UC3 );
while(1)
{
char key = waitKey(33);
//if(key==27) return 0;
cam.read(src);
cvtColor(src,hsv2,CV_BGR2HSV);
inRange(hsv2, Scalar(minH, minS, minV), Scalar(maxH, maxS, maxV), imgthreshold);
medianBlur(imgthreshold, imgthreshold, 11);
vector<vector<Point> > contour;
vector<Vec4i> hierarchy;
vector<Point> approx;
findContours(imgthreshold, contour, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, Point(0,0));
if(contour.size())
{
double maxarea = contourArea(contour[0]);
double area;
int maxindex=0;
for(int i=1; i<contour.size(); i++)
{
area = contourArea(contour[i]);
if(area>maxarea)
{
maxarea = area;
maxindex = i;
}
}
approxPolyDP(Mat(contour[maxindex]), approx, arcLength(Mat(contour[maxindex]), true)*0.02, true );
if( fabs(contourArea(contour[maxindex])) <0.1 || !isContourConvex(approx))
{
flag = 0;
}
else if( approx.size() >7)
{
float radius;
minEnclosingCircle(contour[maxindex], centre, radius);
circle(src,centre,radius,Scalar(0,255,0),2);
flag = 1;
}
}
else
{
flag=0;
}
movement(centre.x, centre.y);
if(t1=='4')
arr[4]++;
else if(t1=='5')
arr[5]++;
else if(t1=='6')
arr[6]++;
else if(t1=='7')
arr[7]++;
else if(t1=='8')
arr[8]++;
if((count5 % 5)==0)
{
int most=0;
for(int j=5; j<9; j++)
{
if(t1=='0')
break;
else
{
most=0;
if(arr[j]>arr[most])
{
most=j;
}
}
}
switch(most)
{
case 4: t1='4';break;
case 5: t1='5';break;
case 6: t1='6';break;
case 7: t1='7';break;
case 8: t1='8';break;
}
for(int k=4; k<9; k++)
{
arr[k]=0;
}
for( int i=0; i<10; i++)
{
fout.open("/dev/ttyACM2");
cerr << t1 ;
fout<<t1;
fout.close();
}
}
count5++;
imshow("Original",src ); //show the original image
int c = waitKey(10);
if( (char)c == 27 ) { break; } // escape
}
cerr<<endl;
return 0;
}
void movement(int nx, int ny)
{
if(flag)
{
if( nx < src.cols/3)
{
t1 = '4';
}
else if( nx > (2*src.cols)/3)
{
t1 = '6';
}
else
{
if( ny > (3*src.rows)/4)
{
t1 = '0';
}
else
{
t1 = '5';
}
}
}
else
{
if( nx == -1 )
{
t1 = '8';
}
else if( nx < (src.cols)/5 || nx > (4*src.cols)/5)
{
t1 = '8';
}
else if( ny > (6*src.rows)/7 )
{
t1 = '0';
}
else
{
t1 = '7';
}
}
}