1. 程式人生 > >最簡單的目標跟蹤-模板匹配跟蹤

最簡單的目標跟蹤-模板匹配跟蹤

 模板匹配TemplateMatching是在影象中尋找目標的方法之一。原理很簡單,就是在一幅影象中尋找和模板影象(patch)最相似的區域。在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,它可以有以下值(匹配的方法):

CV_TM_SQDIFF 平方差匹配法,最好的匹配為0,值越大匹配越差;

CV_TM_SQDIFF_NORMED 歸一化平方差匹配法;

CV_TM_CCORR 相關匹配法,採用乘法操作,數值越大表明匹配越好;

CV_TM_CCORR_NORMED 歸一化相關匹配法;

CV_TM_CCOEFF 相關係數匹配法,最好的匹配為1,-1表示最差的匹配;

CV_TM_CCOEFF_NORMED 歸一化相關係數匹配法;

前面兩種方法為越小的值表示越匹配,後四種方法值越大越匹配。

其中:

CV_TM_SQDIFF為:Sumof Squared Difference (SSD) 差值的平方和:


CV_TM_CCORR 為:

Cross Correlation互相關:

SSD可以看成是歐式距離的平方。我們把SSD展開,可以得到:


      可以看到,上式的第一項(模板影象T的能量)是一個常數,第三項(影象I區域性的能量)也可以近似一個常數,那麼可以看到,剩下的第二項就是和cross correlation一樣的,也就是互相關項。而SSD是數值越大,相似度越小,cross correlation是數值越大,相似度越大。

參考:

Konstantinos G. Derpanis 等《RelationshipBetween the Sum of Squared Difference (SSD) and Cross Correlation for TemplateMatching》

實現:

simpleTracker.cpp
// Object tracking algorithm using matchTemplate

#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 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("david.mpg");
	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;
}