# -*- coding: utf-8 -*- import cv2 file=r'D:/Setting.png' # Big picture temp=r'D:/Battery.png' # Little picture # After the picture pops up CTRL+S Save pictures to local method = cv2.TM_SQDIFF_NORMED # method = cv2.TM_CCOEFF_NORMED # Read the images from the file small_image = cv2.imread(temp) large_image = cv2.imread(file) result = cv2.matchTemplate(small_image, large_image, method) # The least square difference is required mn,_,mnLoc,_ = cv2.minMaxLoc(result) # Start drawing rectangles : # Extract the coordinates of our best match MPx,MPy = mnLoc # To obtain the minimum coordinate print(MPx, MPy) # MPx1,MPy1 = mxLoc # Get the maximum coordinates of # print(MPx1, MPy1) # Step 2: Get the size of the template. This is the same size as the match. trows,tcols = small_image.shape[:2] # Get the width of the picture # Step 3: Draw the rectangle on large_image # Circle the small picture in the big picture with a red line 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) # The method is different , Different coordinates obtained # print(MPx, MPy) # print(MPx1, MPy1) # Notice that one of the results of the two methods is the same #cv2.TM_SQDIFF_NORMED # result : # 96 179 # 70 1122 # cv2.TM_CCOEFF_NORMED # result : # 72 1631 # 96 179
As shown in the figure below , Found in the background picture The search of Icon , And circle it with a red line