几种读nii图像方法的轴序比较

2024-01-08 13:46:41

读 .nii / .nii.gz 图像并转成 numpy 可用 medpy.ionibabelitkSimpleITK 几种方法,然而几种方法读出来的轴序有出入,本篇比较此几种方法。

Datum

所用数据来自 verse,经 iTomxy/data/verse/preprocess.py 预处理,朝向和轴序是 RAI。三视图(可视化:show_nii):
orthogonal-views
其中最短的是 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")

# medpy
lab_medio, _ = medio.load(f)
print("medpy:", lab_medio.shape)

# nibabel
lab_nib = nib.load(f)
print("nibabel:", lab_nib.shape)

# itk(兼测与 numpy、sitk 交叉使用)
lab_itk = itk.imread(f)
print("itk:", lab_itk.shape)
lab_itk_np = itk.GetArrayViewFromImage(lab_itk)
print("itk -> numpy:", lab_itk_np.shape)
# numpy、sitk 保存
np.save("lab_itk_np.npy", lab_itk_np)
lab_itk_np_sitk = sitk.GetImageFromArray(lab_itk_np)
print("lab_itk_np_sitk:", lab_itk_np_sitk.GetSize())
sitk.WriteImage(lab_itk_np_sitk, "lab_itk_np_sitk.nii.gz")
# numpy 重读
lab_itk_np_re = np.load("lab_itk_np.npy")
print("lab_itk_np_re:", lab_itk_np_re.shape)
# itk 重读
lab_itk_np_sitk_itk = itk.imread("lab_itk_np_sitk.nii.gz")
print("lab_itk_np_sitk_itk:", lab_itk_np_sitk_itk.shape)
lab_itk_np_sitk_itk_np = itk.GetArrayViewFromImage(lab_itk_np_sitk_itk)
print("lab_itk_np_sitk_itk -> numpy:", lab_itk_np_sitk_itk_np.shape)
# sitk 重读
lab_itk_np_sitk_sitk = sitk.ReadImage("lab_itk_np_sitk.nii.gz")
print("lab_itk_np_sitk_sitk:", lab_itk_np_sitk_sitk.GetSize())
lab_itk_np_sitk_sitk_np = sitk.GetArrayFromImage(lab_itk_np_sitk_sitk)
print("lab_itk_np_sitk_itk -> numpy:", lab_itk_np_sitk_sitk_np.shape)

# sitk
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)
lab_itk_np_sitk: (35, 118, 214)					# numpy 转 sitk(保存前)
lab_itk_np_re: (214, 118, 35)					# numpy 重读
lab_itk_np_sitk_itk: (214, 118, 35)				# itk 重读
lab_itk_np_sitk_itk -> numpy: (214, 118, 35)	# itk 重读后转 numpy
lab_itk_np_sitk_sitk: (35, 118, 214)			# sitk 重读
lab_itk_np_sitk_itk -> numpy: (214, 118, 35)	# sitk 重读后转 numpy
sitk: (35, 118, 214)
sitk -> numpy: (214, 118, 35)

结论:

  • medpy.io、nibabel 读不改轴序;
  • itk 读会逆转轴序(本例即 RAI 变 IAR);
  • sitk 读不会,但在转 numpy 时逆了,参考 [1,2]。

References

  1. Order of images returned by GetArrayFromImage - where is this this documented? #1088
  2. Conversion between numpy and SimpleITK

文章来源:https://blog.csdn.net/HackerTom/article/details/135397257
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。