The Mean-Shift tracker

The Mean-Shift method allows you to locate the maximum of a density function given discrete data sampled from that function. It is, therefore, useful for detecting the modes of this density. Mean-Shift is an iterative method, and an initial estimation is needed.

The algorithm can be used for visual tracking. In this case, the color histogram of the tracked object is used to compute the confidence map. The simplest of such algorithm would create a confidence map in the new image based on the object histogram taken from the previous image, and Mean-Shift is used to find the peak of the confidence map near the object's previous position. The confidence map is a probability density function on the new image, assigning each pixel of the new image a probability, which is the probability of the pixel color occurring in the object in the previous image. Next, we show you an example (trackingMeanShift) using this function:

void trackingMeanShift(Mat& img, Rect search_window)
{
    // 1-Criteria to MeanShift function
    TermCriteria criteria(TermCriteria::COUNT | TermCriteria::EPS, 10, 1);

    // 2-Tracking using MeanShift
meanShift(img, search_window, criteria);

    // 3-Show the result
    rectangle(img, search_window, Scalar(0,255,0), 3);
} 

This example shows you a window with an initial centered rectangle where the tracking is performed. First, the criteria parameter is set. The function that implements the method needs three parameters: the main image, the interest area that we want to search, and the term criteria for different modes of tracking. Finally, a rectangle is obtained from meanShift(), and search_window is drawn on the main image.

Using a modified videoCamera example, we apply this method for tracking. A static window of the screen is used to search. Of course, you can manually adjust another window or use other functions to detect interest objects and then perform the tracking on them:

...
while(!finish)
{
   capture.read(frame);

   cvtColor(frame,frame,COLOR_BGR2GRAY);

// Tracking using MeanShift with an initial search window
  Rect search_window(200,150,100,100);
  trackingMeanShift(prev_frame, search_window);
...

Here, we can see the following two screen captures:

The Mean-Shift tracker

Output of the trackingMeanShift example

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset