Article Directory
1. Introduction to nibabel
NiBabel provides read/write access to some common medical and neuroimaging file formats, including ANALYZE (plain, SPM99, SPM2 and later), GIFTI , NIfTI1, NIfTI2 , CIFTI-2 , MINC1, MINC2 , AFNI BRIK/HEAD, MGH and ECAT and Philips PAR/REC. This library can fully or selectively access metadata of various image formats, can access image data through NumPy arrays, has very limited support for DICOM , and is also the successor of the PyNIIfTI third-party library.
A nibabel image consists of three aspects
- 3D or 4D image data array
- an affine array that tells where the image array data is in the reference space
- Image metadata (data about data) describing an image, usually in the form of an image header.
This article mainly discusses the use of the nibabel library to read and write brain tumor images in NIfTI format, the file extension is named .nii.gz
, and use matplotlib to display the image data.
2. Nibabel installation and import
Before installing the Python third-party library, it is recommended to configure a domestic image source, such as Tsinghua image source, see Linux configuration pip Tsinghua image source , Windows configuration pip domestic image source
Execute the following command in the Linux system Terminal
pip install nibabel
nibabel import paradigm, generally need to rename the import
import nibabel as nib
3. Nibabel common functions and attributes
3.1nibabel.load(filename, **kwargs)
Loads a file with the given filename, guesses the file type, and returns a SpatialImage instance. Here we get the brain tumor image of the Nifti1Image instance.
import os
import matplotlib.pyplot as plt
import nibabel as nib
# 文件夹路径和文件名
dirname="/dataset/RSNA_ASNR_MICCAI_BraTS2021_TrainingData_16July2021/BraTS2021_00000/"
basename="BraTS2021_00000_t1.nii.gz"
full_path=os.path.join(dirname,basename)
# 加载脑肿瘤图像
brain_tumor_img=nib.load(full_path)
print("加载医学图像类型为{}".format(type(brain_tumor_img)))
3.2nibabel.nifti1.Nifti1Image.get_fdata(self,caching='fill',dtype=<class 'numpy.float64'>,)
Nifti1Image instance method that returns floating point image data with the necessary scaling applied
# 返回应用了必要缩放的浮点图像数据
brain_tumor_img_data=brain_tumor_img.get_fdata()
print("浮点图像数据类型为{}".format(type(brain_tumor_img_data)))
View a sliced array in three dimensions
# 获得图像数据形状
print("图像数据形状为{}".format(brain_tumor_img_data.shape))
def show_slices(slices):
""" 显示一行图像切片 """
fig, axes = plt.subplots(1, len(slices))
for i, slice in enumerate(slices):
axes[i].imshow(slice.T, cmap="gray", origin="lower")
# 获得三个维度的切片
slice_0 = brain_tumor_img_data[120, :, :]
slice_1 = brain_tumor_img_data[:, 120, :]
slice_2 = brain_tumor_img_data[:, :, 77]
show_slices([slice_0, slice_1, slice_2])
plt.suptitle("Center slices for brain tumor image")
3.3nibabel.nifti1.Nifti1Image.affine
Nifti1Image instance attribute, get affine transformation matrix
# 获得仿射变换矩阵
affine=brain_tumor_img.affine
print("仿射变换矩阵为\n{}".format(affine))
3.4nibabel.aff2axcodes(aff, labels=None, tol=None)
Function to get the axis direction code of an affine
# 获得仿射的轴方向代码
orientation = nib.aff2axcodes(affine)
print("仿射的轴方向代码为\n{}".format(orientation))
3.5nib.save(img, filename, **kwargs)
Save the SpatialImage instance img to a file, adapting the format to "filename"
# 将SpatialImage实例img保存到文件brain_tumour.nii
nib.save(brain_tumor_img,"brain_tumour.nii")