【问题标题】:Convert a 3d array to a 4d array将 3d 数组转换为 4d 数组
【发布时间】:2021-01-14 10:19:36
【问题描述】:

我有一个矩阵,我正在获得一个 2 通道矩阵,其图像大小为 256x120。 现在,我需要存储多张图像,因此我需要将矩阵重塑为 (No.ofimages,256,120,2)。

我尝试使用 reshape 然后追加:

但我在使用 reshape 时得到了 TypeError: 'builtin_function_or_method' object is not subscriptable

关于如何解决它的任何想法?

【问题讨论】:

  • 您无法将 (112, 112, 2) 重塑为 (14394,112,112,2)。不确定您使用的 input_shape 是什么。
  • 您应该创建大小为(14394,112,112,2) 的矩阵,然后将您的第一个大小为(112, 112, 2) 的图像作为第一个元素存储在其中。

标签: python arrays reshape


【解决方案1】:

根据我目前对您的问题的理解:

import numpy as np

img = np.random.random((112,112,2))
print(img.shape)

result = np.empty((0, 112, 112, 2))  # first axis is zero, for adding images along it

for i in range(100):        # replace this loop with something that reads in the images
    result = np.append(result, img[np.newaxis, ...], axis=0)    # add a new axis to each image and append them to result

print(result.shape)

将产生:

(112, 112, 2)
(100, 112, 112, 2)

要访问存储在结果变量中的图像,只需使用索引:

print(result[1].shape)   # e.g., access the second image

将产生:

(112, 112, 2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2019-01-08
    • 1970-01-01
    • 2021-08-06
    • 2019-11-07
    相关资源
    最近更新 更多