Ask Your Question
0

Applying Normalised correlation on two vectors of points

asked 2015-04-26 06:31:55 -0600

215 gravatar image

I trying to make a tracker inspired from the TLD, and can see that they calculates a Normalized correlation coefficient, of how much 2 vector of points correlates each other. i use matchTemplate to to calculate the coefficient, though the output doesn't make that much sense to me.. Some form of clarification would appreciated.

My calculation looks like this.

void normCrossCorrelation(Mat prevImg, Mat nextImg, vector<Point2f> pointsPrev, vector<Point2f> pointsNew, vector<uchar> status)
{

    Mat rec0,rec1,res;
    Size size(100,100);
    for(int i = 0; i <= points1.size(); i++)
    {
        if(status[i] == 1)
        {
            cout << points1.size() << endl; 
            cout << "debug: " << i << endl; 
            getRectSubPix(imgI,size, pointsPrev[i],rec0);
            getRectSubPix(imgJ,size, pointsNew[i],rec1);
            matchTemplate(rec0, rec1, res, CV_TM_CCORR_NORMED);    // res is the output..
            cout << ((float *)(res.operator _IplImage().imageData))[0] << endl; // what does it say?.. how say something about it?
        }
    }
    rec0.release();
    rec1.release();
    res.release();
}
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2015-04-26 12:10:47 -0600

Amin_Abouee gravatar image

updated 2015-04-26 12:12:11 -0600

Based on the Template Matching tutorial in opencv doc, After template matching, you need to normalized the result mat between 0 and 1:

cv::normalize( res, res, 0, 1, cv::NORM_MINMAX, -1, cv::Mat() );

After normalization, you need to find the maximum value inside the res matrix. (Because cross correlation try to maximize the product (or cross-correlation) of the two aligned images and so the maximum value inside the res matrix is you answer). For this purpose you can apply cv::minMaxLoc function:

double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
cv::minMaxLoc( res, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );

The maxLoc and maxVal represent the location and the value of best match in the second image (imgJ).

edit flag offensive delete link more

Comments

Thanks for the Response :) when I try to cout the maxLoc and MaxVal i get 0?

215 gravatar image215 ( 2015-04-26 12:46:12 -0600 )edit

Of course you should obtain 0 as the answer (read the tutorial carefully). But why ? Because eventually in template matching, you wanna search for a small patch inside another image. In fact, the size of the second image should be much bigger than the size of first patch (rect0). But in your code, the size of rect0 and rec1 are exactly the same and consequently two patch matched in upper left corner or (0,0). Increase the size of second image (rect1) to the whole image or a bigger image patch.

Amin_Abouee gravatar imageAmin_Abouee ( 2015-04-26 13:07:55 -0600 )edit

The idea of using templateMatching was to see how much the two point clouds correlated with each other, and based on that, fuse 2 distribution into one with one µ and \sigma describing them both.

215 gravatar image215 ( 2015-04-26 13:37:50 -0600 )edit

ok.. i tested on as the tutorial says.. and location doesn't match where the template is.

215 gravatar image215 ( 2015-04-27 12:13:57 -0600 )edit

Is it possible to upload one sample data image here!

Amin_Abouee gravatar imageAmin_Abouee ( 2015-04-29 17:22:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-26 06:31:55 -0600

Seen: 1,578 times

Last updated: Apr 26 '15