Python 图像数据的 numpy 形状的维数顺序是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43272848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is dimension order of numpy shape for image data?
提问by John
I am using nibabel
lib to load data from nii file. I read the document of the lib at http://nipy.org/nibabel/gettingstarted.html, and found that
我正在使用nibabel
lib 从 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
无需将任何主图像数据加载到内存中即可获得此信息。当然也可以访问图像数据作为 NumPy 数组
This is my code to load the data and it shapes
这是我加载数据和形状的代码
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
I got the result
我得到了结果
128, 128, 64
What is order of data shape? Is it WidthxHeightxDepth
? And my input must arranged as depth, height, width
. So I will use input=data.transpose(2,0,1)
. Is it right? Thanks all
什么是数据形状的顺序?是WidthxHeightxDepth
吗?我的输入必须排列为depth, height, width
. 所以我会用input=data.transpose(2,0,1)
. 这样对吗?谢谢大家
Update: I found that the Numpy will read the image by order Height x Width x Depth
as the reference http://www.python-course.eu/images/axis.jpeg
更新:我发现 Numpy 会按顺序读取图像Height x Width x Depth
作为参考http://www.python-course.eu/images/axis.jpeg
回答by kmario23
OK, here's my take:
好的,这是我的看法:
Using scipy.ndimage.imread('img.jpg', mode='RGB')
, the resulting array will always have this order: (H, W, D)
i.e. (height, width, depth) because of the terminology that numpy uses for ndarrays (axis=0, axis=1, axis=2)
or analogously (Y, X, Z)
if one would like to visualize in 3 dimensions.
使用scipy.ndimage.imread('img.jpg', mode='RGB')
,生成的数组将始终具有以下顺序:(H, W, D)
即 (height, width, depth) 因为 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)
If you consider the img_shape as a tuple,
如果您将 img_shape 视为元组,
# index (0, 1, 2)
img_shape = (634, 1366, 3)
# or index (-3, -2, -1)
Choose which one is a convenient way for you to remember.
选择哪种方式方便您记住。
PS: It should also be noted that libraries like tensorflow also (almost) follows the same convention as numpy.
PS:还应该注意,像 tensorflow 这样的库也(几乎)遵循与 numpy 相同的约定。
tf.image_decode_jpeg()returns:
A Tensor of type uint8. 3-D with shape
[height, width, channels]
uint8 类型的张量。3-D 形状
[height, width, channels]