Home>

The image on which I am looking for matches.

Template

Result

Code:

from PIL import ImageGrab
import os
import time
import cv2
import numpy as np
def find_arrow ():
    img= cv2.imread ("screenshot.png") # Picture with object
    gray_img= cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # Convert to gray
    template= cv2.imread ("Screenshot_1.png", cv2.IMREAD_GRAYSCALE) # Template
    w, h= template.shape [:: -1]
    result= cv2.matchTemplate (gray_img, template, cv2.TM_CCOEFF_NORMED)
    loc= np.where (result >
= 0.5)
    # Draws a rectangle around the object
    for pt in zip (* loc [:: -1]):
        cv2.rectangle (img, pt, (pt [0] + w, pt [1] + h), (0, 200, 0), 3)
    cv2.imshow ("img", img) # Displays the result
    cv2.waitKey ()
def screenshot ():
    gameWindow= (300, 100, 1720, 780)
    im= ImageGrab.grab (gameWindow)
    output= im.save (os.getcwd () + '\\ screenshot' + '.png', 'PNG')
    print ('Screenshot taken and saved')
if __name__== '__main__':
    # time.sleep (3)
    # screenshot ()
    find_arrow ()

Use cv2.matchTemplate with object mask. In the screenshot, you have an object not on a white background, as in the template.

Alex Alex2021-02-23 22:53:21