1 #include <opencv2/core/utility.hpp>
2 #include "opencv2/video/tracking.hpp"
3 #include "opencv2/imgproc.hpp"
4 #include "opencv2/videoio.hpp"
5 #include "opencv2/highgui.hpp"
6
7 #include <iostream>
8 #include <ctype.h>
9
10 using namespace cv;
11 using namespace std;
12
13 Mat image;
14
15 bool backprojMode = false;
16 bool selectObject = false;
17 int trackObject = 0;
18 bool showHist = true;
19 Point origin;
20 Rect selection;
21 int vmin = 10, vmax = 256, smin = 30;
22
onMouse(int event,int x,int y,int,void *)23 static void onMouse( int event, int x, int y, int, void* )
24 {
25 if( selectObject )
26 {
27 selection.x = MIN(x, origin.x);
28 selection.y = MIN(y, origin.y);
29 selection.width = std::abs(x - origin.x);
30 selection.height = std::abs(y - origin.y);
31
32 selection &= Rect(0, 0, image.cols, image.rows);
33 }
34
35 switch( event )
36 {
37 case EVENT_LBUTTONDOWN:
38 origin = Point(x,y);
39 selection = Rect(x,y,0,0);
40 selectObject = true;
41 break;
42 case EVENT_LBUTTONUP:
43 selectObject = false;
44 if( selection.width > 0 && selection.height > 0 )
45 trackObject = -1;
46 break;
47 }
48 }
49
help()50 static void help()
51 {
52 cout << "\nThis is a demo that shows mean-shift based tracking\n"
53 "You select a color objects such as your face and it tracks it.\n"
54 "This reads from video camera (0 by default, or the camera number the user enters\n"
55 "Usage: \n"
56 " ./camshiftdemo [camera number]\n";
57
58 cout << "\n\nHot keys: \n"
59 "\tESC - quit the program\n"
60 "\tc - stop the tracking\n"
61 "\tb - switch to/from backprojection view\n"
62 "\th - show/hide object histogram\n"
63 "\tp - pause video\n"
64 "To initialize tracking, select the object with mouse\n";
65 }
66
67 const char* keys =
68 {
69 "{@camera_number| 0 | camera number}"
70 };
71
main(int argc,const char ** argv)72 int main( int argc, const char** argv )
73 {
74 help();
75
76 VideoCapture cap;
77 Rect trackWindow;
78 int hsize = 16;
79 float hranges[] = {0,180};
80 const float* phranges = hranges;
81 CommandLineParser parser(argc, argv, keys);
82 int camNum = parser.get<int>(0);
83
84 cap.open(camNum);
85
86 if( !cap.isOpened() )
87 {
88 help();
89 cout << "***Could not initialize capturing...***\n";
90 cout << "Current parameter's value: \n";
91 parser.printMessage();
92 return -1;
93 }
94
95 namedWindow( "Histogram", 0 );
96 namedWindow( "CamShift Demo", 0 );
97 setMouseCallback( "CamShift Demo", onMouse, 0 );
98 createTrackbar( "Vmin", "CamShift Demo", &vmin, 256, 0 );
99 createTrackbar( "Vmax", "CamShift Demo", &vmax, 256, 0 );
100 createTrackbar( "Smin", "CamShift Demo", &smin, 256, 0 );
101
102 Mat frame, hsv, hue, mask, hist, histimg = Mat::zeros(200, 320, CV_8UC3), backproj;
103 bool paused = false;
104
105 for(;;)
106 {
107 if( !paused )
108 {
109 cap >> frame;
110 if( frame.empty() )
111 break;
112 }
113
114 frame.copyTo(image);
115
116 if( !paused )
117 {
118 cvtColor(image, hsv, COLOR_BGR2HSV);
119
120 if( trackObject )
121 {
122 int _vmin = vmin, _vmax = vmax;
123
124 inRange(hsv, Scalar(0, smin, MIN(_vmin,_vmax)),
125 Scalar(180, 256, MAX(_vmin, _vmax)), mask);
126 int ch[] = {0, 0};
127 hue.create(hsv.size(), hsv.depth());
128 mixChannels(&hsv, 1, &hue, 1, ch, 1);
129
130 if( trackObject < 0 )
131 {
132 Mat roi(hue, selection), maskroi(mask, selection);
133 calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
134 normalize(hist, hist, 0, 255, NORM_MINMAX);
135
136 trackWindow = selection;
137 trackObject = 1;
138
139 histimg = Scalar::all(0);
140 int binW = histimg.cols / hsize;
141 Mat buf(1, hsize, CV_8UC3);
142 for( int i = 0; i < hsize; i++ )
143 buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
144 cvtColor(buf, buf, COLOR_HSV2BGR);
145
146 for( int i = 0; i < hsize; i++ )
147 {
148 int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
149 rectangle( histimg, Point(i*binW,histimg.rows),
150 Point((i+1)*binW,histimg.rows - val),
151 Scalar(buf.at<Vec3b>(i)), -1, 8 );
152 }
153 }
154
155 calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
156 backproj &= mask;
157 RotatedRect trackBox = CamShift(backproj, trackWindow,
158 TermCriteria( TermCriteria::EPS | TermCriteria::COUNT, 10, 1 ));
159 if( trackWindow.area() <= 1 )
160 {
161 int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5)/6;
162 trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
163 trackWindow.x + r, trackWindow.y + r) &
164 Rect(0, 0, cols, rows);
165 }
166
167 if( backprojMode )
168 cvtColor( backproj, image, COLOR_GRAY2BGR );
169 ellipse( image, trackBox, Scalar(0,0,255), 3, LINE_AA );
170 }
171 }
172 else if( trackObject < 0 )
173 paused = false;
174
175 if( selectObject && selection.width > 0 && selection.height > 0 )
176 {
177 Mat roi(image, selection);
178 bitwise_not(roi, roi);
179 }
180
181 imshow( "CamShift Demo", image );
182 imshow( "Histogram", histimg );
183
184 char c = (char)waitKey(10);
185 if( c == 27 )
186 break;
187 switch(c)
188 {
189 case 'b':
190 backprojMode = !backprojMode;
191 break;
192 case 'c':
193 trackObject = 0;
194 histimg = Scalar::all(0);
195 break;
196 case 'h':
197 showHist = !showHist;
198 if( !showHist )
199 destroyWindow( "Histogram" );
200 else
201 namedWindow( "Histogram", 1 );
202 break;
203 case 'p':
204 paused = !paused;
205 break;
206 default:
207 ;
208 }
209 }
210
211 return 0;
212 }
213