1 # Author:Winter Liu is coming! 2 import cv2 as cv 3 import numpy as np 4 import matplotlib.pyplot as plt 5 6 7 def match_demo(src1, src2): 8 res = cv.matchTemplate(src2, src1, cv.TM_CCOEFF_NORMED) 9 # res = cv.matchTemplate(src2, src1, cv.TM_SQDIFF_NORMED) 10 # min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) 11 pt = np.where(res > 0.5) 12 h, w, c = src1.shape 13 print(len(pt[0])) 14 # print(max_val) 15 # print(max_loc) 16 # cv.rectangle(src2,max_loc, (max_loc[0]+h, max_loc[1]+w), (0, 0, 255), 2) 17 a = [(-1, -1)] 18 p_zip = zip(pt[1], pt[0]) 19 p_n = sorted(p_zip, key=lambda x: x[0], reverse=False) 20 for p in p_n: 21 # print(p) 22 if abs(a[-1][0] - p[0]) > 10 or abs(a[-1][1] - p[1]) > 10: 23 print(p) 24 cv.rectangle(src2, p, (p[0]+w, p[1]+h), (0, 0, 255), 2) 25 a.append(p) 26 cv.imshow("src2", src2) 27 cv.waitKey(0) 28 cv.destroyWindow("src2") 29 30 31 src1 = cv.imread(r"C:\PycharmProjects\OpenCV\pic\super_maria_coin.jpg") 32 src2 = cv.imread(r"C:\PycharmProjects\OpenCV\pic\super_maria.jpg") 33 match_demo(src1, src2) 34 down = src1.copy() 35 for i in range(4): 36 down = cv.pyrDown(down) 37 cv.imshow("down", down) 38 cv.waitKey(0) 39 cv.destroyWindow("down") 40 match_demo(down, src2)