0

I have the following code for OpenCV templateMatch, a scan and a template (from a question I asked earlier here):

import cv2
import numpy as np

#load image into variable
img_rgb = cv2.imread('example_scan.jpg')

#load template
template = cv2.imread('example_template.jpg')

#read height and width of template image
w, h = template.shape[0], template.shape[1]

res = cv2.matchTemplate(img_rgb,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,0), 2)

img_rgb = cv2.resize(img_rgb,(800,600))
cv2.imwrite("example_result.png", img_rgb)

Scan: enter image description here

Template:

enter image description here

The result is this: enter image description here

Okay so this works: templateMatch finds locations of the circles that look like the template. Perfect!

Now my question becomes: how certain is OpenCV that these locations found are correct? Let's say on a scale of 0% to 100%? I found something about minMaxLoc, but I don't know if this is what I'm looking for and what the results of minMaxLoc say, let alone how to use those values. I find the documentation not very clear to be honest.

So:

  • Is there a way to show how certain OpenCV is about the points found? Or how well it performed?

  • If yes, how?

I hope someone can help.

Regards, Ganesh

Ganesh Gebhard
  • 453
  • 1
  • 7
  • 20
  • `cv2.matchTemplate` returns a grayscale image, where each pixel denotes how much the neighborhood of that pixel matches with the template. Now, you can get the `maximum value` and `maximum intensity location` in the image via `min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)`. However, depending on the match method you choose, `max_value` will give you values in different ranges. For the `TM_CCOEFF` (Correlation coefficient) method, you can expect match values from `0` (min) to `1.0` (max) – stateMachine Apr 19 '20 at 00:29
  • Okay so now I said cv2.minMaxLoc(res) and the result is: (-0.4250815808773041, 0.999790370464325, (248, 1158), (1330, 576)). What does this tell me? I was hoping to get something where OpenCV shows the certaincy of each of the found locations. – Ganesh Gebhard Apr 19 '20 at 00:50
  • `max_val` is, in this case, `0.999790370464325`. That is "how similar" the input (with max located at `1330, 576`) is to your original template. As you can see, it is almost an identical match (1.0). Of course, multiple matches are located in your image, so `max_val` would need to be multidimensional, if you want to keep track of all the matching values so far. – stateMachine Apr 19 '20 at 01:34
  • I understand. Then, how can I get it multidimensional? So that all found locations show a max_val? – Ganesh Gebhard Apr 19 '20 at 11:29
  • Or is 0.999790370464325 the 'score' of similarity of the overall image? So, all found locations together? – Ganesh Gebhard Apr 19 '20 at 16:42
  • `0.999790370464325` is likely the last "score" you got while matching the last of your "circles" with the "circle template". If you want all the scores for all of the five circles, you need to store each `max_val` value you get in a vector/array. I don't work with python, so I don't know if you can type convert an scalar into an array, for example – stateMachine Apr 19 '20 at 23:59

0 Answers0