繁体   English   中英

我如何获得opencv中显示的图像坐标

[英]how do i get coordinates of image shown in opencv

对不起,但标题没有意义

我正在尝试制作一个点击球使其反弹的人工智能。 对于上下文这里是应用程序的图片在此处输入图片说明

在游戏中,当您点击球时,它会上升然后又下降,游戏的目的是保持它。

我写了一些代码,用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 已停止工作:( 谢谢

你可以这样做:

1.从屏幕截图中裁剪球的图像,等等。 喜欢

img = cv2.imread("screenshot.jpg")
crop_img = img[y:y+h, x:x+w] # you will have to look for the parameters by trial and error

2.使用模板匹配来查看球在图像中的位置

3. 获得结果矩形中间的点并将鼠标移动到那里

我希望这会有所帮助,如果您需要有关如何实现其中任何一项的更多帮助,请随时询问

当您删除它并看到您在定位球时遇到性能问题时,我正在处理您的另一个相关问题。 由于您的球看起来在漂亮、简单的白色背景上(除了得分和右上角的close按钮),找到球的方法更容易/更快。

首先,在灰度中工作,以便您只有 1 个通道,而不是 3 个 RGB 通道来处理 - 这通常更快。

然后,用白色像素覆盖右上角的分数和菜单,以便图像中唯一剩下的就是球。 现在反转图像,使所有白色变为黑色,然后您可以使用findNonZero()找到任何不是背景的东西,即球。

现在找到 y 方向上的最低和最高坐标,并将它们平均为球的中心,同样在 x 方向上以另一种方式。

#!/usr/bin/env python3

# Load image - work in greyscale as 1/3 as many pixels
im = cv2.imread('ball.png',cv2.IMREAD_GRAYSCALE)

# Overwrite "Current Best" with white - these numbers will vary depending on what you capture
im[134:400,447:714] = 255

# Overwrite menu and "Close" button at top-right with white - these numbers will vary depending on what you capture
im[3:107,1494:1726] = 255

# Negate image so whites become black
im=255-im

# Find anything not black, i.e. the ball
nz = cv2.findNonZero(im)

# Find top, bottom, left and right edge of ball
a = nz[:,0,0].min()
b = nz[:,0,0].max()
c = nz[:,0,1].min()
d = nz[:,0,1].max()
print('a:{}, b:{}, c:{}, d:{}'.format(a,b,c,d))

# Average top and bottom edges, left and right edges, to give centre
c0 = (a+b)/2
c1 = (c+d)/2
print('Ball centre: {},{}'.format(c0,c1))

这给出了:

a:442, b:688, c:1063, d:1304
Ball centre: 565.0,1183.5

其中,如果我在节目中画一个红色框:

在此处输入图片说明

在我的 Mac 上处理需要 845 微秒,或者不到一毫秒,相当于每秒 1,183 帧。 显然你有时间抢屏幕,但我无法控制。

请注意,您还可以在每个方向上将图像缩小 4(或者 8 或 16)倍,并且仍然可以确保找到球,这可能会使其更快。

关键词:球、跟踪、跟踪、定位、查找、位置、图像、图像处理、python、OpenCV、numpy、边界框、bbox。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM