-
Notifications
You must be signed in to change notification settings - Fork 0
/
nii_to_numpy.py
41 lines (32 loc) · 916 Bytes
/
nii_to_numpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from nipy import load_image, save_image
import numpy as np
#import getpass
from nipy.core.api import Image, vox2mni
import os
import sys
"""
Takes as input the file path to a nifti (.nii)
and returns a numpy array
"""
def load_nii(nii):
"""
Returns the image data and the coordinate space from the header
The coordinate space is useful for saving the image back in the
same 3D space as the original
"""
img = load_image(nii)
data = img.get_data()
coord = img.coordmap
return data, coord
def save_nii(data, coord, save_file):
"""
Saves a numpy array (data) as a nifti file
The coordinate space must match the array dimensions
"""
arr_img = Image(data, coord)
save_image(arr_img, save_file)
return 0
if __name__ == '__main__':
input_image = os.path.abspath(sys.argv[1])
numpy_array = load_nii(input_image)
print(numpy_array.shape)