-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnc2nii.py
53 lines (42 loc) · 1.36 KB
/
mnc2nii.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
49
50
51
52
53
#!/usr/bin/python
# Load libraries
import argparse
import os
import sys
from import nibabel as nib
import numpy as np
from pyminc.volumes.factory import *
# Parse arguments
parser = argparse.ArgumentParser(description="Convert MINC .mnc files to NIfTI .nii format")
parser.add_argument('file', help="Input mnc file")
parser.add_argument('-o', '--outfile', help="Output nii file")
parser.add_argument('-t', '--time_axis', default=4, type=int, help="Dimension along which temporal variation is captured (default 4)")
args = parser.parse_args()
# Some default settings
all_dims = ["xspace", "yspace", "zspace"]
all_dims.insert(args.time_axis, "time")
all_starts = [0., 0., 0., 0.]
# Load image
try:
img = volumeFromFile(args.file)
except nib.spatialimages.ImageFileError:
print("Error: nibabel could not load image")
sys.exit(1)
# Get data
dims = img.data.shape
data = img.data
# Check to make sure image is 1D-4D
try:
assert len(dims) in range(1,5)
except AssertionError:
print("Image must have between 1 to 4 dimensions (inclusive)")
sys.exit(1)
dimnames = img.dimnames
steps = img.separations
starts = [all_starts[i] for i in range(len(dims))]
sizes = dims
affine = np.diag([1, 2, 3, 1])
header = nib.Nifti1Header()
header['pixdim'][range(1,len(dims)+1)] = steps
out_img = nib.Nifti1Image(data, affine=affine, header=header)
nib.save(out_img, args.outfile)