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
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
58 if [ x = x$MotionMaskOffsetDetect ]; then
59 MotionMaskOffsetDetect="0,-5,0"
61 if [ x = x$FillingLevel ]; then
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
77 input_dir=`dirname $input`
78 input_base=`basename $input`
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
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
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
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'`
105 create_registration_masks()
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
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.
117 echo "$phase_file -> Creating registration masks..."
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
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
138 create_registration_masks
143 #set cmd line variables
151 # import variables specific to each patient
152 if test -e ./variables; then
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
162 echo "------------ Motion mask from create_midP_masks.sh ------------"
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.
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.
182 if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then
183 mkdir $mask_dir_tmp 2> /dev/null
185 if [ $return_mkdir == 0 ]; then
188 while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do
189 echo "waiting creation of motion masks..."
192 echo "finished waiting"
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
202 # multi-threaded pre-processing for motion mask calcs
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/'`
209 check_threads $MAX_THREADS
211 pids=( "${pids[@]}" "$!" )
215 for ret in $ret_codes; do
216 abort_on_error mm_preprocessing $ret clean_up_masks
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]}
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
230 # multi-threaded post-processing of motion mask calcs
232 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
233 phase_nb=${phase_nbs[$i]}
234 phase_file=${phase_files[$i]}
236 check_threads $MAX_THREADS
238 pids=( "${pids[@]}" "$!" )
242 for ret in $ret_codes; do
243 abort_on_error mm_postprocessing $ret clean_up_masks
247 # rename tmp mask directory after mask creation
249 mv -f $mask_dir_tmp $mask_dir
253 echo "-------- Motion mask done ! ---------"
266 echo "Usage: $0 CT_4D RESAMPLE_SPACING RESAMPLE_ALGORITHM"
271 # variables exported in this scope
273 # mask_dir: directory where all masks are kept
276 if [ $1 != "using-as-lib" ]; then