]> Creatis software - clitk.git/blob - scripts/calculate_contour_stats.sh
added new script to install
[clitk.git] / scripts / calculate_contour_stats.sh
1 #! /bin/bash +x 
2
3 select_contour()
4 {
5   local roi_list=$@
6   roi=`zenity --list --title="Available Contours" --column="Please choose a contour:" $roi_list`
7   case $? in
8     0)
9     if [ -z $roi ]
10     then
11       zenity --warning --text="You must choose one contour."
12       select_contour $roi_list
13     else
14       rtstruct_roi_name=$roi
15     fi;;
16     1)
17     if zenity --question --text="Do you really wish to quit?"
18     then
19       exit
20     else
21       select_contour $roi_list
22     fi;;
23     -1)
24       zenity --error --text="Unexpected error. Please relaunch the application."
25       exit;;
26   esac
27 }
28
29 select_roi()
30 {
31   rtstruct_roi_name_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0026" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Name] \|]//'` )
32   rtstruct_roi_number_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0022" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Number] \|]//'` )
33   rtstruct_roi_list=( )
34   for i in $(seq 0 1 $(( ${#rtstruct_roi_name_list[@]} - 1 ))); do
35     rtstruct_roi_list[$i]=${rtstruct_roi_number_list[$i]}:${rtstruct_roi_name_list[$i]}
36   done
37
38   select_contour ${rtstruct_roi_list[@]}
39   rtstruct_roi=`echo ${rtstruct_roi_name} | cut -d ':' -f 1`
40 }
41
42 ############# main 
43
44 if echo $* | grep "\-h"; then
45   echo Usage: calculate_motion_amplitude.sh { RTSTRUCT_FILE REFERENCE_IMAGE | --gui }
46
47   exit 0
48 fi
49
50
51 rtstruct_roi=0
52
53 if echo $* | grep "\-\-gui" > /dev/null 2>&1; then
54   if [ $# != 1 ]; then
55     echo Invalid arguments. Type \'`basename $0` -h\' for help
56     exit -1
57   fi
58
59   rtstruct_file=`zenity --file-selection --title="Select RT Struct file."`
60   select_roi
61   rtstruct_ref_image=`zenity --file-selection --title="Select Reference Image."`
62 else
63   if [ $# != 2 ]; then
64     echo Invalid arguments. Type \'`basename $0` -h\' for help
65     exit -1
66   fi
67
68   rtstruct_file=$1
69   rtstruct_ref_image=$2
70   select_roi
71 fi
72
73
74 #echo "Processing \"${rtstruct_roi_list[$roi_struct]}\"..."
75 # create ROI mask from rtstruct
76 roi_mask=roi_${rtstruct_roi_name}.mhd
77 clitkDicomRTStruct2Image -i ${rtstruct_file} -o ${roi_mask} -j ${rtstruct_ref_image} -r ${rtstruct_roi} 2> /tmp/err.txt
78 if cat /tmp/err.txt | grep -q "No ROI"; then
79   echo Invalid contour number.
80   exit -3
81 fi
82
83 # guarantees the same sampling for roi mask and vector image
84 roi_mask2=resampled_${roi_mask}
85 clitkResampleImage -i ${roi_mask} -o ${roi_mask2} --like ${rtstruct_ref_image}
86
87 # calculate stats
88 clitkImageStatistics -i ${rtstruct_ref_image} -m ${roi_mask2} --verbose 2> /dev/null | tail -n 8 | zenity --text-info --title "Restuls for \"${rtstruct_roi_name}\""
89
90 rm `basename $roi_mask .mhd`.{mhd,raw}
91 rm `basename $roi_mask2 .mhd`.{mhd,raw}
92 rm /tmp/err.txt