G
G
golub4ik2018-06-11 10:25:06
Python
golub4ik, 2018-06-11 10:25:06

Python + cv how to get the coordinates of an object found using matchTemplate?

The bottom line is this... I'm making a bot for the game, the principle of operation:
1. Takes a screenshot of the game #implemented 2. Looks
for an object in the screenshot, in my case mana
#implemented 3. Returns coordinates similar to the object #the question itself
4. Then clicks on these coordinates # I know how to implement
Maybe there is a better way to implement this? For help, I'm ready to throw money away!
The code:

from PIL import ImageGrab
import os
import time
import cv2
import numpy as np

def find_mana():
    img = cv2.imread("screenshot.png") #картинка, на которой ищем объект
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #преобразуем её в серуюш
    template = cv2.imread("mana.png", cv2.IMREAD_GRAYSCALE) #объект, который преобразуем в серый, и ищем его на gray_img
    w, h = template.shape[::-1] #инвертируем из (y,x) в (x,y)
     
    result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
    loc = np.where(result >= 0.5)
    #рисует прямоугольник вокруг объекта
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 3)
     
     
    cv2.imshow("img", img) #выводит на экран результат

def main():
    #делает скриншот игры, закоментируйте, если понадобится, так как скриншот я выложил снизу, как и сам объект
    gameWindow = (0, 31, 1280, 747)
    im = ImageGrab.grab(gameWindow)
    output = im.save(os.getcwd() + '\\screenshot' + '.png', 'PNG')
    print('\nСкриншот сделан и сохранён\n')

    find_mana()

if __name__ == '__main__':
    main()

Images: Image
we are looking for: https://prntscr.com/jtiqoj
Object we are looking for: https://prntscr.com/jtiqnp

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly, 2018-06-11
@golub4ik

You draw a rectangle around the object, so take the coordinates from there.
pt - upper left corner, (pt[0] + w, pt[1] + h) - lower right.
It turns out that the middle of the object will be calculated according to the formulas (I would substitute it myself, but without a visible array it’s hard, and I always confuse these X and Y in places):
For X:

(X координата нижнего угла - X координата верхнего угла) / 2

For Y:
(Y координата нижнего угла - Y координата верхнего угла) / 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question