1

i was wondering how you would use opencv (cv2) in python for making an alternative to pyautogui.locatecenteronscreen() function, just useing an image instead of an screen. i will try useing an example.

maybe an user defined function,locateCenterOfTemplate("Path/to/template.png") and now since im useing a screenshot as original image, it will ofc be the same as if i would use pyautoguis, but for my main purpose i wont ofc.

import cv2
import pyautogui

pyautogui.screenshot(Path/to/original_image.png)

def locateCenterOfTemplate(image, template, accuracy=100,
region=#whole screen idk how to do this eaither):


temp = locateCenterOfTemplate("Path/to/original_image.png", "Path/to/template.png")
# now variable "temp" is the same as the posision of the center of the template,
# inside of the source immage
pyautogui.click(temp)

Basicly, i would like to have template matching with reagion, confidence and both template and original image as a functino :)

Thanks :D

Community
  • 1
  • 1
kallemoto
  • 11
  • 1
  • 3

1 Answers1

0

If you load the image and template using cv2.imread(path). You can use cv2.matchTemplate. A while back I used this code to match all templates on a screen with a confidence higher than threshold. You can use debug=True, to draw a box around the found templates in red (cv2 uses BGR).

 def match_all(image, template, threshold=0.8, debug=False, color=(0, 0, 255)):
        """ Match all template occurrences which have a higher likelihood than the threshold """
        width, height = template.shape[:2]
        match_probability = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
        match_locations = np.where(match_probability >= threshold)

        # Add the match rectangle to the screen
        locations = []
        for x, y in zip(*match_locations[::-1]):
            locations.append(((x, x + width), (y, y + height)))

            if debug:
                cv2.rectangle(image, (x, y), (x + width, y + height), color, 1)
        return locations

It will return a list of bounding boxes for the areas that match. If you only want to return the highest match, you should adjust the match_locations line to:

match_location = np.unravel_index(match_probability.argmax(), match_probability.shape)

Alternatively if you are OK to use another library, you can take a look at Multi-Template-Matching, which returns a pandas DataFrame with the template name, bounding box and score.

Thymen
  • 2,089
  • 1
  • 9
  • 13