]> Creatis software - clitk.git/blob - scripts/calculate_motion_amplitude.sh
motion masks with and without bands
[clitk.git] / scripts / calculate_motion_amplitude.sh
1 #! /bin/bash +x 
2
3 ############# main 
4
5 if echo $* | grep "\-h"; then
6   echo Usage: calculate_motion_amplitude.sh VECTOR_FILE RTSTRUCT_FILE RTSTRUCT_ROI_NUMBER RTSTRUCT_REF_IMAGE
7   echo "  VECTOR_FILE: mhd of the vector field from where to extract motion information (may also be 4D)"
8   echo "  RTSTRUCT_FILE: dicom with contours"
9   echo "  RTSTRUCT_ROI: number of the contour whose motion to analyze"
10   echo "  RTSTRUCT_REF_IMAGE: mhd of the reference image used in the contour delineation"
11
12   exit 0
13 fi
14
15 if [ $# != 4 ]; then
16   echo Invalid arguments. Type \'`basename $0` -h\' for help
17   exit -1
18 fi
19
20 vector_file=$1
21 rtstruct_file=$2
22 rtstruct_roi=$3
23 rtstruct_ref_image=$4
24
25 if ! cat $vector_file | grep -q "Channels = 3"; then
26   echo Vector file must have 3 channels.
27   exit -2
28 fi
29
30 # create ROI mask from rtstruct
31 roi_mask=roi_${rtstruct_roi}.mhd
32 clitkDicomRTStruct2BinaryImage -i ${rtstruct_file} -o ${roi_mask} -j ${rtstruct_ref_image} -r ${rtstruct_roi} 2> /tmp/err.txt
33 if cat /tmp/err.txt | grep -q "No ROI"; then
34   echo Invalid contour number.
35   exit -3
36 fi
37
38 # guarantees the same sampling for roi mask and vector image
39 roi_mask2=resampled_${roi_mask}
40 clitkResampleImage -i ${roi_mask} -o ${roi_mask2} --like ${vector_file}
41
42 # calculate motion amplitudes along the 3 image axes
43 dir=( sagittal coronal axial )
44 for i in 0 1 2; do
45   echo Motion along ${dir[$i]} direction
46   clitkImageStatistics -i ${vector_file} -m ${roi_mask2} -c $i --verbose 2> /dev/null | tail -n 8 | head -n 6 > /tmp/res.txt
47   min=`tail -n 2 /tmp/res.txt | head -n 1 | cut -f 2 -d ':'`
48   max=`tail -n 1 /tmp/res.txt | cut -f 2 -d ':'`
49   amp=`echo $max-$min | bc`
50   cat /tmp/res.txt
51   echo "Amplitude: $amp"
52   echo
53 done
54
55 rm `basename $roi_mask .mhd`.{mhd,raw}
56 rm `basename $roi_mask2 .mhd`.{mhd,raw}
57 rm /tmp/err.txt
58 rm /tmp/res.txt