使用 SimpleITK 和 Nibabel 读取医学nii数据

使用 SimpleITK 和 Nibabel 读取医学nii数据

SimpleITK 和 Nibabel 的区别:

SimpleITK 加载数据是channel_first,即(155,240,240);

Nibabel 是 channel_last,即(240,240,155),其中155是图像通道数,也就是155张图像,可以把nii看成二维图像,也可以看成三维。

SimpleITK

import SimpleITK as sitk
import numpy as np
from PIL import Image

def read_img(path):
img = sitk.ReadImage(path)
data = sitk.GetArrayFromImage(img)
return data

# 显示一个系列图
def show_imgs(data):
for i in range(data.shape[2]):
img = Image.fromarray(data[:, :, i])
img.show()

# 单张显示
def show_img(ori_img):
img = Image.fromarray(ori_img[:, :, 80])
img.show()

path = '/home/ltb/braTS/MICCAI_BraTS_2019_Data_Training/HGG/BraTS19_2013_2_1/BraTS19_2013_2_1_flair.nii.gz'
data = read_img(path)
data = np.array(data)
data = np.transpose(data, (1, 2, 0))
show_imgs(data)

Nibabel

import nibabel as nib
from PIL import Image
import numpy as np


def read_data(path):
image_data = nib.load(path).get_data()
return image_data


# 显示一个系列图
def show_imgs(data):
for i in range(data.shape[2]):
img = Image.fromarray(data[:, :, i])
img.show()

# 单张显示
def show_img(ori_img):
img = Image.fromarray(ori_img[:, :, 80])
img.show()

path = '/home/ltb/braTS/MICCAI_BraTS_2019_Data_Training/HGG/BraTS19_2013_2_1/BraTS19_2013_2_1_flair.nii.gz'
data = read_data(path)
print(data.shape)
data = np.array(data)
show_imgs(data)

参考

https://blog.csdn.net/weixin_42338058/article/details/84190420

Author: pangzibo243
Link: https://litianbo243.github.io/2019/08/31/使用SimpleITK-和-Nibabel-读取医学nii数据/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
支付宝打赏
微信打赏