Fix Python – imread returns None, violating assertion !_src.empty() in function ‘cvtColor’ error

Question

Asked By – Charklewis

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'

Now we will see solution for issue: imread returns None, violating assertion !_src.empty() in function ‘cvtColor’ error


Answer

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

This question is answered By – shaked litbak

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0