简体   繁体   English

OpenCV和Matlab中不同的模板匹配结果

[英]Different template matching result in OpenCV and Matlab

I do template matching in MATLAB and C++ using OpenCV with two sample image and I get different results. 我使用带有两个示例图像的OpenCV在MATLAB和C ++中进行模板匹配,但得到了不同的结果。

My sample images are: 我的示例图像是:

crop 作物

在此处输入图片说明

temp 温度

在此处输入图片说明

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 . maxvalue值为0.93058246374130249

In Matlab: 在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 . 在这种情况下,答案为0.5753

Why the maximum value of the normalized cross-correlation is different? 为什么归一化互相关的最大值不同?

  • In your OpenCV code, you're mixing obsolete C syntax with C++ syntax. 在您的OpenCV代码中,您正在将过时的C语法与C ++语法混合在一起。 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: 为了使其工作,我使用了以下参考图像:

在此处输入图片说明

and as template this: 并作为模板:

在此处输入图片说明

This is the (correct) OpenCV code to use: 这是要使用的(正确)OpenCV代码:

#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: 产生以下结果:

在此处输入图片说明

This is the (correct) Matlab code to use: 这是要使用的(正确)Matlab代码:

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: 产生以下结果:

在此处输入图片说明

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. 细微的差异可能是由于内部实施中的细微差异,但并不相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM