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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.