您的位置:首页 > 运维架构

学习OpenCV2——CamShift之目标跟踪

2016-03-31 19:20 330 查看

1. CamShift思想       

       Camshift全称是"Continuously Adaptive Mean-SHIFT",即连续自适应的MeanShift算法,是MeanShift算法的改进。CamShift的基本思想是视频图像的所有帧作MeanShift运算,并将上一帧的结果(即Search Window的中心和大小)作为下一帧MeanShift算法的Search Window的初始值,如此迭代下去。

       这个过程其实和用MeanShift做跟踪一样,可以参见我的另一篇博文“Meanshift之目标跟踪”,这里把我画的流程图搬过来。



2. cvCamShift( )详解

     CamShift号称连续自适应MeanShift,在算法理论上并没有什么区别,甚至在编程的流程上也没什么区别,他们的区别体现在程序内部

int cvCamShift( const void* imgProb,        //概率图
CvRect windowIn,                    //起始跟踪区域
CvTermCriteria criteria,            //迭代终止条件
CvConnectedComp* _comp,             //可选参数,表示连通域结构体
CvBox2D* box )                      //可选参数,存储旋转矩形的坐标,包括中心,尺寸和旋转角


和MeanShift一样,返回值是迭代次数。这里比MeanShift多了一个参数box。

函数原型见 ..\OpenCV249\sources\modules\video\src\camshift.cpp

CV_IMPL int
cvCamShift( const void* imgProb, CvRect windowIn,
CvTermCriteria criteria,
CvConnectedComp* _comp,
CvBox2D* box )
{
const int TOLERANCE = 10;  //公差=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;

__BEGIN__;

CV_CALL( mat = cvGetMat( mat, &stub ));

//调用cvMeanShift函数
CV_CALL( itersUsed = cvMeanShift( mat, windowIn, criteria, &comp ));
windowIn = comp.rect;

//-------------下面的程序是和MeanShift( )的区别所在------------
//区别1:对边界情况进行处理,
//CamShift()中将windowIn沿x和y方向拉大了2个TOLERANCE,并且确保windowIn不越界。Meanshift无此操作
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 ));//在mat中提取windowIn区域

/* Calculating moments in new center mass */
//计算新中心处的颜色统计矩
CV_CALL( cvMoments( &cur_win, &moments ));

//区别2:计算并保存了目标旋转的结果,meanshit()并未考虑旋转
m00 = moments.m00;		//0阶空间矩
m10 = moments.m10;		//水平1阶
m01 = moments.m01;		//垂直1阶
mu11 = moments.mu11;	//水平垂直2阶中心距
mu20 = moments.mu20;	//水平2阶
mu02 = moments.mu02;	//垂直2阶

//目标矩形的质量太小了就退出
if( fabs(m00) < DBL_EPSILON )//系统预定于的值,DBL_EPSILON=2.2204460492503131e-016
EXIT;

//质量的倒数,只是为了下面计算方便,可以把除法表示成乘法
inv_m00 = 1. / m00;
xc = cvRound( m10 * inv_m00 + windowIn.x );
yc = cvRound( m01 * inv_m00 + windowIn.y );  //(xc,yc)是重心相对于图像的坐标想
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 );   //theta是与x轴的夹角

/* 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;
//下次搜索窗口的长宽,注意不是width和height
length = sqrt( rotate_a * inv_m00 ) * 4;
width = sqrt( rotate_c * inv_m00 ) * 4;

/*根据length和width的大小对length、width、theta进行调整*/
if( length < width )
{
double t;

CV_SWAP( length, width, t );
CV_SWAP( cs, sn, t );
theta = CV_PI*0.5 - theta;
}

/* 结果保存在comp中 */
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里存的是目标的相关参数
{
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;   //返回迭代次数
}


将CamShift( )和MeanShift( )对比,可以看到这些差别

1、CamShift( )中将cur_win沿x和y方向拉大了2个TOLERANCE,MeanShift( )无此操作

2、CamShift( )考虑了目标发生旋转的情况,并给出了旋转角,MeanShift( )无此操作

3.实验代码及结果

来看看OpenCV自带的demo,原程序见D:\Programs_L\OpenCV249\sources\samples\cpp\camshiftdemo.cpp

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

Mat image;

bool backprojMode = false;
bool selectObject = false;
int trackObject = 0;
bool showHist = true;
Point origin;
Rect selection;
int vmin = 10, vmax = 256, smin = 30;

static void onMouse( int event, int x, int y, int, void* )
{
if( selectObject )
{
selection.x = MIN(x, origin.x);
selection.y = MIN(y, origin.y);
selection.width = std::abs(x - origin.x);
selection.height = std::abs(y - origin.y);

selection &= Rect(0, 0, image.cols, image.rows);
}

switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = Point(x,y);
selection = Rect(x,y,0,0);
selectObject = true;
break;
case CV_EVENT_LBUTTONUP:
selectObject = false;
if( selection.width > 0 && selection.height > 0 )
trackObject = -1;
break;
}
}

static void help()
{
cout << "\nThis is a demo that shows mean-shift based tracking\n"
"You select a color objects such as your face and it tracks it.\n"
"This reads from video camera (0 by default, or the camera number the user enters\n"
"Usage: \n"
" ./camshiftdemo [camera number]\n";

cout << "\n\nHot keys: \n"
"\tESC - quit the program\n"
"\tc - stop the tracking\n"
"\tb - switch to/from backprojection view\n"
"\th - show/hide object histogram\n"
"\tp - pause video\n"
"To initialize tracking, select the object with mouse\n";
}

const char* keys =
{
"{1| | 0 | camera number}"
};

int main( int argc, const char** argv )
{
help();

VideoCapture cap;
Rect trackWindow;
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
CommandLineParser parser(argc, argv, keys);
int camNum = parser.get<int>("1");

cap.open(camNum);

if( !cap.isOpened() )
{
help();
cout << "***Could not initialize capturing...***\n";
cout << "Current parameter's value: \n";
parser.printParams();
return -1;
}

namedWindow( "Histogram", 0 );
namedWindow( "CamShift Demo", 0 );
setMouseCallback( "CamShift Demo", onMouse, 0 );
createTrackbar( "Vmin", "CamShift Demo", &vmin, 256, 0 );
createTrackbar( "Vmax", "CamShift Demo", &vmax, 256, 0 );
createTrackbar( "Smin", "CamShift Demo", &smin, 256, 0 );

Mat frame, hsv, hue, mask, hist, histimg = Mat::zeros(200, 320, CV_8UC3), backproj;
bool paused = false;

for(;;)
{
if( !paused )
{
cap >> frame;
if( frame.empty() )
break;
}

frame.copyTo(image);

if( !paused )
{
cvtColor(image, hsv, COLOR_BGR2HSV);

if( trackObject )
{
int _vmin = vmin, _vmax = vmax;

inRange(hsv, Scalar(0, smin, MIN(_vmin,_vmax)),
Scalar(180, 256, MAX(_vmin, _vmax)), mask); //mask初始化
int ch[] = {0, 0};
hue.create(hsv.size(), hsv.depth());
mixChannels(&hsv, 1, &hue, 1, ch, 1); //提取h通道

if( trackObject < 0 )
{
Mat roi(hue, selection), maskroi(mask, selection);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges); //计算目标直方图
normalize(hist, hist, 0, 255, CV_MINMAX);

trackWindow = selection;
trackObject = 1;

histimg = Scalar::all(0);
int binW = histimg.cols / hsize;
Mat buf(1, hsize, CV_8UC3);
for( int i = 0; i < hsize; i++ )
buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
cvtColor(buf, buf, CV_HSV2BGR);

for( int i = 0; i < hsize; i++ )
{
int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
rectangle( histimg, Point(i*binW,histimg.rows),
Point((i+1)*binW,histimg.rows - val),
Scalar(buf.at<Vec3b>(i)), -1, 8 );
}
}

calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
backproj &= mask;
RotatedRect trackBox = CamShift(backproj, trackWindow,
TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
if( trackWindow.area() <= 1 )
{
int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5)/6;
trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
trackWindow.x + r, trackWindow.y + r) &
Rect(0, 0, cols, rows);
}

if( backprojMode )
cvtColor( backproj, image, COLOR_GRAY2BGR );
ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA );
//rectangle( image, trackBox, Scalar(0,0,255), 3, CV_AA );
}
}
else if( trackObject < 0 )
paused = false;

if( selectObject && selection.width > 0 && selection.height > 0 )
{
Mat roi(image, selection);
bitwise_not(roi, roi);
}

imshow( "CamShift Demo", image );
imshow( "Histogram", histimg );

char c = (char)waitKey(10);
if( c == 27 )
break;
switch(c)
{
case 'b':
backprojMode = !backprojMode;
break;
case 'c':
trackObject = 0;
histimg = Scalar::all(0);
break;
case 'h':
showHist = !showHist;
if( !showHist )
destroyWindow( "Histogram" );
else
namedWindow( "Histogram", 1 );
break;
case 'p':
paused = !paused;
break;
default:
;
}
}

return 0;
}实验效果和之前的MeanShift差不多,图就懒得帖了。

用的时候感觉有时候甚至不如MeanShift。比如用meanshift跟踪时,手消失在人脸的位置后,meanshift会跟踪人脸,当人手再次从人脸位置出现时,会再跟踪手;而camshift被人脸干扰后,不会再跟踪手。原因未明。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息