]> Creatis software - clitk.git/blob - scripts/registration.sh
8f6a5f29af3d465cfea649aeb0f56d8eb68b69e7
[clitk.git] / scripts / registration.sh
1 #! /bin/sh
2
3 ###############################################################################
4 #
5 # FILE: registration.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
7 #
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:
11
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
28 #
29 # New registration functions may be added to this file at any moment, 
30 # respecting the interface defined above.
31 #
32 ###############################################################################
33
34 source `dirname $0`/midp_common.sh
35
36
37 ################# BLUTDIR #####################
38 registration_blutdir()
39 {
40   local reference=$1
41   local target=$2
42   local mask_ref=$3
43   local mask_targ=$4
44   local vf=$5
45   local result=$6
46   local nb_iter=$7
47   local nb_samples=$8
48   local sampling_algo=$9
49   local hist_bins=${10}
50   local nb_levels=${11}
51   local spacing=${12}
52   local metric=${13}
53   local optimizer=${14}
54   local interpolator=${15}
55   local log=${16}
56   local coeff=${17}
57   local init_coeff=${18}
58
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"
61
62   if [ -n "$coeff" ]; then
63     coeff="--coeff $coeff"
64   fi
65   
66   if [ -n "$init_coeff" ]; then
67     init_coeff="--initCoeff $init_coeff"
68   fi
69   
70   cmd="clitkBLUTDIR -r $reference -t $target -m $mask_ref --targetMask $mask_targ --vf $vf $coeff $init_coeff -o $result $blutdir_params"
71   $cmd > $log
72
73   abort_on_error registration_blutdir $? clean_up_registration
74 }
75
76 ################# ELASTIX #####################
77 registration_elastix()
78 {
79   local reference=$1
80   local target=$2
81   local mask_ref=$3
82   local mask_targ=$4
83   local vf=$5
84   local result=$6
85   local nb_iter=$7
86   local nb_samples=$8
87   local sampling_algo=$9
88   local hist_bins=${10}
89   local nb_levels=${11}
90   local spacing=${12}
91   local metric=${13}
92   local optimizer=${14}
93   local interpolator=${15}
94   local log=${16}
95   local coeff=${17}
96   local init_coeff=${18}
97
98 # RP 25/06/2012: unused variables (for now at elast)  
99 #   local log=${16}
100 #   local coeff=${17}
101 #   local init_coeff=${18}
102   
103   ########## register in ##########
104   for reg_in in $reg_in_list
105   do
106     if [ ! -z `echo $reg_in | grep "_$phase"` ]
107     then
108       target_in=$reg_in
109     fi
110   done
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 
124
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 
128
129   vf_dir=`dirname $vf`
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`
135
136   # image registration
137   cmd="elastix -f $reference -m $target -fMask $mask_ref -mMask $mask_targ -out $result_dir -p params_elastix_${suffix}.txt"
138   $cmd  > /dev/null
139   abort_on_error registration_elastix $? clean_up_registration
140
141   # generate vector field
142   # cmd="transformix -tp $result_dir/TransformParameters.0.txt -out $vf_dir -def all"
143   # $cmd  > /dev/null
144   # abort_on_error registration_elastix $? clean_up_registration
145
146   # post-processing
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
151
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
155
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
159
160   mv $result_dir/elastix.log $log
161   mv $result_dir/TransformParameters.0.txt $result_dir/${result_base}_TransformParameters.0.txt
162 }