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