函数说明:
Parameters:
imgProb
windowIn
numIters
windowOut
CAMSHIFT window
- 2D object probability distribution
- CvRect of CAMSHIFT Window intial size
- If CAMSHIFT iterates this many times, stop
- Location, height and width of converged
len
width
itersUsed
- If != NULL, return equivalent len
- If != NULL, return equivalent width
- Returns number of iterations CAMSHIFT took
to converge
Returns:
The function itself returns the area found
int cvMeanShift( const void* imgProb, CvRect windowIn,
CvTermCriteria criteria, CvConnectedComp* comp )
{
i = 0, eps;
CvMoments moments;
int
CvMat
CvMat
CvRect cur_rect = windowIn;
stub, *mat = (CvMat*)imgProb;
cur_win;
CV_FUNCNAME( "cvMeanShift" );
if( comp )
comp->rect = windowIn;
moments.m00 = moments.m10 = moments.m01 = 0;
__BEGIN__;
CV_CALL( mat = cvGetMat( mat, &stub ));
if( windowIn.height <= 0 || windowIn.width <= 0 )
CV_ERROR( CV_StsBadArg, "Input window has non-positive sizes" );
if( windowIn.x < 0 || windowIn.x + windowIn.width > mat->cols ||
windowIn.y < 0 || windowIn.y + windowIn.height > mat->rows )
CV_ERROR( CV_StsBadArg, "Initial window is not inside the image
ROI" );
CV_CALL( criteria = cvCheckTermCriteria( criteria, 1., 100 ));
eps = cvRound( criteria.epsilon * criteria.epsilon );
for( i = 0; i < criteria.max_iter; i++ )
{
int dx, dy, nx, ny;
double inv_m00;
CV_CALL( cvGetSubRect( mat, &cur_win, cur_rect ));
CV_CALL( cvMoments( &cur_win, &moments ));
if( fabs(moments.m00) < DBL_EPSILON )
break;
inv_m00 = moments.inv_sqrt_m00*moments.inv_sqrt_m00;
dx = cvRound( moments.m10 * inv_m00 - windowIn.width*0.5 );
dy = cvRound( moments.m01 * inv_m00 - windowIn.height*0.5 );
nx = cur_rect.x + dx;
ny = cur_rect.y + dy;
if( nx < 0 )
nx = 0;
else if( nx + cur_rect.width > mat->cols )
nx = mat->cols - cur_rect.width;
if( ny < 0 )
ny = 0;
else if( ny + cur_rect.height > mat->rows )
ny = mat->rows - cur_rect.height;
dx = nx - cur_rect.x;
dy = ny - cur_rect.y;
cur_rect.x = nx;
cur_rect.y = ny;
if( dx*dx + dy*dy < eps )
break;
}
__END__;
if( comp )
{
comp->rect = cur_rect;
comp->area = (float)moments.m00;
}
return i;
}