【问题标题】:cv2.error: OpenCV(4.5.2) ???? error: (-5:Bad argument) in function 'cvtColor'cv2.error: OpenCV(4.5.2) ????错误:(-5:错误参数)在函数'cvtColor'中
【发布时间】:2021-09-09 15:02:31
【问题描述】:

这是我的代码,我遇到了问题。请问有什么办法解决? 我做了一个屏幕录像机:

import numpy as np
import cv2
import pyautogui

codec = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("Recorded.avi", codec, 60, (1366,768))
cv2.namedWindow("Recording", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Recording", 480, 270)

while True:
     img = pyautogui.screenshot #capturing screenshot
     frame = np.array(img) # converting the image into numpy array representation
     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # converting the BGR image into RGB image
     out.write(frame) # writing the RBG image to file
     cv2.imshow('Recording', frame) # display screen/frame being recorded
     if cv2.waitKey(1) == ord('q'): # Wait for the user to press 'q' key to stop the recording
          break

 out.release() # closing the video file
 cv2.destroyAllWindows() # destroying the recording window

这里的问题:

Traceback(最近一次调用最后一次): 文件“C:\Users\mhmdj\PycharmProjects\learn\main.py”,第 13 行,在 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 将BGR图像转换为RGB图像 cv2.error: OpenCV(4.5.2) ????错误:(-5:Bad argument) in function 'cvtColor'

重载解析失败:

不支持 src 数据类型 = 17 参数“src”的预期 Ptrcv::UMat

【问题讨论】:

  • 你可以试试frame = np.array(img, dtype="uint8")吗?
  • 数据类型 17 可能是 CV_8SC3,这是一个奇怪的类型
  • 您确定不必在此处拨打电话吗?:img = pyautogui.screenshot

标签: python numpy opencv screen-resolution pyautogui


【解决方案1】:

您可以尝试使用 cv2.imread() 而不是 np.array() 加载图像,看看是否可行,因为示例使用此方法:

img = cv2.imread(imagePath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

【讨论】:

  • Anders Elmgren 谢谢你,兄弟,我试过了,它可以工作,但问题是程序在工作时它只是打开图像但没有记录,我想要以前的问题的解决方案跨度>
猜你喜欢
  • 2022-10-17
  • 2021-07-30
  • 2020-06-26
  • 2019-10-07
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
相关资源
最近更新 更多