在一幅图像中寻找和模板图像(patch)最相似的区域。
1.判断相似性:
OpenCV中有对应的函数——void matchTemplate( const Mat& image, const Mat& templ, Mat&result, int method )
该函数的功能为,在输入源图像Sourceimage(I)中滑动框,寻找各个位置与模板图像Template image(T)的相似度,并将结果保存在结果矩阵result matrix(R)中。该矩阵的每一个点的亮度表示与模板T的匹配程度。然后可以通过函数minMaxLoc定位矩阵R中的最大值(该函数也可以确定最小值)。那通过什么去评价两个图像相似呢?这就存在一个评价准则,也就是参数method,它可以有以下值(匹配的方法):
-
CVTMSQDIFF 平方差匹配法,最好的匹配为0,值越大匹配越差;
-
CVTMSQDIFF_NORMED 归一化平方差匹配法;
-
CVTMCCORR 相关匹配法,采用乘法操作,数值越大表明匹配越好;
-
CVTMCCORR_NORMED 归一化相关匹配法;
-
CVTMCCOEFF 相关系数匹配法,最好的匹配为1,-1表示最差的匹配;
-
CVTMCCOEFF_NORMED 归一化相关系数匹配法;
前面两种方法为越小的值表示越匹配,后四种方法值越大越匹配。
2.更新跟踪目标
在第t帧寻找目标的时候,是与t-1帧中我们找到的目标来进行比较的。这样目标的外观变化就会及时的更新,但容易导致跟踪漂移。
3.示例代码
基于VS2013+OpenCV 2.4.9:
///Object tracking algorithm using matchTemplate
///Author:JA
///Date:2015-1-24
#include
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 tracking(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);
}
int main(int argc, char * argv[])
{
VideoCapture capture;
capture.open("video.mp4");
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", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("Tracker", mouseHandler, NULL);
Mat frame, model;
capture >> frame;
while (!gotBB)
{
if (!fromfile)
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;
while (1)
{
capture >> frame;
if (frame.empty())
return -1;
double t = (double)cvGetTickCount();
frameCount++;
// tracking
tracking(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)
break;
}
return 0;
}
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 tracking(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);
}
int main(int argc, char * argv[])
{
VideoCapture capture;
capture.open("video.mp4");
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", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("Tracker", mouseHandler, NULL);
Mat frame, model;
capture >> frame;
while (!gotBB)
{
if (!fromfile)
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;
while (1)
{
capture >> frame;
if (frame.empty())
return -1;
double t = (double)cvGetTickCount();
frameCount++;
// tracking
tracking(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)
break;
}
return 0;
}
参考文献: