-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
48 lines (37 loc) · 1.1 KB
/
image.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
42
43
44
45
46
47
48
from __future__ import division, print_function, absolute_import
import numpy as np
import nibabel as nib
def load_nifti(fname, return_img=False, return_voxsize=False,
return_coords=False):
img = nib.load(fname)
data = img.get_data()
vox_size = img.header.get_zooms()[:3]
ret_val = [data, img.affine]
if return_img:
ret_val.append(img)
if return_voxsize:
ret_val.append(vox_size)
if return_coords:
ret_val.append(nib.aff2axcodes(img.affine))
return tuple(ret_val)
def save_nifti(fname, data, affine, hdr=None):
result_img = nib.Nifti1Image(data, affine, header=hdr)
result_img.to_filename(fname)
def save_affine_matrix(fname, affine):
"""
Parameters
---------
fname : str
File name to save the affine matrix.
affine : numpy array
The object containing the affine matrix.
"""
np.savetxt(fname, affine)
def load_affine_matrix(fname):
"""
Parameters
---------
fname : str
The file containing the saved affine matrix.
"""
return np.loadtxt(fname)