简体   繁体   中英

cv2.imread error:img = cv2.imread(0) SystemError: <built-in function imread> returned NULL without setting an error

I was making a simple program to take an image from the camera but a strange error has occurred:

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'

And here is the simple code:

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

pls help

as Roland mentioned in comment

you should provide path to image you want to open

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

if you want to read from a camera, imread is the wrong procedure. its purpose is to read picture files.

use OpenCV's VideoCapture instead.

The following code will take the picture when you press c from your keyboard then it will store images in your current directory. Hope this will work for you, peace!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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