Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maxvalue in cv2.minMaxLoc()?

I'm having a bit of trouble with opencv and template matching, so I was hoping someone here could help a lost soul out.

So as part of the code I'm using, I've got the following 2 lines which I don't quite understand as well as I should.

result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

From my understanding, the first line stores a correlation coefficient in the variable "result". This in turn is passed into cv2.minMaxLoc(...) which in turn generates a 4 element array composing of (minVal, maxVal, minLoc, maxLoc) of which we are only interested in maxVal and maxLoc.

Upon printing the value of maxVal, I seem to be getting values in the between 2,000,000 to 7,000,000 depending on the template, lighting conditions etc.

My questions are as follows:

What does maxVal mean?

What is the range of maxVal?

What physical characteristics affect the values of maxVal?

Thank you in advance for all your help and guidance!

like image 246
Jason Law Avatar asked Jul 12 '17 03:07

Jason Law


3 Answers

Ideally, cv2.matchTemplate returns a correlation map, essentially a grayscale image, where each pixel denotes how much does the neighborhood of that pixel match with the template.

You suggest that we are interested only in maxLoc and maxVal and that isn't true, it is subject to the type of correlation you are considering when matching a template.

Now, to your questions, the minMaxLoc function returns the max and min intensity values in a Mat or an array along with the location of these intensities.

MaxLoc means where is the highest intensity in the image returned by matchTemplate which would correspond to the best match in your image w.r.t. to your template ( for specific correlation methods only, for TM_SQDIFF or TM_SQDIFF_NORMED the best match would be the minVal).

Since the image returned by matchTemplate is gray-scale, the range should be dependent on the original image, so 2000000 to 7000000 seem a bit out of order to me.

The only "physical characteristics" that affect the maxVal should be the degree of correlation the template has with the image and nothing else.

Hope it helps!

like image 110
Rick M. Avatar answered Oct 16 '22 06:10

Rick M.


As the other answers already explain, you are matching based on cross-correlation. So maxVal is the maximum of your cross-correlation. It is difficult to make a generic guess about the range. But you can always limit the range to [0, 1] by

normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
like image 42
Julian Avatar answered Oct 16 '22 06:10

Julian


If you crop the region of the image that best matches the template, then the value of the peak of the cross-correlation function is

np.sum(cropped * template)

This value will get bigger when the image is brighter, when the template is brighter, and when the template is bigger.

like image 1
Cris Luengo Avatar answered Oct 16 '22 06:10

Cris Luengo