大神的作品是利用逐帧更新实现目标跟踪,博客地址为:http://blog.csdn.net/zouxy09/article/details/13358977?userName=wbyr&userInfo=8poweXHRcTRq9VM1MGnnVqJ26a7MFr5nKCubFer7H3V0C%2FAvXe8GVZJWegm%2BjHpzV9UkhjiWsCcyHUOOxR%2B04KZxMP5033QuSqNq0visw3FBxqXTWoTDPGK%2BKZ6KzY%2BB
对此我加了不更新和有条件更新,代码效率比较低,偶是小白,试试手的
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// Global variables
Rect box;
bool drawing_box = false;
bool gotBB = false;
// bounding box mouse callback 鼠标处理函数
void mouseHandler(int event, int x, int y, int flags, void *param){
switch( event ){
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = Rect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
gotBB = true;
break;
}
}
// tracker: get search patches around the last tracking box,
// and find the most similar one
void tracking1(Mat frame, Mat &model, Rect &trackBox)
{
Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY);
Rect searchWindow;
searchWindow.width = trackBox.width * 3;
searchWindow.height = trackBox.height * 3;
searchWindow.x = trackBox.x + trackBox.width * 0.5 - searchWindow.width * 0.5;
searchWindow.y = trackBox.y + trackBox.height * 0.5 - searchWindow.height * 0.5;
searchWindow &= Rect(0, 0, frame.cols, frame.rows);
Mat similarity;
matchTemplate(gray(searchWindow), model, similarity, CV_TM_CCOEFF_NORMED);
double mag_r;
Point point;
minMaxLoc(similarity, 0, &mag_r, 0, &point);
trackBox.x = point.x + searchWindow.x;
trackBox.y = point.y+ searchWindow.y;
model = model;
}
void tracking2(Mat frame, Mat &model, Rect &trackBox)
{
Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY);
Rect searchWindow;
searchWindow.width = trackBox.width * 3;
searchWindow.height = trackBox.height * 3;
searchWindow.x = trackBox.x + trackBox.width * 0.5 - searchWindow.width * 0.5;
searchWindow.y = trackBox.y + trackBox.height * 0.5 - searchWindow.height * 0.5;
searchWindow &= Rect(0, 0, frame.cols, frame.rows);
Mat similarity;
matchTemplate(gray(searchWindow), model, similarity, CV_TM_CCOEFF_NORMED);
double mag_r;
Point point;
minMaxLoc(similarity, 0, &mag_r, 0, &point);
trackBox.x = point.x + searchWindow.x;
trackBox.y = point.y+ searchWindow.y;
model = gray(trackBox);
}
void tracking3(Mat frame, Mat &model, Rect &trackBox)
{
Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY);
Rect searchWindow;
searchWindow.width = trackBox.width * 3;
searchWindow.height = trackBox.height * 3;
searchWindow.x = trackBox.x + trackBox.width * 0.5 - searchWindow.width * 0.5;
searchWindow.y = trackBox.y + trackBox.height * 0.5 - searchWindow.height * 0.5;
searchWindow &= Rect(0, 0, frame.cols, frame.rows);
Mat similarity;
matchTemplate(gray(searchWindow), model, similarity, CV_TM_CCOEFF_NORMED);
double mag_r;
Point point;
minMaxLoc(similarity, 0, &mag_r, 0, &point);
trackBox.x = point.x + searchWindow.x;
trackBox.y = point.y+ searchWindow.y;
if(mag_r<0.2)
model = 0.6 * model + 0.4 * gray(trackBox);
else
model = gray(trackBox);
}
/*if(mag_r<0.2)
model = 0.6 * model + 0.4 * gray(trackBox);
else
model = gray(trackBox);//此处为模板逐帧更新的标志,屏蔽后就为不更新。*/
int main(int argc, char * argv[])
{
VideoCapture capture("D:/videos/舞蹈.avi");//
bool fromfile = true;
//Init camera
if (!capture.isOpened())
{
cout << "capture device failed to open!" << endl;
return -1;
}
//Register mouse callback to draw the bounding box
cvNamedWindow("Tracker",0 );
cvSetMouseCallback("Tracker", mouseHandler, NULL );
Mat frame, model;
capture >> frame;
while(!gotBB)
{
if (!fromfile)//如果是!的话,未选定模板时视频不启动,摄像头读不出第一帧,打开avi文件还是用!好。
capture >> frame;
imshow("Tracker", frame);
if (cvWaitKey(20) == 'q')
return 1;
}
//Remove callback
cvSetMouseCallback("Tracker", NULL, NULL );
Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY);
model = gray(box);
int frameCount = 0;
cout<<"请输入模板更新方式:"<<"1为不更新模板"<<endl<<"2为逐帧更新模板"<<endl<<"3为有条件更新模板";
int k;
cin>>k;
switch (k){
case 1 :
while (1)
{
capture >> frame;
if (frame.empty())
return -1;
double t = (double)cvGetTickCount();
frameCount++;
// tracking
tracking1(frame, model, box);
// show
stringstream buf;
buf << frameCount;
string num = buf.str();
//putText(frame, num, Point(20, 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
rectangle(frame, box, Scalar(0, 0, 255), 3);
imshow("Tracker", frame);
t = (double)cvGetTickCount() - t;
//cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl;
if ( cvWaitKey(1) == 27 )//ESC的ASCII值为27,事件处理函数cvWaitKey
break;
}
break;
case 2 :
while (1)
{
capture >> frame;
if (frame.empty())
return -1;
double t = (double)cvGetTickCount();
frameCount++;
// tracking
tracking2(frame, model, box);
// show
stringstream buf;
buf << frameCount;
string num = buf.str();
//putText(frame, num, Point(20, 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
rectangle(frame, box, Scalar(0, 0, 255), 3);
imshow("Tracker", frame);
t = (double)cvGetTickCount() - t;
//cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl;
if ( cvWaitKey(1) == 27 )//ESC的ASCII值为27,事件处理函数cvWaitKey
break;
}
break;
case 3:
while (1)
{
capture >> frame;
if (frame.empty())
return -1;
double t = (double)cvGetTickCount();
frameCount++;
// tracking
tracking3(frame, model, box);
// show
stringstream buf;
buf << frameCount;
string num = buf.str();
//putText(frame, num, Point(20, 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
rectangle(frame, box, Scalar(0, 0, 255), 3);
imshow("Tracker", frame);
t = (double)cvGetTickCount() - t;
//cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl;
if ( cvWaitKey(1) == 27 )//ESC的ASCII值为27,事件处理函数cvWaitKey
break;
}
break;
default : cout<<"输入错误";
break;
}
return 0;
}