首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用2D nibabel图像创建三维nibabel图像?

如何使用2D nibabel图像创建三维nibabel图像?
EN

Stack Overflow用户
提问于 2022-03-17 05:48:16
回答 1查看 255关注 0票数 0

我有很多二维的nibabel (.nii)切片,我需要从这些切片创建一个3D nibabel (.nii)图像,有什么方法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-03-25 15:21:06

如果使用numpy将2D切片数据作为数组保存,则可以将表示每个切片的2D数组组合到一个3D数组中,并在python中执行所需的任何处理:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71507596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档