forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
antsRegistrationSyN.sh
executable file
·667 lines (541 loc) · 17.9 KB
/
antsRegistrationSyN.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/bin/bash
VERSION="0.0.0 test"
# trap keyboard interrupt (control-c)
trap control_c SIGINT
function setPath {
cat <<SETPATH
--------------------------------------------------------------------------------------
Error locating ANTS
--------------------------------------------------------------------------------------
It seems that the ANTSPATH environment variable is not set. Please add the ANTSPATH
variable. This can be achieved by editing the .bash_profile in the home directory.
Add:
ANTSPATH=/home/yourname/bin/ants/
Or the correct location of the ANTS binaries.
Alternatively, edit this script ( `basename $0` ) to set up this parameter correctly.
SETPATH
exit 1
}
# Uncomment the line below in case you have not set the ANTSPATH variable in your environment.
# export ANTSPATH=${ANTSPATH:="$HOME/bin/ants/"} # EDIT THIS
#ANTSPATH=YOURANTSPATH
if [[ ${#ANTSPATH} -le 3 ]];
then
setPath >&2
fi
ANTS=${ANTSPATH}/antsRegistration
if [[ ! -s ${ANTS} ]];
then
echo "antsRegistration program can't be found. Please (re)define \$ANTSPATH in your environment."
exit
fi
function Usage {
cat <<USAGE
Usage:
`basename $0` -d ImageDimension -f FixedImage -m MovingImage -o OutputPrefix
Compulsory arguments:
-d: ImageDimension: 2 or 3 (for 2 or 3 dimensional registration of single volume)
-f: Fixed image(s) or source image(s) or reference image(s)
-m: Moving image(s) or target image(s)
-o: OutputPrefix: A prefix that is prepended to all output files.
Optional arguments:
-n: Number of threads (default = ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS if defined, otherwise 1)
-i: initial transform(s) --- order specified on the command line matters
-t: transform type (default = 's')
t: translation (1 stage)
r: rigid (1 stage)
a: rigid + affine (2 stages)
s: rigid + affine + deformable syn (3 stages)
sr: rigid + deformable syn (2 stages)
so: deformable syn only (1 stage)
b: rigid + affine + deformable b-spline syn (3 stages)
br: rigid + deformable b-spline syn (2 stages)
bo: deformable b-spline syn only (1 stage)
-r: radius for cross correlation metric used during SyN stage (default = 4)
-s: spline distance for deformable B-spline SyN transform (default = 26)
-g: gradient step size for SyN and B-spline SyN (default = 0.1)
-x: mask(s) for the fixed image space. Should specify either a single image to be used for
all stages or one should specify a mask image for each "stage" (cf -t option). If
no mask is to be used for a particular stage, the keyword 'NULL' should be used
in place of a file name.
-p: precision type (default = 'd')
f: float
d: double
-j: use histogram matching (default = 0)
0: false
1: true
-y: use 'repro' mode for exact reproducibility of output. Uses GC metric for linear
stages and a fixed random seed (default = 0).
0: false
1: true
-z: collapse output transforms (default = 1)
-e: Fix random seed to an int value
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 registration
stages.
Example:
`basename $0` -d 3 -f fixedImage.nii.gz -m movingImage.nii.gz -o output
--------------------------------------------------------------------------------------
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
USAGE
exit 1
}
function Help {
cat <<HELP
Usage:
`basename $0` -d ImageDimension -f FixedImage -m MovingImage -o OutputPrefix
Example Case:
`basename $0` -d 3 -f fixedImage.nii.gz -m movingImage.nii.gz -o output
Compulsory arguments:
-d: ImageDimension: 2 or 3 (for 2 or 3 dimensional registration of single volume)
-f: Fixed image(s) or source image(s) or reference image(s)
-m: Moving image(s) or target image(s)
-o: OutputPrefix: A prefix that is prepended to all output files.
Optional arguments:
-n: Number of threads (default = ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS if defined, otherwise 1)
-i: initial transform(s) --- order specified on the command line matters
-t: transform type (default = 's')
t: translation (1 stage)
r: rigid (1 stage)
a: rigid + affine (2 stages)
s: rigid + affine + deformable syn (3 stages)
sr: rigid + deformable syn (2 stages)
so: deformable syn only (1 stage)
b: rigid + affine + deformable b-spline syn (3 stages)
br: rigid + deformable b-spline syn (2 stages)
bo: deformable b-spline syn only (1 stage)
-r: radius for cross correlation metric used during SyN stage (default = 4)
-s: spline distance for deformable B-spline SyN transform (default = 26)
-g: gradient step size for SyN and B-spline SyN (default = 0.1)
-x: mask(s) for the fixed image space. Should specify either a single image to be used for
all stages or one should specify a mask image for each "stage" (cf -t option). If
no mask is to be used for a particular stage, the keyword 'NULL' should be used
in place of a file name.
-p: precision type (default = 'd')
f: float
d: double
-j: use histogram matching (default = 0)
0: false
1: true
-y: use 'repro' mode for exact reproducibility of output. Uses GC metric for linear
stages and a fixed random seed (default = 0).
0: false
1: true
-z: collapse output transforms (default = 1)
0: false
1: true
-e: Fix random seed to an int value
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/ANTsX/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
--------------------------------------------------------------------------------------
ANTSPATH is $ANTSPATH
Dimensionality: $DIM
Output name prefix: $OUTPUTNAME
Fixed images: ${FIXEDIMAGES[@]}
Moving images: ${MOVINGIMAGES[@]}
Mask images: ${MASKIMAGES[@]}
Initial transforms: ${INITIALTRANSFORMS[@]}
Number of threads: $NUMBEROFTHREADS
Spline distance: $SPLINEDISTANCE
SyN gradient step: $SYNGRADIENTSTEP
Transform type: $TRANSFORMTYPE
CC radius: $CCRADIUS
Precision: $PRECISIONTYPE
Use histogram matching $USEHISTOGRAMMATCHING
Repro $REPRO
======================================================================================
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=()
INITIALTRANSFORMS=()
OUTPUTNAME=output
NUMBEROFTHREADS=0
SPLINEDISTANCE=26
SYNGRADIENTSTEP=0.1
TRANSFORMTYPE='s'
PRECISIONTYPE='d'
CCRADIUS=4
MASKIMAGES=()
USEHISTOGRAMMATCHING=0
COLLAPSEOUTPUTTRANSFORMS=1
RANDOMSEED=0
REPRO=0
# reading command line arguments
while getopts "d:e:f:g:h:i:m:j:n:o:p:r:s:t:x:y:z:" OPT
do
case $OPT in
h) #help
Help
exit 0
;;
d) # dimensions
DIM=$OPTARG
;;
e) # seed
RANDOMSEED=$OPTARG
;;
x) # inclusive mask
MASKIMAGES[${#MASKIMAGES[@]}]=$OPTARG
;;
f) # fixed image
FIXEDIMAGES[${#FIXEDIMAGES[@]}]=$OPTARG
;;
g) # SyN gradient step
SYNGRADIENTSTEP=$OPTARG
;;
j) # histogram matching
USEHISTOGRAMMATCHING=$OPTARG
;;
m) # moving image
MOVINGIMAGES[${#MOVINGIMAGES[@]}]=$OPTARG
;;
i) # initial transform
INITIALTRANSFORMS[${#INITIALTRANSFORMS[@]}]=$OPTARG
;;
n) # number of threads
NUMBEROFTHREADS=$OPTARG
;;
o) #output name prefix
OUTPUTNAME=$OPTARG
;;
p) # precision type
PRECISIONTYPE=$OPTARG
;;
r) # cc radius
CCRADIUS=$OPTARG
;;
s) # spline distance
SPLINEDISTANCE=$OPTARG
;;
t) # transform type
TRANSFORMTYPE=$OPTARG
;;
y) # reproducibility
REPRO=$OPTARG
;;
z) # collapse output transforms
COLLAPSEOUTPUTTRANSFORMS=$OPTARG
;;
\?) # getopts issues an error message
echo "$USAGE" >&2
exit 1
;;
esac
done
###############################
#
# Check inputs
#
###############################
if [[ ${#FIXEDIMAGES[@]} -ne ${#MOVINGIMAGES[@]} ]];
then
echo "Number of fixed images is not equal to the number of moving images."
exit 1
fi
for(( i=0; i<${#FIXEDIMAGES[@]}; i++ ))
do
if [[ ! -f "${FIXEDIMAGES[$i]}" ]];
then
echo "Fixed image '${FIXEDIMAGES[$i]}' does not exist. See usage: '$0 -h 1'"
exit 1
fi
if [[ ! -f "${MOVINGIMAGES[$i]}" ]];
then
echo "Moving image '${MOVINGIMAGES[$i]}' does not exist. See usage: '$0 -h 1'"
exit 1
fi
done
##############################
#
# Mask stuff
#
##############################
NUMBEROFMASKIMAGES=${#MASKIMAGES[@]}
MASKCALL=""
if [[ ${#MASKIMAGES[@]} -gt 0 ]];
then
for (( i = 0; i < ${#MASKIMAGES[@]}; i++ ))
do
MASKCALL="${MASKCALL} -x [ ${MASKIMAGES[$i]}, NULL ]"
done
fi
###############################
#
# Set number of threads
#
###############################
ORIGINALNUMBEROFTHREADS=${ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS}
# NUMBEROFTHREADS is > 0 if the option has been set to a positive value
if [[ $NUMBEROFTHREADS -lt 1 ]];
then
# Number of threads not set on the command line, try ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS
if [[ $ORIGINALNUMBEROFTHREADS -gt 0 ]];
then
NUMBEROFTHREADS=$ORIGINALNUMBEROFTHREADS
else
NUMBEROFTHREADS=1
fi
fi
ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$NUMBEROFTHREADS
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS
##############################
#
# Print out options
#
##############################
reportMappingParameters
##############################
#
# Infer the number of levels based on
# the size of the input fixed image.
#
##############################
ISLARGEIMAGE=0
SIZESTRING=$( ${ANTSPATH}/PrintHeader ${FIXEDIMAGES[0]} 2 )
SIZESTRING="${SIZESTRING%\\n}"
SIZE=( `echo $SIZESTRING | tr 'x' ' '` )
for (( i=0; i<${#SIZE[@]}; i++ ))
do
if [[ ${SIZE[$i]} -gt 256 ]];
then
ISLARGEIMAGE=1
break
fi
done
##############################
#
# Construct mapping stages
#
##############################
RIGIDCONVERGENCE="[ 1000x500x250x100,1e-6,10 ]"
RIGIDSHRINKFACTORS="8x4x2x1"
RIGIDSMOOTHINGSIGMAS="3x2x1x0vox"
AFFINECONVERGENCE="[ 1000x500x250x100,1e-6,10 ]"
AFFINESHRINKFACTORS="8x4x2x1"
AFFINESMOOTHINGSIGMAS="3x2x1x0vox"
SYNCONVERGENCE="[ 100x70x50x20,1e-6,10 ]"
SYNSHRINKFACTORS="8x4x2x1"
SYNSMOOTHINGSIGMAS="3x2x1x0vox"
if [[ $ISLARGEIMAGE -eq 1 ]];
then
RIGIDCONVERGENCE="[ 1000x500x250x100,1e-6,10 ]"
RIGIDSHRINKFACTORS="12x8x4x2"
RIGIDSMOOTHINGSIGMAS="4x3x2x1vox"
AFFINECONVERGENCE="[ 1000x500x250x100,1e-6,10 ]"
AFFINESHRINKFACTORS="12x8x4x2"
AFFINESMOOTHINGSIGMAS="4x3x2x1vox"
SYNCONVERGENCE="[ 100x100x70x50x20,1e-6,10 ]"
SYNSHRINKFACTORS="10x6x4x2x1"
SYNSMOOTHINGSIGMAS="5x3x2x1x0vox"
fi
LINEARMETRIC="MI"
LINEARMETRICPARAMETER=32
# Precedence for random seeding
# 1. Command line option -e
# 2. Environment variable ANTS_RANDOM_SEED
# 3. Fixed seed = 1 if run in repro mode
# 4. ITK default (system time)
if [[ -n ${ANTS_RANDOM_SEED} ]] && [[ ${RANDOMSEED} -eq 0 ]];
then
RANDOMSEED=${ANTS_RANDOM_SEED}
fi
if [[ $REPRO -eq 1 ]];
then
LINEARMETRIC="GC"
LINEARMETRICPARAMETER=1
if [[ ${RANDOMSEED} -eq 0 ]];
then
RANDOMSEED=1
fi
fi
INITIALSTAGE="--initial-moving-transform [ ${FIXEDIMAGES[0]},${MOVINGIMAGES[0]},1 ]"
if [[ ${#INITIALTRANSFORMS[@]} -gt 0 ]];
then
INITIALSTAGE=""
for(( i=0; i<${#INITIALTRANSFORMS[@]}; i++ ))
do
INITIALSTAGE="$INITIALSTAGE --initial-moving-transform ${INITIALTRANSFORMS[$i]}"
done
fi
tx=Rigid
if [[ $TRANSFORMTYPE == 't' ]] ; then
tx=Translation
fi
RIGIDSTAGE="--transform ${tx}[ 0.1 ] \
--metric ${LINEARMETRIC}[ ${FIXEDIMAGES[0]},${MOVINGIMAGES[0]},1,${LINEARMETRICPARAMETER},Regular,0.25 ] \
--convergence $RIGIDCONVERGENCE \
--shrink-factors $RIGIDSHRINKFACTORS \
--smoothing-sigmas $RIGIDSMOOTHINGSIGMAS"
AFFINESTAGE="--transform Affine[ 0.1 ] \
--metric ${LINEARMETRIC}[ ${FIXEDIMAGES[0]},${MOVINGIMAGES[0]},1,${LINEARMETRICPARAMETER},Regular,0.25 ] \
--convergence $AFFINECONVERGENCE \
--shrink-factors $AFFINESHRINKFACTORS \
--smoothing-sigmas $AFFINESMOOTHINGSIGMAS"
SYNMETRICS=''
for(( i=0; i<${#FIXEDIMAGES[@]}; i++ ))
do
SYNMETRICS="$SYNMETRICS --metric CC[ ${FIXEDIMAGES[$i]},${MOVINGIMAGES[$i]},1,${CCRADIUS} ]"
done
SYNSTAGE="${SYNMETRICS} \
--convergence $SYNCONVERGENCE \
--shrink-factors $SYNSHRINKFACTORS \
--smoothing-sigmas $SYNSMOOTHINGSIGMAS"
if [[ $TRANSFORMTYPE == 'sr' ]] || [[ $TRANSFORMTYPE == 'br' ]];
then
SYNCONVERGENCE="[ 50x20,1e-6,10 ]"
SYNSHRINKFACTORS="2x1"
SYNSMOOTHINGSIGMAS="1x0vox"
SYNSTAGE="${SYNMETRICS} \
--convergence $SYNCONVERGENCE \
--shrink-factors $SYNSHRINKFACTORS \
--smoothing-sigmas $SYNSMOOTHINGSIGMAS"
fi
if [[ $TRANSFORMTYPE == 'b' ]] || [[ $TRANSFORMTYPE == 'br' ]] || [[ $TRANSFORMTYPE == 'bo' ]];
then
SYNSTAGE="--transform BSplineSyN[ ${SYNGRADIENTSTEP},${SPLINEDISTANCE},0,3 ] \
$SYNSTAGE"
fi
if [[ $TRANSFORMTYPE == 's' ]] || [[ $TRANSFORMTYPE == 'sr' ]] || [[ $TRANSFORMTYPE == 'so' ]];
then
SYNSTAGE="--transform SyN[ ${SYNGRADIENTSTEP},3,0 ] \
$SYNSTAGE"
fi
NUMBEROFREGISTRATIONSTAGES=0
STAGES=''
case "$TRANSFORMTYPE" in
"r" | "t")
STAGES="$INITIALSTAGE $RIGIDSTAGE"
NUMBEROFREGISTRATIONSTAGES=1
;;
"a")
STAGES="$INITIALSTAGE $RIGIDSTAGE $AFFINESTAGE"
NUMBEROFREGISTRATIONSTAGES=2
;;
"b" | "s")
STAGES="$INITIALSTAGE $RIGIDSTAGE $AFFINESTAGE $SYNSTAGE"
NUMBEROFREGISTRATIONSTAGES=3
;;
"br" | "sr")
STAGES="$INITIALSTAGE $RIGIDSTAGE $SYNSTAGE"
NUMBEROFREGISTRATIONSTAGES=2
;;
"bo" | "so")
STAGES="$INITIALSTAGE $SYNSTAGE"
NUMBEROFREGISTRATIONSTAGES=1
;;
*)
echo "Transform type '$TRANSFORMTYPE' is not an option. See usage: '$0 -h 1'"
exit
;;
esac
if [[ $NUMBEROFMASKIMAGES -ne 0 && $NUMBEROFMASKIMAGES -ne 1 && $NUMBEROFMASKIMAGES -ne $NUMBEROFREGISTRATIONSTAGES ]];
then
echo "The specified number of mask images is not correct. Please see help menu."
exit
fi
PRECISION=''
case "$PRECISIONTYPE" in
"f")
PRECISION="--float 1"
;;
"d")
PRECISION="--float 0"
;;
*)
echo "Precision type '$PRECISIONTYPE' is not an option. See usage: '$0 -h 1'"
exit
;;
esac
RANDOMOPT=""
if [[ ! $RANDOMSEED -eq 0 ]]; then
RANDOMOPT=" --random-seed $RANDOMSEED "
fi
COMMAND="${ANTS} --verbose 1 $RANDOMOPT \
--dimensionality $DIM $PRECISION \
--collapse-output-transforms $COLLAPSEOUTPUTTRANSFORMS \
--output [ $OUTPUTNAME,${OUTPUTNAME}Warped.nii.gz,${OUTPUTNAME}InverseWarped.nii.gz ] \
--interpolation Linear \
--use-histogram-matching ${USEHISTOGRAMMATCHING} \
--winsorize-image-intensities [ 0.005,0.995 ] \
$MASKCALL \
$STAGES"
echo " antsRegistration call:"
echo "--------------------------------------------------------------------------------------"
echo ${COMMAND}
echo "--------------------------------------------------------------------------------------"
$COMMAND
###############################
#
# Restore original number of threads
#
###############################
ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=$ORIGINALNUMBEROFTHREADS
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS