将 3 维数组转换为 4 维数组
Convert a 3d array to a 4d array
我有一个矩阵,我正在获取一个 2 通道矩阵,其中图像大小为 256x120。
现在,我需要存储多张图像,因此我需要将矩阵重塑为 (No.ofimages,256,120,2).
我尝试使用重塑然后追加:
但是我在使用 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, 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)
我有一个矩阵,我正在获取一个 2 通道矩阵,其中图像大小为 256x120。 现在,我需要存储多张图像,因此我需要将矩阵重塑为 (No.ofimages,256,120,2).
我尝试使用重塑然后追加:
但是我在使用 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, 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)