【问题标题】:Resize 3D array to 2D将 3D 数组大小调整为 2D
【发布时间】:2018-08-17 03:25:35
【问题描述】:

我有一些看起来像这样的代码

    number_of_pairs = int(len(path_list) / 2)
    pairs_of_images = [np.zeros(
        (number_of_pairs, self.image_height, self.image_height, 1)) for i in range(2)]
    labels = np.zeros((number_of_pairs, 1))
    size = 105,105
    for pair in range(number_of_pairs):
        image = Image.open(path_list[pair * 2])
        image = image.resize((105,105))
        image = np.asarray(image).astype(np.float64)
        print("before resize is{}".format(image))

        pairs_of_images[0][pair, :, :, 0] = image

但是,我在哪里遇到了一个错误

pairs_of_images[1][pair, :, :, 0] = 图像
值错误:不能 将输入数组从形状 (105,105,4) 广播到形状 (105,105)

有没有办法摆脱数组的第三维?

【问题讨论】:

  • 我建议您仔细研究一下您从 np.asarray() 中得到了什么。你让它从你传入的数据中推断出数据类型,你知道它实际上在做什么吗?你能依赖它吗?
  • 那个 3d 维度可能对颜色进行编码。如果是这样,没有它你只会有一个黑白图像。
  • 看起来像对设置为拍摄单通道图像,但您正在加载 3 通道图像。

标签: python numpy


【解决方案1】:

替换

image = Image.open(path_list[pair * 2])

image = Image.open(path_list[pair * 2]).convert('L')

“L”模式将图像转换为单通道图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2011-06-20
    • 2010-10-07
    • 2016-06-01
    相关资源
    最近更新 更多