0
votes

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

3
cv2.imread() expects a string, which is the path to your stored image afaik. I guess, that is your problem. Have a look here.Roland Deschain
You might have a look into this thread here stackoverflow.com/questions/34588464/…Andreas B
@AndreasB good point, as it seems like OP wants to capture images from the webcam rather than load some stored ones :)Roland Deschain
@AndreasB post that as an answerChristoph Rackwitz
You might have a look into this thread here. stackoverflow.com/questions/34588464/…Andreas B

3 Answers

0
votes

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)
0
votes

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.

0
votes

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