Is the document of CV_TM_SQDIFF_NORMED template matching method correct ?

asked 2014-07-11 05:24:33 -0500

tm-newbie gravatar image

updated 2020-11-02 17:32:03 -0500

The link to the document is here http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html#template-matching

According to the formula (or my understanding of the formula), in some cases, the result would be larger than 1.

For example, let Img be a 4x4 image:

Img={1,2,3,4, 5,6,7,8,  9,10,11,12, 13,14,15,16}

let Tpl be a 2x2 template:

Tpl={11,12 15,16}

The dividend of the first element of the matching result would be:

(11-1)^2 + (12 - 2)^2 + (15 - 5)^2 + (16 - 6)^2 = 400

The divisor of the first element of the matching result would be:

sqrt( (1^2 + 2^2 + 5^2 + 6^2) * (11^2 + 12^2 + 15^2 + 16^2)  ) = 221.89

So the first element would roughly be:

400 / 221.89 = 1.802

According to the documentation, CV_TM_SQDIFF_NORMED results would be between 0 and 1.0, apparently, my result isn't. Which part of my calculation is wrong ? Or the documentation is incorrect ?

Thanks

edit retag flag offensive close merge delete

Comments

I need the formula in order to implement the algorithm in OpenCL, because the OpenCL implementation from OpenCV is way too slow.

tm-newbie gravatar imagetm-newbie ( 2014-07-12 23:42:15 -0500 )edit

I ended up reading the source code of OpenCV. The document is actually correct, but OpenCV's implementation is a little bit different: if the calculated value is larger than 1, the value would be set to 1.

tm-newbie gravatar imagetm-newbie ( 2014-07-23 04:36:23 -0500 )edit