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