SHOW:
|
|
- or go back to the newest paste.
1 | #ifdef _CH_ | |
2 | #pragma package <opencv> | |
3 | #endif | |
4 | ||
5 | #define CV_NO_BACKWARD_COMPATIBILITY | |
6 | ||
7 | #ifndef _EiC | |
8 | #include "cv.h" | |
9 | #include "highgui.h" | |
10 | #include <stdio.h> | |
11 | #include <ctype.h> | |
12 | #endif | |
13 | ||
14 | IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0; | |
15 | CvHistogram *hist = 0; | |
16 | CvArr *traj; | |
17 | ||
18 | int backproject_mode = 0; | |
19 | int select_object = 0; | |
20 | int track_object = 0; | |
21 | int show_hist = 1; | |
22 | CvPoint origin; | |
23 | CvRect selection; | |
24 | CvRect track_window; | |
25 | CvBox2D track_box; | |
26 | CvConnectedComp track_comp; | |
27 | CvPoint p1; | |
28 | CvPoint p2; | |
29 | int hdims = 16; | |
30 | float hranges_arr[] = {0,180}; | |
31 | float* hranges = hranges_arr; | |
32 | int vmin = 10, vmax = 256, smin = 30; | |
33 | ||
34 | void on_mouse( int event, int x, int y, int flags, void* param ) | |
35 | { | |
36 | if( !image ) | |
37 | return; | |
38 | ||
39 | if( image->origin ) | |
40 | y = image->height - y; | |
41 | ||
42 | if( select_object ) | |
43 | { | |
44 | selection.x = MIN(x,origin.x); | |
45 | selection.y = MIN(y,origin.y); | |
46 | selection.width = selection.x + CV_IABS(x - origin.x); | |
47 | selection.height = selection.y + CV_IABS(y - origin.y); | |
48 | ||
49 | selection.x = MAX( selection.x, 0 ); | |
50 | selection.y = MAX( selection.y, 0 ); | |
51 | selection.width = MIN( selection.width, image->width ); | |
52 | selection.height = MIN( selection.height, image->height ); | |
53 | selection.width -= selection.x; | |
54 | selection.height -= selection.y; | |
55 | } | |
56 | ||
57 | switch( event ) | |
58 | { | |
59 | case CV_EVENT_LBUTTONDOWN: | |
60 | origin = cvPoint(x,y); | |
61 | selection = cvRect(x,y,0,0); | |
62 | select_object = 1; | |
63 | break; | |
64 | case CV_EVENT_LBUTTONUP: | |
65 | select_object = 0; | |
66 | if( selection.width > 0 && selection.height > 0 ) | |
67 | track_object = -1; | |
68 | break; | |
69 | } | |
70 | } | |
71 | ||
72 | ||
73 | CvScalar hsv2rgb( float hue ) | |
74 | { | |
75 | int rgb[3], p, sector; | |
76 | static const int sector_data[][3]= | |
77 | {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}}; | |
78 | hue *= 0.033333333333333333333333333333333f; | |
79 | sector = cvFloor(hue); | |
80 | p = cvRound(255*(hue - sector)); | |
81 | p ^= sector & 1 ? 255 : 0; | |
82 | ||
83 | rgb[sector_data[sector][0]] = 255; | |
84 | rgb[sector_data[sector][1]] = 0; | |
85 | rgb[sector_data[sector][2]] = p; | |
86 | ||
87 | return cvScalar(rgb[2], rgb[1], rgb[0],0); | |
88 | } | |
89 | ||
90 | int main( int argc, char** argv ) | |
91 | { | |
92 | //p1.x = 0; | |
93 | //p1.y = 0; | |
94 | CvCapture* capture = 0; | |
95 | ||
96 | if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) | |
97 | capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 ); | |
98 | else if( argc == 2 ) | |
99 | capture = cvCaptureFromAVI( argv[1] ); | |
100 | ||
101 | if( !capture ) | |
102 | { | |
103 | fprintf(stderr,"Could not initialize capturing...\n"); | |
104 | return -1; | |
105 | } | |
106 | printf( "Hot keys: \n" | |
107 | "\tESC - quit the program\n" | |
108 | "\tc - stop the tracking\n" | |
109 | "\tb - switch to/from backprojection view\n" | |
110 | "\th - show/hide object histogram\n" | |
111 | "To initialize tracking, select the object with mouse\n" ); | |
112 | ||
113 | cvNamedWindow( "Histogram", 1 ); | |
114 | cvNamedWindow( "CamShiftDemo", 1 ); | |
115 | cvNamedWindow( "Trajektoria", 1 ); | |
116 | cvSetMouseCallback( "CamShiftDemo", on_mouse, 0 ); | |
117 | cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 ); | |
118 | cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 ); | |
119 | cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 ); | |
120 | ||
121 | for(;;) | |
122 | { | |
123 | IplImage* frame = 0; | |
124 | int i, bin_w, c; | |
125 | ||
126 | frame = cvQueryFrame( capture ); | |
127 | if( !frame ) | |
128 | break; | |
129 | ||
130 | if( !image ) | |
131 | { | |
132 | /* allocate all the buffers */ | |
133 | image = cvCreateImage( cvGetSize(frame), 8, 3 ); | |
134 | image->origin = frame->origin; | |
135 | hsv = cvCreateImage( cvGetSize(frame), 8, 3 ); | |
136 | hue = cvCreateImage( cvGetSize(frame), 8, 1 ); | |
137 | mask = cvCreateImage( cvGetSize(frame), 8, 1 ); | |
138 | backproject = cvCreateImage( cvGetSize(frame), 8, 1 ); | |
139 | hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 ); | |
140 | histimg = cvCreateImage( cvSize(320,200), 8, 3 ); | |
141 | cvZero( histimg ); | |
142 | traj = cvCreateImage( cvGetSize(frame), 8, 3 ); | |
143 | } | |
144 | ||
145 | cvCopy( frame, image, 0 ); | |
146 | cvCvtColor( image, hsv, CV_BGR2HSV ); | |
147 | ||
148 | if( track_object ) | |
149 | { | |
150 | int _vmin = vmin, _vmax = vmax; | |
151 | ||
152 | cvInRangeS( hsv, cvScalar(0,smin,MIN(_vmin,_vmax),0), | |
153 | cvScalar(180,256,MAX(_vmin,_vmax),0), mask ); | |
154 | cvSplit( hsv, hue, 0, 0, 0 ); | |
155 | ||
156 | if( track_object < 0 ) | |
157 | { | |
158 | float max_val = 0.f; | |
159 | cvSetImageROI( hue, selection ); | |
160 | cvSetImageROI( mask, selection ); | |
161 | cvCalcHist( &hue, hist, 0, mask ); | |
162 | cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 ); | |
163 | cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 ); | |
164 | cvResetImageROI( hue ); | |
165 | cvResetImageROI( mask ); | |
166 | track_window = selection; | |
167 | track_object = 1; | |
168 | ||
169 | cvZero( histimg ); | |
170 | bin_w = histimg->width / hdims; | |
171 | for( i = 0; i < hdims; i++ ) | |
172 | { | |
173 | int val = cvRound( cvGetReal1D(hist->bins,i)*histimg->height/255 ); | |
174 | CvScalar color = hsv2rgb(i*180.f/hdims); | |
175 | cvRectangle( histimg, cvPoint(i*bin_w,histimg->height), | |
176 | cvPoint((i+1)*bin_w,histimg->height - val), | |
177 | color, -1, 8, 0 ); | |
178 | } | |
179 | } | |
180 | ||
181 | cvCalcBackProject( &hue, backproject, hist ); | |
182 | cvAnd( backproject, mask, backproject, 0 ); | |
183 | cvCamShift( backproject, track_window, | |
184 | cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ), | |
185 | &track_comp, &track_box ); | |
186 | track_window = track_comp.rect; | |
187 | ||
188 | if( backproject_mode ) | |
189 | cvCvtColor( backproject, image, CV_GRAY2BGR ); | |
190 | if( !image->origin ) | |
191 | track_box.angle = -track_box.angle; | |
192 | cvEllipseBox( image, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 ); | |
193 | } | |
194 | ||
195 | if( select_object && selection.width > 0 && selection.height > 0 ) | |
196 | { | |
197 | cvSetImageROI( image, selection ); | |
198 | cvXorS( image, cvScalarAll(255), image, 0 ); | |
199 | cvResetImageROI( image ); | |
200 | } | |
201 | ||
202 | cvShowImage( "CamShiftDemo", image ); | |
203 | cvShowImage( "Histogram", histimg ); | |
204 | p2.x = track_comp.rect.x; | |
205 | p2.y = track_comp.rect.y; | |
206 | cvLine(traj, p1, p2, CV_RGB(255,0,0), 1, 8, 0); | |
207 | cvShowImage( "Trajektoria", traj); | |
208 | p1 = p2; | |
209 | ||
210 | c = cvWaitKey(10); | |
211 | if( (char) c == 27 ) | |
212 | break; | |
213 | switch( (char) c ) | |
214 | { | |
215 | case 'b': | |
216 | backproject_mode ^= 1; | |
217 | break; | |
218 | case 'c': | |
219 | track_object = 0; | |
220 | cvZero( histimg ); | |
221 | break; | |
222 | case 'h': | |
223 | show_hist ^= 1; | |
224 | if( !show_hist ) | |
225 | cvDestroyWindow( "Histogram" ); | |
226 | else | |
227 | cvNamedWindow( "Histogram", 1 ); | |
228 | break; | |
229 | default: | |
230 | ; | |
231 | } | |
232 | } | |
233 | ||
234 | cvReleaseCapture( &capture ); | |
235 | cvDestroyWindow("CamShiftDemo"); | |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
240 | #ifdef _EiC | |
241 | main(1,"camshiftdemo.c"); | |
242 | #endif |