0

I do template matching in MATLAB and C++ using OpenCV with two sample image and I get different results.

My sample images are:

crop

enter image description here

temp

enter image description here

when I use:

Mat crop = imread("crop.jpg",0),
temp = imread("temp.jpg",0);
int resultWidth = crop.cols-temp.cols + 1;  
int resultHeigth = crop.rows -temp.rows + 1;
Mat result = cvCreateImage(cvSize(resultWidth ,resultHeigth),32,1);
matchTemplate(crop,temp,result ,CV_TM_CCORR_NORMED);
double minval, maxval;
CvPoint minloc, maxloc;
cvMinMaxLoc(&(IplImage)result ,&minval,&maxval,&minloc,&maxloc,NULL);

maxvalue value is 0.93058246374130249.

In Matlab:

temp = rgb2gray(imread('temp.jpg'));    
crop = rgb2gray(imread('crop.jpg'));
tempMat = normxcorr2(tmep,crop);  
[res,index] = max(max(abs(tempMat)));

And at this case, answer was 0.5753.

Why the maximum value of the normalized cross-correlation is different?

Miki
  • 40,887
  • 13
  • 123
  • 202
s.a.t
  • 39
  • 8

1 Answers1

4
  • In your OpenCV code, you're mixing obsolete C syntax with C++ syntax. You should really avoid to do that.
  • Your template image is bigger than the image itself. This won't work (you probably uploaded the wrong template).

In order to make it work, I used as reference image this:

enter image description here

and as template this:

enter image description here

This is the (correct) OpenCV code to use:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
    Mat1b templ = imread("path_to_template", IMREAD_GRAYSCALE);

    // Compute match
    Mat result;
    matchTemplate(img, templ, result, TM_CCORR_NORMED);

    // Get best match
    Point maxLoc;
    double maxVal;
    minMaxLoc(result, NULL, &maxVal, NULL, &maxLoc);

    // Display result
    Mat3b res;
    cvtColor(img, res, COLOR_GRAY2BGR);
    rectangle(res, Rect(maxLoc.x, maxLoc.y, templ.cols, templ.rows), Scalar(0, 255, 0));

    imshow("Match", res);
    waitKey();

    return 0;
}

that produces this result:

enter image description here

This is the (correct) Matlab code to use:

temp = rgb2gray(imread('path_to_template'));    
img = rgb2gray(imread('path_to_image'));

% Perform cross-correlation
c = normxcorr2(temp,img);  

% Find peak in cross-correlation
[ypeak, xpeak] = find(c==max(c(:)));

% Account for the padding that normxcorr2 adds
yoffSet = ypeak-size(temp,1);
xoffSet = xpeak-size(temp,2);

% Displat matched area
hFig = figure;
hAx  = axes;
imshow(img,'Parent', hAx);
imrect(hAx, [xoffSet, yoffSet, size(temp,2), size(temp,1)]);

that produces this result:

enter image description here

As you can see, the results are equivalent. The actual maximum number in the match result matrix is:

OpenCV: 0.99999815225601196
Matlab: 0.999988754172261

which we can consider as equal. The small difference is probably due to minor differences in the internal implementation, but is not relevant.

Miki
  • 40,887
  • 13
  • 123
  • 202
  • Thanks @Miki for your answer. But there are some issues. – s.a.t Aug 25 '15 at 07:57
  • 1.In uploaded pictures "crop.jpg" (first) is my main image and "temp.jpg" (second one) is template. So I didn't wrong for uploading template. 2. The most important thing for my issue is final result of template matching operation.(0.0 ~> 1.0, bigger number means template is more similar to image) 3. In your MATLAB code, how can I access to cross correlation coefficient? 4. In fact I want to know why in opencv, obtained coefficient is not indicative of the correct amount of similarities between two images? (in my case result is 0.93!!) – s.a.t Aug 25 '15 at 08:25
  • 1) ok, just got confused because it's basically white noise image. 2) I mentioned values in last part of the answer. 3) mavVal = max(c(:)); 4) unclear. It is indicative. Have you tried my codes on your original images? What's the output? – Miki Aug 25 '15 at 08:33
  • You can do it for these pictures: [image](http://i.share.pho.to/c752df6c_c.png) [template](http://i.share.pho.to/1f3ad265_c.png) @Miki – s.a.t Aug 25 '15 at 08:42
  • I probably got what you mean. But template matching is not made to compare the similarity between two images, but to find a small template inside a bigger image. I obtain two very different results (like 0.8 and 0.2) of maximum value of match, but that's because you're doing something that shouldn't be done. – Miki Aug 25 '15 at 14:06
  • OK,thanks. you say template matching ″find a small template inside a bigger image.″ I have a part of big image as a template and so many pictures of that big picture type.I look at these pictures for template,however at specified area. (I hope that I can explain my purpose!!) So I've used template matching to sure in desired coordinates really exist that template(because this is possible that my desired template is moved or covered just like what you see in the sent pictures) Now,according to this description, what is your suggestion? – s.a.t Aug 25 '15 at 18:28
  • Not that clear to me, sorry. Give a look at http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography – Miki Aug 25 '15 at 18:58
  • Oh! I'm really so sorry, it's because of my weak English!, forgive me,please. Can you tell me which part of my explanations is ambiguous(please don't say all of them!!) to try to improve them. – s.a.t Aug 26 '15 at 08:50
  • I am looking to find a specific object in a big picture. What is your proposed solution? @Miki – s.a.t Aug 26 '15 at 10:20
  • I'm quite busy today sorry... Specific object in big picture: check again the link in my previous comment – Miki Aug 26 '15 at 12:10
  • sorry, @Miki can I talk to you out of here (like email)? please – s.a.t Aug 30 '15 at 19:03
  • @s.a.t sorry, but no. Consider creating a new question that addresses your particular issue. – Miki Aug 30 '15 at 19:28
  • according to this conversation(!) can you tell me what is "maxval" in minmaxLoc function in opencv that Called after using of matchTemplate? Because I think that this value is amount of similarity between image and template. If this is not so how can I calculate percentage of similarity between two arbitrary images? – s.a.t Aug 31 '15 at 05:10