]> Creatis software - clitk.git/blob - scripts/calculate_motion_amplitude.sh
c1128b0ebdec75f3896fd5298bfe6efedf4db8df
[clitk.git] / scripts / calculate_motion_amplitude.sh
1 #! /bin/bash +x 
2
3 source `dirname $0`/midp_common.sh
4
5
6 calculate_motion()
7 {
8   # calculate motion amplitudes along the 3 image axes
9   rm /tmp/result.txt 2> /dev/null
10   dir=( "\"left-right\"" "\"anterior-posterior\"" "\"cranio-caudal\"" )
11   pct=( 0 33 67 )
12   for i in 0 1 2; do
13     if [ $gui_mode = 1 ]; then
14       echo "${pct[$i]}"; echo "# Calculating motion along ${dir[$i]} direction.."
15     else
16       echo "Calculating motion along ${dir[$i]} direction.."
17     fi
18
19     echo "Motion along ${dir[$i]} direction" >> /tmp/result.txt
20     clitkImageStatistics -i ${vector_file} -m ${roi_mask2} -c $i --verbose 2> /dev/null | tail -n 8 | head -n 6 >> /tmp/result.txt
21     min=`tail -n 2 /tmp/result.txt | head -n 1 | cut -f 2 -d ':'`
22     max=`tail -n 1 /tmp/result.txt | cut -f 2 -d ':'`
23     amp=`echo $max-$min | bc`
24
25     echo "Amplitude: $amp" >> /tmp/result.txt
26     echo "" >> /tmp/result.txt
27   done
28 }
29
30 ############# main 
31
32 if echo $* | grep "\-h"; then
33   echo "Usage: calculate_motion_amplitude.sh { VECTOR_FILE RTSTRUCT_REF_IMAGE RTSTRUCT_FILE [ RTSTRUCT_ROI_NUMBER | --gui ] } | --gui "
34   echo "  VECTOR_FILE: mhd of the vector field from where to extract motion information (may also be 4D)"
35   echo "  RTSTRUCT_REF_IMAGE: mhd of the reference image used in the contour delineation"
36   echo "  RTSTRUCT_FILE: dicom with contours"
37   echo "  RTSTRUCT_ROI: number of the contour whose motion to analyze"
38   echo "  --gui: use GUI to select files"
39   exit 0
40 fi
41
42 if echo $* | grep "\-\-gui" > /dev/null 2>&1; then
43   gui_mode=1
44   if [ $# = 1 ]; then
45     vector_file=`zenity --file-selection --title="Select 4D Vector Field file."`
46     rtstruct_ref_image=`zenity --file-selection --title="Select Reference Image."`
47     rtstruct_file=`zenity --file-selection --title="Select RT Struct file."`
48     select_roi
49   elif [ $# = 4 ]; then
50     vector_file=$1
51     rtstruct_ref_image=$2
52     rtstruct_file=$3
53     select_roi
54   else
55     echo Invalid arguments. Type \'`basename $0` -h\' for help
56     exit -1
57   fi
58 else
59   gui_mode=0
60   vector_file=$1
61   rtstruct_ref_image=$2
62   rtstruct_file=$3
63   if [ $# = 4 ]; then
64     rtstruct_roi=$4
65     rtstruct_roi_name=$rtstruct_roi
66   elif [ $# = 3 ]; then
67     select_roi
68   else
69     echo Invalid arguments. Type \'`basename $0` -h\' for help
70     exit -1
71   fi
72 fi
73
74 if ! cat $vector_file | grep -q "Channels = 3"; then
75   echo Vector file must have 3 channels.
76   exit -2
77 fi
78
79 # create ROI mask from rtstruct
80 roi_mask=roi_${rtstruct_roi}.mhd
81 clitkDicomRTStruct2Image -i ${rtstruct_file} -o ${roi_mask} -j ${rtstruct_ref_image} -r ${rtstruct_roi} 2> /tmp/err.txt
82 if cat /tmp/err.txt | grep -q "No ROI"; then
83   echo Invalid contour number.
84   exit -3
85 fi
86
87 # guarantees the same sampling for roi mask and vector image
88 roi_mask2=resampled_${roi_mask}
89 clitkResampleImage -i ${roi_mask} -o ${roi_mask2} --like ${vector_file}
90
91 if [ $gui_mode = 1 ]; then
92   calculate_motion | zenity --progress --auto-close --percentage=0 --text=""
93 else
94   calculate_motion
95 fi
96
97 if [ $gui_mode = 1 ]; then
98   cat /tmp/result.txt | zenity --text-info --title "Restuls for \"${rtstruct_roi_name}\""
99 else
100   echo "Restuls for \"${rtstruct_roi_name}\""
101   cat /tmp/result.txt 
102 fi
103
104 rm `basename $roi_mask .mhd`.{mhd,raw}
105 rm `basename $roi_mask2 .mhd`.{mhd,raw}
106 rm /tmp/err.txt
107 rm /tmp/result.txt