使用 SimpleITK 和 Nibabel 读取医学nii数据 SimpleITK 和 Nibabel 的区别:
SimpleITK 加载数据是channel_first,即(155,240,240);
Nibabel 是 channel_last,即(240,240,155),其中155是图像通道数,也就是155张图像,可以把nii看成二维图像,也可以看成三维。
SimpleITK import SimpleITK as sitkimport numpy as npfrom 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 nibfrom PIL import Imageimport numpy as npdef 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