]> Creatis software - clitk.git/blob - scripts/create_midP_masks.sh
new midP related scripts
[clitk.git] / scripts / create_midP_masks.sh
1 #! /bin/bash -x
2
3 source `dirname $0`/common.sh
4
5 extract_patient()
6 {
7   echo "$image_name -> Extracting patient..."
8   clitkExtractPatient -i $image_name -o $mask_dir_tmp/patient_$image_name --noAutoCrop -a $afdb_name $ExtractPatientExtra
9   clitkBinarizeImage -i $mask_dir_tmp/patient_$image_name -o $mask_dir_tmp/patient_$image_name -l 1 -u 1 --mode=BG
10   clitkSetBackground -i $image_name -o $mask_dir_tmp/patient_only_$image_name --mask $mask_dir_tmp/patient_$image_name --outsideValue -1000
11 }
12
13 extract_bones()
14 {
15   if [ x = x$ExtractBonesLower1 ]; then
16     ExtractBonesLower1=120
17   fi
18   if [ x = x$ExtractBonesLower2 ]; then
19     ExtractBonesLower2=80
20   fi
21   echo "$image_name -> Extracting bones..."
22   clitkImageConvert -i $image_name -o $mask_dir_tmp/float_$image_name -t float
23   #clitkExtractBones -i $mask_dir_tmp/float_$image_name -o $mask_dir_tmp/bones_$image_name --lower1 120 --upper1 2000 --lower2 70 --upper2 2000 --smooth --time 0.0625 --noAutoCrop
24   clitkExtractBones -i $mask_dir_tmp/float_$image_name -o $mask_dir_tmp/bones_$image_name -a $afdb_name --lower1 $ExtractBonesLower1 --upper1 2000 --lower2 $ExtractBonesLower2 --upper2 2000 --smooth --time 0.0625 --noAutoCrop
25   #clitkMorphoMath -i $mask_dir_tmp/bones_$image_name -o $mask_dir_tmp/bones_$image_name --type 2 --radius 4,4,2
26 }
27
28 extract_lungs()
29 {
30   echo "$image_name -> Extracting lungs..."
31   clitkExtractLung -i $image_name -o $mask_dir_tmp/lungs_$image_name -a $afdb_name --noAutoCrop
32 }
33
34 resample()
35 {
36   echo "$image_name -> Resampling..."
37   clitkResampleImage -i $mask_dir_tmp/patient_$image_name -o $mask_dir_tmp/patient_$image_name --spacing $resample_spacing --interp $resample_algo
38   clitkResampleImage -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/patient_only_$image_name --spacing $resample_spacing --interp $resample_algo
39   clitkResampleImage -i $mask_dir_tmp/bones_$image_name -o $mask_dir_tmp/bones_$image_name --like $mask_dir_tmp/patient_only_$image_name --interp $resample_algo
40   clitkResampleImage -i $mask_dir_tmp/lungs_$image_name -o $mask_dir_tmp/lungs_$image_name --like $mask_dir_tmp/patient_only_$image_name
41 }
42
43 compute_motion_mask()
44 {
45   if [ x = x$MotionMaskOffsetDetect ]; then
46     MotionMaskOffsetDetect="0,-5,0"
47   fi
48   if [ x = x$FillingLevel ]; then
49     FillingLevel=94
50   fi
51   clitkMotionMask -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/mm_$image_name --featureBones=$mask_dir_tmp/bones_$image_name --featureLungs=$mask_dir_tmp/lungs_$image_name --upperThresholdLungs -400 --fillingLevel $FillingLevel --offsetDetect 0,-5,0 --pad --writeFeature=$mask_dir_tmp/feature_$image_name $MotionMaskExtra --monitor=$mask_dir_tmp/monitor_$image_name
52 }
53
54 set_background()
55 {
56   echo "$image_name -> Setting Background..."
57   clitkSetBackground -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/inside_$image_name --mask $mask_dir_tmp/mm_$image_name --outsideValue -1200
58   clitkSetBackground -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/outside_$image_name --mask $mask_dir_tmp/mm_$image_name --outsideValue -1200 --fg
59 }
60
61 create_registration_masks()
62 {
63   echo "$image_name -> Creating registration masks..."
64   clitkMorphoMath -i $mask_dir_tmp/mm_$image_name -o $mask_dir_tmp/regmask_in_$image_name --type 1 --radius 8
65   clitkExtractPatient -i $mask_dir_tmp/outside_$image_name -o $mask_dir_tmp/regmask_out_$image_name --noAutoCrop
66   clitkMorphoMath -i $mask_dir_tmp/regmask_out_$image_name -o $mask_dir_tmp/regmask_out_$image_name --type 1 --radius 8
67 }
68
69 remove_tmp_masks()
70 {
71   echo "Removing temporary files..."
72   image_name_base=`echo $image_name | sed 's/mhd//'`
73   case $1 in
74   1)
75     #rm $mask_dir_tmp/float_$image_name_base*
76     ;;
77   2)
78     #rm $mask_dir_tmp/bones_$image_name_base*
79     ;;
80   3)
81     #rm $mask_dir_tmp/patient_only_$image_name_base*
82     ;;
83   esac
84 }
85
86 mm_preprocessing()
87 {
88   extract_patient
89   extract_bones
90   extract_lungs
91   remove_tmp_masks 1
92   resample
93 }
94
95 mm_postprocessing()
96 {
97   remove_tmp_masks 2
98   set_background
99   remove_tmp_masks 3
100   create_registration_masks
101 }
102
103 mm_workflow()
104 {
105   extract_patient
106   extract_bones
107   remove_tmp_masks 1
108   resample
109   echo "$image_name -> Computing motion mask..."
110   compute_motion_mask >> $mask_log_dir/motion_mask_$image_name.log
111   remove_tmp_masks 2
112   set_background
113   remove_tmp_masks 3
114   create_registration_masks
115 }
116
117 motion_mask()
118 {
119   #set cmd line variables
120   echo "4D CT -> "$1
121   phases=`grep ".raw" $1`
122   echo "mask phases -> " $phases
123
124   dir=`dirname $1`
125   cd $dir
126
127   # import variables specific to each patient
128   source variables
129
130   resample_spacing=$2
131   resample_algo=$3
132
133     
134   #set other global variables
135   mask_dir="MASK-$resample_spacing""mm""-$resample_algo"
136   mask_dir_tmp="tmp."$mask_dir
137
138   echo
139   echo "------------ Motion mask from create_midP_masks.sh ------------"
140   start=`date`
141   echo "start: $start"
142   echo
143
144   # the motion masks are first created in a tmp directory. this directory is 
145   # later going to be renamed to the final motion mask directory. concurrent
146   # executions trying to create the same set of masks will be blocked until
147   # the first execution finishes. naturally, these other executions will not
148   # recreate the masks. so first we try to create the tmp directory. 
149   # if the creation fails, it means that another execution is
150   # already creating the masks, so this execution will be blocked. the
151   # execution is unblocked only when the creation of masks is finished and
152   # the mask directory is renamed.
153   #
154   # ATTENTION: RP - 08/02/2011
155   # robustness issue: tmp directory may exist but may be empty or 
156   # incomplete. the solution is to check per file, but I'll leave it like 
157   # this for the moment.
158   do_mm=0
159   if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then
160     mkdir $mask_dir_tmp 2> /dev/null
161     return_mkdir=$?
162     if [ $return_mkdir == 0 ]; then
163       do_mm=1 
164     else
165       while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do
166         echo "waiting creation of motion masks..."
167         sleep 2
168       done
169       echo "finished waiting"
170     fi  
171   fi
172
173   regmask_in_list=""
174   regmask_out_list=""
175   reg_in_list=""
176   reg_out_list=""
177
178   # multi-threaded pre-processing for motion mask calcs
179   if [ $do_mm == 1 ]; then
180     mask_log_dir=$mask_dir_tmp/LOG
181     mkdir -p $mask_log_dir
182
183     for phase in $phases
184     do
185       image_name=`echo $phase | sed 's/raw/mhd/'`
186       afdb_name=`echo $phase | sed 's/raw/afdb/'`
187       check_threads $MAX_THREADS
188       mm_preprocessing &
189     done
190
191     # single-threaded motion mask calc
192     for phase in $phases
193     do
194       image_name=`echo $phase | sed 's/raw/mhd/'`
195
196       check_threads 1
197       echo "$image_name -> Computing motion mask..."
198       compute_motion_mask >> $mask_log_dir/motion_mask_$image_name.log
199     done
200   fi
201
202   # multi-threaded post-processing of motion mask calcs
203   for phase in $phases
204   do
205     image_name=`echo $phase | sed 's/raw/mhd/'`
206     if [ $do_mm = 1 ]; then
207       check_threads $MAX_THREADS 
208       mm_postprocessing &
209     fi
210
211     regmask_in_list="$regmask_in_list regmask_in_$image_name"
212     regmask_out_list="$regmask_out_list regmask_out_$image_name"
213     reg_in_list="$reg_in_list inside_$image_name"
214     reg_out_list="$reg_out_list outside_$image_name"
215   done
216
217   # rename tmp mask directory after mask creation
218   check_threads 1
219   mv $mask_dir_tmp $mask_dir
220
221   wait
222
223   echo
224   echo "-------- Motion mask done ! ---------"
225   end=`date`
226   echo "start: $start"
227   echo "end: $end"
228   echo
229 }
230
231
232 #################
233 # main  #
234 #################
235
236 if [ $# != 3 ]; then
237   echo "Usage: create_midP_masks.sh 4D_CT.mhd resample_spacing resample_algorithm"
238   exit -1
239 fi
240
241 #
242 # variables exported in this scope
243 #
244 # mask_dir: directory where all masks are kept
245 # regmask_in_list: list of registration mask files (inside lungs)
246 # regmask_out_list: list of registration mask files (outised lungs)
247 # reg_in_list: list of registration image files (inside lungs)
248 # reg_out_list: list of registration image files (outside lungs)
249 #
250
251 if [ $1 != "using-as-lib" ]; then
252   motion_mask $1 $2 $3
253 fi