如何解决将 3d 数组转换为 4d 数组
我有一个矩阵,我正在获得一个 2 通道矩阵,图像大小为 256x120。 现在,我需要存储多个图像,因此我需要将矩阵重塑为 (No.ofimages,256,120,2)。
我尝试使用 reshape 然后追加:
但是我在使用 reshape 时得到了 TypeError: 'builtin_function_or_method' object is not subscriptable
关于如何解决它的任何想法?
解决方法
根据我目前对您的问题的理解:
import numpy as np
img = np.random.random((112,112,2))
print(img.shape)
result = np.empty((0,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,2)
(100,2)
要访问存储在结果变量中的图像,只需使用索引:
print(result[1].shape) # e.g.,access the second image
将产生:
(112,2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。