Collaxd
Collaxd

Reputation: 181

python + opencv find image in screen area and return x, y

I'm trying to locate an image based on x, y of the screen, that is, look for the image under a certain window that I already know the coordinates and size and so check if the image is in it or not, for that I would like to use opencv , I have something similar that I found in a script on github

the problem is that the script I saw is not clear on how to get this image of the past coordinates, maybe I need to take a screenshot of the location and save it as a temp screenshot, I didn't find anything about it on the internet

the question is, how can i build a takeimage from a selected region? thanks

def LocateImage(image, Region=None, Precision=0.8):
    TakedImage = TakeImage(Region)

    img_rgb = np.array(TakedImage)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(image, 0)

    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    min_val, LocatedPrecision, min_loc, Position = cv2.minMaxLoc(res)
    if LocatedPrecision > Precision:
        return Position[0], Position[1]
    return 0, 0

build a takeimage based on a specific region of the screen, based on passed coordinates to compare and return the image coordinates in x, y

Upvotes: 0

Views: 3162

Answers (1)

Collaxd
Collaxd

Reputation: 181

for future readers the solution found was with Imagegrab from PIL, you can pass the coords and generate an image to compare things

def GetImageFromScreen(coords):
    img = np.array(ImageGrab.grab(bbox=(coords)))
    img = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2RGB)
    cv2.imshow('Test', img)
    cv2.waitKey(5000)

Upvotes: 2

Related Questions