Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm attempting to follow the instructional exercise here and I continue getting the accompanying mistake. 

The issue is on the grounds that cvtColor just takes 8 or 16-bit images

My picture is a png, from plt.savefig() in matplotlib and I think it is a 24-bit picture

1) Is my diagnosis of the problem correct or am I completely wrong?

2) Assuming 1) is true, I have been looking for ways to turn the 24-bit image into a 8/16 bit, but I haven't found anything so far. My matplotlib plot was a picture saved from a numpy array that is saved as array.npy file. Should I try to modify the array.npy file instead?

Code for reference:

import cv2 as cv

im = cv.imread("binary_graph_8.png")

imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)

ret, thresh = cv.threshold(imgray, 127, 255, 0)

im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

Error message:

File "C:/Users/ek247/.PyCharm2018.3/config/scratches/scratch_1.py", line 5, in <module>

    imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

1 Answer

0 votes
by (26.4k points)

Please check the answer below:

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

- check if the images exist in the path you give

- check the count variable if he have valid number

I hope, this helps you

Looking for a good python tutorial course? Join the python certification course and get certified.

For more details, do check out the below video tutorial...

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...