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