TypeError: Expected Ptr<cv::UMat> for argument 'mat' using mss library in python

TypeError: Expected Ptr<cv::UMat> for argument 'mat' using mss library in python

我正在尝试使用 mss 库添加屏幕截图并使用以下代码显示它,但每次都出现相同的错误。 是否有针对此错误的修复程序

TypeError: Expected Ptr<cv::UMat> for argument 'mat'

** 我在 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')

您必须将“屏幕截图”转换为 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

这是来自 here

的示例