图像数据的numpy形状的尺寸顺序是什么? [英] What is dimension order of numpy shape for image data?
问题描述
我正在使用nibabel
lib从nii文件中加载数据.我在 http://nipy.org/nibabel/gettingstarted.html 上阅读了lib的文档. ,并发现
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
此信息可用,而无需将任何主图像数据加载到内存中.当然也可以将图像数据作为NumPy数组访问
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
这是我的代码,用于加载数据及其形状
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
我得到了结果
128, 128, 64
什么是数据形状顺序?是WidthxHeightxDepth
吗?而且我的输入必须安排为depth, height, width
.因此,我将使用input=data.transpose(2,0,1)
.这样对吗?谢谢大家
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
更新:我发现Numpy将按命令Height x Width x Depth
读取图像作为参考 http://www.python-course.eu/images/axis.jpeg
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
推荐答案
好的,这是我的看法:
使用scipy.ndimage.imread('img.jpg', mode='RGB')
,结果数组将始终具有以下顺序:(H, W, D)
即((高度,宽度,深度)),因为numpy用于ndarrays (axis=0, axis=1, axis=2)
或类似地使用(Y, X, Z)
的术语(如果愿意)可以在三个维度上可视化.
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.
# 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视为元组,
If you consider the img_shape as a tuple,
# 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 :还应该注意,像tensorflow这样的库也(几乎)遵循与numpy相同的约定.
PS: It should also be noted that libraries like tensorflow also (almost) follows the same convention as numpy.
uint8类型的张量.形状为
[height, width, channels]
这篇关于图像数据的numpy形状的尺寸顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!