Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Nifti file to Numpy array?

Tags:

numpy

nifti

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?

like image 605
Char Avatar asked Jun 07 '17 20:06

Char


2 Answers

From nipy

import numpy as np
import nibabel as nib

img = nib.load(example_filename)

a = np.array(img.dataobj)
like image 142
piRSquared Avatar answered Oct 21 '22 11:10

piRSquared


You can also do this:

import numpy as np
import nibabel as nib

img_nifti = nib.load(filepath)
img = img_nifti.get_data()
like image 25
schrodingercat Avatar answered Oct 21 '22 09:10

schrodingercat