1
votes

I have implemented this code on Raspberry-Pi module to read png images from a folder and convert it to gray, the code is as follows:

x = glob.glob("/home/pi/pngimages/ss*png")

for imagefile in x[0300:0302]:

 img = cv2.imread(imagefile)

 gray = cvt.cvtColor(img,cv2.COLOR_BGR2GRAY)

but I get the following error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp, line 3205 Traceback (most recent call last): File in gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor

1
Have you tried printing imagefile before the imread? Your glob call could return files of the form sstextpng. Probably best to make it ss*.png and retest.Martin Evans
x = glob.glob("/home/pi/png_images/ss*.png") for imagefile in x[0300:0302]:... this is what i use but what i could observe was cvt.cvtColor() function works for one image but when the loop runs for second image i get that error..?prinks

1 Answers

1
votes

Generally this assertion happens if the image is None. Try checking the image is being read properly first.

x = glob.glob("/home/pi/pngimages/ss*png")

for imagefile in x[0300:0302]:
    img = cv2.imread(imagefile)
    # You can do a print img.shape here if you want to see what's going on
    # If it returns NULL, something's wrong with your image or the path or something else
    if img:
        gray = cvt.cvtColor(img,cv2.COLOR_BGR2GRAY)

If you find it doesn't do anything because img is None, check your directory and check that it's looking for the correct images

Have a look here as well: Python-OpenCV cv2 OpenCV Error: Assertion failed (scn == 3 || scn == 4) in unknown function, file ..\..\..\modules\imgproc\src\color.cpp