-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnifti2jpg.py
executable file
·123 lines (88 loc) · 3.13 KB
/
nifti2jpg.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
Convert single Nifti-1 3D or 4D volume to set of JPEG stacks.
Usage
----
nifti2jpg.py -i <Nifti filename> -o <JPEG image stub>
nifti2jpg.py -h
Example
----
>>> nifti2jpg.py -i mynifti.nii.gz -o myjpgs
Authors
----
Mike Tyszka, Caltech Brain Imaging Center
Dates
----
2015-09-03 JMT From scratch
License
----
This file is part of atlaskit.
atlaskit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
atlaskit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with atlaskit. If not, see <http://www.gnu.org/licenses/>.
Copyright
----
2015 California Institute of Technology.
"""
__version__ = '0.1.0'
import os
import sys
import cv2
import argparse
import nibabel as nib
import numpy as np
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Convert 3D Nifti volume to JPEG stack')
parser.add_argument('-i','--nii_file', help='3D or 4D Nifti image')
parser.add_argument('-o','--jpg_stub', help='JPEG stack stub')
args = parser.parse_args()
nii_file = args.nii_file
jpg_stub = args.jpg_stub
# Strip extension from Nifti filename for use as a stub
nii_stub, fext = os.path.splitext(nii_file)
if fext == '.gz':
nii_stub, _ = os.path.splitext(nii_stub)
# Load nifti volume
print('Opening Nifti-1 volume')
nii_obj = nib.load(nii_file)
# Image dimensions
nx,ny,nz,nt = nii_obj.header.get_data_shape()
# Load image data
print('Loading voxel data')
s = nii_obj.get_data()
print(' Matrix size : (%d, %d, %d, %d)' % (nx, ny, nz, nt))
# Rescale to 0..255 uint8
imin, imax = np.min(s), np.max(s)
print(' Voxel intensity range : [%0.3f, %0.3f]' % (imin, imax))
s = np.uint8(255.0 * (s - imin) / (imax - imin))
# Loop over each volume
for t in range(0, nt):
# Image stack output directory name
jpg_dir = nii_stub + '_%04d' % t
print(' Volume %d -> %s' % (t, jpg_dir))
# Create jpg_dir if necessary
if not os.path.exists(jpg_dir):
os.makedirs(jpg_dir)
# Current volume
st = s[:,:,:,t]
# Loop over each z-slice, outputting as JPEG
for z in range(0, nz):
# JPEG filename
jpg_path = os.path.join(jpg_dir, jpg_stub + '_%04d.jpg' % z)
# Write single byte image slice to jpg file
sz_rgb = cv2.cvtColor(st[:,:,z], cv2.COLOR_GRAY2RGB)
cv2.imwrite(jpg_path, sz_rgb)
print('Done')
# Clean exit
sys.exit(0)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()