0

I have a 7-segment image, and a template, I've tried to do template matching, but there was no matching for the provided template, can you please tell me how to improve the matching?

  • should the template be 100% as same as the desired pattern to be detected in the image?

Template

Image

import numpy as np
import matplotlib.pyplot as plt
import cv2

R = cv2.imread('image.png')
R = cv2.Canny(R, 50, 200)

template = cv2.imread('templ.png',0)
template = cv2.Canny(template, 50, 200)

h, w = template.shape

res = cv2.matchTemplate(R,template,cv2.TM_CCOEFF_NORMED)

threshold = 0.8

loc = np.where( res >= threshold)
for pt in zip(*loc):
    cv2.rectangle(R, pt, (pt[0] + w, pt[1] + h), 200, 2)

plt.subplot(221)
plt.imshow(R, cmap='gray')
plt.subplot(222)
plt.imshow(template, cmap='gray')
plt.show()

Bilal
  • 3,191
  • 4
  • 21
  • 49

2 Answers2

1

Your result will depend on the method you use for template matching, since in your case the values are binary (0 or 255), I expected the cross correlation to work well, I tried it and voilà:

Result using cv2.TM_CCORR

It seems that it's not well documented how each method works.

But a good debugging method for these problems is to see the result of the matching to see where it's giving the maximum values, in your case the res variable.

I followed the tutorial in this website, my final code is:

import numpy as np
import matplotlib.pyplot as plt
import cv2

R = cv2.imread('image.png')
R = cv2.Canny(R, 50, 200)


template = cv2.imread('templ.png',0)
template = cv2.Canny(template, 50, 200)
w, h = template.shape[::-1]


res = cv2.matchTemplate(R,template,cv2.TM_CCORR )
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(R,top_left, bottom_right, 255, 2)

cv2.imwrite( './result.png', R)
  • I have tried `res = cv2.matchTemplate(R,template,cv2.TM_CCORR_NORMED)` and didn't get the desired result, also when applied **CCOR** method `res = cv2.matchTemplate(R,template,cv2.TM_CCORR)` I have got a weird [Result](https://i.stack.imgur.com/JpJY6.jpg). couldn't reproduce your result! I don't know why! – Bilal Jan 22 '21 at 05:39
  • sorry for the incomplete answer, check the update – Guilherme Correa Jan 22 '21 at 11:11
0

Recognizing these well binarized digits with high reliability is fairly easy: set 7 regions of interest over the position of the segments and check the amount of white. This tells you if the segment is on or off.

enter image description here