-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseg.sh
280 lines (257 loc) · 9 KB
/
seg.sh
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
if ! command -v mri_watershed &> /dev/null
then
echo "mri_watershed command not found. Please make sure FreeSurfer is installed and set up correctly."
exit 1
fi
if ! command -v N4BiasFieldCorrection &> /dev/null
then
echo "N4BiasFieldCorrection command not found. Please make sure ANTs is installed and set up correctly."
exit 1
fi
if ! command -v antsRegistration &> /dev/null
then
echo "antsRegistration command not found. Please make sure ANTs is installed and set up correctly."
exit 1
fi
if ! command -v antsApplyTransforms &> /dev/null
then
echo "antsApplyTransforms command not found. Please make sure ANTs is installed and set up correctly."
exit 1
fi
if ! command -v ImageMath &> /dev/null
then
echo "ImageMath command not found. Please make sure ANTs is installed and set up correctly."
exit 1
fi
if ! command -v ThresholdImage &> /dev/null
then
echo "ThresholdImage command not found. Please make sure ANTs is installed and set up correctly."
exit 1
fi
args=()
# Path to the directory containing the atlas and mask images
atlas_dir="./atlas"
# Type of transform to use in registration
transform="SyNQuick"
# Preprocessing: skull stripping and N4 bias correction
skullstrip=true
n4=true
# Threads to use
threads=1
usage() {
echo
echo "Atlas-based pituitary segmentation using ANTs."
echo
echo "Usage: $0 <input> <output> [-a atlas_dir] [-t transform] [-s] [-n] [-m threads] [-h]"
echo
echo "Options:"
echo " <input> Input image filename."
echo " <output> Output image filename."
echo " -a atlas_dir Path to the directory containing the atlas and mask images. Default: ./atlas."
echo " -t transform Type of transform to use in registration. Default: SyNQuick. Currently supported: Affine, SyN, SyNQuick."
echo " -s Supply this flag to SKIP skull stripping on the input image."
echo " -n Supply this flag to SKIP N4 bias correction to the input image."
echo " -m threads Number of threads to use. Default: 1. Increase this value to speed up the registration process."
echo " -h Display this help message."
echo
echo "------------------------------------------------------------"
echo "Script written by:"
echo "------------------------------------------------------------"
echo "Kaibo Tang"
echo "Department of Radiology and Biomedical Research Imaging Center (BRIC)"
echo "University of North Carolina at Chapel Hill"
echo "Contact: [email protected]"
echo "------------------------------------------------------------"
echo
exit 0
}
if [ "$#" -lt 2 ]
then
usage
fi
while [ $OPTIND -le "$#" ]
do
if getopts a:t:snm:h option
then
case $option
in
a) atlas_dir="$OPTARG";;
t) transform="$OPTARG";;
s) skullstrip=false;;
n) n4=false;;
m) threads="$OPTARG";;
h) usage;;
esac
else
args+=("${!OPTIND}")
((OPTIND++))
fi
done
input="${args[0]}"
output="${args[1]}"
atlas_lst=("$atlas_dir"/{01..10}_t1_strip_n4.nii.gz)
mask_lst=("$atlas_dir"/{01..10}_m_resampled.nii.gz)
if [ ! -f "$input" ]
then
echo "Input file $input not found. Exiting..."
exit 1
fi
if [ "$transform" != "Affine" ] && [ "$transform" != "SyN" ] && [ "$transform" != "SyNQuick" ]
then
echo "Unsupported transform type $transform. Exiting..."
exit 1
fi
if [ "$threads" -lt 1 ]
then
echo "Number of threads must be at least 1. Exiting..."
exit 1
fi
echo "============================================================"
echo "Input: $input"
echo "Output: $output"
echo "Atlas directory: $atlas_dir"
echo "Transform: $transform"
echo "Skull stripping: $skullstrip"
echo "N4 bias correction: $n4"
echo "Threads: $threads"
echo "============================================================"
# Skull stripping
if [ "$skullstrip" = true ]
then
echo "Skull stripping $input..."
time \
mri_watershed -atlas \
"$input" \
"${input%.nii.gz}_strip.nii.gz" > /dev/null
echo
echo "Done skull stripping $input."
echo "------------------------------------------------------------"
input="${input%.nii.gz}_strip.nii.gz"
else
echo "Skipping skull stripping..."
echo "------------------------------------------------------------"
fi
# N4 bias correction
if [ "$n4" = true ]
then
echo "Performing N4 bias correction on $input..."
time \
N4BiasFieldCorrection -d 3 \
-i "$input" \
-o "${input%.nii.gz}_n4.nii.gz" > /dev/null
echo
echo "Done N4 bias correction on $input."
echo "------------------------------------------------------------"
input="${input%.nii.gz}_n4.nii.gz"
else
echo "Skipping N4 bias correction..."
echo "------------------------------------------------------------"
fi
# Register the atlas to the input image
for i in {0..9}
do
echo "Registering atlas ${atlas_lst[$i]} to the input image..."
if [ "$transform" = "Affine" ]
then
echo "Performing affine registration..."
time \
antsRegistrationSyN.sh -d 3 \
-f "${input}" \
-m "${atlas_lst[$i]}" \
-o "${input%.nii.gz}_atlas_${i}_" \
-t a \
-n "$threads" > /dev/null
elif [ "$transform" = "SyN" ]
then
echo "Performing SyN registration..."
time \
antsRegistrationSyN.sh -d 3 \
-f "${input}" \
-m "${atlas_lst[$i]}" \
-o "${input%.nii.gz}_atlas_${i}_" \
-t s \
-n "$threads" > /dev/null
elif [ "$transform" = "SyNQuick" ]
then
echo "Performing SyNQuick registration..."
time \
antsRegistrationSyNQuick.sh -d 3 \
-f "${input}" \
-m "${atlas_lst[$i]}" \
-o "${input%.nii.gz}_atlas_${i}_" \
-t s \
-n "$threads" > /dev/null
else
echo "Unsupported transform type $transform. Exiting..."
exit 1
fi
echo
echo "Done registering atlas ${atlas_lst[$i]} to the input image."
echo "------------------------------------------------------------"
echo "Applying transformation to the mask ${mask_lst[$i]}..."
if [ "$transform" = "Affine" ]
then
time \
antsApplyTransforms -d 3 \
-i "${mask_lst[$i]}" \
-r "${input}" \
-o "${input%.nii.gz}_atlas_${i}_mask.nii.gz" \
-n GenericLabel \
-t "${input%.nii.gz}_atlas_${i}_0GenericAffine.mat" > /dev/null
elif [ "$transform" = "SyN" ] || [ "$transform" = "SyNQuick" ]
then
time \
antsApplyTransforms -d 3 \
-i "${mask_lst[$i]}" \
-r "${input}" \
-o "${input%.nii.gz}_atlas_${i}_mask.nii.gz" \
-n GenericLabel \
-t "${input%.nii.gz}_atlas_${i}_1Warp.nii.gz" \
-t "${input%.nii.gz}_atlas_${i}_0GenericAffine.mat" > /dev/null
else
echo "Unsupported transform type $transform. Exiting..."
exit 1
fi
echo
echo "Done applying transformation to the mask ${mask_lst[$i]}."
echo "------------------------------------------------------------"
done
# Combine the transformed masks
echo "Combining the transformed masks..."
ImageMath 3 \
"${output%.nii.gz}_all.nii.gz" \
+ \
"${input%.nii.gz}_atlas_0_mask.nii.gz" \
"${input%.nii.gz}_atlas_1_mask.nii.gz" > /dev/null
for i in {2..9}
do
ImageMath 3 \
"${output%.nii.gz}_all.nii.gz" \
+ \
"${output%.nii.gz}_all.nii.gz" \
"${input%.nii.gz}_atlas_${i}_mask.nii.gz" > /dev/null
done
echo
echo "Raw segmentation saved as ${output%.nii.gz}_all.nii.gz."
echo "------------------------------------------------------------"
# Apply thresholds
for i in {1..10}
do
echo "Applying threshold $i..."
time \
ThresholdImage 3 \
"${output%.nii.gz}_all.nii.gz" \
"${output%.nii.gz}_threshold_${i}.nii.gz" \
"$i" \
10000 > /dev/null
echo
echo "Threshold $i segmentation saved as ${output%.nii.gz}_threshold_${i}.nii.gz."
echo "------------------------------------------------------------"
done
# Clean up
echo "Cleaning up..."
rm "${input%.nii.gz}_atlas_"*
echo "Done."
echo "------------------------------------------------------------"
exit 0