]> Creatis software - clitk.git/blob - scripts/create_midP-2.0.sh
new midP related scripts
[clitk.git] / scripts / create_midP-2.0.sh
1 #!/bin/sh -x
2
3 ###############################################################################
4 #
5 # FILE: create_midP-2.0.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
7 #
8 # Version 2.0 of the create_midP.sh script. The most relevant changes are:
9 #   * receives a .conf file as input, with variables related to the registration
10 #   parameters and to paramters of the script itself (see accompanying midp_template.conf)
11 #   for details.
12 #   * separates execution steps: it's now possible to choose which operation to execute
13 #   (mask, registration, midp, or all).
14 #   * some steps are now in different modules, to facilitate re-use (see "includes" section).
15 #   * minor modifications on output file names.
16 #   * attempt to simplify the code a bit.
17 #
18 ###############################################################################
19
20 ######################### includes
21
22 source `dirname $0`/create_midP_masks-2.0.sh using-as-lib 2 nn
23 source `dirname $0`/registration.sh
24 source `dirname $0`/common.sh
25
26 registration()
27 {
28   echo
29   echo "----------- Registration ------------"
30   start=`date`
31   echo "start: $start"
32   echo
33
34   mkdir -p $vf_dir
35   mkdir -p $output_dir
36
37   # banded images may be created as separate files,
38   # with the specified preffix, which is interesting for debugging. 
39   # if blank, it means that the original images (those without bands) 
40   # will be used (see create_midP_masks-2.0.sh for details).
41   banded="banded_"
42
43   # params read from conf file
44   params="$nb_iter $nb_samples $sampling_algo $nb_hist_bins $nb_levels $bspline_spacing $metric $optimizer $interpolator"
45
46   # register all phases to the reference
47   for i in $( seq 0 $((${#phase_files[@]} - 1))); do
48     phase_file=${phase_files[$i]}
49     phase_nb=${phase_nbs[$i]}
50     
51     if [ "$phase_nb" != "$ref_phase_nb" ]; then
52       # inside params
53       reference_in=$mask_dir/${banded}inside_$ref_phase_nb.mhd
54       target_in=$mask_dir/${banded}inside_$phase_nb.mhd
55       mask_ref_in=$mask_dir/mask_inside_$ref_phase_nb.mhd
56       mask_targ_in=$mask_dir/mask_inside_$phase_nb.mhd
57       vf_in=$vf_dir/vf_inside_${ref_phase_nb}_$phase_nb.mhd
58       result_in=$output_dir/result_inside_${ref_phase_nb}_$phase_nb.mhd
59       log_in=$log_dir/log_inside_${ref_phase_nb}_$phase_nb.log
60
61       # outside params
62       reference_out=$mask_dir/${banded}outside_$ref_phase_nb.mhd
63       target_out=$mask_dir/${banded}outside_$phase_nb.mhd
64       mask_ref_out=$mask_dir/mask_outside_$ref_phase_nb.mhd
65       mask_targ_out=$mask_dir/mask_outside_$phase_nb.mhd
66       vf_out=$vf_dir/vf_outside_$ref_phase_nb\_$phase_nb.mhd
67       result_out=$output_dir/result_outside_$ref_phase_nb\_$phase_nb.mhd
68       log_out=$log_dir/log_outside_${ref_phase_nb}_$phase_nb.log
69
70       # registration
71       if [ "$method" == "blutdir" ]; then
72         registration_blutdir $reference_in $target_in $mask_ref_in $mask_targ_in $vf_in $result_in $params $log_in
73         registration_blutdir $reference_out $target_out $mask_ref_out $mask_targ_out $vf_out $result_out $params $log_out
74       elif [ "$method" == "elastix" ]; then
75         registration_elastix $reference_in $target_in $mask_ref_in $mask_targ_in $vf_in $result_in $params $log_in
76         registration_elastix $reference_out $target_out $mask_ref_out $mask_targ_out $vf_out $result_out $params $log_out
77       fi
78
79       # combine in and out vf
80       motion_mask=$mask_dir/mm_$phase_nb.mhd
81       vf_result=$vf_dir/vf_${ref_phase_nb}_$phase_nb.mhd
82       clitkCombineImage -i $vf_in -j $vf_out -m $motion_mask -o $vf_result
83       clitkZeroVF -i $vf_in -o vf_zero.mhd
84       clitkCombineImage -i $vf_result -j vf_zero.mhd -m $patient_mask -o $vf_result
85       rm vf_zero.*
86
87       # save for later...
88       vf_ref=$vf_in
89     fi
90   done
91
92   # create (zero) vf from reference to reference
93   clitkZeroVF -i $vf_ref -o $vf_dir/vf_${ref_phase_nb}_${ref_phase_nb}.mhd
94
95   # create 4D vf
96   create_mhd_4D_pattern.sh $vf_dir/vf_${ref_phase_nb}_
97
98   echo
99   echo "-------- Registration done ! --------"
100   end=`date`
101   echo "start: $start"
102   echo "end: $end"
103   echo
104 }
105
106 midp()
107 {
108   echo
109   echo "----------- Mid-position ------------"
110   start=`date`
111   echo "start: $start"
112   echo
113
114   mkdir -p $midp_dir
115
116   ########### calculate the midp wrt the reference phase
117   phase_nb=$ref_phase_nb
118   echo "Calculating midp_$phase_nb.mhd..."
119   vf_midp=$midp_dir/vf_$phase_nb\_midp.mhd
120   midp=$midp_dir/midp_$phase_nb.mhd
121   # average the vf's from reference phase to phase
122   clitkAverageTemporalDimension -i $vf_dir/vf_${ref_phase_nb}_4D.mhd -o $vf_midp
123   # invert the vf (why?)
124   clitkInvertVF -i $vf_midp -o $vf_midp
125   # create the midp by warping the reference phase with the reference vf
126   clitkWarpImage -i $ref_phase_file -o $midp --vf=$vf_midp -s 1
127
128   ref_vf_midp=$vf_midp
129   ref_midp=$midp
130   clitkImageConvert -i $ref_midp -o $ref_midp -t float
131
132   ########### calculate the midp wrt the other phases
133   for i in $( seq 0 $((${#phase_files[@]} - 1))); do
134     phase_file=${phase_files[$i]}
135     phase_nb=${phase_nbs[$i]}
136     vf_midp=$midp_dir/vf_$phase_nb\_midp.mhd
137     midp=$midp_dir/midp_$phase_nb.mhd
138
139     if [ "$phase_nb" != "$ref_phase_nb" ]; then
140       echo "Calculating midp_$phase_nb.mhd..."
141       # calculate vf from phase to midp, using the vf from reference phase to midp (-i)
142       # and the vf from reference phase to phase (-j)
143       clitkComposeVF -i $ref_vf_midp -j $vf_dir/vf_$ref_phase_nb\_$phase_nb.mhd -o $vf_midp
144       clitkWarpImage -i $phase_file -o $midp --vf=$vf_midp -s 1
145       clitkImageConvert -i $midp -o $midp -t float
146     fi
147   done
148
149   create_mhd_4D_pattern.sh $midp_dir/midp_
150   echo "Calculating midp_avg.mhd..."
151   clitkAverageTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd
152   echo "Calculating midp_med.mhd..."
153   clitkMedianTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_med.mhd
154
155   # clean-up
156   rm $midp_dir/vf_*
157       
158   echo
159   echo "-------- Mid-position done ! --------"
160   end=`date`
161   echo "start: $start"
162   echo "end: $end"
163   echo
164 }
165
166
167
168 ######################### main
169
170 if [ $# != 3 ]; then
171   echo "Usage: create_midP-2.0.sh CT_4D REF_PHASE CONF_FILE"
172   exit -1
173 fi
174
175 echo
176 echo "--------------- START ---------------"
177 begining=`date --rfc-3339=seconds`
178 # echo "beginning: $begining"
179 echo
180
181 # variable declarations
182 mhd4d=$1
183 ref_phase=$2
184 conf=$3
185 source $conf
186
187 mkdir -p $log_dir
188 mask_dir="MASK-${mask_interpolation_spacing}mm-$mask_interpolation_algorithm"
189
190 extract_4d_phases_ref $mhd4d $ref_phase
191
192 if [ "$step" == "mask" -o "$step" == "all" ]; then
193   motion_mask $mhd4d $mask_interpolation_spacing $mask_interpolation_algorithm 
194 fi 
195
196 if [ "$step" == "registration" -o "$step" == "all" ]; then
197   registration
198 fi 
199
200 if [ "$step" == "midp" -o "$step" == "all" ]; then
201   midp
202 fi 
203
204 echo
205 echo "---------------- END ----------------"
206 terminating=`date --rfc-3339=seconds`
207 echo "beginning: $begining"
208 echo "terminating: $terminating"
209 echo