如何在opencv模板匹配中拓宽匹配

时间:2017-07-01 16:22:22

标签: python opencv template-matching

我有一个这样的模板: Example of object to be found

我使用上面的模板在较大的图像中查找类似的对象,如下所示:enter image description here

我使用以下代码在第二和第三个通道中找到两个匹配的对象:

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()

但它只能检测第二个通道中的对象而不能检测另一个通道。

输出如下:Object shown in small blue box in the second channel

如何扩大搜索范围以查找其他对象?

0 个答案:

没有答案