這裏主要介紹下MeanShift算法的迭代過程,畢竟Camshift算法是以它爲核心的。MeanShift算法是一種尋找局部極值的方法。做爲一種直觀上的理解是它一步一步爬向最高點即爬山算法.而怎麼個爬法,用計算出的重心做爲下一步窗口的中心,直到窗口的位置不再變化。在理解MeanShift算法的時候,可以先不加入核函數(是計算距離對統計分佈的影響)和權重函數(如人爲主觀的影響)。
在Camshift算法中MeanShift是通過1階矩除以0階矩來計算重心的。其算法的代碼如下:
CV_IMPL int
cvMeanShift( const void* imgProb, CvRect windowIn,
CvTermCriteria criteria, CvConnectedComp* comp )
{
CvMoments moments;
int i = 0, eps;
CvMat stub, *mat = (CvMat*)imgProb;//輸入的整個圖像
CvMat cur_win;
CvRect cur_rect = windowIn;//當前矩形窗口初始化爲輸入窗口
CV_FUNCNAME( "cvMeanShift" );
if( comp )
comp->rect = windowIn;//初始化聯通區域
moments.m00 = moments.m10 = moments.m01 = 0; //初始化0、1階矩
__BEGIN__;
CV_CALL( mat = cvGetMat( mat, &stub ));
if( CV_MAT_CN( mat->type ) > 1 )
CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat );
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 || //x,y是指角點座標而不是中心座標
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 )); //cur_win指向窗口內的數據
CV_CALL( cvMoments( &cur_win, &moments )); //計算窗口內的各種矩
/* Calculating center of mass */
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;//新的x座標
ny = cur_rect.y + dy;//新的y座標
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;
/* Check for coverage centers mass & window */
if( dx*dx + dy*dy < eps ) //迭代終止
break;
}
__END__;
if( comp )//返回矩形和0階矩
{
comp->rect = cur_rect;
comp->area = (float)moments.m00;
}
return i; //返回迭代次數
}
Camshift算法代碼:
CV_IMPL int
cvCamShift( const void* imgProb, CvRect windowIn,
CvTermCriteria criteria,
CvConnectedComp* _comp,
CvBox2D* box )
{
const int TOLERANCE = 10;
CvMoments moments;
double m00 = 0, m10, m01, mu20, mu11, mu02, inv_m00;
double a, b, c, xc, yc;
double rotate_a, rotate_c;
double theta = 0, square;
double cs, sn;
double length = 0, width = 0;
int itersUsed = 0;
CvConnectedComp comp;
CvMat cur_win, stub, *mat = (CvMat*)imgProb;
CV_FUNCNAME( "cvCamShift" );
comp.rect = windowIn;//初始化comp
__BEGIN__;
CV_CALL( mat = cvGetMat( mat, &stub ));
CV_CALL( itersUsed = cvMeanShift( mat, windowIn, criteria, &comp ));//調用meanshift計算質心
windowIn = comp.rect;//獲得新的窗口的位置
//爲了容錯,窗口的四邊都增大了TOLERANCE
windowIn.x -= TOLERANCE;
if( windowIn.x < 0 )
windowIn.x = 0;
windowIn.y -= TOLERANCE;
if( windowIn.y < 0 )
windowIn.y = 0;
windowIn.width += 2 * TOLERANCE;
if( windowIn.x + windowIn.width > mat->width )
windowIn.width = mat->width - windowIn.x;
windowIn.height += 2 * TOLERANCE;
if( windowIn.y + windowIn.height > mat->height )
windowIn.height = mat->height - windowIn.y;
CV_CALL( cvGetSubRect( mat, &cur_win, windowIn ));//獲得指向子窗口的數據指針
/* Calculating moments in new center mass */
CV_CALL( cvMoments( &cur_win, &moments ));//重新計算窗口內的各種矩
m00 = moments.m00;
m10 = moments.m10;
m01 = moments.m01;
mu11 = moments.mu11;
mu20 = moments.mu20;
mu02 = moments.mu02;
if( fabs(m00) < DBL_EPSILON )
EXIT;
inv_m00 = 1. / m00;
xc = cvRound( m10 * inv_m00 + windowIn.x );//新的中心座標
yc = cvRound( m01 * inv_m00 + windowIn.y );
a = mu20 * inv_m00;
b = mu11 * inv_m00;
c = mu02 * inv_m00;
/* Calculating width & height */
square = sqrt( 4 * b * b + (a - c) * (a - c) );
/* Calculating orientation */
theta = atan2( 2 * b, a - c + square );
/* Calculating width & length of figure */
cs = cos( theta );
sn = sin( theta );
rotate_a = cs * cs * mu20 + 2 * cs * sn * mu11 + sn * sn * mu02;
rotate_c = sn * sn * mu20 - 2 * cs * sn * mu11 + cs * cs * mu02;
length = sqrt( rotate_a * inv_m00 ) * 4;//長與寬的計算
width = sqrt( rotate_c * inv_m00 ) * 4;
/* In case, when tetta is 0 or 1.57... the Length & Width may be exchanged */
if( length < width )
{
double t;
CV_SWAP( length, width, t );
CV_SWAP( cs, sn, t );
theta = CV_PI*0.5 - theta;
}
/* Saving results */
//由於有寬和高的重新計算,使得能自動調整窗口大小
if( _comp || box )
{
int t0, t1;
int _xc = cvRound( xc );//取整
int _yc = cvRound( yc );
t0 = cvRound( fabs( length * cs ));
t1 = cvRound( fabs( width * sn ));
t0 = MAX( t0, t1 ) + 2;//寬的重新計算
comp.rect.width = MIN( t0, (mat->width - _xc) * 2 );//保證寬不超出範圍
t0 = cvRound( fabs( length * sn ));
t1 = cvRound( fabs( width * cs ));
t0 = MAX( t0, t1 ) + 2;//高的重新計算
comp.rect.height = MIN( t0, (mat->height - _yc) * 2 );//保證高不超出範圍
comp.rect.x = MAX( 0, _xc - comp.rect.width / 2 );
comp.rect.y = MAX( 0, _yc - comp.rect.height / 2 );
comp.rect.width = MIN( mat->width - comp.rect.x, comp.rect.width );
comp.rect.height = MIN( mat->height - comp.rect.y, comp.rect.height );
comp.area = (float) m00;
}
__END__;
if( _comp )
*_comp = comp;
if( box )
{
box->size.height = (float)length;
box->size.width = (float)width;
box->angle = (float)(theta*180./CV_PI);
box->center = cvPoint2D32f( comp.rect.x + comp.rect.width*0.5f,
comp.rect.y + comp.rect.height*0.5f);
}
return itersUsed;
}
原文地址:http://www.cnblogs.com/seacode/archive/2010/09/15/1827380.html