使用 cv2 到 read/resize 个文件

Using cv2 to read/resize files

我想使用 cv2 到 read/resize 个文件,但出现错误。

我该如何解决这个错误?

错误OpenCV(4.4.0) ..\modules\imgproc\src\resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

代码:

def read_and_process_image(list_of_images):
    """
    Returns two arrays: 
        X is an array of resized images
        y is an array of labels
    """
    X = [] # images
    y = [] # labels
    
    for image in list_of_images:
        X.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (nrows,ncolumns), interpolation=cv2.INTER_CUBIC))
        #get the labels
        if 'car' in image:
            y.append(1)
        elif 'neg' in image:
            y.append(0)
    
    return X, y

X, y = read_and_process_image(train_imgs)

您未能读取图像,很可能您没有将图像的完整路径提供给 cv2.imread 函数,因此 cv2.resize 函数接收 None

你需要这样的东西:

cv2.imread(os.path.join(image_path, image), cv2.IMREAD_COLOR)

确保您 import os 首先位于代码的顶部。

用图像的路径填充 image_path 变量。