-
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathants.sh
executable file
·142 lines (114 loc) · 4.59 KB
/
ants.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
#!/bin/bash
NUMPARAMS=$#
MAXITERATIONS=30x90x20
if ! command -v "ANTS" &> /dev/null
then
echo "Cannot find the ANTS program. Please \(re\)define \$PATH in your environment."
exit 1
fi
if [ $NUMPARAMS -lt 3 ]
then
echo " USAGE :: "
echo " sh ants.sh ImageDimension fixed.ext moving.ext OPTIONAL-OUTPREFIX OPTIONAL-max-iterations OPTIONAL-Labels-In-Fixed-Image-Space-To-Deform-To-Moving-Image Option-DoANTSQC "
echo " Max-Iterations in form : JxKxL where "
echo " J = max iterations at coarsest resolution (here, reduce by power of 2^2) "
echo " K = middle resolution iterations ( here, reduce by power of 2 ) "
echo " L = fine resolution iterations ( here, full resolution ) -- this level takes much more time per iteration "
echo " an extra Ix before JxKxL would add another level "
echo " Default iterations is $MAXITERATIONS -- you can often get away with fewer for many apps "
echo " Other parameters are updates of the defaults used in the A. Klein evaluation paper in Neuroimage, 2009 "
exit
fi
#initialization, here, is unbiased
DIM=$1
if [ ${#DIM} -gt 1 ]
then
echo " Problem with specified ImageDimension => User Specified Value = $DIM "
exit
fi
FIXED=$2
if [ ${#FIXED} -lt 1 -o ! -f $FIXED ]
then
echo " Problem with specified Fixed Image => User Specified Value = $FIXED "
exit
fi
MOVING=$3
if [ ${#MOVING} -lt 1 -o ! -f $MOVING ]
then
echo " Problem with specified Moving Image => User Specified Value = $MOVING "
exit
fi
OUTPUTNAME=` echo $MOVING | cut -d '.' -f 1 `
if [ $NUMPARAMS -gt 4 ]
then
MAXITERATIONS=$5
fi
LABELIMAGE=0
if [ $NUMPARAMS -gt 5 ]
then
LABELIMAGE=$6
fi
DoANTSQC=0
if [ $NUMPARAMS -gt 6 ]
then
DoANTSQC=$7
fi
if [ $NUMPARAMS -gt 3 ]
then
OUTPUTNAME=${4}
fi
# Mapping Parameters
TRANSFORMATION="SyN[ 0.25 ]"
ITERATLEVEL=(`echo $MAXITERATIONS | tr 'x' ' '`)
NUMLEVELS=${#ITERATLEVEL[@]}
echo $NUMLEVELS
REGULARIZATION="Gauss[ 3,0 ]"
METRIC="CC[ "
METRICPARAMS="1,4 ]"
# echo " $METRICPARAMS & $METRIC "
# exit
echo " Mapping Parameters :: "
echo " Transformation is: $TRANSFORMATION "
echo " MaxIterations : $MAXITERATIONS "
echo " Number Of MultiResolution Levels $NUMLEVELS "
echo " Regularization : $REGULARIZATION "
echo " Metric : ${METRIC} "
echo " OutputName : $OUTPUTNAME "
echo " "
echo " if the files and parameters are all ok then uncomment the exit call below this line "
echo " "
#exit
if [[ ! -s ${OUTPUTNAME}repaired.nii.gz ]] ; then
N4BiasFieldCorrection -d $DIM -i $MOVING -o ${OUTPUTNAME}repaired.nii.gz -s 2 -c [ 50x50x50x50,0.000001 ] -b [ 200 ]
# N3BiasFieldCorrection $DIM $MOVING ${OUTPUTNAME}repaired.nii.gz 4
fi
exe=" ANTS $DIM -m ${METRIC}${FIXED},${OUTPUTNAME}repaired.nii.gz,${METRICPARAMS} -t $TRANSFORMATION -r $REGULARIZATION -o ${OUTPUTNAME} -i $MAXITERATIONS --use-Histogram-Matching --number-of-affine-iterations 10000x10000x10000x10000x10000 --MI-option 32x16000 "
echo " $exe "
$exe
#below, some affine options
#--MI-option 16x8000 #-a InitAffine.txt --continue-affine 0
WarpImageMultiTransform $DIM ${OUTPUTNAME}repaired.nii.gz ${OUTPUTNAME}deformed.nii.gz ${OUTPUTNAME}Warp.nii.gz ${OUTPUTNAME}Affine.txt -R ${FIXED}
if [ ${#LABELIMAGE} -gt 3 ]
then
WarpImageMultiTransform $DIM $LABELIMAGE ${OUTPUTNAME}labeled.nii.gz -i ${OUTPUTNAME}Affine.txt ${OUTPUTNAME}InverseWarp.nii.gz -R ${MOVING} --use-NN
fi
exit
if [ $DoANTSQC -eq 1 ] ; then
# measure image similarity
for SIM in 0 1 2 ; do
MeasureImageSimilarity $DIM $SIM $FIXED ${OUTPUTNAME}deformed.nii.gz
done
# measure dice overlap and mds
ThresholdImage $DIM $FIXED ${OUTPUTNAME}fixthresh.nii.gz Otsu 4
ThresholdImage $DIM $MOVING ${OUTPUTNAME}movthresh.nii.gz Otsu 4
WarpImageMultiTransform $DIM ${OUTPUTNAME}movthresh.nii.gz ${OUTPUTNAME}defthresh.nii.gz ${OUTPUTNAME}Warp.nii.gz ${OUTPUTNAME}Affine.txt -R ${FIXED} --use-NN
ImageMath $DIM ${OUTPUTNAME}dicestats.txt DiceAndMinDistSum ${OUTPUTNAME}fixthresh.nii.gz ${OUTPUTNAME}movthresh.nii.gz ${OUTPUTNAME}mindistsum.nii.gz
# labelstats for jacobian wrt segmenation
# below, to compose
# ComposeMultiTransform $DIM ${OUTPUTNAME}CompWarp.nii.gz -R $FIXED ${OUTPUTNAME}Warp.nii.gz ${OUTPUTNAME}Affine.txt
# CreateJacobianDeterminantImage $DIM ${OUTPUTNAME}CompWarp.nii.gz ${OUTPUTNAME}jacobian.nii.gz 0
# ImageMath $DIM ${OUTPUTNAME}movlabstat.txt LabelStats ${OUTPUTNAME}movthresh.nii.gz ${OUTPUTNAME}movthresh.nii.gz
# ImageMath $DIM ${OUTPUTNAME}jaclabstat.txt LabelStats ${OUTPUTNAME}defthresh.nii.gz ${OUTPUTNAME}jacobian.nii.gz
# we compare the output of these last two lines:
# the Volume of the movlabstat computation vs. the mass of the jaclabstat
fi