]> Creatis software - clitk.git/blob - scripts/create_midP_masks-2.0.sh
more refined abort-on-exception mechanism
[clitk.git] / scripts / create_midP_masks-2.0.sh
1 #! /bin/bash 
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_masks.sh script. The most relevant changes are:
9 #   * creation of bands around input and output image regions to try to improve 
10 #   the registration along lung boundaries (naturally, it depends on the quality
11 #   of motion mask generation).
12 #   * some steps are now in different modules, to facilitate re-use (see "includes" section).
13 #   * minor modifications on output file names.
14 #   * attempt to simplify the code a bit.
15 #
16 ###############################################################################
17
18 source `dirname $0`/midp_common.sh
19
20 extract_patient()
21 {
22   echo "$phase_file -> Extracting patient..."
23   clitkExtractPatient -i $phase_file -o $mask_dir_tmp/patient_mask_$phase_nb.mhd --noAutoCrop -a $afdb_file $ExtractPatientExtra
24 #   abort_on_error clitkExtractPatient $?
25
26   clitkSetBackground -i $phase_file -o $mask_dir_tmp/patient_$phase_nb.mhd --mask $mask_dir_tmp/patient_mask_$phase_nb.mhd --outsideValue -1000
27 #   abort_on_error clitkSetBackground $?
28 }
29
30 extract_bones()
31 {
32   if [ x = x$ExtractBonesLower1 ]; then
33     ExtractBonesLower1=120
34   fi
35   if [ x = x$ExtractBonesLower2 ]; then
36     ExtractBonesLower2=80
37   fi
38   echo "$phase_file -> Extracting bones..."
39   clitkImageConvert -i $phase_file -o $mask_dir_tmp/float_$phase_nb.mhd -t float
40   clitkExtractBones -i $mask_dir_tmp/float_$phase_nb.mhd -o $mask_dir_tmp/bones_$phase_nb.mhd -a $afdb_file --lower1 $ExtractBonesLower1 --upper1 2000 --lower2 $ExtractBonesLower2 --upper2 2000 --smooth --time 0.0625 --noAutoCrop
41 }
42
43 extract_lungs()
44 {
45   echo "$phase_file -> Extracting lungs..."
46   clitkExtractLung -i $phase_file -o $mask_dir_tmp/lungs_$phase_nb.mhd -a $afdb_file --noAutoCrop
47 }
48
49 resample()
50 {
51   echo "$phase_file -> Resampling..."
52   clitkResampleImage -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/patient_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo
53   clitkResampleImage -i $mask_dir_tmp/lungs_$phase_nb.mhd -o $mask_dir_tmp/lungs_$phase_nb.mhd --like $mask_dir_tmp/patient_$phase_nb.mhd
54 }
55
56 compute_motion_mask()
57 {
58   if [ x = x$MotionMaskOffsetDetect ]; then
59     MotionMaskOffsetDetect="0,-5,0"
60   fi
61   if [ x = x$FillingLevel ]; then
62     FillingLevel=94
63   fi
64
65   clitkMotionMask -i $mask_dir_tmp/patient_$phase_nb.mh -o $mask_dir_tmp/mm_$phase_nb.mhd --featureLungs $mask_dir_tmp/lungs_$phase_nb.mhd --upperThresholdLungs -400 --fillingLevel $FillingLevel --offsetDetect $MotionMaskOffsetDetect --pad --writeFeature=$mask_dir_tmp/feature_$phase_nb.mhd $MotionMaskExtra 
66   #--monitor=$mask_dir_tmp/monitor_$phase_nb.mhd
67 }
68
69 create_banded_mask()
70 {
71   input=$1
72   input_mask=$2
73   output=$3
74   output_mask=$4
75   radius=$5
76
77   input_dir=`dirname $input`
78   input_base=`basename $input`
79
80   # first band
81   clitkMorphoMath -i $input_mask -o $input_dir/extra1_$input_base --type 1 --radius $radius
82   clitkImageArithm -i $input_dir/extra1_$input_base -j $input_mask -o $input_dir/band1_$input_base -t 7
83   clitkBinarizeImage -i $input_dir/band1_$input_base -o $input_dir/band1_$input_base -l 1 -u 1 --fg 100 --mode both
84   clitkImageConvert -i $input_dir/band1_$input_base -o $input_dir/short_band1_$input_base -t short
85   
86   # second band
87   clitkMorphoMath -i $input_dir/extra1_$input_base -o $input_dir/extra2_$input_base --type 1 --radius $radius
88   clitkImageArithm -i $input_dir/extra2_$input_base -j $input_dir/extra1_$input_base -o $input_dir/band2_$input_base -t 7
89   clitkBinarizeImage -i $input_dir/band2_$input_base -o $input_dir/band2_$input_base -l 1 -u 1 --fg 200 --mode both
90   clitkImageConvert -i $input_dir/band2_$input_base -o $input_dir/short_band2_$input_base -t short
91   
92   # combine bands with masks
93   clitkImageArithm -i $input_mask -j $input_dir/band1_$input_base -o $output_mask -t 0
94   clitkImageArithm -i $output_mask -j $input_dir/band2_$input_base -o $output_mask -t 0
95   # combine bands with image
96   clitkCombineImage -i $input_dir/short_band1_$input_base -j $input -m $input_dir/band1_$input_base -o $output
97   clitkCombineImage -i $input_dir/short_band2_$input_base -j $output -m $input_dir/band2_$input_base -o $output
98
99   # clean-up
100   rm `echo $input_dir/extra?_$input_base | sed 's:.mhd:.*:g'`
101   rm `echo $input_dir/band?_$input_base | sed 's:.mhd:.*:g'`
102   rm `echo $input_dir/short_band?_$input_base | sed 's:.mhd:.*:g'`
103 }
104
105 create_registration_masks()
106 {
107   # extract inside and outside lung regions from the patient image, 
108   # using the motion mask computed previously
109   echo "$phase_file -> Setting Background..."
110   clitkSetBackground -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/inside_$phase_nb.mhd --mask $mask_dir_tmp/mm_$phase_nb.mhd --outsideValue -1200
111   clitkSetBackground -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/outside_$phase_nb.mhd --mask $mask_dir_tmp/mm_$phase_nb.mhd --outsideValue -1200 --fg
112
113   # the registration masks for inside (and outside) region correspond
114   # to the motion mask (and its complement) plus extra grey value bands,
115   # obtained with morphological dilations.
116   # 
117   echo "$phase_file -> Creating registration masks..."
118   # inside
119   create_banded_mask $mask_dir_tmp/inside_$phase_nb.mhd $mask_dir_tmp/mm_$phase_nb.mhd $mask_dir_tmp/banded_inside_$phase_nb.mhd $mask_dir_tmp/mask_inside_$phase_nb.mhd 4
120   # outside 
121   clitkExtractPatient -i $mask_dir_tmp/outside_$phase_nb.mhd -o $mask_dir_tmp/mm_outside_$phase_nb.mhd --noAutoCrop
122   create_banded_mask $mask_dir_tmp/outside_$phase_nb.mhd $mask_dir_tmp/mm_outside_$phase_nb.mhd $mask_dir_tmp/banded_outside_$phase_nb.mhd $mask_dir_tmp/mask_outside_$phase_nb.mhd 4
123 }
124
125 mm_preprocessing()
126 {
127   extract_patient
128   # extract_bones
129   extract_lungs
130   # remove_tmp_masks 1
131   resample
132 }
133
134 mm_postprocessing()
135 {
136   # remove_tmp_masks 2
137   # remove_tmp_masks 3
138   create_registration_masks
139 }
140
141 motion_mask()
142 {
143   #set cmd line variables
144   mhd4d=$1
145   resample_spacing=$2
146   resample_algo=$3
147
148   dir=`dirname $1`
149   cd $dir
150     
151   # import variables specific to each patient
152   if test -e ./variables; then
153     source ./variables
154   fi
155
156   #set other global variables
157   mask_dir="MASK-${resample_spacing}mm-$resample_algo"
158   mask_dir_tmp="tmp.$mask_dir"
159   extract_4d_phases $mhd4d
160
161   echo
162   echo "------------ Motion mask from create_midP_masks.sh ------------"
163   start=`date`
164   echo "start: $start"
165   echo
166
167   # the motion masks are first created in a tmp directory. this directory is 
168   # later going to be renamed to the final motion mask directory. concurrent
169   # executions trying to create the same set of masks will be blocked until
170   # the first execution finishes. naturally, these other executions will not
171   # recreate the masks. so first we try to create the tmp directory. 
172   # if the creation fails, it means that another execution is
173   # already creating the masks, so this execution will be blocked. the
174   # execution is unblocked only when the creation of masks is finished and
175   # the mask directory is renamed.
176   #
177   # ATTENTION: RP - 08/02/2011
178   # robustness issue: tmp directory may exist but may be empty or 
179   # incomplete. the solution is to check per file, but I'll leave it like 
180   # this for the moment.
181   do_mm=0
182   if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then
183     mkdir $mask_dir_tmp 2> /dev/null
184     return_mkdir=$?
185     if [ $return_mkdir == 0 ]; then
186       do_mm=1 
187     else
188       while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do
189         echo "waiting creation of motion masks..."
190         sleep 2
191       done
192       echo "finished waiting"
193     fi  
194   fi
195   
196 #   do_mm=1
197 #   mask_dir_tmp=$mask_dir
198   if [ $do_mm == 1 ]; then
199     mask_log_dir=$mask_dir_tmp/LOG
200     mkdir -p $mask_log_dir
201
202     # multi-threaded pre-processing for motion mask calcs
203     pids=( )
204     for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
205       phase_nb=${phase_nbs[$i]}
206       phase_file=${phase_files[$i]}
207       afdb_file=`echo $phase_file | sed 's/mhd/afdb/'`
208
209       check_threads $MAX_THREADS
210       mm_preprocessing &
211       pids=( "${pids[@]}" "$!" )
212     done
213
214     wait_pids ${pids[@]}
215     for ret in $ret_codes; do
216       abort_on_error mm_preprocessing $ret clean_up_masks
217     done
218
219     # single-threaded motion mask calc
220     for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
221       phase_nb=${phase_nbs[$i]}
222       phase_file=${phase_files[$i]}
223
224       check_threads 1
225       echo "$phase_file -> Computing motion mask..."
226       compute_motion_mask > $mask_log_dir/motion_mask_$phase_file.log
227       abort_on_error compute_motion_mask $? clean_up_masks
228     done
229
230     # multi-threaded post-processing of motion mask calcs
231     pids=( )
232     for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
233       phase_nb=${phase_nbs[$i]}
234       phase_file=${phase_files[$i]}
235
236       check_threads $MAX_THREADS 
237       mm_postprocessing &
238       pids=( "${pids[@]}" "$!" )
239     done
240   
241     wait_pids ${pids[@]}
242     for ret in $ret_codes; do
243       abort_on_error mm_postprocessing $ret clean_up_masks
244     done
245
246
247     # rename tmp mask directory after mask creation
248     check_threads 1
249     mv -f $mask_dir_tmp $mask_dir
250   fi
251
252   echo
253   echo "-------- Motion mask done ! ---------"
254   end=`date`
255   echo "start: $start"
256   echo "end: $end"
257   echo
258 }
259
260
261 #################
262 # main  #
263 #################
264
265 if [ $# != 3 ]; then
266   echo "Usage: $0 CT_4D RESAMPLE_SPACING RESAMPLE_ALGORITHM"
267   exit -1
268 fi
269
270 #
271 # variables exported in this scope
272 #
273 # mask_dir: directory where all masks are kept
274 #
275
276 if [ $1 != "using-as-lib" ]; then
277   motion_mask $1 $2 $3
278 fi