3 ###############################################################################
5 # FILE: create_midP-2.0.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
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.
16 ###############################################################################
18 source `dirname $0`/midp_common.sh
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 $?
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 $?
32 if [ x = x$ExtractBonesLower1 ]; then
33 ExtractBonesLower1=120
35 if [ x = x$ExtractBonesLower2 ]; then
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
45 echo "$phase_file -> Extracting lungs..."
46 clitkExtractLung -i $phase_file -o $mask_dir_tmp/lungs_$phase_nb.mhd -a $afdb_file --noAutoCrop --doNotSeparateLungs
53 echo "$phase_file -> Resampling..."
54 clitkResampleImage -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/patient_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo
55 clitkResampleImage -i $mask_dir_tmp/patient_mask_$phase_nb.mhd -o $mask_dir_tmp/patient_mask_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo
56 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
61 if [ x = x$MotionMaskOffsetDetect ]; then
62 MotionMaskOffsetDetect="0,-5,0"
64 if [ x = x$FillingLevel ]; then
68 clitkMotionMask -i $mask_dir_tmp/patient_$phase_nb.mhd -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
69 #--monitor=$mask_dir_tmp/monitor_$phase_nb.mhd
80 input_dir=`dirname $input`
81 input_base=`basename $input`
84 clitkMorphoMath -i $input_mask -o $input_dir/extra1_$input_base --type 1 --radius $radius
85 clitkImageArithm -i $input_dir/extra1_$input_base -j $input_mask -o $input_dir/band1_$input_base -t 7
86 clitkBinarizeImage -i $input_dir/band1_$input_base -o $input_dir/band1_$input_base -l 1 -u 1 --fg 100 --mode both
87 clitkImageConvert -i $input_dir/band1_$input_base -o $input_dir/short_band1_$input_base -t short
90 clitkMorphoMath -i $input_dir/extra1_$input_base -o $input_dir/extra2_$input_base --type 1 --radius $radius
91 clitkImageArithm -i $input_dir/extra2_$input_base -j $input_dir/extra1_$input_base -o $input_dir/band2_$input_base -t 7
92 clitkBinarizeImage -i $input_dir/band2_$input_base -o $input_dir/band2_$input_base -l 1 -u 1 --fg 200 --mode both
93 clitkImageConvert -i $input_dir/band2_$input_base -o $input_dir/short_band2_$input_base -t short
95 # combine bands with masks
96 clitkImageArithm -i $input_mask -j $input_dir/band1_$input_base -o $output_mask -t 0
97 clitkImageArithm -i $output_mask -j $input_dir/band2_$input_base -o $output_mask -t 0
98 # combine bands with image
99 clitkCombineImage -i $input_dir/short_band1_$input_base -j $input -m $input_dir/band1_$input_base -o $output
100 clitkCombineImage -i $input_dir/short_band2_$input_base -j $output -m $input_dir/band2_$input_base -o $output
103 rm `echo $input_dir/extra?_$input_base | sed 's:.mhd:.*:g'`
104 rm `echo $input_dir/band?_$input_base | sed 's:.mhd:.*:g'`
105 rm `echo $input_dir/short_band?_$input_base | sed 's:.mhd:.*:g'`
108 create_registration_masks()
110 # extract inside and outside lung regions from the patient image,
111 # using the motion mask computed previously
112 echo "$phase_file -> Setting Background..."
113 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
114 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
116 # the registration masks for inside (and outside) region correspond
117 # to the motion mask (and its complement) plus extra grey value bands,
118 # obtained with morphological dilations.
120 echo "$phase_file -> Creating registration masks..."
122 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
124 clitkExtractPatient -i $mask_dir_tmp/outside_$phase_nb.mhd -o $mask_dir_tmp/mm_outside_$phase_nb.mhd --noAutoCrop
125 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
134 if [ $resample_spacing -ne 0 ] ; then
143 create_registration_masks
148 #set cmd line variables
150 if [ $# -eq 3 ] ; then
161 # import variables specific to each patient
162 if test -e ./variables; then
166 #set other global variables
167 if [ $resample_spacing -ne 0 ] ; then
168 mask_dir="MASK-${resample_spacing}mm-$resample_algo"
172 mask_dir_tmp="tmp.$mask_dir"
173 extract_4d_phases $mhd4d
176 echo "------------ Motion mask from create_midP_masks.sh ------------"
181 # the motion masks are first created in a tmp directory. this directory is
182 # later going to be renamed to the final motion mask directory. concurrent
183 # executions trying to create the same set of masks will be blocked until
184 # the first execution finishes. naturally, these other executions will not
185 # recreate the masks. so first we try to create the tmp directory.
186 # if the creation fails, it means that another execution is
187 # already creating the masks, so this execution will be blocked. the
188 # execution is unblocked only when the creation of masks is finished and
189 # the mask directory is renamed.
191 # ATTENTION: RP - 08/02/2011
192 # robustness issue: tmp directory may exist but may be empty or
193 # incomplete. the solution is to check per file, but I'll leave it like
194 # this for the moment.
196 if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then
197 mkdir $mask_dir_tmp 2> /dev/null
199 if [ $return_mkdir == 0 ]; then
202 while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do
203 echo "waiting creation of motion masks..."
206 echo "finished waiting"
211 # mask_dir_tmp=$mask_dir
212 if [ $do_mm == 1 ]; then
213 mask_log_dir=$mask_dir_tmp/LOG
214 mkdir -p $mask_log_dir
216 # multi-threaded pre-processing for motion mask calcs
218 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
219 phase_nb=${phase_nbs[$i]}
220 phase_file=${phase_files[$i]}
221 afdb_file=`echo $phase_file | sed 's/mhd/afdb/'`
223 check_threads $MAX_THREADS
225 pids=( "${pids[@]}" "$!" )
229 for ret in $ret_codes; do
230 abort_on_error mm_preprocessing $ret clean_up_masks
233 # single-threaded motion mask calc
234 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
235 phase_nb=${phase_nbs[$i]}
236 phase_file=${phase_files[$i]}
239 echo "$phase_file -> Computing motion mask..."
240 compute_motion_mask > $mask_log_dir/motion_mask_$phase_file.log
241 abort_on_error compute_motion_mask $? clean_up_masks
244 # multi-threaded post-processing of motion mask calcs
246 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
247 phase_nb=${phase_nbs[$i]}
248 phase_file=${phase_files[$i]}
250 check_threads $MAX_THREADS
252 pids=( "${pids[@]}" "$!" )
256 for ret in $ret_codes; do
257 abort_on_error mm_postprocessing $ret clean_up_masks
261 # rename tmp mask directory after mask creation
263 mv -f $mask_dir_tmp $mask_dir
267 echo "-------- Motion mask done ! ---------"
279 if [ $# -ne 3 -a $# -ne 1 ]; then
280 echo "Usage: $0 CT_4D [RESAMPLE_SPACING RESAMPLE_ALGORITHM]"
285 # variables exported in this scope
287 # mask_dir: directory where all masks are kept
290 if [ $1 != "using-as-lib" ]; then
291 if [ $# -eq 3 ] ; then