Target tracking for the simplest target tracking (template matching)

Source: Internet
Author: User
Tags scalar

Transferred from: http://blog.csdn.net/zouxy09/article/details/13358977

simplest target tracking (template matching)

I. Overview

Target tracking is an important branch in the field of computer vision. Many people have studied, and in recent years there have been many and many algorithms. Let's see the paper. But here, we also focus on the more simple algorithm to see where its advantages are. After all, sometimes simplicity is a kind of beauty.

Here, let's take a look at the tracking algorithm for the simple point of "template matching". The idea is simple, we save the target to be tracked, and then, at each frame, we look for the whole image to be the most similar to the goal, and we believe this is the goal. How to judge the similarity. has used a number of related things, this one in my previous blog in the introduction, we can refer to the following:

The relationship between the square sum (SSD) of the difference in template matching and the mutual correlation criterion

http://blog.csdn.net/zouxy09/article/details/8549743

Then in order to adapt to the changes in the target, we need to update the target we are tracking at any time. In other words, when tracking T-frames, that is, when looking for a target in frame t, it is compared with the target we found in the t-1 frame. Changes in the appearance of such targets will be updated in a timely manner. This is called the online tracking method. Of course, this strategy will lead to the problem of tracking drift, which is one of the important issues that many tracking algorithms have been concerned about in recent years.

Second, the code implementation

My code is based on vs2010+ OpenCV2.4.2. The code can be read into the video or read the camera, and the choice of both needs to be slightly modified in the code. For video, the first frame is displayed, and then we use the mouse to select the target to track, and the tracker starts to track each frame. For the camera, the image is collected all the time, then we use the mouse to select the target to track, then the tracker starts to track each frame behind it. The specific code is as follows:

SimpleTracker.cpp

[CPP] View Plain copy print?  Object tracking algorithm using matchTemplate  // author :  zouxy  // date   : 2013-10-28  // homepage :  http://blog.csdn.net/zouxy09  // email  : zouxy09@qq.com       #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;        } &nbsP     if ( box.height < 0 ) {            box.y += box.height;            box.height *= -1;       }   &NBSP;&NBSP;&NBSP;&NBSP;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;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;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 ()  ==  ' 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, &NBSP;20),  font_hershey_simplex, 1, scalar (0, 0, 255),  3);       &nbsP;    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;  }  

Object tracking algorithm using matchtemplate//AUTHOR:ZOUXY//date:2013-10-28//HOMEPAGE:HTTP://BLOG.CSDN.N
ET/ZOUXY09//email:zouxy09@qq.com #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) {CA
        Se 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 () = = ' 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 (), 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) = =) break;
} return 0; }


iii. Results

We test the effect of the code with a benchmark video-david in the target tracking area. So, the frame number for each frame is shown in the upper-right corner. The illumination change of this video is quite big, but the simple template matching method can be very effective tracking, and very fast, in this video, only spent about 1ms (time-consuming length and the size of the target frame and the performance of the machine).





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.