-
Notifications
You must be signed in to change notification settings - Fork 7
/
smooth_labels.py
executable file
·117 lines (86 loc) · 3.35 KB
/
smooth_labels.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
#!/usr/bin/env python3
"""
Smooth one or more labels within an integer atlas volume
Usage
----
smooth_labels.py -i <input label image> -o <output label image> [label numbers]
smooth_labels.py -h
Example
----
>>> smooth_labels.py -i atlas.nii.gz -o atlas_smooth_5.nii.gz 5 10 11
Authors
----
Mike Tyszka, Caltech Brain Imaging Center
Dates
----
2015-04-07 JMT From scratch
2015-12-08 JMT Update command line arguments and port to python 3
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 sys
import argparse
from scipy.ndimage.filters import gaussian_filter
import nibabel as nib
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Smooth one or more atlas labels')
parser.add_argument('-i','--in_file', help="source atlas labels filename")
parser.add_argument('-o','--out_file', help="smoothed atlas labels filename")
parser.add_argument('labels', metavar='label', type=int, nargs='+',
help='label numbers to smooth')
args = parser.parse_args()
in_file = args.in_file
out_file = args.out_file
labels = args.labels
# Load the source atlas image
print('Opening %s' % in_file)
in_nii = nib.load(in_file)
# Load label image
print('Loading labels')
src_labels = in_nii.get_data()
# Duplicate into output image
print('Creating new label image')
out_labels = src_labels.copy()
for label in labels:
print(' Smoothing label %d' % label)
# Extract target label as a boolean mask
print(' Identifying target label region %d' % label)
label_mask = (src_labels == label)
# Smooth target label region
print(' Gaussian smoothing original target label')
label_mask_smooth = gaussian_filter(label_mask.astype(float), sigma=1.0)
# Normalize smoothed intensities
label_mask_smooth = label_mask_smooth / label_mask_smooth.max()
# Threshold smoothed mask at 0.5 to create new boolean mask
print(' Thresholding smoothed label')
label_mask_smooth = label_mask_smooth > 0.5
# Replace unsmoothed with smoothed label, overwriting other labels
print(' Inserting smoothed target label')
out_labels[label_mask] = 0
out_labels[label_mask_smooth] = label
# Save smoothed labels image
print('Saving smoothed labels to %s' % out_file)
out_nii = nib.Nifti1Image(out_labels, in_nii.affine)
out_nii.to_filename(out_file)
print('Done')
# Clean exit
sys.exit(0)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()