【发布时间】:2019-01-07 22:46:24
【问题描述】:
对不起,标题没有意义
我正在尝试制作一个点击球使其弹跳的 AI。 对于上下文这里是应用程序的图片
在游戏中,当您点击球时,它会上升,然后又下降,游戏的目的是保持它向上。
我写了一些代码,用opencv将图像变成掩码,这是结果的图片
我现在需要做的是在像素/坐标中找到球的位置,这样我就可以让鼠标移动到它并单击它。顺便说一下,球的左右两边都有边距,所以它不仅会上下移动,也会左右移动。球也没有动画,只是一个移动的图像。
我如何以像素/坐标获得球的位置,以便我可以将鼠标移动到它。
这是我的代码的副本:
import numpy as np
from PIL import ImageGrab
import cv2
import time
import pyautogui
def draw_lines(img,lines):
for line in lines:
coords = line[0]
cv2.line(img, (coords[0], coords[1]), (coords[2], coords[3]), [255,255,255], 3)
def process_img(original_image):
processed_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
vertices = np.array([[0,0],[0,800],[850,800],[850,0]
], np.int32)
processed_img = roi(processed_img, [vertices])
# more info: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html
# edges rho theta thresh # min length, max gap:
lines = cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 20, 15)
draw_lines(processed_img,lines)
return processed_img
def roi(img, vertices):
#blank mask:
mask = np.zeros_like(img)
# fill the mask
cv2.fillPoly(mask, vertices, 255)
# now only show the area that is the mask
masked = cv2.bitwise_and(img, mask)
return masked
def main():
last_time = time.time()
while(True):
screen = np.array(ImageGrab.grab(bbox=(0,40, 800, 850)))
new_screen = process_img(screen)
print('Loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
cv2.imshow('window', new_screen)
#cv2.imshow('window2', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
def mouse_movement():
##Set to move relative to where ball is
pyautogui.moveTo(300,400)
pyautogui.click();
main()
抱歉,如果这令人困惑,但 brain.exe 已停止工作 :( 谢谢
【问题讨论】:
-
当你点击球时它是动画的还是只是一个从左到右向上移动的图像?
-
不仅仅是动态图像
-
如果问题得到解答,您可以将其标记为已解决。
标签: python opencv image-processing pygame artificial-intelligence