【问题标题】:OpenCV2 error trying to convert images from RGB to YCrCb尝试将图像从 RGB 转换为 YCrCb 时出现 OpenCV2 错误
【发布时间】:2020-12-11 17:33:43
【问题描述】:

我正在尝试为机器学习模型准备图像数据集,并且我使用的功能之一是调整大小和更改颜色,但是遇到以下错误消息

  File "prepare_data.py", line 129, in <module>
    data, label = prepare_crop_data(DATA_PATH)
  File "prepare_data.py", line 70, in prepare_crop_data
    hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
cv2.error: OpenCV(4.0.1) C:\ci\opencv-suite_1573470242804\work\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

我已经仔细检查了图像是否包含在我提供的路径中,并且我尝试使用不同的库来加载它们 (io),但这似乎没有帮助。

第 70 行在此函数内(我已在上面注释过):

def prepare_crop_data(_path):
    names = os.listdir(_path)
    names = sorted(names)
    nums = names.__len__()

    data = []
    label = []

    for i in range(nums):
        name = _path + names[i]
        hr_img = cv2.imread(name, cv2.IMREAD_COLOR)
        # Line 70
        hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
        hr_img = hr_img[:, :, 0]
        shape = hr_img.shape

        # two resize operation to produce training data and labels
        lr_img = cv2.resize(hr_img, (shape[1] / scale, shape[0] / scale))
        lr_img = cv2.resize(lr_img, (shape[1], shape[0]))

        width_num = (shape[0] - (BLOCK_SIZE - BLOCK_STEP) * 2) / BLOCK_STEP
        height_num = (shape[1] - (BLOCK_SIZE - BLOCK_STEP) * 2) / BLOCK_STEP
        for k in range(width_num):
            for j in range(height_num):
                x = k * BLOCK_STEP
                y = j * BLOCK_STEP
                hr_patch = hr_img[x: x + BLOCK_SIZE, y: y + BLOCK_SIZE]
                lr_patch = lr_img[x: x + BLOCK_SIZE, y: y + BLOCK_SIZE]

                lr_patch = lr_patch.astype(float) / 255.
                hr_patch = hr_patch.astype(float) / 255.

                lr = numpy.zeros((1, Patch_size, Patch_size), dtype=numpy.double)
                hr = numpy.zeros((1, label_size, label_size), dtype=numpy.double)

                lr[0, :, :] = lr_patch
                hr[0, :, :] = hr_patch[conv_side: -conv_side, conv_side: -conv_side]

                data.append(lr)
                label.append(hr)

    data = numpy.array(data, dtype=float)
    label = numpy.array(label, dtype=float)
    return data, label

第 129 行正好是在 main 函数中调用该函数的时候

if __name__ == "__main__":
    data, label = prepare_crop_data(DATA_PATH)

【问题讨论】:

  • 错误提示 hr_img 为空。检查这是否属实。检查为什么它是空的。 print 您正在构建的文件路径。在pdb 中运行您的程序,单步执行,查看变量。熟悉基本调试。

标签: python machine-learning computer-vision


【解决方案1】:

@WhynarySearch 此错误表示您传递的图像未正确加载或为空,因此每次运行时检查hr_img = cv2.imread(name, cv2.IMREAD_COLOR) 中的名称并支持图像路径

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-21
    • 2014-06-17
    • 2015-12-31
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多