3 #################################################################
4 # create_MidP arguments : CT_4D.mhd ref_phase spacing #
5 #################################################################
6 source `dirname $0`/midp_common.sh
10 echo "$image_name -> Extracting patient..."
11 $CLITK/clitkExtractPatient -i $image_name -o MASK/patient_$image_name --noAutoCrop
12 $CLITK/clitkBinarizeImage -i MASK/patient_$image_name -o MASK/patient_$image_name -l 1 -u 1 --mode=BG
13 $CLITK/clitkSetBackground -i $image_name -o MASK/patient_only_$image_name --mask MASK/patient_$image_name --outsideValue -1000
18 echo "$image_name -> Extracting bones..."
19 $CLITK/clitkImageConvert -i $image_name -o MASK/float_$image_name -t float
20 $CLITK/clitkExtractBones -i MASK/float_$image_name -o MASK/bones_$image_name --lower1 120 --upper1 2000 --lower2 70 --upper2 2000 --smooth --time 0.0625 --noAutoCrop
21 $CLITK/clitkMorphoMath -i MASK/bones_$image_name -o MASK/bones_$image_name --type 2 --radius 4,4,2
26 echo "$image_name -> Resampling..."
27 $CLITK/clitkResampleImage -i MASK/patient_$image_name -o MASK/patient_$image_name --spacing $spacing
28 $CLITK/clitkResampleImage -i MASK/patient_only_$image_name -o MASK/patient_only_$image_name --spacing $spacing
29 #$CLITK/clitkResampleImage -i MASK/bones_$image_name -o MASK/bones_$image_name --like MASK/patient_only_$image_name
34 # $CLITK/clitkMotionMask -i MASK/patient_only_$image_name -o MASK/mm_$image_name --featureBones=MASK/bones_$image_name --upperThresholdLungs -400 --fillingLevel 94 --offsetDetect 0,-5,0 --pad --writeFeature=MASK/feature_$image_name --writeEllips=MASK/inital_ellipse_$image_name --writeGrownEllips=MASK/growing_ellipse_$image_name;
35 $CLITK/clitkMotionMask -i MASK/patient_only_$image_name -o MASK/mm_$image_name --upperThresholdLungs -400 --fillingLevel 94 --offsetDetect 0,-5,0 --pad --writeFeature=MASK/feature_$image_name --writeEllips=MASK/inital_ellipse_$image_name --writeGrownEllips=MASK/growing_ellipse_$image_name;
40 echo "$image_name -> Setting Background..."
41 $CLITK/clitkSetBackground -i MASK/patient_only_$image_name -o MASK/inside_$image_name --mask MASK/mm_$image_name --outsideValue -1200
42 $CLITK/clitkSetBackground -i MASK/patient_only_$image_name -o MASK/outside_$image_name --mask MASK/mm_$image_name --outsideValue -1200 --fg
45 create_registration_masks()
47 echo "$image_name -> Creating registration masks..."
48 $CLITK/clitkMorphoMath -i MASK/mm_$image_name -o MASK/regmask_in_$image_name --type 1 --radius 8
49 $CLITK/clitkExtractPatient -i MASK/outside_$image_name -o MASK/regmask_out_$image_name --noAutoCrop
50 $CLITK/clitkMorphoMath -i MASK/regmask_out_$image_name -o MASK/regmask_out_$image_name --type 1 --radius 8
55 echo "Removing temporary files..."
56 image_name_base=`echo $image_name | sed 's/mhd//'`
59 rm MASK/float_$image_name_base*;;
61 rm MASK/bones_$image_name_base*;;
63 rm MASK/patient_only_$image_name_base*;;
65 #rm MASK/patient_$image_name_base*
66 #rm MASK/mm_$image_name_base*
67 rm -f $vf_dir/vf_tmp_in_${ref}_${phase}.*
68 rm -f $vf_dir/vf_tmp_out_${ref}_${phase}.*
69 #rm MASK/regmask_in_$image_name_base*
70 #rm MASK/regmask_out_$image_name_base*
82 #rm -f $vf_dir/vf_MIDP_${ref}.*
83 #rm -f $vf_dir/vf_${ref}_MIDP.*;;
85 ;; #rm $vf_dir/vf_MIDP_${phase}.*;;
105 create_registration_masks
114 # echo "$image_name -> Computing motion mask..."
115 # compute_motion_mask >> LOG/motion_mask_$image_name.log
119 # create_registration_masks
125 echo "------------ Motion mask ------------"
130 rm -f "LOG/motion_mask*.log"
136 # multi-threaded pre-processing for motion mask calcs
137 for phase in $phase_list
139 image_name=`echo $phase | sed 's/raw/mhd/'`
140 check_threads $MAX_THREADS
144 # single-threaded motion mask calc
146 for phase in $phase_list
148 image_name=`echo $phase | sed 's/raw/mhd/'`
150 echo "$image_name -> Computing motion mask..."
151 #compute_motion_mask >> LOG/motion_mask_$image_name.log
154 # multi-threaded post-processing of motion mask calcs
155 for phase in $phase_list
157 image_name=`echo $phase | sed 's/raw/mhd/'`
158 check_threads $MAX_THREADS
160 regmask_in_list="$regmask_in_list regmask_in_$image_name"
161 regmask_out_list="$regmask_out_list regmask_out_$image_name"
162 reg_in_list="$reg_in_list inside_$image_name"
163 reg_out_list="$reg_out_list outside_$image_name"
168 echo "-------- Motion mask done ! ---------"
177 ########## register in ##########
178 for reg_in in $reg_in_list
180 if [ ! -z `echo $reg_in | grep "$phase"` ]
185 echo "Computing BLUTDIR $reference_in -> $target_in ..."
186 #$CLITK/clitkBLUTDIR --reference="MASK/$reference_in" --target="MASK/$target_in" --output="MASK/reg_$target_in" --referenceMask="MASK/$reference_mask_in" --vf="$vf_dir/vf_in_${ref}_${phase}.mhd" $coeff_in_ini --coeff="$coeff_dir/coeff_in_${ref}_${phase}.mhd" $registration_parameters_BLUTDIR >> LOG/registration_${ref}_${phase}.log
187 $CLITK/clitkBLUTDIR --reference="MASK/$reference_in" --target="MASK/$target_in" --output="MASK/reg_$target_in" --referenceMask="MASK/$reference_mask_in" --vf="$vf_dir/vf_in_${ref}_${phase}.mhd" --coeff="$coeff_dir/coeff_in_${ref}_${phase}.mhd" $registration_parameters_BLUTDIR >> LOG/registration_${ref}_${phase}.log
188 coeff_in_ini="--initCoeff=$coeff_dir/coeff_in_${ref}_${phase}.mhd"
189 ########## register out ##########
190 for reg_out in $reg_out_list
192 if [ ! -z `echo $reg_out | grep "$phase"` ]
197 echo "Computing BLUTDIR $reference_out -> $target_out ..."
198 #$CLITK/clitkBLUTDIR --reference="MASK/$reference_out" --target="MASK/$target_out" --output="MASK/reg_$target_out" --referenceMask="MASK/$reference_mask_out" --vf="$vf_dir/vf_out_${ref}_${phase}.mhd" $coeff_out_ini --coeff="$coeff_dir/coeff_out_${ref}_${phase}.mhd" $registration_parameters_BLUTDIR >> LOG/registration_${ref}_${phase}.log
199 $CLITK/clitkBLUTDIR --reference="MASK/$reference_out" --target="MASK/$target_out" --output="MASK/reg_$target_out" --referenceMask="MASK/$reference_mask_out" --vf="$vf_dir/vf_out_${ref}_${phase}.mhd" --coeff="$coeff_dir/coeff_out_${ref}_${phase}.mhd" $registration_parameters_BLUTDIR >> LOG/registration_${ref}_${phase}.log
200 coeff_out_ini="--initCoeff=$coeff_dir/coeff_out_${ref}_${phase}.mhd"
201 ##################################
202 $CLITK/clitkCombineImage -i $vf_dir/vf_in_${ref}_${phase}.mhd -j $vf_dir/vf_out_${ref}_${phase}.mhd -m MASK/mm_$image_name -o $vf_dir/vf_${ref}_${phase}.mhd
203 $CLITK/clitkZeroVF -i $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_${ref}_${ref}.mhd
204 $CLITK/clitkCombineImage -i $vf_dir/vf_${ref}_${phase}.mhd -j $vf_dir/vf_${ref}_${ref}.mhd -m MASK/patient_$image_name -o $vf_dir/vf_${ref}_${phase}.mhd
210 ########## register in ##########
211 for reg_in in $reg_in_list
213 if [ ! -z `echo $reg_in | grep "_$phase"` ]
218 echo "Computing DEMONSDIR $reference_in -> $target_in ..."
219 $CLITK/clitkDemonsDeformableRegistration --reference="MASK/$reference_in" --target="MASK/$target_in" --output="MASK/reg_$target_in" --vf="$vf_dir/vf_in_${ref}_${phase}.mhd" $vf_in_ini $registration_parameters_DEMONSDIR #&>> LOG/registration_${ref}_${phase}.log
220 vf_in_ini="--init=$vf_dir/vf_in_${ref}_${phase}.mhd"
221 ########## register out ##########
222 for reg_out in $reg_out_list
224 if [ ! -z `echo $reg_out | grep "_$phase"` ]
229 echo "Computing DEMONSDIR $reference_out -> $target_out ..."
230 $CLITK/clitkDemonsDeformableRegistration --reference="MASK/$reference_out" --target="MASK/$target_out" --output="MASK/reg_$target_out" --vf="$vf_dir/vf_out_${ref}_${phase}.mhd" $vf_out_ini $registration_parameters_DEMONSDIR #&>> LOG/registration_${ref}_${phase}.log
231 vf_out_ini="--init=$vf_dir/vf_out_${ref}_${phase}.mhd"
232 ##################################
233 $CLITK/clitkCombineImage -i $vf_dir/vf_in_${ref}_${phase}.mhd -j $vf_dir/vf_out_${ref}_${phase}.mhd -m MASK/mm_$image_name -o $vf_dir/vf_${ref}_${phase}.mhd
234 $CLITK/clitkZeroVF -i $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_${ref}_${ref}.mhd
235 $CLITK/clitkCombineImage -i $vf_dir/vf_${ref}_${phase}.mhd -j $vf_dir/vf_${ref}_${ref}.mhd -m MASK/patient_$image_name -o $vf_dir/vf_${ref}_${phase}.mhd
241 ########## register in ##########
242 for reg_in in $reg_in_list
244 if [ ! -z `echo $reg_in | grep "_$phase"` ]
249 echo "Computing ELASTIX $reference_in -> $target_in ..."
250 exec_dir=`which elastix`
251 exec_dir=`dirname $exec_dir`
252 cat $exec_dir/params_BSpline.txt | sed -e "s+<NbIterations>+500+" \
253 -e "s+<LabelsFile>++" \
254 -e "s+<HistBins>+25+" \
256 -e "s+<NbSamples>+2000+" \
257 -e "s+<SamplerType>+Random+" \
258 -e "s+<Spacing>+32+" > params_BSpline.txt
259 elastix -f "MASK/$reference_in" -m "MASK/$target_in" -fMask "MASK/$reference_mask_in" -out $vf_dir -p params_BSpline.txt > /dev/null
260 transformix -tp $vf_dir/TransformParameters.0.txt -out $vf_dir -def all > /dev/null
261 mv $vf_dir/deformationField.mhd $vf_dir/vf_in_${ref}_${phase}.mhd
262 mv $vf_dir/deformationField.raw $vf_dir/vf_in_${ref}_${phase}.raw
263 sed -i "s:deformationField:vf_in_${ref}_${phase}:" $vf_dir/vf_in_${ref}_${phase}.mhd
264 mv $vf_dir/result.0.mhd MASK/reg_$target_in
265 targetraw=`echo reg_$target_in | sed 's:mhd:raw:'`
266 sed -i "s:result.0.mhd:$targetraw" MASK/reg_$target_in
267 mv $vf_dir/result.0.raw MASK/$targetraw
270 ########## register out ##########
271 for reg_out in $reg_out_list
273 if [ ! -z `echo $reg_out | grep "_$phase"` ]
278 echo "Computing ELASTIX $reference_out -> $target_out ..."
279 elastix -f "MASK/$reference_out" -m "MASK/$target_out" -fMask "MASK/$reference_mask_out" -out $vf_dir -p params_BSpline.txt > /dev/null
280 transformix -tp $vf_dir/TransformParameters.0.txt -out $vf_dir -def all > /dev/null
281 mv $vf_dir/deformationField.mhd $vf_dir/vf_out_${ref}_${phase}.mhd
282 mv $vf_dir/deformationField.raw $vf_dir/vf_out_${ref}_${phase}.raw
283 sed -i "s:deformationField:vf_out_${ref}_${phase}:" $vf_dir/vf_out_${ref}_${phase}.mhd
284 mv $vf_dir/result.0.mhd MASK/reg_$target_out
285 targetraw=`echo reg_$target_out | sed 's:mhd:raw:'`
286 sed -i "s:result.0.mhd:$targetraw" MASK/reg_$target_out
287 mv $vf_dir/result.0.raw MASK/$targetraw
290 ##################################
291 $CLITK/clitkCombineImage -i $vf_dir/vf_in_${ref}_${phase}.mhd -j $vf_dir/vf_out_${ref}_${phase}.mhd -m MASK/mm_$image_name -o $vf_dir/vf_${ref}_${phase}.mhd
292 $CLITK/clitkZeroVF -i $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_${ref}_${ref}.mhd
293 $CLITK/clitkCombineImage -i $vf_dir/vf_${ref}_${phase}.mhd -j $vf_dir/vf_${ref}_${ref}.mhd -m MASK/patient_$image_name -o $vf_dir/vf_${ref}_${phase}.mhd
300 echo "----------- Registration ------------"
305 rm -f "LOG/registration*.log"
307 # wait any unfinished threads
310 for reg_in in $reg_in_list
312 if [ ! -z `echo $reg_in | grep "$ref"` ]
317 for reg_out in $reg_out_list
319 if [ ! -z `echo $reg_out | grep "$ref"` ]
321 reference_out=$reg_out
324 for regmask_in in $regmask_in_list
326 if [ ! -z `echo $regmask_in | grep "$ref"` ]
328 reference_mask_in=$regmask_in
331 for regmask_out in $regmask_out_list
333 if [ ! -z `echo $regmask_out | grep "$ref"` ]
335 reference_mask_out=$regmask_out
339 registration_parameters_BLUTDIR="--spacing=32,32,32 --interp=2 --metric=11 --bins=25 --samples=1 --levels=3 --verbose " #--coeffEveryN 5"
340 registration_parameters_DEMONSDIR="--demons=3 --levels=1"
341 registration_parameters_ELASTIX="--demons=3 --levels=1"
348 for phase in $list_phases
350 for img in $phase_list
352 if [ ! -z `echo $img | grep "$phase" | grep -v "[0-9]$phase"` ]
354 image_name=`echo $img | sed 's/raw/mhd/'`
371 echo "-------- Registration done ! --------"
378 calculate_vf_MIDP_REF()
380 echo "Calculating vf_REF_MIDP.mhd..."
382 create_mhd_4D.sh $vf_dir #"vf_4D.mhd"
383 $CLITK/clitkAverageTemporalDimension -i $vf_dir/_4D.mhd -o $vf_dir/vf_${ref}_MIDP.mhd
384 $CLITK/clitkInvertVF -i $vf_dir/vf_${ref}_MIDP.mhd -o $vf_dir/vf_MIDP_${ref}.mhd
387 calculate_CT_MIDP_REF()
389 reference=`ls *.mhd | grep $ref #| grep -v "[0-9]$ref.mhd"`
390 echo "Calculating CT_MIDP_REF.mhd '$reference'..."
391 $CLITK/clitkWarpImage -i $reference -o CT_MIDP_REF.mhd --vf=$vf_dir/vf_MIDP_${ref}.mhd -s 1
394 calculate_CT_MIDP_PHASE()
396 echo "Calculating CT_MIDP_${phase}.mhd..."
397 $CLITK/clitkComposeVF -i $vf_dir/vf_MIDP_${ref}.mhd -j $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_MIDP_${phase}.mhd
398 phase_img=`ls *.mhd | grep "${phase}" # | grep -v "[0-9]$ref.mhd"`
399 $CLITK/clitkWarpImage -i $phase_img -o MIDP/CT_MIDP_${phase}.mhd --vf=$vf_dir/vf_MIDP_${phase}.mhd -s 1
400 $CLITK/clitkImageConvert -i MIDP/CT_MIDP_${phase}.mhd -o MIDP/CT_MIDP_${phase}.mhd -t float
404 prepare_MIDP_images()
406 echo "Preparing MIDP images..."
407 cp CT_MIDP_REF.mhd MIDP/CT_MIDP_${ref}.mhd
408 cp CT_MIDP_REF.raw MIDP/CT_MIDP_${ref}.raw
409 cat MIDP/CT_MIDP_${ref}.mhd | sed "s/ElementDataFile = .*/ElementDataFile = CT\_MIDP\_${ref}\.raw/" > MIDP/CT_MIDP_${ref}_tmp.mhd
410 rm MIDP/CT_MIDP_${ref}.mhd
411 mv MIDP/CT_MIDP_${ref}_tmp.mhd MIDP/CT_MIDP_${ref}.mhd
412 $CLITK/clitkImageConvert -i MIDP/CT_MIDP_${ref}.mhd -o MIDP/CT_MIDP_${ref}.mhd -t float
413 create_mhd_4D.sh MIDP #"CT_MIDP_4D.mhd"
416 calculate_CT_MIDP_MOY()
418 echo "Calculating CT_MIDP_MOY.mhd..."
419 $CLITK/clitkAverageTemporalDimension -i MIDP/_4D.mhd -o CT_MIDP_MOY.mhd
422 calculate_CT_MIDP_MED()
424 echo "Calculating CT_MIDP_MED.mhd..."
425 $CLITK/clitkMedianTemporalDimension -i MIDP/_4D.mhd -o CT_MIDP_MED.mhd
428 calculate_CT_MIDP_MIP()
430 echo "Calculating CT_MIDP_MIP.mhd..."
431 $CLITK/clitkMIP -i MIDP/_4D.mhd -o CT_MIDP_MIP.mhd -d 3
437 echo "----------- Mid-position ------------"
444 calculate_vf_MIDP_REF
445 calculate_CT_MIDP_REF
446 for phase in $list_phases
448 check_threads $MAX_THREADS
449 calculate_CT_MIDP_PHASE &
453 calculate_CT_MIDP_MED &
454 calculate_CT_MIDP_MIP &
455 calculate_CT_MIDP_MOY &
459 echo "-------- Mid-position done ! --------"
472 echo "Usage: create_midP.sh CT_4D.mhd ref_phase computation_spacing(mm) method(1:BSPLINE, 2:DEMONS)"
475 CLITK=~/creatis/clitk3/build/bin
477 CT_4D=`basename $CT_4D_path`
478 work_dir=`dirname $CT_4D_path`
485 coeff_dir="$vf_dir/coeff"
491 echo "--------------- START ---------------"
493 echo "start: $begining"
497 phase_list=`grep ".raw" $CT_4D`
498 echo "phases -> " $phase_list
499 nb_phases=`grep ".raw" $CT_4D | wc -l`
503 phase_files=( `cat $CT_4D | grep ".raw" | sed 's:.raw:.mhd:'` )
504 echo "Phase files are ${phase_files[@]}"
506 phase_nbs=( `echo ${phase_files[@]} | grep -o '[[:alpha:][:punct:]][0-9]\{1,2\}[[:punct:]]' | grep -o '[0-9]\{1,2\}'` )
507 #phase_nbs=( `echo ${phase_files[@]} | grep -o '[0-9]\{1,2\}'` )
508 echo "Phase numbers are ${phase_nbs[@]}"
511 for ph in $phase_list
513 #ph_nb=`echo $ph | grep -o "[0-9][0-9]*\.raw" | sed -e 's/\.raw//'`
514 ph_nb=`echo $ph | grep -o "[0-9][0-9]"`
515 if [ $ph_nb -gt $ref ]
517 gt_ref="$gt_ref $ph_nb"
518 elif [ $ph_nb -lt $ref ]
520 lt_ref="$lt_ref $ph_nb"
523 list_phases="$gt_ref $lt_ref"
524 echo list_phases $list_phases
531 echo "---------------- END ----------------"
533 echo "start: $begining"
534 echo "end: $terminating"