单目标匹配
import cv2
import matplotlib.pyplot as plt
import numpy as np
img =cv2.imread('number1.jpg')
template = cv2.imread('22.png')
w,h = template.shape[:2]
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
pt = max_loc
cv2.rectangle(img,pt,(pt[0]+h,pt[1]+w),(0,0,255),2)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
多目标匹配使用阀值,这个是要自己优化把握
import cv2
import matplotlib.pyplot as plt
import numpy as np
img =cv2.imread('number1.jpg')
template = cv2.imread('1.png')
w,h = template.shape[:2]
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.85
loc = np.where(res>=threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img,pt,(pt[0]+h,pt[1]+w),(0,0,255),2)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()