【问题标题】:Trying to template match on screen尝试在屏幕上匹配模板
【发布时间】:2020-12-09 01:47:51
【问题描述】:

我目前正在屏幕上制作一个对象查找器,但这是我的代码似乎无法正常工作的东西我收到错误AttributeError: 'numpy.ndarray' object has no attribute 'read'

我的对象查找器代码:

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


def find(file):
    while True:
        Screen = np.array(ImageGrab.grab(bbox=(13, 32,805, 623)))
        gray = cv2.cvtColor(Screen, cv2.COLOR_BGR2GRAY)
        
    
    
        img_rgb = Screen.read()
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(file,0)
        w, h = template.shape[::-1]
        
        res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
        threshold = 0.8
        loc = np.where( res >= threshold)
        for pt in zip(*loc[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
        
        
        
        k = cv2.waitKey(30) & 0xff
    
        if k == 27:
            cv2.destroyAllWindows()
            break
        
        
        cv2.imshow('Frame', gray)

find('13.png')

发生这种情况有什么原因吗?

【问题讨论】:

    标签: python numpy python-imaging-library cv2


    【解决方案1】:

    我不确定你想用Screen.read 完成什么,但这似乎没有必要。您可以将Screen 直接传递给cv2.cvtColor

    img_gray = cv2.cvtColor(Screen, cv2.COLOR_BGR2GRAY)
    

    一个 numpy 数组没有错误提到的方法(或属性)read

    【讨论】:

    • 我正在尝试寻找实时屏幕对象检测
    • Screen 直接传递给cvtColor 是否适合您的情况?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多