Prv*_*dav 5 python numpy nifti nibabel neuro-image
从这个问题如何将 Nifti 文件转换为 Numpy 数组?,我创建了一个 nifti 图像的 3D numpy 数组。我对这个数组做了一些修改,比如我通过添加零填充来改变数组的深度。现在我想将此数组转换回 nifti 图像,我该怎么做?
我试过:
imga = Image.fromarray(img, 'RGB')
imga.save("modified/volume-20.nii")
Run Code Online (Sandbox Code Playgroud)
但它不识别nii扩展名。我也试过:
nib.save(img,'modified/volume-20.nii')
Run Code Online (Sandbox Code Playgroud)
这也不起作用,因为如果我想使用功能img必须是。在上面的两个例子中都是一个 3D numpy 数组。nibabel.nifti1.Nifti1Imagenib.saveimg
假设你是一个 numpy 数组并且你想使用nib.save函数,你需要首先获得affine转换。
例子:
# define the path to the data
func_filename = os.path.join(data_path, 'task-rest_bold.nii.gz')
# load the data
func = nib.load(func_filename)
# do computations that lead to a 3D numpy array called "output"
# bla bla bla
# output = np.array(....)
# to save this 3D (ndarry) numpy use this
ni_img = nib.Nifti1Image(output, func.affine)
nib.save(ni_img, 'output.nii.gz')
Run Code Online (Sandbox Code Playgroud)
现在您将能够output.nii.gz将task-rest_bold.nii.gz
| 归档时间: |
|
| 查看次数: |
5285 次 |
| 最近记录: |