1

我正在尝试使用 mss 库进行屏幕截图添加使用下面的代码显示它,但每次都会出现相同的错误。是否有解决此错误的方法

类型错误:参数“mat”的预期 Ptr<cv::UMat>

** 我在 Macos 而不是 windows 中使用它

import cv2 as cv
import numpy as np
import os
from time import time
from mss import mss

os.chdir(os.path.dirname(os.path.abspath(__file__)))

loop_time = time()
with mss() as sct:
    while (True):
        
        monitor_1 = sct.monitors[1]  # Identify the display to capture
        screenshot = sct.grab(monitor_1)
        
        cv.imshow('result', screenshot)
        
        print('FPS {}',format(1 / (time() - loop_time)))
        loop_time = time()
        
        if cv.waitKey(1) == ord('q'):
            cv.destroyAllWindows()
            break
    

print('done')
4

1 回答 1

3

您必须将“屏幕截图”转换为 cv::UMat :

import time

import cv2
import mss
import numpy


with mss.mss() as sct:
    # Part of the screen to capture
    monitor = {"top": 40, "left": 0, "width": 800, "height": 640}

    while "Screen capturing":
        last_time = time.time()

        # Get raw pixels from the screen, save it to a Numpy array
        img = numpy.array(sct.grab(monitor))

        # Display the picture
        cv2.imshow("OpenCV/Numpy normal", img)

        # Display the picture in grayscale
        # cv2.imshow('OpenCV/Numpy grayscale',
        #            cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))

        print("fps: {}".format(1 / (time.time() - last_time)))

        # Press "q" to quit
        if cv2.waitKey(25) & 0xFF == ord("q"):
            cv2.destroyAllWindows()
            break

这是这里的例子

于 2020-12-26T20:44:52.323 回答