import cv2
import numpy as np
img_rgb = cv2.imread(r’C:\Users\Administrator\Desktop\screenshot\my_screenshot20.png’)#大图
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(r’C:\Users\Administrator\Desktop\screenshot\my_screenshot20_1.png’, 0)#小图
h, w = template.shape[:2]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
取匹配程度大于%80的坐标
print(res)
loc = np.where(res >= threshold)
print(loc)
np.where返回的坐标值(x,y)是(h,w),注意h,w的顺序
print(list(zip(*loc[::-1])))
for pt in zip(*loc[::-1]):
bottom_right = (pt[0] + w, pt[1] + h)
cv2.rectangle(img_rgb, pt, bottom_right, (0, 0, 255), 2)
cv2.imwrite(“…/img.jpg”, img_rgb)
cv2.imshow(‘img’, img_rgb)
cv2.waitKey(0)