Nibabel 是 channel_last,即(240,240,155),其中155是图像通道数,也就是155张图像,可以把nii看成二维图像,也可以看成三维。
- Nibabel的安装
pip install nibabel
- 图像的加载
example_filename = os.path.join(data_path, 'example.nii.gz')
img = nib.load(example_filename)
- 图像的查看
img.shape
(512, 512, 75)
512x512矩阵中存储的是 CT值,单位为HU
数据包含三个主要的部分
- image data array 存放图像数据的矩阵
- an affine array 定义了图像数据在参考空间的位置
- image metadata 存放图像的一些属性信息,采集设备名称,体素的大小,扫描层数等等。
如果你要对 CT图像进行 HU值的处理, 那么就一定要在使用nibabel 库之后立刻就进行操作,不要等使用 opencv 等工具后在进行操作。
使用 get_fdata , 就可以转为float 为 dtype 的 ndarray类型的 object
img = img.get_fdata()
- 图像进行仿射变换
img.affine.shape
(4, 4)
- 图像保存
#写法1
img.to_filename(os.path.join('output', 'test.nii.gz')
#写法2
nib.save(img, os.path.join('output', 'test.nii.gz')
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pylab as plt
import nibabel as nib
from nibabel import nifti1
from nibabel.viewers import OrthoSlicer3D
example_filename = './Brats18_2013_2_1_flair.nii.gz'
img = nib.load(example_filename)
print (img)
print (img.header['db_name']) # 输出头信息
# 由文件本身维度确定,可能是3维,也可能是4维
width,height,queue=img.dataobj.shape
OrthoSlicer3D(img.dataobj).show()
num = 1
for i in range(0,queue,10):
img_arr = img.dataobj[:,:,i]
plt.subplot(5,4,num)
plt.imshow(img_arr,cmap='gray')
num +=1
plt.show()
https://blog.csdn.net/qq_37471316/article/details/86217476
https://blog.csdn.net/alxe_made/article/details/80512423
参考链接:
医学图像分析--雷锋网
NiBabel 手册
CT图像的处理--Pierce_KK
采用最大连通域算法对三维医学图像分割结果做后处
如何读取并对nii三维数据进行切片处理、转换格式保存
NIFTI(.nii)数据头文件解析
网友评论