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

What is dimension order of numpy shape for image data?

我正在使用 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

好的,这是我的看法:

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

# 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_shape 视为一个元组,

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

选择一种便于记忆的方式。


注意scipy.ndimage.imread() API has been removed since Scipy 1.2.0. So, it is now recommended to use imageio.imread()读取图像和returns数组,numpy数组的一个子类,遵循上面讨论的相同约定。

# read image
$ img = imageio.imread('suza.jpg', format='jpg')

# convert the image to a numpy array
$ img_np = np.asarray(img)

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

tf.image_decode_jpeg() returns:

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