How to convert Nifti file to Numpy array?
Asked Answered
E

2

11

I have 3D array in Nifti file (.ii.gz) and I want to save it as a 3D numpy array. I used Nibabel to convert Numpy to Nifti1. Can I do the opposite?

Entryway answered 7/6, 2017 at 20:32 Comment(0)
M
24

From nipy

import numpy as np
import nibabel as nib

img = nib.load(example_filename)

a = np.array(img.dataobj)
Malony answered 7/6, 2017 at 20:51 Comment(0)
F
9

You can also do this:

import numpy as np
import nibabel as nib
    
img_nifti = nib.load(filepath)
img = img_nifti.get_fdata()
Freedwoman answered 5/6, 2020 at 10:22 Comment(1)
get_data() is now deprecated, now you can use get_fdata() insteadCachinnate

© 2022 - 2024 — McMap. All rights reserved.