我正在尝试从文件中读取图像,但这给了我这个错误-
cv2.error:OpenCV(4.0.0) C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp:181: 错误:(-215:声明失败)函数中的__src.empty() 'cv :: cvtColor'
我也曾尝试将src_path上的“ /”更改为“ \”,但这没有用。有什么想法吗?
这是代码-
def create_imageset(excludeHardClasses=True):
#Variables
images_path = "C:/Users/bipin/Anaconda3/ASLConvNet-master/src/images/"
#Read for images folder
image_files = [f for f in listdir(images_path) if isfile(join(images_path, f))]
#processing and reading image files
image_set = [] #contains all images
for i in image_files :
#split
info = i.split('_')
if excludeHardClasses and info[1] in exclude_label_list:
continue
if info[1] == 'o':
info[1] = '0'
if info[1] == 'v':
info[1] = '2'
matrix = cv2.imread(images_path + '/' + i)
RGB_img = cv2.cvtColor(matrix, cv2.COLOR_BGR2RGB)
答案 0 :(得分:1)
尽量使用 \\
而不是 \
。因为在 Python 中 \
用于转义和获取 \
,您必须使用 \\
。这表示反斜杠选项。
根据你的代码应该是
images_path = "C:\\Users\\bipin\\Anaconda3\\ASLConvNet-master\\src\\images\\"
答案 1 :(得分:0)
记住isfile(join(images_path, f))
不仅返回图像,还返回list of all regular files。
我怀疑您已读取不是图像的文件。这将使您的matrix
变量None
。当您使用cv2.cvtColor
值调用None
时,会看到此错误。
如果您尝试读取png
文件,则可以尝试
image_files = [f for f in listdir(images_path) if f.endswith(".png")]
此外,您可能需要更改
matrix = cv2.imread(images_path + '/' + i)
到
matrix = cv2.imread(join(images_path, i))
答案 2 :(得分:-2)
这对我有用。试试:
def create_imageset(excludeHardClasses=True):
#Variables
images_path = "C:\Users\bipin\Anaconda3\ASLConvNet-master\src\images\\"
#Read for images folder
image_files = [f for f in listdir(images_path) if isfile("".join(images_path, f))]
#processing and reading image files
image_set = [] #contains all images
for i in image_files :
#split
info = i.split('_')
if excludeHardClasses and info[1] in exclude_label_list:
continue
if info[1] == 'o':
info[1] = '0'
if info[1] == 'v':
info[1] = '2'
matrix = cv2.imread(images_path + '\\' + i)
RGB_img = cv2.cvtColor(matrix, cv2.COLOR_BGR2RGB)