其他分享
首页 > 其他分享> > 读取nii文件

读取nii文件

作者:互联网

方式一

import matplotlib

matplotlib.use('TkAgg')
# 需要查看的nii文件名文件名.nii或nii.gz
from matplotlib import pylab as plt
import nibabel as nib
from nibabel import nifti1
from nibabel.viewers import OrthoSlicer3D

example_filename = './result/4.000000-Ax T2 brain-74802.nii' # 文件路径

img = nib.load(example_filename)
# 打印文件信息
print(img)
print(img.header['db_name'])  # 输出头信息
#shape不一定只有三个参数,打印出来看一下
width, height, queue = img.dataobj.shape
# 显示3D图像
OrthoSlicer3D(img.dataobj).show()
# 计算看需要多少个位置来放切片图
x = int((queue/10) ** 0.5) + 1
num = 1
# 按照10的步长,切片,显示2D图像
for i in range(0, queue, 6):
    img_arr = img.dataobj[:, :, i]
    plt.subplot(x, x, num)
    plt.imshow(img_arr, cmap='gray')
    num += 1

plt.show()

发法二

import SimpleITK as sitk
import matplotlib.pyplot as plt
import numpy as np

filename_PET = './result/4.000000-Ax T2 brain-74802.nii'

PET_itk = sitk.ReadImage(fileName=filename_PET)
PET_img = sitk.GetArrayFromImage(PET_itk)

Z_pet, X_pet, Y_pet = PET_img.shape

out = sitk.GetImageFromArray(PET_img)
sitk.WriteImage(out, 'path_saved.nii')

for item in PET_img:
    plt.imshow(item, cmap='Greys_r')
    plt.axis('off')
    plt.show()

标签:文件,plt,读取,img,PET,sitk,nii,import
来源: https://blog.csdn.net/weixin_44822684/article/details/121074112