案例 Fu Xianjun. All Rights Reserved.
图片1。
图片2
内容利用代码匹配图片一中的图片二
导包
import cv2
import numpy as np
操作代码
img = cv2.imread("1.png") #读取原图 template = cv2.imread("2.png") #读取模板 result = cv2.matchTemplate(img,template,cv2.TM_SQDIFF_NORMED) #进行模板匹配 h,w = template.shape[:2]#获取模板高和宽 threshold = 0.01 #定义阈值 loc = np.where(result <= threshold) #匹配结果小于阈值的位置 for pt in zip(*loc[::-1]): #遍历位置,zip把两个列表依次参数打包 right_bottom = (pt[0] + w,pt[1] + h) #右下角位置 cv2.rectangle(img, pt, right_bottom, (0,0,255),2) #绘制匹配到的矩阵
显示窗口代码