#! /bin/bash +x
-select_contour_gui()
-{
- local roi_list=$@
- roi=`zenity --list --title="Available Contours" --column="Please choose a contour:" $roi_list`
- case $? in
- 0)
- if [ -z $roi ]
- then
- zenity --warning --text="You must choose one contour."
- select_contour $roi_list
- else
- rtstruct_roi_name=$roi
- fi;;
- 1)
- if zenity --question --text="Do you really wish to quit?"
- then
- exit
- else
- select_contour $roi_list
- fi;;
- -1)
- zenity --error --text="Unexpected error. Please relaunch the application."
- exit;;
- esac
-}
+source `dirname $0`/midp_common.sh
-select_contour()
-{
- local roi_list=$@
- echo "Available Contours:"
- for r in $roi_list; do
- echo $r
- done
-
- echo "Please choose a contour number:"
- read rtstruct_roi_number
-}
-
-select_roi()
-{
- rtstruct_roi_name_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0026" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Name] \|]//'` )
- rtstruct_roi_number_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0022" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Number] \|]//'` )
- rtstruct_roi_list=( )
- for i in $(seq 0 1 $(( ${#rtstruct_roi_name_list[@]} - 1 ))); do
- rtstruct_roi_list[$i]=${rtstruct_roi_number_list[$i]}:${rtstruct_roi_name_list[$i]}
- done
-
- if [ $gui_mode = 1 ]; then
- select_contour_gui ${rtstruct_roi_list[@]}
- rtstruct_roi=`echo ${rtstruct_roi_name} | cut -d ':' -f 1`
- rtstruct_roi_name=`echo ${rtstruct_roi_name} | cut -d ':' -f 2`
- else
- select_contour ${rtstruct_roi_list[@]}
- rtstruct_roi=$rtstruct_roi_number
- rtstruct_roi_name=${rtstruct_roi_name_list[$(( $rtstruct_roi_number - 1))]}
- fi
-}
############# main
if echo $* | grep "\-h"; then
- echo Usage: calculate_motion_amplitude.sh { RTSTRUCT_FILE REFERENCE_IMAGE | --gui }
-
+ echo Usage: calculate_motion_amplitude.sh { REFERENCE_IMAGE RTSTRUCT_FILE [ RTSTRUCT_ROI | --gui ] } | --gui
+ echo " RTSTRUCT_REF_IMAGE: mhd of the reference image used in the contour delineation"
+ echo " RTSTRUCT_FILE: dicom with contours"
+ echo " RTSTRUCT_ROI: number of the contour whose motion to analyze"
+ echo " --gui: use GUI to select files"
exit 0
fi
-rtstruct_roi=0
+rtstruct_roi=
if echo $* | grep "\-\-gui" > /dev/null 2>&1; then
- if [ $# != 1 ]; then
+ gui_mode=1
+ if [ $# = 1 ]; then
+ rtstruct_ref_image=`zenity --file-selection --title="Select Reference Image."`
+ rtstruct_file=`zenity --file-selection --title="Select RT Struct file."`
+ select_roi
+ elif [ $# = 3 ]; then
+ rtstruct_ref_image=$1
+ rtstruct_file=$2
+ select_roi
+ else
echo Invalid arguments. Type \'`basename $0` -h\' for help
exit -1
fi
- gui_mode=1
- rtstruct_file=`zenity --file-selection --title="Select RT Struct file."`
- select_roi
- rtstruct_ref_image=`zenity --file-selection --title="Select Reference Image."`
else
- if [ $# != 2 ]; then
+ gui_mode=0
+ rtstruct_ref_image=$1
+ rtstruct_file=$2
+ if [ $# = 2 ]; then
+ select_roi
+ elif [ $# = 3 ]; then
+ rtstruct_roi=$3
+ rtstruct_roi_name=$rtstruct_roi
+ else
echo Invalid arguments. Type \'`basename $0` -h\' for help
exit -1
fi
- gui_mode=0
- rtstruct_file=$1
- rtstruct_ref_image=$2
- select_roi
fi
#! /bin/bash +x
+source `dirname $0`/midp_common.sh
+
+
+calculate_motion()
+{
+ # calculate motion amplitudes along the 3 image axes
+ rm /tmp/result.txt 2> /dev/null
+ dir=( "\"left-right\"" "\"anterior-posterior\"" "\"cranio-caudal\"" )
+ pct=( 0 33 67 )
+ for i in 0 1 2; do
+ if [ $gui_mode = 1 ]; then
+ echo "${pct[$i]}"; echo "# Calculating motion along ${dir[$i]} direction.."
+ else
+ echo "Calculating motion along ${dir[$i]} direction.."
+ fi
+
+ echo "Motion along ${dir[$i]} direction" >> /tmp/result.txt
+ clitkImageStatistics -i ${vector_file} -m ${roi_mask2} -c $i --verbose 2> /dev/null | tail -n 8 | head -n 6 >> /tmp/result.txt
+ min=`tail -n 2 /tmp/result.txt | head -n 1 | cut -f 2 -d ':'`
+ max=`tail -n 1 /tmp/result.txt | cut -f 2 -d ':'`
+ amp=`echo $max-$min | bc`
+
+ echo "Amplitude: $amp" >> /tmp/result.txt
+ echo "" >> /tmp/result.txt
+ done
+}
+
############# main
if echo $* | grep "\-h"; then
- echo Usage: calculate_motion_amplitude.sh VECTOR_FILE RTSTRUCT_FILE RTSTRUCT_ROI_NUMBER RTSTRUCT_REF_IMAGE
+ echo "Usage: calculate_motion_amplitude.sh { VECTOR_FILE RTSTRUCT_REF_IMAGE RTSTRUCT_FILE [ RTSTRUCT_ROI_NUMBER | --gui ] } | --gui "
echo " VECTOR_FILE: mhd of the vector field from where to extract motion information (may also be 4D)"
+ echo " RTSTRUCT_REF_IMAGE: mhd of the reference image used in the contour delineation"
echo " RTSTRUCT_FILE: dicom with contours"
echo " RTSTRUCT_ROI: number of the contour whose motion to analyze"
- echo " RTSTRUCT_REF_IMAGE: mhd of the reference image used in the contour delineation"
-
+ echo " --gui: use GUI to select files"
exit 0
fi
-if [ $# != 4 ]; then
- echo Invalid arguments. Type \'`basename $0` -h\' for help
- exit -1
+if echo $* | grep "\-\-gui" > /dev/null 2>&1; then
+ gui_mode=1
+ if [ $# = 1 ]; then
+ vector_file=`zenity --file-selection --title="Select 4D Vector Field file."`
+ rtstruct_ref_image=`zenity --file-selection --title="Select Reference Image."`
+ rtstruct_file=`zenity --file-selection --title="Select RT Struct file."`
+ select_roi
+ elif [ $# = 4 ]; then
+ vector_file=$1
+ rtstruct_ref_image=$2
+ rtstruct_file=$3
+ select_roi
+ else
+ echo Invalid arguments. Type \'`basename $0` -h\' for help
+ exit -1
+ fi
+else
+ gui_mode=0
+ vector_file=$1
+ rtstruct_ref_image=$2
+ rtstruct_file=$3
+ if [ $# = 4 ]; then
+ rtstruct_roi=$4
+ rtstruct_roi_name=$rtstruct_roi
+ elif [ $# = 3 ]; then
+ select_roi
+ else
+ echo Invalid arguments. Type \'`basename $0` -h\' for help
+ exit -1
+ fi
fi
-vector_file=$1
-rtstruct_file=$2
-rtstruct_roi=$3
-rtstruct_ref_image=$4
-
if ! cat $vector_file | grep -q "Channels = 3"; then
echo Vector file must have 3 channels.
exit -2
# create ROI mask from rtstruct
roi_mask=roi_${rtstruct_roi}.mhd
-clitkDicomRTStruct2BinaryImage -i ${rtstruct_file} -o ${roi_mask} -j ${rtstruct_ref_image} -r ${rtstruct_roi} 2> /tmp/err.txt
+clitkDicomRTStruct2Image -i ${rtstruct_file} -o ${roi_mask} -j ${rtstruct_ref_image} -r ${rtstruct_roi} 2> /tmp/err.txt
if cat /tmp/err.txt | grep -q "No ROI"; then
echo Invalid contour number.
exit -3
roi_mask2=resampled_${roi_mask}
clitkResampleImage -i ${roi_mask} -o ${roi_mask2} --like ${vector_file}
-# calculate motion amplitudes along the 3 image axes
-dir=( sagittal coronal axial )
-for i in 0 1 2; do
- echo Motion along ${dir[$i]} direction
- clitkImageStatistics -i ${vector_file} -m ${roi_mask2} -c $i --verbose 2> /dev/null | tail -n 8 | head -n 6 > /tmp/res.txt
- min=`tail -n 2 /tmp/res.txt | head -n 1 | cut -f 2 -d ':'`
- max=`tail -n 1 /tmp/res.txt | cut -f 2 -d ':'`
- amp=`echo $max-$min | bc`
- cat /tmp/res.txt
- echo "Amplitude: $amp"
- echo
-done
+if [ $gui_mode = 1 ]; then
+ calculate_motion | zenity --progress --auto-close --percentage=0 --text=""
+else
+ calculate_motion
+fi
+
+if [ $gui_mode = 1 ]; then
+ cat /tmp/result.txt | zenity --text-info --title "Restuls for \"${rtstruct_roi_name}\""
+else
+ echo "Restuls for \"${rtstruct_roi_name}\""
+ cat /tmp/result.txt
+fi
rm `basename $roi_mask .mhd`.{mhd,raw}
rm `basename $roi_mask2 .mhd`.{mhd,raw}
rm /tmp/err.txt
-rm /tmp/res.txt
+rm /tmp/result.txt
nb_phases=${#phase_files[@]}
# get everything except numbers and punctuation
- cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[^0-9[:punct:]]*" | sort -u > /tmp/patterns.txt
+ cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[^0-9[:punct:]]*" | sort -u > /tmp/patterns.txt
# find which patterns have the phases connected to it
patterns=`cat /tmp/patterns.txt`
if [ -z "$patterns" ]; then
- phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep "[0-9]\+"` )
+ phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep "[0-9]\+"` )
else
for i in $patterns; do
# check if the pattern appears before the phase number
- nb_phases_found=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | sort -u | wc -l`
+ nb_phases_found=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | sort -u | wc -l`
if [ $nb_phases_found == $nb_phases ]; then
# keep only what identifies the phase number
- phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` )
+ phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` )
break
fi
# check if the pattern appears after the phase number
- nb_phases_found=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | sort -u | wc -l`
+ nb_phases_found=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | sort -u | wc -l`
if [ $nb_phases_found == $nb_phases ]; then
# keep only what identifies the phase number
- phase_nbs=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` )
+ phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` )
break
fi
echo "4D file is $mhd4d"
# array of phase files
- phase_files=( `cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:'` )
+ phase_files=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:'` )
echo "Phase files are ${phase_files[@]}"
extract_4d_phase_numbers $mhd4d
extract_4d_phases $1
# reference phase file
- ref_phase_file=`cat $mhd4d | grep ".raw" | sed 's:.raw:.mhd:' | grep $2`
+ ref_phase_file=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | grep $2`
echo "Reference phase is $ref_phase_file"
# reference phase number
local tot=$tmp.mhd
local dir=`dirname $1`
- local first=`grep raw $1 | sed 's/raw/mhd/g' | head -n 1`
+ local first=`grep z*raw $1 | sed 's/z*raw/mhd/g' | head -n 1`
clitkImageArithm -i $dir/$first -o $tot -t 1 -s 0
- local nbphases=`grep raw $1 | sed 's/raw/mhd/g' | wc -l`
- for i in $(grep raw $1 | sed 's/raw/mhd/g'); do
+ local nbphases=`grep z*raw $1 | sed 's/z*raw/mhd/g' | wc -l`
+ for i in $(grep z*raw $1 | sed 's/z*raw/mhd/g'); do
clitkImageArithm -i $dir/$i -j $tot -o $tot
done
clitkImageArithm -i $tot -o $2 -t 11 -s $nbphases
rm $tmp.*
}
+
+select_contour_gui()
+{
+ local roi_list=$@
+ roi=`zenity --list --title="Available Contours" --column="Please choose a contour:" $roi_list`
+ case $? in
+ 0)
+ if [ -z $roi ]
+ then
+ zenity --warning --text="You must choose one contour."
+ select_contour $roi_list
+ else
+ rtstruct_roi_name=$roi
+ fi;;
+ 1)
+ if zenity --question --text="Do you really wish to quit?"
+ then
+ exit
+ else
+ select_contour $roi_list
+ fi;;
+ -1)
+ zenity --error --text="Unexpected error. Please relaunch the application."
+ exit;;
+ esac
+}
+
+select_contour()
+{
+ local roi_list=$@
+ echo "Available Contours:"
+ for r in $roi_list; do
+ echo $r
+ done
+
+ echo "Please choose a contour number:"
+ read rtstruct_roi_index
+ let i=0
+ for r in $roi_list; do
+ if [ $rtstruct_roi_index = `echo $r | cut -d ':' -f 1` ]; then
+ rtstruct_roi_index=$i
+ break;
+ fi
+ let i=i+1
+ done
+
+}
+
+select_roi()
+{
+ rtstruct_roi_name_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0026" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Name] \|]//'` )
+ rtstruct_roi_number_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0022" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Number] \|]//'` )
+ rtstruct_roi_list=( )
+ for i in $(seq 0 1 $(( ${#rtstruct_roi_name_list[@]} - 1 ))); do
+ rtstruct_roi_list[$i]=${rtstruct_roi_number_list[$i]}:${rtstruct_roi_name_list[$i]}
+ done
+
+ if [ $gui_mode = 1 ]; then
+ select_contour_gui ${rtstruct_roi_list[@]}
+ rtstruct_roi=`echo ${rtstruct_roi_name} | cut -d ':' -f 1`
+ rtstruct_roi_name=`echo ${rtstruct_roi_name} | cut -d ':' -f 2`
+ else
+ select_contour ${rtstruct_roi_list[@]}
+ rtstruct_roi=${rtstruct_roi_number_list[$(( $rtstruct_roi_index ))]}
+ rtstruct_roi_name=${rtstruct_roi_name_list[$(( $rtstruct_roi_index ))]}
+ fi
+}
\ No newline at end of file