- 安装cv2
pip install opencv-python
- 选择匹配模板
temp.png
- 使用cv2匹配
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 待匹配
img = cv2.imread('1.png', 0)
# 匹配模板
template = cv2.imread('temp.png', 0)
h, w = template.shape
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
# 相似度阀值
threshold = 0.7
loc = np.where( res >= threshold)
color = (33, 33, 0)
thickness = 2
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt, (pt[0] w, pt[1] h), color, thickness)
cv2.imshow('thresh', img)
cv2.waitKey()