3 ###############################################################################
5 # FILE: create_midP-2.0.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
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)
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.
18 ###############################################################################
20 ######################### includes
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`/midp_common.sh
29 echo "----------- Registration ------------"
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).
43 # params read from conf file
44 params="$nb_iter $nb_samples $sampling_algo $nb_hist_bins $nb_levels $bspline_spacing $metric $optimizer $interpolator"
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]}
51 if [ "$phase_nb" != "$ref_phase_nb" ]; then
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
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
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
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
92 # create (zero) vf from reference to reference
93 clitkZeroVF -i $vf_ref -o $vf_dir/vf_${ref_phase_nb}_${ref_phase_nb}.mhd
96 create_mhd_4D_pattern.sh $vf_dir/vf_${ref_phase_nb}_
99 echo "-------- Registration done ! --------"
109 echo "----------- Mid-position ------------"
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
130 clitkImageConvert -i $ref_midp -o $ref_midp -t float
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
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
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
159 echo "-------- Mid-position done ! --------"
168 ######################### main
171 echo "Usage: create_midP-2.0.sh CT_4D REF_PHASE CONF_FILE"
176 echo "--------------- START ---------------"
177 begining=`date --rfc-3339=seconds`
178 # echo "beginning: $begining"
181 # variable declarations
188 mask_dir="MASK-${mask_interpolation_spacing}mm-$mask_interpolation_algorithm"
190 extract_4d_phases_ref $mhd4d $ref_phase
192 if [ "$step" == "mask" -o "$step" == "all" ]; then
193 motion_mask $mhd4d $mask_interpolation_spacing $mask_interpolation_algorithm
196 if [ "$step" == "registration" -o "$step" == "all" ]; then
200 if [ "$step" == "midp" -o "$step" == "all" ]; then
205 echo "---------------- END ----------------"
206 terminating=`date --rfc-3339=seconds`
207 echo "beginning: $begining"
208 echo "terminating: $terminating"