我有很多二维的nibabel (.nii)切片,我需要从这些切片创建一个3D nibabel (.nii)图像,有什么方法吗?
发布于 2022-03-25 15:21:06
如果使用numpy将2D切片数据作为数组保存,则可以将表示每个切片的2D数组组合到一个3D数组中,并在python中执行所需的任何处理:
import nibabel
import numpy as np
#The variable imgs here represent a list of your 2D .nii nibabel data
#initialize an empty 3D array with shape
# determined by your 2D slice shape and the number of slices
num_imgs = len(imgs)
dims_single_2D = imgs[0].shape
dim_1 = dims_single_2D[0]
dim_2 = dims_single_2D[1]
stack_of_slices = np.zeros((dim_1, dim_2, num_imgs))
stack_id = 0
for img in imgs:
img_data = img.get_data()
# Convert to numpy ndarray (dtype: uint16)
img_data_arr = np.asarray(img_data)
stack_of_slices[stack_id] = img_data_arr
stack_id = stack_id + 1
# followed by the rest of your processing on the 3D array stack_of_slices请注意,如果您正在使用的.nii是NIFTI文件(由于这通常是卷格式,所以我还没有看到使用中的2D NIFTI文件),这是一个更复杂的操作。如果要保存NIFTIs,则需要头和仿射矩阵才能形成完整的文件:
https://nipy.org/nibabel/nifti_images.html?highlight=dim#working-with-nifti-images
https://nipy.org/nibabel/reference/nibabel.nifti2.html#module-nibabel.nifti2
更常见的2D图像格式是DICOM (https://nipy.org/nibabel/dicom/dicom_intro.html?highlight=dicom),nibabel也有读取和转换数组的工具:https://nipy.org/nibabel/reference/nibabel.nicom.html?highlight=dicom#module-nibabel.nicom。
https://nipy.org/nibabel/reference/nibabel.nicom.html?highlight=dicom#module-nibabel.nicom
https://stackoverflow.com/questions/71507596
复制相似问题