简体   繁体   English

Python OpenCV imshow 预期的垫子参数

[英]Python OpenCV imshow Expected mat Argument

I'm trying to write a program that shows a specific position on a given window.我正在尝试编写一个程序,在给定的 window 上显示特定的 position。 When I run the code OpenCV gives me an error message telling me unexpected type for argument mat.当我运行代码 OpenCV 时,我会收到一条错误消息,告诉我参数垫的意外类型。 I have no idea what's wrong because when I use the position value in the following code it works fine.我不知道出了什么问题,因为当我在以下代码中使用 position 值时,它可以正常工作。 Please be nice as I'm fairly new to OpenCV.请善待,因为我对 OpenCV 还很陌生。

Code:代码:

#Imports
import numpy as np
import cv2
import pytesseract
from PIL import ImageGrab
import win32gui


#Assign Tesseract and other Variables
winList = []
toplist = []
pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
lH = 33
lW = 20

#Window Handle Object
def enum_win(hwnd, result):

    winText = win32gui.GetWindowText(hwnd)

    winList.append((hwnd, winText))

win32gui.EnumWindows(enum_win, toplist)


typeHwnd = 0

#Find Window Handle
for (hwnd, winText) in winList:

    if winText == "Program WinText Name Here":
        typeHwnd = hwnd
if typeHwnd == 0:
    print("Oops no Window Found")


while True:
    position = win32gui.GetWindowRect(typeHwnd)

    mid = (position[0] + (position[0] + position[2]))/2

    sPos = [int(mid)-267, position[1] + 295, lW, lH]

    screenshot = ImageGrab.grab(bbox=sPos)
    screenshot = np.array(screenshot)

    cv2.imshow("Screen", screenshot)
    key = cv2.waitKey(25)

print(position)
print(mid)

Error Message:错误信息:

Traceback (most recent call last):
  File "C:/Users/MyName/PycharmProjects/Typing Bot/main.py", line 47, in <module>
    cv2.imshow("Screen", screenshot)
TypeError: Expected Ptr<cv::UMat> for argument 'mat'

System Specifications:系统规格:

I am running Windows 10 64 bit and Python 3 with the most recent version of all the imported modules installed.我正在运行 Windows 10 64 位和 Python 3 并安装了所有导入模块的最新版本。

Error shows problem with array in imshow but your problem starts with sPos in grab .错误显示imshow中的array存在问题,但您的问题从grab中的sPos开始。

grab needs bbox which means [x1, y1, x2, y2] but you use [x, y, width, height] and this starts problem because grab returns image with size (0,0) and np.array() can't convert it so it returns pillow image instead of numpy array and then imshow gets pillow image instead of numpy array and it can't display it. bbox grab这意味着[x1, y1, x2, y2]但您使用[x, y, width, height]这会引发问题,因为grab返回大小为(0,0)的图像而np.array()不能将其转换为返回pillow image而不是numpy array ,然后imshow获取pillow image而不是numpy array并且无法显示它。


You have to use [x, y, x+width, y+height]你必须使用[x, y, x+width, y+height]


Minimal working example - with wrong and correct pos最小的工作示例 - 错误和正确的pos

import cv2
import numpy as np
import PIL.ImageGrab

x = 100
y = 100
w = 50 
h = 50 

pos = [x, y, w, h]      # wrong 
#pos = [x, y, x+w, y+h]  # correct 

print('bbox:', pos)
print('---')

img = PIL.ImageGrab.grab(pos)
print('img:', img)
print('type(img):', type(img))
print('img.size:', img.size)
print('---')

arr = np.array(img)
print('arr:', arr)
print('type(arr):', type(arr))
print('arr.size:', arr.size)
print('arr.shape:', arr.shape)
print('---')

cv2.imshow('test', arr)

print('Press ESC to exit')
while cv2.waitKey(100) != 27:
    pass

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

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