OpenCV读取图片时 error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘cv::imshow‘
问题描述:
OpenCV使用imread()
读取图片文件并使用cv2.imshow()
显示图片时,如下:
import cv2
img = cv2.imread(img_path)
cv2.imshow('image melon',img)
cv2.waitKey(0)
cv2.destoryAllWindows()
会产生报错:
error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘cv::imshow‘
原因分析及解决方案:
情况一:img_path
中的路径或者图片文件中有汉字字符;
解决方案:创建新的路径,或将图片文件中的汉字改成英文/数字/下划线等合法字符。
情况二:使用
for img in os.listdir(self.filepath):
self.imgname.append(self.filepath+'/'+img)
print(self.imgname) # 应该是输出输入文件夹的各文件名
遍历文件夹[filepath]中的文件(夹),并以此当做图片文件输出时,里面混有文件夹或其他类型文件;
解决方案:删去其他文件,只保留图片文件。
注意:
情况二情况特殊,容易忽略,请仔细辨别。