我在Raspberry-Pi模块上实现了这个代码,从文件夹中读取png图像并将其转换为灰色,代码如下:
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)
但是我收到以下错误:
OpenCV错误:断言失败(scn == 3 || scn == 4)在cvtColor中,文件/home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp,line 3205 Traceback(大多数)最近调用last):文件灰色= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)cv2.error:/home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp:3739:错误:( - 215)scn == 3 || scn == 4 in function cvtColor
答案 0 :(得分:1)
通常,如果图像为None
,则会发生此断言。尝试先检查图像是否正确读取。
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)
如果您发现它没有做任何事情因为img是None
,请检查您的目录并检查它是否正在寻找正确的图像