-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathantsRegistrationSpaceTime.sh
executable file
·370 lines (302 loc) · 9.58 KB
/
antsRegistrationSpaceTime.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
VERSION="0.0.0 test"
# trap keyboard interrupt (control-c)
trap control_c SIGINT
ANTS=antsRegistration
if ! command -v ${ANTS} &> /dev/null
then
echo "antsRegistration program can't be found. Please (re)define \$PATH in your environment."
exit
fi
function Usage {
cat <<USAGE
Usage:
`basename $0` -d ImageDimension -f FixedImage -o OutputPrefix -m MovingImages*
Compulsory arguments:
-d: ImageDimension: 2 or 3 (for 2 or 3 dimensional registration of single volume)
-f: ND fixed image
-o: OutputPrefix: A prefix that is prepended to all output files.
Optional arguments:
-n: Number of threads (default = 1)
-t: timespacing
-r: number of repeats for each time point
-s: spline distance for deformable B-spline SyN transform (default = 26)
-x: mask for the fixed image space
-p: precision type (default = 'd')
f: float
d: double
-m: Moving images
Example:
`basename $0` -d 3 -f fixedImage.nii.gz -o output -m movingImage*.nii.gz
--------------------------------------------------------------------------------------
ANTs was created by:
--------------------------------------------------------------------------------------
Brian B. Avants, Nick Tustison and Gang Song
Penn Image Computing And Science Laboratory
University of Pennsylvania
script by Nick Tustison, BB Avants
USAGE
exit 1
}
function Help {
cat <<HELP
Usage:
`basename $0` -d ImageDimension -f FixedImage -o OutputPrefix -m MovingImage*
Example Case:
`basename $0` -d 3 -f fixedImage.nii.gz -o output -m movingImage*.nii.gz
Compulsory arguments:
-d: ImageDimension: 2 or 3 (for 2 or 3 dimensional registration of single volume)
-f: Fixed image or source image or reference image
-m: Moving images
-o: OutputPrefix: A prefix that is prepended to all output files.
Optional arguments:
-n: Number of threads (default = 1)
-t: timespacing
-j: use histogram matching
-r: number of repeats for each time point
-s: spline distance for deformable B-spline SyN transform (default = 26)
-x: mask for the fixed image space
-p: precision type (default = 'd')
f: float
d: double
NB: Multiple image pairs can be specified for registration during the SyN stage.
Specify additional images using the '-m' and '-f' options. Note that image
pair correspondence is given by the order specified on the command line.
Only the first fixed and moving image pair is used for the linear resgitration
stages.
--------------------------------------------------------------------------------------
Get the latest ANTs version at:
--------------------------------------------------------------------------------------
https://github.com/stnava/ANTs/
--------------------------------------------------------------------------------------
Read the ANTS documentation at:
--------------------------------------------------------------------------------------
http://stnava.github.io/ANTs/
--------------------------------------------------------------------------------------
ANTS was created by:
--------------------------------------------------------------------------------------
Brian B. Avants, Nick Tustison and Gang Song
Penn Image Computing And Science Laboratory
University of Pennsylvania
Relevent references for this script include:
* http://www.ncbi.nlm.nih.gov/pubmed/20851191
* http://www.frontiersin.org/Journal/10.3389/fninf.2013.00039/abstract
--------------------------------------------------------------------------------------
script by Nick Tustison
--------------------------------------------------------------------------------------
HELP
exit 1
}
function reportMappingParameters {
cat <<REPORTMAPPINGPARAMETERS
--------------------------------------------------------------------------------------
Mapping parameters
--------------------------------------------------------------------------------------
Dimensionality: $DIM
Output name prefix: $OUTPUTNAME
Fixed images: ${FIXEDIMAGES[@]}
Moving images: ${MOVINGIMAGES[@]}
Number of threads: $NUMBEROFTHREADS
Spline distance: $SPLINEDISTANCE
Transform type: $TRANSFORMTYPE
CC radius: $nrepeats
Precision: $PRECISIONTYPE
======================================================================================
REPORTMAPPINGPARAMETERS
}
cleanup()
{
echo "\n*** Performing cleanup, please wait ***\n"
runningANTSpids=$( ps --ppid $$ -o pid= )
for thePID in $runningANTSpids
do
echo "killing: ${thePID}"
kill ${thePID}
done
return $?
}
control_c()
# run if user hits control-c
{
echo -en "\n*** User pressed CTRL + C ***\n"
cleanup
exit $?
echo -en "\n*** Script cancelled by user ***\n"
}
# Provide output for Help
if [[ "$1" == "-h" || $# -eq 0 ]];
then
Help >&2
fi
#################
#
# default values
#
#################
DIM=3
FIXEDIMAGES=()
MOVINGIMAGES=()
OUTPUTNAME=output
NUMBEROFTHREADS=1
SPLINEDISTANCE=26
TRANSFORMTYPE='s'
PRECISIONTYPE='d'
nrepeats=2
MASK=0
USEHISTOGRAMMATCHING=0
timespacing=1
# reading command line arguments
while getopts "d:f:h:m:j:n:o:p:r:s:t:x:" OPT
do
case $OPT in
h) #help
Help
exit 0
;;
d) # dimensions
DIM=$OPTARG
;;
j) # histogram matching
USEHISTOGRAMMATCHING=$OPTARG
;;
x) # inclusive mask
MASK=$OPTARG
;;
f) # fixed image
FIXEDIMAGES[${#FIXEDIMAGES[@]}]=$OPTARG
;;
m) # moving image
MOVINGIMAGES[${#MOVINGIMAGES[@]}]=$OPTARG
;;
n) # number of threads
NUMBEROFTHREADS=$OPTARG
;;
o) #output name prefix
OUTPUTNAME=$OPTARG
;;
p) # precision type
PRECISIONTYPE=$OPTARG
;;
r) # n repeats
nrepeats=$OPTARG
;;
s) # spline distance
SPLINEDISTANCE=$OPTARG
;;
t) # spacing in time
timespacing=$OPTARG
;;
\?) # getopts issues an error message
echo "$USAGE" >&2
exit 1
;;
esac
done
###############################
#
# Check inputs
#
###############################
for(( i=0; i<${#MOVINGIMAGES[@]}; i++ ))
do
if [[ ! -f "${MOVINGIMAGES[$i]}" ]];
then
echo "Moving image '${MOVINGIMAGES[$i]}' does not exist. See usage: '$0 -h 1'"
exit 1
fi
done
###############################
#
# Set number of threads
#
###############################
ORIGINALNUMBEROFTHREADS=${ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS}
ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$NUMBEROFTHREADS
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS
##############################
#
# Print out options
#
##############################
reportMappingParameters
# what we do below
#
# stack the moving images together to produce ND+1 input
# antsMotionCorr to get affine to the fixed template
# stack fixed template to ND+1
# register with -r warp to the stacked template
# jacobian ...
let DIMP1=$DIM+1
echo $DIMP1
zero=${OUTPUTNAME}zero.nii.gz
zerob=${OUTPUTNAME}zerob.nii.gz
stack=${OUTPUTNAME}stack.nii.gz
nmov=${#MOVINGIMAGES[@]}
let nmov=$nmov-1
MultiplyImages $DIM ${MOVINGIMAGES[0]} 1 $zero
stackmovparam=" $zero $zero "
for mov in ${MOVINGIMAGES[@]} ; do
for k in `seq 1 $nrepeats ` ; do
stackmovparam=" $stackmovparam $mov "
done
done
let nm1=${#MOVINGIMAGES[@]}-1
MultiplyImages $DIM ${MOVINGIMAGES[nm1]} 1 $zerob
stackmovparam=" $stackmovparam $zerob $zerob "
if [[ $DIM == 2 ]] ; then
StackSlices $stack -1 -1 0 $stackmovparam | grep -v Slice
fi
if [[ $DIM == 3 ]] ; then
ImageMath $DIMP1 $stack TimeSeriesAssemble $timespacing 0 ${stackmovparam}
fi
ImageMath $DIMP1 $stack SetTimeSpacing $stack $timespacing
nm=${OUTPUTNAME}
MultiplyImages $DIM ${FIXEDIMAGES} 1 $zero
stackparam=" $zero $zero "
stacktemplate=${OUTPUTNAME}template.nii.gz
for mov in ${MOVINGIMAGES[@]} ; do
for k in `seq 1 $nrepeats ` ; do
stackparam=" $stackparam ${FIXEDIMAGES} "
done
done
stackparam=" $stackparam $zero $zero "
if [[ $DIM == 2 ]] ; then
StackSlices $stacktemplate -1 -1 0 ${stackparam}
fi
if [[ $DIM == 3 ]] ; then
ImageMath $DIMP1 $stacktemplate TimeSeriesAssemble $timespacing 0 ${stackparam}
fi
ImageMath $DIMP1 $stacktemplate SetTimeSpacing $stacktemplate $timespacing
# echo $stackparam
# echo $stackmovparam
# echo $nrepeats $timespacing
if [[ $DIM == 2 ]] ; then rxt="1x1x0"; fi
if [[ $DIM == 3 ]] ; then rxt="1x1x1x0"; fi
antsMotionCorr -d $DIM \
-o [ ${nm}aff, ${nm}aff.nii.gz,${nm}_affavg.nii.gz ] \
-m MI[ ${FIXEDIMAGES}, ${stack}, 1 , 20, Regular, 0.1 ] \
-t rigid[ 0.1 ] -u 1 -e 1 -s 4x2x1x0 -f 6x4x2x1 \
-i 100x100x0x0 \
-m MI[ ${FIXEDIMAGES}, ${stack}, 1 , 20, Regular, 0.2 ] \
-t Affine[ 0.1 ] -u 1 -e 1 -s 4x2x1x0 -f 6x4x2x1 \
-i 100x100x100x15 \
-n ${#MOVINGIMAGES[@]} -w 1 --verbose 1
ImageMath $DIMP1 ${nm}affWarp.nii.gz SetTimeSpacingWarp ${nm}affWarp.nii.gz $timespacing
ImageMath $DIMP1 ${nm}affInverseWarp.nii.gz SetTimeSpacingWarp ${nm}affInverseWarp.nii.gz $timespacing
# if below does not work - have to drop the -r
# and put the aff version into the metric
antsRegistration -d $DIMP1 -r ${nm}affWarp.nii.gz \
-c [ 100x70x50x10,1e-6,10 ] \
-f 6x4x2x1 \
-s 3x2x1x0vox \
-m CC[ $stacktemplate, ${nm}stack.nii.gz, 1, 2 ] \
-t SyN[ 0.15,3,0 ] --restrict-deformation $rxt \
-o [ ${nm},${nm}diffeoWarped.nii.gz,${nm}diffeoInvWarped.nii.gz ] -z 0
CreateJacobianDeterminantImage $DIMP1 ${nm}1Warp.nii.gz ${nm}0logjacobian.nii.gz 1 1
###############################
#
# Restore original number of threads
#
###############################
ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$ORIGINALNUMBEROFTHREADS
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS