繁体   English   中英

将 3d 数组转换为 4d 数组

[英]Convert a 3d array to a 4d array

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

我尝试使用 reshape,然后使用 append:

但是我收到一个TypeError: 'builtin_function_or_method' object is not subscriptable when using reshape

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

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM