python,matlab 读取NIFTI(.nii)格式图像、FSL安装

1.NIFTI格式图像图像来源

很有必要自己浏览这个网址,详细介绍了NIFTI的细节
有助于代码理解的点做以下总结:

  1. NIFTI格式 是“换装”后的ANALYZE 7.5 格式。 ANALYZE 7.5 中的header文件(扩展名为.hdr)用于存储meta-information,而 实际数据 以扩展名为.img 存储,故NIFTI格式存储的数据使用了一对文件 .hdr/.img , 扩展名为.nii
  2. NIFTIheader :一共有348 bytes(字节),存放不同作用的field(字段)。其中 field short dim[8]

The field short dim[8] contains the size of the image array. The first element (dim[0]) contains the number of dimensions (1-7). If dim[0] is not in this interval, the data is assumed to have opposite endianness and so, should be byte-swapped (the nifti standard does not specify a specific field for endianness, but encourages the use of dim[0] for this purpose). The dimensions 1, 2 and 3 are assumed to refer to space (x, y, z), the 4th dimension is assumed to refer to time, and the remaining dimensions, 5, 6 and 7, can be anything else. The value dim[i] is a positive integer representing the length of the i-th dimension.

对以上的理解:前三个维度(1,2,3)以定义三个空间维度-x,-y和-z,第四个维度定义时间点-t。其余维度(五到七)用于其他用途。比如,第五维可以存一些预定义的用途,例如存储体素特定的分布参数或保存基于矢量的数据。

2.NiBabel包

NiBabel包是可以对常见的医学和神经影像文件格式进行读写

import matplotlib
from matplotlib import pylab as plt
import nibabel as nib
from nibabel import nifti1
from nibabel.viewers import OrthoSlicer3D

img_path="E:/Pyproject/registration/icnet_data/image_A.nii"
img=nib.load(img_path)
print(img)
#print(img.header['db_name'])  #显示header 当中db_name
#有些可能图片是四维的
width,height,queue=img.dataobj.shape
OrthoSlicer3D(img.dataobj).show()
读取出来的图片格式
print(img)

result:

<class 'nibabel.nifti1.Nifti1Image'>
data shape (160, 192, 224)
affine: 
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
metadata:
<class 'nibabel.nifti1.Nifti1Header'> object, endian='<'
sizeof_hdr      : 348
data_type       : b''
db_name         : b''
extents         : 0
session_error   : 0
regular         : b''
dim_info        : 0
dim             : [  3 160 192 224   1   1   1   1]
intent_p1       : 0.0
intent_p2       : 0.0
intent_p3       : 0.0
intent_code     : none
datatype        : float32
bitpix          : 32
slice_start     : 0
pixdim          : [1. 1. 1. 1. 1. 1. 1. 1.]
vox_offset      : 0.0
scl_slope       : nan
scl_inter       : nan
slice_end       : 0
slice_code      : unknown
xyzt_units      : 0
cal_max         : 0.0
cal_min         : 0.0
slice_duration  : 0.0
toffset         : 0.0
glmax           : 0
glmin           : 0
descrip         : b''
aux_file        : b''
qform_code      : unknown
sform_code      : aligned
quatern_b       : 0.0
quatern_c       : 0.0
quatern_d       : 0.0
qoffset_x       : 0.0
qoffset_y       : 0.0
qoffset_z       : 0.0
srow_x          : [1. 0. 0. 0.]
srow_y          : [0. 1. 0. 0.]
srow_z          : [0. 0. 1. 0.]
intent_name     : b''
magic           : b'n+1'
3D显示输出结果如下:

注意 刚开始出现三个黑色方框,可以滚动鼠标,移动绿色的线。因为这是一组序列图。
在这里插入图片描述

分段显示如下:
import matplotlib
from matplotlib import pylab as plt
import nibabel as nib
from nibabel import nifti1
from nibabel.viewers import OrthoSlicer3D

img_path="E:/Pyproject/registration/icnet_data/image_A.nii"
img=nib.load(img_path)
#print(img.shape)#(160, 192, 144)
width,height,queue=img.dataobj.shape

num=1
for i in range(0,queue,8):#也可以取10等
    img_arr=img.dataobj[:,:,i]
    plt.subplot(5,4,num)
    plt.imshow(img_arr,cmap='gray')
    num+=1
plt.show()

结果:
在这里插入图片描述

3. SimpleIKT 读取数据

注意,SimpleIKT读取出来的图片的维度顺序与NiBabel 不同

import SimpleITK as itk 
from matplotlib import pyplot as plt 

def show_nii(img):
    num=1
    for i in range(0,img.shape[0],10):
        plt.imshow(img[i,:,:],cmap='gray')
        plt.subplot(5,4,num)
        num+=1
        
itk_img=itk.ReadImage('E:/Pyproject/registration/icnet_data/image_A.nii')
img=itk.GetArrayFromImage(itk_img)
print(img.shape) #(144, 192, 160) 
show_nii(img)
plt.show()

结果:
在这里插入图片描述
如果想要得到不同方向的切片,对show_nii 函数进行修改

def show_nii(img):
    num=1
    for i in range(0,img.shape[1],10): 
        plt.imshow(img[:,i,:],cmap='gray')
        plt.subplot(5,4,num)
        num+=1
        

结果:在这里插入图片描述

4. 其他应用

将.nii 转存为.npy

5. python 版本FSL 安装

fslpy官网安装指南
官网使用指南

6. matlab+Nifti安装包

6.1 数据集是IXI数据集
6.2 安装matlabr2020a
6.3 下载Nifti软件包官方Nifti
6.4 读取IXI
nii=load_untouch_nii("filename")

IXI数据集比较特殊,没有affine, 无法用load_nii 函数加载
在这里插入图片描述
切片显示

nii=load_untouch_nii('IXI002-Guys-0828-MPRAGESEN_-s256_-0301-00003-000001-01.nii');
img=nii.img;
save img.mat;
load img;
[n1,n2,n3]=size(img);
%imshow(img(:,:,100),[]);%是正常显示第100个切片的图像
for i=1:n3
    %figure(i)%%建立一个图形窗口,如果没有这一句则所有切片只会依次显示于一个窗口
    ti=imshow(img(:,:,i),[]);
    pause(0.1);
end

  • 8
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值