Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imread returns None, violating assertion !_src.empty() in function 'cvtColor' error

Tags:

python

opencv

I am trying to do a basic colour conversion in python however I can't seem to get past the below error. I have re-installed python, opencv and tried on both python 3.4.3 (latest) and python 2.7 (which is on my Mac).

I installed opencv using python's package manager opencv-python.

Here is the code that fails:

frame = cv2.imread('frames/frame%d.tiff' % count) frame_HSV= cv2.cvtColor(frame,cv2.COLOR_RGB2HSV) 

This is the error message:

cv2.error: OpenCV(3.4.3) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor' 
like image 737
Charklewis Avatar asked Oct 06 '18 05:10

Charklewis


People also ask

What does cvtColor return?

The cvtColor() function returns the image with changed color space.

What is the purpose of cv2 cvtColor () function?

cv2. cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV.

What does CV Imread return?

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).


2 Answers

This error happened because the image didn't load properly. So you have a problem with the previous line cv2.imread. My suggestion is :

  • check if the image exists in the path you give

  • check if the count variable has a valid number

like image 98
shaked litbak Avatar answered Sep 23 '22 22:09

shaked litbak


If anyone is experiencing this same problem when reading a frame from a webcam:

Verify if your webcam is being used on another task and close it. This wil solve the problem.

I spent some time with this error when I realized my camera was online in a google hangouts group. Also, Make sure your webcam drivers are up to date

like image 24
Leonardo Mariga Avatar answered Sep 23 '22 22:09

Leonardo Mariga