#匹配多个对象
'''
注意OpenCV读取图片的shape顺序为(高,宽,通道),
而rectangle()函数的坐标点是(x, y),所以需要将坐标进行转化。
'''
img = cv.imread("./img/mario.jpg")
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
template = cv.imread("./img/mario_coin.jpg", 0)
h, w = template.shape[:2]
res = cv.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED)
"""
res中越接近1的像素点匹配的程度越高, 由于是多个对象匹配所以在这里不直接选取最大值,
而是选取一个阈值,在超过这个阈值时,我们认为是需要匹配的对象
"""
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
#-1的作用是将坐标点的高和宽倒置以使得满足(x, y)
bottom_right = (pt[0] + w,pt[1] + h)
cv.rectangle(img, pt, bottom_right, (0,0,255), 2)
cv_show("img",img)