numpy数组形状中图像数据的维度顺序是什么?

2024-05-20 17:59:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用nibabel库从nii文件加载数据。我在http://nipy.org/nibabel/gettingstarted.html阅读了lib的文档,发现

This information is available without the need to load anything of the main image data into the memory. Of course there is also access to the image data as a NumPy array

这是我的代码来加载数据

import nibabel as nib
img = nib.load('example.nii')
data = img.get_data()
data = np.squeeze(data)
data = np.copy(data, order="C")
print data.shape

我得到了结果

128, 128, 64

数据形状的顺序是什么?是不是WidthxHeightxDepth?我的输入必须按depth, height, width排列。所以我会用input=data.transpose(2,0,1)。对吗?谢谢大家

更新:我发现Numpy将按顺序读取图像Height x Width x Depth作为引用http://www.python-course.eu/images/axis.jpeg


Tags: theto数据imagehttpimgdatais
1条回答
网友
1楼 · 发布于 2024-05-20 17:59:19

好吧,这是我的看法:

使用scipy.ndimage.imread('img.jpg', mode='RGB'),结果数组将始终具有以下顺序:(H, W, D),即(高度、宽度、深度),因为numpy使用ndarrays的术语(axis=0, axis=1, axis=2),或者类似的(Y, X, Z),如果您想在三维中可视化的话。

# read image
In [21]: img = scipy.ndimage.imread('suza.jpg', mode='RGB')

# image shape as (H, W, D)
In [22]: img.shape
Out[22]: (634, 1366, 3)

# transpose to shape as (D, H, W)
In [23]: tr_img = img.transpose((-1, 0, 1))    

In [23]: tr_img.shape
Out[23]: (3, 634, 1366)

如果你把img_形状看作一个元组

#  index    (0,   1,    2)
img_shape = (634, 1366, 3)
# or index  (-3,  -2,  -1)

选择哪一个对你来说是一种方便的记忆方式。


PS:还应该注意,像tensorflow这样的库也(几乎)遵循与numpy相同的约定。

tf.image_decode_jpeg()返回:

A Tensor of type uint8. 3-D with shape [height, width, channels]

相关问题 更多 >