import cv2 as cv import numpy as np """ matchTemplate(): 参数image:待搜索的图像(大图) 参数temple:搜索模板,需要和原图一样的数据类型且尺寸不能大于源图像 参数result:比较结果的映射图像,其必须为单通道,32位浮点型图像,如果原图(待搜索图像)尺寸为W*H,而temple尺寸为w*h,则result尺寸一定是 (W-w+1)*(H-h+1) 参数method:指定匹配方法,有如下几种: CV_TM_SQDIFF:平方差匹配法 CV_TM_SQDIFF_NORMED:归一化平方差匹配法 CV_TM_CCORR:相关匹配法 CV_TM_CCORR_NORMED:归一化相关匹配法 CV_TM_CCOEFF:系数匹配法 CV_TM_CCOEFF_NORMED:化相关系数匹配法 """ """ minMaxLoc()函数 作用:一维数组当作向量,寻找矩阵中最小值和最大值位置 """ def match_image(): target = cv.imread(r"C:\Users\lenovo\Desktop\test\2.jpg") temple = cv.imread(r"C:\Users\lenovo\Desktop\test\1.png") # shape是获取矩阵的长度 print(temple.shape) # 获取到小图的尺寸 th, tw = temple.shape[:2] result = cv.matchTemplate(target, temple, cv.TM_SQDIFF_NORMED) # 返回匹配的最小坐标 min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result) tl=min_loc print(tl) br = (int(tl[0]) + tw, int(tl[1]) + th) print('br==',br) cv.rectangle(target, tl, br, [0, 255, 0]) cv.imshow("匹配结果" + np.str(cv.TM_SQDIFF_NORMED), target) match_image() cv.waitKey(0) cv.destroyAllWindows()