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 clitkSetBackground -i $phase_file -o $mask_dir_tmp/patient_$phase_nb.mhd --mask $mask_dir_tmp/patient_mask_$phase_nb.mhd --outsideValue -1000
29 if [ x = x$ExtractBonesLower1 ]; then
30 ExtractBonesLower1=120
32 if [ x = x$ExtractBonesLower2 ]; then
35 echo "$phase_file -> Extracting bones..."
36 clitkImageConvert -i $phase_file -o $mask_dir_tmp/float_$phase_nb.mhd -t float
37 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
42 echo "$phase_file -> Extracting lungs..."
43 clitkExtractLung -i $phase_file -o $mask_dir_tmp/lungs_$phase_nb.mhd -a $afdb_file --noAutoCrop
48 echo "$phase_file -> Resampling..."
49 clitkResampleImage -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/patient_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo
51 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
56 if [ x = x$MotionMaskOffsetDetect ]; then
57 MotionMaskOffsetDetect="0,-5,0"
59 if [ x = x$FillingLevel ]; then
63 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
64 #--monitor=$mask_dir_tmp/monitor_$phase_nb.mhd
75 input_dir=`dirname $input`
76 input_base=`basename $input`
79 clitkMorphoMath -i $input_mask -o $input_dir/extra1_$input_base --type 1 --radius $radius
80 clitkImageArithm -i $input_dir/extra1_$input_base -j $input_mask -o $input_dir/band1_$input_base -t 7
81 clitkBinarizeImage -i $input_dir/band1_$input_base -o $input_dir/band1_$input_base -l 1 -u 1 --fg 100 --mode both
82 clitkImageConvert -i $input_dir/band1_$input_base -o $input_dir/short_band1_$input_base -t short
85 clitkMorphoMath -i $input_dir/extra1_$input_base -o $input_dir/extra2_$input_base --type 1 --radius $radius
86 clitkImageArithm -i $input_dir/extra2_$input_base -j $input_dir/extra1_$input_base -o $input_dir/band2_$input_base -t 7
87 clitkBinarizeImage -i $input_dir/band2_$input_base -o $input_dir/band2_$input_base -l 1 -u 1 --fg 200 --mode both
88 clitkImageConvert -i $input_dir/band2_$input_base -o $input_dir/short_band2_$input_base -t short
90 # combine bands with masks
91 clitkImageArithm -i $input_mask -j $input_dir/band1_$input_base -o $output_mask -t 0
92 clitkImageArithm -i $output_mask -j $input_dir/band2_$input_base -o $output_mask -t 0
93 # combine bands with image
94 clitkCombineImage -i $input_dir/short_band1_$input_base -j $input -m $input_dir/band1_$input_base -o $output
95 clitkCombineImage -i $input_dir/short_band2_$input_base -j $output -m $input_dir/band2_$input_base -o $output
98 rm `echo $input_dir/extra?_$input_base | sed 's:.mhd:.*:g'`
99 rm `echo $input_dir/band?_$input_base | sed 's:.mhd:.*:g'`
100 rm `echo $input_dir/short_band?_$input_base | sed 's:.mhd:.*:g'`
103 create_registration_masks()
105 # extract inside and outside lung regions from the patient image,
106 # using the motion mask computed previously
107 echo "$phase_file -> Setting Background..."
108 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
109 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
111 # the registration masks for inside (and outside) region correspond
112 # to the motion mask (and its complement) plus extra grey value bands,
113 # obtained with morphological dilations.
115 echo "$phase_file -> Creating registration masks..."
117 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
119 clitkExtractPatient -i $mask_dir_tmp/outside_$phase_nb.mhd -o $mask_dir_tmp/mm_outside_$phase_nb.mhd --noAutoCrop
120 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
136 create_registration_masks
141 #set cmd line variables
149 # import variables specific to each patient
152 #set other global variables
153 mask_dir="MASK-${resample_spacing}mm-$resample_algo"
154 mask_dir_tmp="tmp.$mask_dir"
155 extract_4d_phases $mhd4d
158 echo "------------ Motion mask from create_midP_masks.sh ------------"
163 # the motion masks are first created in a tmp directory. this directory is
164 # later going to be renamed to the final motion mask directory. concurrent
165 # executions trying to create the same set of masks will be blocked until
166 # the first execution finishes. naturally, these other executions will not
167 # recreate the masks. so first we try to create the tmp directory.
168 # if the creation fails, it means that another execution is
169 # already creating the masks, so this execution will be blocked. the
170 # execution is unblocked only when the creation of masks is finished and
171 # the mask directory is renamed.
173 # ATTENTION: RP - 08/02/2011
174 # robustness issue: tmp directory may exist but may be empty or
175 # incomplete. the solution is to check per file, but I'll leave it like
176 # this for the moment.
178 if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then
179 mkdir $mask_dir_tmp 2> /dev/null
181 if [ $return_mkdir == 0 ]; then
184 while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do
185 echo "waiting creation of motion masks..."
188 echo "finished waiting"
193 # mask_dir_tmp=$mask_dir
194 if [ $do_mm == 1 ]; then
195 mask_log_dir=$mask_dir_tmp/LOG
196 mkdir -p $mask_log_dir
198 # multi-threaded pre-processing for motion mask calcs
199 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
200 phase_nb=${phase_nbs[$i]}
201 phase_file=${phase_files[$i]}
202 afdb_file=`echo $phase_file | sed 's/mhd/afdb/'`
204 check_threads $MAX_THREADS
208 # single-threaded motion mask calc
209 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
210 phase_nb=${phase_nbs[$i]}
211 phase_file=${phase_files[$i]}
214 echo "$phase_file -> Computing motion mask..."
215 compute_motion_mask > $mask_log_dir/motion_mask_$phase_file.log
218 # multi-threaded post-processing of motion mask calcs
219 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
220 phase_nb=${phase_nbs[$i]}
221 phase_file=${phase_files[$i]}
223 check_threads $MAX_THREADS
227 # rename tmp mask directory after mask creation
229 mv -f $mask_dir_tmp $mask_dir
233 echo "-------- Motion mask done ! ---------"
246 echo "Usage: $0 CT_4D RESAMPLE_SPACING RESAMPLE_ALGORITHM"
251 # variables exported in this scope
253 # mask_dir: directory where all masks are kept
256 if [ $1 != "using-as-lib" ]; then