Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Template matching results from basic SQDIFF method

The provided template matching documentation (/template_matching.html) gives the formulae for the different template matching methods. I needed a simple template matching calculation in my Java code and including OpenCV in my Java app just for a simple evaluation would be overkill.

So, using the formula (a) in the template matching documentation I programmed my own function to calculate the squared difference from two equal sized images. I see the calculation very straight forward, but when comparing the numerical values from my Java code and OpenCV, I get slightly different results. For example, from comparing two grayscale png images of resolution 1366x768 I got a SQDIFF value 751601095 from my Java code and OpenCV gave me 1645311360, which is just over two times bigger value. I loaded the image in OpenCV using CV_LOAD_IMAGE_UNCHANGED and used CV_8U when creating the result matrix.

The funniest thing is, that when comparing two exactly same images (copies of each other), my own implementation gives 0 (as expected), but OpenCV gives a squared difference of 3020. This makes me doubt about what tricks is OpenCV actually doing in the template maching process.

So, any ideas what would explain the difference in the results and the OpenCV faulty value with two equal images?