# -*- coding: utf-8 -*- import cv2 file1=r'E:aabb esize esize_image11.jpg' file2=r'E:aabb esize esize_image12.jpg' temp=r'E:aabb esize emp.jpg' method = cv2.TM_SQDIFF_NORMED # Read the images from the file small_image = cv2.imread(temp) large_image = cv2.imread(file2) result = cv2.matchTemplate(small_image, large_image, method) # 需要最小平方差 mn,_,mnLoc,_ = cv2.minMaxLoc(result) # 开始画矩形: # Extract the coordinates of our best match MPx,MPy = mnLoc print(MPx, MPy) # Step 2: Get the size of the template. This is the same size as the match. trows,tcols = small_image.shape[:2] # Step 3: Draw the rectangle on large_image cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2) cv2.rectangle(large_image, (MPx+169,MPy+76),(MPx+719,MPy+117),(0,0,255),2) # Display the original image with the rectangle around the match. cv2.imshow('output',large_image) # The image is only displayed if we call this cv2.waitKey(0)