4 #include <opencv2/highgui/highgui.hpp>
5 #include <opencv/cv.hpp>
18 CvTermCriteria criteria, CvConnectedComp* comp )
22 CvMat stub, *mat = (CvMat*)imgProb;
24 CvRect cur_rect = windowIn;
27 comp->rect = windowIn;
29 moments.m00 = moments.m10 = moments.m01 = 0;
31 mat = cvGetMat( mat, &stub );
33 if( CV_MAT_CN( mat->type ) > 1 )
34 CV_Error( CV_BadNumChannels, cvUnsupportedFormat );
36 if( windowIn.height <= 0 || windowIn.width <= 0 )
37 CV_Error( CV_StsBadArg,
"Input window has non-positive sizes" );
39 windowIn = cv::Rect(windowIn) & cv::Rect(0, 0, mat->cols, mat->rows);
41 criteria = cvCheckTermCriteria( criteria, 1., 100 );
42 eps = cvRound( criteria.epsilon * criteria.epsilon );
44 for( i = 0; i < criteria.max_iter; i++ )
48 cur_rect = cv::Rect(cur_rect) & cv::Rect(0, 0, mat->cols, mat->rows);
49 if( cv::Rect(cur_rect) == cv::Rect() )
51 cur_rect.x = mat->cols/2;
52 cur_rect.y = mat->rows/2;
54 cur_rect.width = MAX(cur_rect.width, 1);
55 cur_rect.height = MAX(cur_rect.height, 1);
57 cvGetSubRect( mat, &cur_win, cur_rect );
58 cvMoments( &cur_win, &moments );
61 if( fabs(moments.m00) < DBL_EPSILON )
64 inv_m00 = moments.inv_sqrt_m00*moments.inv_sqrt_m00;
65 dx = cvRound( moments.m10 * inv_m00 - windowIn.width*0.5 );
66 dy = cvRound( moments.m01 * inv_m00 - windowIn.height*0.5 );
73 else if( nx + cur_rect.width > mat->cols )
74 nx = mat->cols - cur_rect.width;
78 else if( ny + cur_rect.height > mat->rows )
79 ny = mat->rows - cur_rect.height;
87 if( dx*dx + dy*dy < eps )
93 comp->rect = cur_rect;
94 comp->area = (float)moments.m00;
126 CvTermCriteria criteria,
127 CvConnectedComp* _comp,
130 const int TOLERANCE = 10;
132 double m00 = 0, m10, m01, mu20, mu11, mu02, inv_m00;
133 double a, b, c, xc, yc;
134 double rotate_a, rotate_c;
135 double theta = 0, square;
137 double length = 0, width = 0;
139 CvConnectedComp comp;
140 CvMat cur_win, stub, *mat = (CvMat*)imgProb;
142 comp.rect = windowIn;
144 mat = cvGetMat( mat, &stub );
146 itersUsed =
cvmeanShift( mat, windowIn, criteria, &comp );
147 windowIn = comp.rect;
149 windowIn.x -= TOLERANCE;
153 windowIn.y -= TOLERANCE;
157 windowIn.width += 2 * TOLERANCE;
158 if( windowIn.x + windowIn.width > mat->width )
159 windowIn.width = mat->width - windowIn.x;
161 windowIn.height += 2 * TOLERANCE;
162 if( windowIn.y + windowIn.height > mat->height )
163 windowIn.height = mat->height - windowIn.y;
165 cvGetSubRect( mat, &cur_win, windowIn );
168 cvMoments( &cur_win, &moments );
177 if( fabs(m00) < DBL_EPSILON )
181 xc = cvRound( m10 * inv_m00 + windowIn.x );
182 yc = cvRound( m01 * inv_m00 + windowIn.y );
188 square = sqrt( 4 * b * b + (a - c) * (a - c) );
191 theta = atan2( 2 * b, a - c + square );
197 rotate_a = cs * cs * mu20 + 2 * cs * sn * mu11 + sn * sn * mu02;
198 rotate_c = sn * sn * mu20 - 2 * cs * sn * mu11 + cs * cs * mu02;
199 length = sqrt( rotate_a * inv_m00 ) * 4;
200 width = sqrt( rotate_c * inv_m00 ) * 4;
207 CV_SWAP( length, width, t );
208 CV_SWAP( cs, sn, t );
209 theta = CV_PI*0.5 - theta;
216 int _xc = cvRound( xc );
217 int _yc = cvRound( yc );
219 t0 = cvRound( fabs( length * cs ));
220 t1 = cvRound( fabs( width * sn ));
222 t0 = MAX( t0, t1 ) + 2;
223 comp.rect.width = MIN( t0, (mat->width - _xc) * 2 );
225 t0 = cvRound( fabs( length * sn ));
226 t1 = cvRound( fabs( width * cs ));
228 t0 = MAX( t0, t1 ) + 2;
229 comp.rect.height = MIN( t0, (mat->height - _yc) * 2 );
231 comp.rect.x = MAX( 0, _xc - comp.rect.width / 2 );
232 comp.rect.y = MAX( 0, _yc - comp.rect.height / 2 );
234 comp.rect.width = MIN( mat->width - comp.rect.x, comp.rect.width );
235 comp.rect.height = MIN( mat->height - comp.rect.y, comp.rect.height );
236 comp.area = (float) m00;
244 box->size.height = (float)length;
245 box->size.width = (float)width;
246 box->angle = (float)((CV_PI*0.5+theta)*180./CV_PI);
247 while(box->angle < 0)
249 while(box->angle >= 360)
251 if(box->angle >= 180)
253 box->center = cvPoint2D32f( comp.rect.x + comp.rect.width*0.5f,
254 comp.rect.y + comp.rect.height*0.5f);
260 RotatedRect
camShift( InputArray _probImage, Rect& window,
261 TermCriteria criteria )
263 CvConnectedComp comp;
266 box.center.x = box.center.y = 0; box.angle = 0; box.size.width = box.size.height = 0;
267 comp.rect.x = comp.rect.y = comp.rect.width = comp.rect.height = 0;
269 Mat probImage = _probImage.getMat();
270 CvMat c_probImage = probImage;
271 cvcamShift(&c_probImage, window, (CvTermCriteria)criteria, &comp, &box);
273 return RotatedRect(Point2f(box.center), Size2f(box.size), box.angle);
276 int meanShift( InputArray _probImage, Rect& window, TermCriteria criteria )
278 CvConnectedComp comp;
279 Mat probImage = _probImage.getMat();
280 CvMat c_probImage = probImage;
281 int iters =
cvmeanShift(&c_probImage, window, (CvTermCriteria)criteria, &comp );