Reputation: 2046
I'm trying to do a cross correlation in C++. Problem is that I'm not getting same result in matlab.
C++ code
float telo[5] = {-1, 0, 1, 2, 3};
Mat prueba(1, 5, CV_32F, telo);
float telo2[3] = { 0, 1, 2 };
Mat prueba2(1, 3, CV_32F, telo2);
Mat result;
matchTemplate(prueba, prueba2, result, CV_TM_CCORR);
Result: 2048.0004882812500 2.660783784765e-314#DEN -6.2774385622041925e+066
Matlab code:
a = [-1,0,1,2,3]
b = [0,1,2]
xcorr2(a,b)
Result: -2 -1 2 5 8 3 0
What am I doing wrong?
Upvotes: 2
Views: 2495
Reputation: 9376
When I try your C++ code sample with OpenCV 3 I get the following result, as expected:
2, 5, 8
Which is the same as the Matlab solution, but without the tails.
Edit:
To obtain the same result as with Matlab you can add some zero-padding to your input data. Do it manually in your data:
float telo[9] = {0, 0, -1, 0, 1, 2, 3, 0, 0};
Mat prueba(1, 9, CV_32F, telo);
Or a more generic solution (which should also work with 2D data) would be to call
copyMakeBorder(prueba, prueba, prueba2.rows - 1, prueba2.rows - 1, prueba2.cols - 1, prueba2.cols - 1, cv::BORDER_CONSTANT);
before matchTemplate
.
Upvotes: 2