【发布时间】:2019-05-23 19:03:33
【问题描述】:
我想为一款游戏制作一个机器人,它会在地板上寻找某个物品,然后点击它。我设法让第一部分正确(它甚至在它周围画了一个矩形)但令人尴尬的是我无法正确获得该对象的坐标。我使用cv2.matchTemplate 方法。这是我的代码:
import numpy as np
import pyautogui
img_bgr = cv2.imread('gra.png')
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
template = cv2.imread('bones2.png', 0)
w, h = template.shape[:: -1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshhold = 0.90
loc = np.where( res >= threshhold)
for pt in zip(*loc[:: -1]):
cv2.rectangle(img_bgr, pt, (pt[0] + w, pt[1] + h),(0, 255, 255), 2 )
#here i wanted to move the mouse to the coordinates of a found item, however
#i cant get these two right ↓ ↓
pyautogui.moveTo( ? , ? ,duration=0.5)
cv2.imshow('znalezione', img_bgr)
cv2.waitKey()
cv2.destroyAllWindows()
我试过了:
pyautogui.moveTo( (pt[0] *2 + w)/2 , (pt[1] *2 + h)/2 ,duration=0.5)
但这根本不起作用。有人可以向我解释一下 pt 到底是什么以及如何获取坐标吗?
另外这里是我目前取得的成果的屏幕截图:
【问题讨论】:
标签: python opencv image-processing detection