读 .nii / .nii.gz 图像并转成 numpy 可用 medpy.io、nibabel、itk、SimpleITK 几种方法,然而几种方法读出来的轴序有出入,本篇比较此几种方法。
Datum
所用数据来自 verse,经 iTomxy/data/verse/preprocess.py 预处理,朝向和轴序是 RAI。三视图(可视化:show_nii):
其中最短的是 LR = 35,次短 AP = 118,最长 SI = 214,所以如果维持 RAI 轴序,读出来的形状应是 [35, 118, 214],给下面代码结果作参考。
Test
import os
import numpy as np
import itk
import SimpleITK as sitk
import nibabel as nib
import medpy.io as medio
# 所选的一个 verse 数据
f = os.path.expanduser("~/data/verse/processed-verse19/test/sub-verse012_image.nii.gz")
lab_medio, _ = medio.load(f)
print("medpy:", lab_medio.shape)
lab_nib = nib.load(f)
print("nibabel:", lab_nib.shape)
lab_itk = itk.imread(f)
print("itk:", lab_itk.shape)
lab_itk_np = itk.GetArrayViewFromImage(lab_itk)
print("itk -> numpy:", lab_itk_np.shape)
lab_sitk = sitk.ReadImage(f)
print("sitk:", lab_sitk.GetSize())
lab_sitk_np = sitk.GetArrayFromImage(lab_sitk)
print("sitk -> numpy:", lab_sitk_np.shape)
输出:
medpy: (35, 118, 214)
nibabel: (35, 118, 214)
itk: (214, 118, 35)
itk -> numpy: (214, 118, 35)
sitk: (35, 118, 214)
sitk -> numpy: (214, 118, 35)
结论:
- medpy.io、nibabel 读不改轴序;
- itk 读会逆转轴序(本例即 RAI 变 IAR);
- sitk 读不会,但在转 numpy 时逆了,参考 [1,2]。
References
- Order of images returned by GetArrayFromImage - where is this this documented? #1088
- Conversion between numpy and SimpleITK