0

I have a template like so: Example of object to be found

I use the above template to find similar objects in a larger image such as the following:enter image description here

I use the following code to find the two matching objects in the second and third channel:

img = cv2.imread(r'C:\Users\hramanna\Desktop\dye_image.png')
img2 = cv2.imread(r'C:\Users\hramanna\Desktop\dye_image.png',0)

template = cv2.imread(r'C:\Users\hramanna\Desktop\dye_template.png',0)

w, h = template.shape[::-1]

res = cv2.matchTemplate(img2,template,eval('cv2.TM_SQDIFF_NORMED'))
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
top_left = min_loc

bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img,top_left, bottom_right, 255, 2)

cv2.imshow('Template Matching',img)
while True:
    ch = 0xFF & cv2.waitKey()
    if ch == 27:
        break
cv2.destroyAllWindows()

But it is only able to detect the object in the second channel and not the other one.

The output is as follows:Object shown in small blue box in the second channel

How can I broaden the search to find the other object as well?

Community
  • 1
  • 1
  • You're taking only the max (or min) value: i.e. you're looking only for the single best match. Have a look [here](https://stackoverflow.com/a/32095085/5008845) to account for multiple matches – Miki Jul 01 '17 at 16:26
  • try to understand the code you copied from some tutorial... what could min_val or max_val mean? Why are they extract and from where are they extracted? – Micka Jul 01 '17 at 17:33

0 Answers0