Joh*_*ohn 12 python numpy shape scipy python-2.7
我使用nibabel
lib从nii文件加载数据.我在http://nipy.org/nibabel/gettingstarted.html上阅读了lib的文档,并发现了这一点
无需将任何主图像数据加载到存储器中即可获得该信息.当然,还可以将图像数据作为NumPy阵列访问
这是我的代码加载数据和它的形状
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
Run Code Online (Sandbox Code Playgroud)
我得到了结果
128, 128, 64
Run Code Online (Sandbox Code Playgroud)
什么是数据形状的顺序?是WidthxHeightxDepth
吗?我的意见必须安排为depth, height, width
.所以我会用input=data.transpose(2,0,1)
.这样对吗?谢谢大家
更新:我发现Numpy将按顺序读取图像Height x Width x Depth
作为参考http://www.python-course.eu/images/axis.jpeg
kma*_*o23 16
好的,这是我的看法:
使用时scipy.ndimage.imread('img.jpg', mode='RGB')
,生成的数组将始终具有以下顺序:(H, W, D)
即(高度,宽度,深度),因为numpy用于ndarray的术语(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)
Run Code Online (Sandbox Code Playgroud)
如果你认为img_shape是一个元组,
# index (0, 1, 2)
img_shape = (634, 1366, 3)
# or index (-3, -2, -1)
Run Code Online (Sandbox Code Playgroud)
选择哪一种方便您记忆.
PS:还应该注意像tensorflow这样的库也(几乎)遵循与numpy相同的约定.
uint8类型的张量.3-D形状
[height, width, channels]