3 ###############################################################################
5 # FILE: registration.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
8 # Helper file with registration functions using different methods.
9 # Each function receives a set of parameters that overall apply to any
10 # registration algorithm, as follows:
12 # reference=$1 : reference (fixed) image
13 # target=$2 : target (moving) image
14 # mask_ref=$3 : mask for the reference image
15 # mask_targ=$4 : mask for the moving image
16 # vf=$5 : output vector field representing the registration
17 # result=$6 : result image after applying the vector field
18 # nb_iter=$7 : maximum number of iterations
19 # nb_samples=$8 : number of image samples used in the metric calulcation
20 # sampling_algo=$9 : algorithm used in the selection of image samples
21 # hist_bins=${10} : number of histogram bins used in the metric calculation
22 # nb_levels=${11} : number of image resolutions
23 # spacing=${12} : spacing of the b-spline grid of the fines resolution
24 # metric=${13} : metric algorithm
25 # optimizer=${14} : optimizer
26 # interpolator=${15} : image interpolator
27 # log=${16} : log file
29 # New registration functions may be added to this file at any moment,
30 # respecting the interface defined above.
32 ###############################################################################
34 source `dirname $0`/midp_common.sh
37 ################# BLUTDIR #####################
38 registration_blutdir()
48 local sampling_algo=$9
54 local interpolator=${15}
57 local init_coeff=${18}
59 echo "Computing BLUTDIR $reference -> $target ..."
60 blutdir_params="--levels $nb_levels --metric $metric --optimizer $optimizer --samples $nb_samples --spacing $spacing,$spacing,$spacing --bins $hist_bins --maxIt $nb_iter --interp $interpolator --centre --verbose"
62 if [ -n "$coeff" ]; then
63 coeff="--coeff $coeff"
66 if [ -n "$init_coeff" ]; then
67 init_coeff="--initCoeff $init_coeff"
70 cmd="clitkBLUTDIR -r $reference -t $target -m $mask_ref --targetMask $mask_targ --vf $vf $coeff $init_coeff -o $result $blutdir_params"
73 abort_on_error registration_blutdir $? clean_up_registration
76 ################# ELASTIX #####################
77 registration_elastix()
87 local sampling_algo=$9
93 local interpolator=${15}
96 local init_coeff=${18}
98 # RP 25/06/2012: unused variables (for now at elast)
101 # local init_coeff=${18}
103 ########## register in ##########
104 for reg_in in $reg_in_list
106 if [ ! -z `echo $reg_in | grep "_$phase"` ]
111 echo "Computing ELASTIX $reference -> $target ..."
112 exec_dir=`which elastix`
113 exec_dir=`dirname $exec_dir`
114 suffix=${nb_samples}_${nb_iter}_${nb_levels}
115 cat $exec_dir/params_elastix_template.txt | sed -e "s+<NbIterations>+$nb_iter+" \
116 -e "s+<HistBins>+$hist_bins+" \
117 -e "s+<Levels>+$nb_levels+" \
118 -e "s+<PctSamples>+$nb_samples+" \
119 -e "s+<SamplerType>+$sampling_algo+" \
120 -e "s+<Metric>+$metric+" \
121 -e "s+<Optimizer>+$optimizer+" \
122 -e "s+<Interpolator>+$interpolator+" \
123 -e "s+<Spacing>+$spacing+" > params_elastix_${suffix}.txt
125 # set elastix to write out the coefficient images and corresponding DVFs
126 echo "(WriteCoefficientImage \"true\")" >> params_elastix_${suffix}.txt
127 echo "(WriteDVFFromCoeff \"true\")" >> params_elastix_${suffix}.txt
130 vf_base=`basename $vf .mhd`
131 coeff_dir=`dirname $coeff`
132 coeff_base=`basename $coeff .mhd`
133 result_dir=`dirname $result`
134 result_base=`basename $result .mhd`
137 cmd="elastix -f $reference -m $target -fMask $mask_ref -mMask $mask_targ -out $result_dir -p params_elastix_${suffix}.txt"
139 abort_on_error registration_elastix $? clean_up_registration
141 # generate vector field
142 # cmd="transformix -tp $result_dir/TransformParameters.0.txt -out $vf_dir -def all"
144 # abort_on_error registration_elastix $? clean_up_registration
147 local level=$(($nb_levels - 1))
148 mv $result_dir/dvf.0.R$level.mhd $vf
149 mv $result_dir/dvf.0.R$level.raw `echo $vf | sed 's/mhd/raw/'`
150 sed -i "s+dvf.0.R$level+$vf_base+" $vf
152 mv $result_dir/coeff.0.R$level.mhd $coeff_dir/${coeff_base}_0.mhd
153 mv $result_dir/coeff.0.R$level.raw $coeff_dir/${coeff_base}_0.raw
154 sed -i "s+coeff.0.R$level+$coeff_base+" $coeff_dir/${coeff_base}_0.mhd
156 mv $result_dir/result.0.mhd $result
157 mv $result_dir/result.0.raw `echo $result | sed 's/mhd/raw/'`
158 sed -i "s+result.0+$result_base+" $result
160 mv $result_dir/elastix.log $log
161 mv $result_dir/TransformParameters.0.txt $result_dir/${result_base}_TransformParameters.0.txt