Ask Your Question
0

error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

asked 2020-01-02 11:14:08 -0600

kpyopvision gravatar image

Following an OpenCV tutorial. Followed instructions on https://pypi.org/project/opencv-contr.... The package works great until the tutorial asks to include the following in the script

gray = cv2.cvtColor(color, cv2.COLOR_RGB2GRAY)

Write here how did you expect the library to function. The tutorial asks to create a script to transform different layers Following the video it is supposed to split the channels and convert them to grayscale. I'm not 100% sure since I'm new.

Actual behaviour The following error occurs when running the command:

python3 02_05.py

with pwd showing that I am in the required folder

File "02_05.py", line 6, in <module> gray = cv2.cvtColor(color, cv2.COLOR_RGB2GRAY) cv2.error: OpenCV(4.1.2) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:182:

error: (-215:Assertion failed) !_src.empty() in function 'cvtColor' I don't have a user called Travis on my computer in case that is not obvious Steps to reproduce example code:: This is the full script::

   import numpy as np

   import cv2
   color = cv2.imread("butterfly,jpg",1 )

gray = cv2.cvtColor(color, cv2.COLOR_RGB2GRAY)
cv2.imwrite("gray.jpg", gray)

b = color [:,:, 0]
g = color [:,:, 1]
r = color [:,:, 2]

rgba = cv2.merge((b,g,r, g))
cv2.imwrite("rgba.png", rgba)

` That was the full script

operating system : Mac OSX Mojave 10.14.4 architecture (e.g. x86) : Mac OSX Mojave 10.14.4 (Apple?) opencv-python version : opencv-contrib-python 4.1.2.30 Thank you.

edit retag flag offensive close merge delete

Comments

Check image path

LBerger gravatar imageLBerger ( 2020-01-02 11:25:52 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2020-01-02 15:00:58 -0600

supra56 gravatar image

updated 2020-01-02 15:01:54 -0600

Typo error coma to period(.).

color = cv2.imread("butterfly,jpg",1 )

to:

color = cv2.imread("butterfly.jpg", 1 )
edit flag offensive delete link more

Comments

1

ah , cool found the reason for the load failure ;9

berak gravatar imageberak ( 2020-01-02 15:32:38 -0600 )edit
1

Thank you, that was very kind of you. spotted. And I feel like the very thing that I should have typed in the first place -- a small little spot.

Other comments also noted

if np.shape(img) == (): # using py3 and newer numpy

Now, I just need a hole big enough to slither in

kpyopvision gravatar imagekpyopvision ( 2020-01-02 18:29:33 -0600 )edit

@kpyopvision. I can't test it for you. Because I don't used Mac OSX Mojave. I used Raspberry pi linux

supra56 gravatar imagesupra56 ( 2020-01-02 21:31:46 -0600 )edit
1

answered 2020-01-02 11:50:09 -0600

berak gravatar image

updated 2020-01-02 11:55:08 -0600

it is pretty simple.

to the dismay of most python noobs, cv2.imread() does NOT throw an exception on failure,

it's your resposibility to check the outcome similar to:

img = cv2.imread("butterfly,jpg",1 )
if img == None: # older numpy / py2
     # fail !!

or:

if np.shape(img) == (): # latest numpy / py3
     # fail !!

if you don't, your NEXT line of code will fail (because you supplied invalid input), to ultimate confusion of anyone ...

ps: the code path in the error msg points to the location (in the underlyingc++ code), where it was built (svark's box in the case of pypi), not your one.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-01-02 11:14:08 -0600

Seen: 99,215 times

Last updated: Jan 02 '20