我使用以下代码在第二和第三个通道中找到两个匹配的对象:
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()
但它只能检测第二个通道中的对象而不能检测另一个通道。
如何扩大搜索范围以查找其他对象?