cv2.error:函数'cv :: cvtColor'中的OpenCV(4.0.0)(-215:断言失败)!_src.empty()

时间:2019-03-14 06:00:20

标签: python-3.x opencv

我正在尝试从文件中读取图像,但这给了我这个错误-

  

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)

3 个答案:

答案 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)