-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectTracking.cpp
More file actions
330 lines (253 loc) · 8.97 KB
/
Copy pathObjectTracking.cpp
File metadata and controls
330 lines (253 loc) · 8.97 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* ObjectTracking.cpp
*
* Created on: Mar 17, 2017
* Author: ash
*/
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv/cv.h"
using namespace cv;
using namespace std;
//These values will get changed with trackbars
int hMin4HSV = 0, sMin4HSV = 0, lMin4HSV = 0, hMax4HSV = 256, sMax4HSV = 256, lMax4HSV = 256;
int hMin4HSL = 0, sMin4HSL = 0, lMin4HSL = 0, hMax4HSL = 256, sMax4HSL = 256, lMax4HSL = 256;
int blurVal = 1;
int x = 0, y = 0;
int const MAX_HSV = 255;
int const MAX_HSL = 255;
int const MAX_BLUR = 31;
bool bothDisplays = false;
const string OG_IMAGE = "Original Image", HSL_IMAGE = "HSL", HSV_IMAGE = "HSV", THRESHOLD_HSL = "Threshold HSL", THRESHOLD_HSV = "Threshold HSV";
//HSV
void lowHSlider4HSV(int, void*);
void highHSlider4HSV(int, void*);
void lowSSlider4HSV(int, void*);
void highSSlider4HSV(int, void*);
void lowVSlider4HSV(int, void*);
void highVSlider4HSV(int, void*);
//HSL
void lowHSlider4HSL(int, void*);
void highHSlider4HSL(int, void*);
void lowSSlider4HSL(int, void*);
void highSSlider4HSL(int, void*);
void lowVSlider4HSL(int, void*);
void highVSlider4HSL(int, void*);
//Blur
void blurImage(int, void*);
//Tracking
void trackFilteredObject(int &x, int &y, Mat threshold, Mat &cam);
//Line
void drawCenterScreen(Mat);
Mat thresh_callback(Mat);
int main (int argc, char* argv[]) {
//Creating windows
Mat cam, hsl, hsv, thresholdHSL, thresholdHSV, drawing;
namedWindow(OG_IMAGE, WINDOW_AUTOSIZE);
namedWindow(HSL_IMAGE, WINDOW_AUTOSIZE);
namedWindow(THRESHOLD_HSL, WINDOW_AUTOSIZE);
namedWindow("Contours", WINDOW_AUTOSIZE);
if(bothDisplays) {
namedWindow(THRESHOLD_HSV, WINDOW_AUTOSIZE);
namedWindow(HSV_IMAGE, WINDOW_AUTOSIZE);
}
//Create video capture
VideoCapture capture;
capture.open(0);
while((char)waitKey(1)!='q') {
//Decode and return video frame
capture.read(cam);
if(bothDisplays) {
//Maintain trackbars
createTrackbar("H Low", THRESHOLD_HSV, &hMin4HSV, MAX_HSV, lowHSlider4HSV);
createTrackbar("H High", THRESHOLD_HSV, &hMax4HSV, MAX_HSV, highHSlider4HSV);
createTrackbar("S Low", THRESHOLD_HSV, &sMin4HSV, MAX_HSV, lowSSlider4HSV);
createTrackbar("S High", THRESHOLD_HSV, &sMax4HSV, MAX_HSV, highSSlider4HSV);
createTrackbar("V Low", THRESHOLD_HSV, &lMin4HSV, MAX_HSV, lowVSlider4HSV);
createTrackbar("V High", THRESHOLD_HSV, &lMax4HSV, MAX_HSV, highVSlider4HSV);
}
createTrackbar("H Low", THRESHOLD_HSL, &hMin4HSL, MAX_HSL, lowHSlider4HSL);
createTrackbar("H High", THRESHOLD_HSL, &hMax4HSL, MAX_HSL, highHSlider4HSL);
createTrackbar("S Low", THRESHOLD_HSL, &sMin4HSL, MAX_HSL, lowSSlider4HSL);
createTrackbar("S High", THRESHOLD_HSL, &sMax4HSL, MAX_HSL, highSSlider4HSL);
createTrackbar("L Low", THRESHOLD_HSL, &lMin4HSL, MAX_HSL, lowVSlider4HSL);
createTrackbar("L High", THRESHOLD_HSL, &lMax4HSL, MAX_HSL, highVSlider4HSL);
createTrackbar("Blur Value", OG_IMAGE, &blurVal, MAX_BLUR, blurImage);
//Conversions for images
blur(cam, cam, Size(blurVal, blurVal), Point(-1, -1));
cvtColor(cam, hsl, COLOR_RGB2HLS);
if(bothDisplays)
cvtColor(cam, hsv, COLOR_RGB2HSV);
inRange(hsl, Scalar(hMin4HSL, sMin4HSL, lMin4HSL), Scalar(hMax4HSL, sMax4HSL, lMax4HSL), thresholdHSL);
// inRange(hsl, Scalar(0, 84, 75), Scalar(57, 172, 168), thresholdHSL);
if(bothDisplays)
inRange(hsv, Scalar(hMin4HSV, sMin4HSV, lMin4HSV), Scalar(hMax4HSV, sMax4HSV, lMax4HSV), thresholdHSV);
trackFilteredObject(x, y, thresholdHSL, cam);
drawCenterScreen(cam);
imshow(OG_IMAGE, cam);
imshow(HSL_IMAGE, hsl);
imshow("Contours", thresh_callback(thresholdHSL));
if(bothDisplays)
imshow(HSV_IMAGE, hsv);
imshow(THRESHOLD_HSL, thresholdHSL);
if(bothDisplays)
imshow(THRESHOLD_HSV, thresholdHSV);
}
return 0;
}
string toString(int num) {
std::ostringstream ss;
ss << num;
return ss.str();
}
void drawCenterScreen(Mat cam) {
line(cam, Point(320, 480), Point(320, 0), Scalar(0,0,255), 2);
}
void drawObject(int &x, int &y, Mat cam) {
circle(cam,Point(x,y),20,Scalar(0,255,0),2);
if(y-25>0)
line(cam,Point(x,y),Point(x,y-25),Scalar(0,255,0),2);
else line(cam,Point(x,y),Point(x,0),Scalar(0,255,0),2);
if(y+25<480)
line(cam,Point(x,y),Point(x,y+25),Scalar(0,255,0),2);
else line(cam,Point(x,y),Point(x,480),Scalar(0,255,0),2);
if(x-25>0)
line(cam,Point(x,y),Point(x-25,y),Scalar(0,255,0),2);
else line(cam,Point(x,y),Point(0,y),Scalar(0,255,0),2);
if(x+25<640)
line(cam,Point(x,y),Point(x+25,y),Scalar(0,255,0),2);
else line(cam,Point(x,y),Point(640,y),Scalar(0,255,0),2);
}
void trackFilteredObject(int &x, int &y, Mat threshold, Mat &cam) {
Mat temp;
threshold.copyTo(temp);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(temp, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
double refArea = 0;
bool objectFound = false;
if(hierarchy.size() > 0) {
int numObject = hierarchy.size();
if(numObject<300) {
for(int i = 0; i >= 0; i = hierarchy[i][0]){
Moments moment = moments((cv::Mat)contours[i]);
double area = moment.m00;
if(area>1000 && area < 150000) {
//More about moments
// http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=moments#moments
x = moment.m10/area;
y = moment.m01/area;
objectFound = true;
refArea = area;
putText(cam, toString(area), Point(0,50),2,1,Scalar(0,225,0),2);
/* failure
int arLen = arcLength(contours, true);
putText(cam, toString(arLen), Point(0,250),2,1,Scalar(0,225,0),2);
*/
// RotatedRect rect = minAreaRect(contours);
// boxPoints(rect, cam);
// drawContours(cam, )
/*second fail vector<RotatedRect> minRect(contours.size());
for( int i = 0; i< contours.size(); i++ ) {
Point2f rect_points[4]; minRect[i].points( rect_points );
for( int j = 0; j < 4; j++ )
line(cam, rect_points[j], rect_points[(j+1)%4], Scalar(200,255,9), 4, 8 );
}
*/
} else {
objectFound = false;
}
if (objectFound) {
drawObject(x,y,cam);
// drawContours(cam, contours, -1, Scalar(0,255,0), 3);
} else {
// putText(cam, "No Target Lock", Point(0,50),2,1,Scalar(0,225,0),2);
}
}
}
}
}
//Update sliders for hsl
void lowHSlider4HSL(int, void*) {
hMin4HSL = min(hMax4HSL-1, hMin4HSL);
setTrackbarPos("H Low", THRESHOLD_HSL, hMin4HSL);
}
void highHSlider4HSL(int, void*) {
hMax4HSL = max(hMax4HSL, hMin4HSL+1);
setTrackbarPos("H High", THRESHOLD_HSL, hMax4HSL);
}
void lowSSlider4HSL(int, void*) {
sMin4HSL = min(sMax4HSL-1, sMin4HSL);
setTrackbarPos("S Low", THRESHOLD_HSL, sMin4HSL);
}
void highSSlider4HSL(int, void*) {
sMax4HSL = max(sMax4HSL, sMin4HSL+1);
setTrackbarPos("S High", THRESHOLD_HSL, sMax4HSL);
}
void lowVSlider4HSL(int, void*) {
lMin4HSL = min(lMax4HSL-1, lMin4HSL);
setTrackbarPos("L Low", THRESHOLD_HSL, lMin4HSL);
}
void highVSlider4HSL(int, void*) {
lMax4HSL = max(lMax4HSL, lMin4HSL+1);
setTrackbarPos("L Max", THRESHOLD_HSL, lMax4HSL);
}
//HSV values
void lowHSlider4HSV(int, void*) {
hMin4HSV = min(hMax4HSV-1, hMin4HSV);
setTrackbarPos("H Low", THRESHOLD_HSV, hMin4HSV);
}
void highHSlider4HSV(int, void*) {
hMax4HSV = max(hMax4HSV, hMin4HSV+1);
setTrackbarPos("H High", THRESHOLD_HSV, hMax4HSV);
}
void lowSSlider4HSV(int, void*) {
sMin4HSV = min(sMax4HSV-1, sMin4HSV);
setTrackbarPos("S Low", THRESHOLD_HSV, sMin4HSV);
}
void highSSlider4HSV(int, void*) {
sMax4HSV = max(sMax4HSV, sMin4HSV+1);
setTrackbarPos("S High", THRESHOLD_HSV, sMax4HSV);
}
void lowVSlider4HSV(int, void*) {
lMin4HSV = min(lMax4HSV-1, lMin4HSV);
setTrackbarPos("V Low", THRESHOLD_HSV, lMin4HSV);
}
void highVSlider4HSV(int, void*) {
lMax4HSV = max(lMax4HSV, lMin4HSV+1);
setTrackbarPos("V Max", THRESHOLD_HSV, lMax4HSV);
}
Mat thresh_callback(Mat thresh)
{
Mat temp;
thresh.copyTo(temp);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using Threshold
/// Find contours
findContours( temp, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect( contours.size() );
vector<RotatedRect> minEllipse( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ minRect[i] = minAreaRect( Mat(contours[i]) );
if( contours[i].size() > 5 )
{ minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
}
/// Draw contours + rotated rects + ellipses
Mat drawing = Mat::zeros( temp.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{ // contour
drawContours( drawing, contours, i, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point() );
// rotated rectangle
Point2f rect_points[4]; minRect[i].points( rect_points );
for( int j = 0; j < 4; j++ )
line( drawing, rect_points[j], rect_points[(j+1)%4], Scalar(255, 255, 255), 1, 8 );
}
return drawing;
}
//Blur value
void blurImage(int, void*) {
setTrackbarPos("Blur Value", OG_IMAGE, (blurVal<1)?blurVal=1:blurVal);
}