0

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

  • `cv2.imread()` expects a string, which is the path to your stored image afaik. I guess, that is your problem. Have a look [here](https://www.geeksforgeeks.org/python-opencv-cv2-imread-method/). – Roland Deschain Dec 29 '20 at 07:33
  • 2
    You might have a look into this thread here https://stackoverflow.com/questions/34588464/python-how-to-capture-image-from-webcam-on-click-using-opencv – Andreas B Dec 29 '20 at 07:44
  • @AndreasB good point, as it seems like OP wants to capture images from the webcam rather than load some stored ones :) – Roland Deschain Dec 29 '20 at 07:52
  • @AndreasB post that as an answer – Christoph Rackwitz Dec 29 '20 at 08:28
  • You might have a look into this thread here. https://stackoverflow.com/questions/34588464/python-how-to-capture-image-from-webcam-on-click-using-opencv – Andreas B Jan 04 '21 at 17:01

3 Answers3

0

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)
Kwon Jungsu
  • 71
  • 1
  • 3
0

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.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
0

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