-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtensor2nifti.py
executable file
·38 lines (36 loc) · 1.19 KB
/
tensor2nifti.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
import nibabel as nb
import numpy as np
import os
from torch.nn import functional as F
def tensor2nifti_mprage(t, savepath):
mni = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c.nii")
affine = mni.affine
for i in range(t.shape[0]):
data = t[i].numpy()
data = np.moveaxis(data,0,-1)
#import IPython; IPython.embed()
nii = nb.Nifti1Image(data, affine)
hdr = nii.header
hdr.set_data_dtype(data.dtype)
if i==0:
path = savepath + ".nii.gz"
else:
path = savepath + "_" + str(i) + ".nii.gz"
nii.to_filename(path)
return path
def numpy2nifti_mprage(t, savepath):
mni = nb.load("mni_icbm152_nlin_asym_09c/mni_icbm152_t1_tal_nlin_asym_09c.nii")
affine = mni.affine
for i in range(t.shape[0]):
data = t[i,:,:,:,:]
data = np.moveaxis(data,0,-1)
#import IPython; IPython.embed()
nii = nb.Nifti1Image(data, affine)
hdr = nii.header
hdr.set_data_dtype(data.dtype)
if i==0:
path = savepath + ".nii.gz"
else:
path = savepath + "_" + str(i) + ".nii.gz"
nii.to_filename(path)
return path