cv2.imread 错误:img = cv2.imread(0) SystemError: <内置函数 imread> 返回 NULL 没有设置错误

时间:2020-12-29 07:26:28

标签: python opencv cv2 imread

我正在编写一个简单的程序来从相机拍摄图像,但发生了一个奇怪的错误:

Traceback (most recent call last):
File "the path was deleted for stackoverflow", line 3, in <module>
cv2.imshow("",img)
TypeError: Expected Ptr<cv::UMat> for argument 'mat'

这是简单的代码:

import cv2
img = cv2.imread(0)
cv2.imshow("",img)

请帮忙

3 个答案:

答案 0 :(得分:0)

正如 Roland 在评论中提到的

您应该提供要打开的图像的路径

import cv2
imageArray = cv2.imread('path/to/image.jpg')
cv2.imshow("",imageArray)

答案 1 :(得分:0)

如果您想从相机读取,imread 是错误的过程。其目的是读取图片文件。

改用 OpenCV 的 VideoCapture

答案 2 :(得分:0)

当您从键盘上按 c 键时,以下代码将拍摄图片,然后将图像存储在您的当前目录中。希望这对你有用,平安!

import cv2

cap = cv2.VideoCapture(0)

width = 400
height = 300
num = 0
while True:
    ret, frame = cap.read()
    frame = cv2.resize (frame, (width, height))
    cv2.imshow("frame", frame)
    #When you press c then it would capture a picture and write on the same folder
    if cv2.waitKey(1) & 0xff == ord('c'):
        cv2.imwrite("./Image_"+str(num)+".jpg", frame)
        num+=1
    if cv2.waitKey(1) & 0xff == ord('q'):
        break
cv2.destroyAllWindows()
相关问题