From b0a5bad2f1ac4991b4d2bdb1c015275128441635 Mon Sep 17 00:00:00 2001 From: Romulo Pinho Date: Fri, 6 Jul 2012 10:51:01 +0200 Subject: [PATCH] Moved from repository src2 to private2/dicom_scripts/clitk --- scripts/CMakeLists.txt | 20 - scripts/calculate_contour_stats.sh | 78 ---- scripts/calculate_motion_amplitude.sh | 107 ------ scripts/compress_mhd.sh | 69 ---- scripts/create_mhd_3D.sh | 50 --- scripts/create_mhd_4D-2.0.sh | 49 --- scripts/create_mhd_4D.sh | 154 -------- scripts/create_mhd_4D_pattern.sh | 70 ---- scripts/create_midP-2.0.sh | 461 ---------------------- scripts/create_midP.sh | 535 -------------------------- scripts/create_midP_masks-2.0.sh | 369 ------------------ scripts/create_midP_masks.sh | 253 ------------ scripts/dicom_info.sh | 3 - scripts/midp_common.sh | 281 -------------- scripts/midp_template.conf | 76 ---- scripts/pts_to_landmarks.sh | 32 -- scripts/registration.sh | 162 -------- 17 files changed, 2769 deletions(-) delete mode 100644 scripts/CMakeLists.txt delete mode 100755 scripts/calculate_contour_stats.sh delete mode 100755 scripts/calculate_motion_amplitude.sh delete mode 100755 scripts/compress_mhd.sh delete mode 100755 scripts/create_mhd_3D.sh delete mode 100755 scripts/create_mhd_4D-2.0.sh delete mode 100755 scripts/create_mhd_4D.sh delete mode 100755 scripts/create_mhd_4D_pattern.sh delete mode 100755 scripts/create_midP-2.0.sh delete mode 100755 scripts/create_midP.sh delete mode 100755 scripts/create_midP_masks-2.0.sh delete mode 100755 scripts/create_midP_masks.sh delete mode 100755 scripts/dicom_info.sh delete mode 100755 scripts/midp_common.sh delete mode 100644 scripts/midp_template.conf delete mode 100755 scripts/pts_to_landmarks.sh delete mode 100755 scripts/registration.sh diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt deleted file mode 100644 index 6c444dc..0000000 --- a/scripts/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -#========================================================= -# Install scripts when running make install -SET(SCRIPTS - ${CLITK_SOURCE_DIR}/scripts/calculate_motion_amplitude.sh - ${CLITK_SOURCE_DIR}/scripts/calculate_contour_stats.sh - ${CLITK_SOURCE_DIR}/scripts/midp_common.sh - ${CLITK_SOURCE_DIR}/scripts/registration.sh - ${CLITK_SOURCE_DIR}/scripts/compress_mhd.sh - ${CLITK_SOURCE_DIR}/scripts/create_midP.sh - ${CLITK_SOURCE_DIR}/scripts/create_midP-2.0.sh - ${CLITK_SOURCE_DIR}/scripts/create_mhd_4D.sh - ${CLITK_SOURCE_DIR}/scripts/create_mhd_4D_pattern.sh - ${CLITK_SOURCE_DIR}/scripts/create_midP_masks.sh - ${CLITK_SOURCE_DIR}/scripts/create_midP_masks-2.0.sh - ${CLITK_SOURCE_DIR}/scripts/pts_to_landmarks.sh - ${CLITK_SOURCE_DIR}/scripts/create_mhd_3D.sh - ${CLITK_SOURCE_DIR}/scripts/dicom_info.sh -) - -INSTALL (FILES ${SCRIPTS} DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE) diff --git a/scripts/calculate_contour_stats.sh b/scripts/calculate_contour_stats.sh deleted file mode 100755 index be10e9d..0000000 --- a/scripts/calculate_contour_stats.sh +++ /dev/null @@ -1,78 +0,0 @@ -#! /bin/bash +x - -source `dirname $0`/midp_common.sh - - -############# main - -if echo $* | grep "\-h"; then - 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= - -if echo $* | grep "\-\-gui" > /dev/null 2>&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 - -else - 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 - -fi - - -#echo "Processing \"${rtstruct_roi_list[$roi_struct]}\"..." -# create ROI mask from rtstruct -roi_mask=roi_${rtstruct_roi_name}.mhd -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 -fi - -# guarantees the same sampling for roi mask and vector image -roi_mask2=resampled_${roi_mask} -clitkResampleImage -i ${roi_mask} -o ${roi_mask2} --like ${rtstruct_ref_image} - -# calculate stats -clitkImageStatistics -i ${rtstruct_ref_image} -m ${roi_mask2} --verbose 2> /dev/null | tail -n 8 > /tmp/result.txt - -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/result.txt diff --git a/scripts/calculate_motion_amplitude.sh b/scripts/calculate_motion_amplitude.sh deleted file mode 100755 index c1128b0..0000000 --- a/scripts/calculate_motion_amplitude.sh +++ /dev/null @@ -1,107 +0,0 @@ -#! /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_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 " --gui: use GUI to select files" - exit 0 -fi - -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 - -if ! cat $vector_file | grep -q "Channels = 3"; then - echo Vector file must have 3 channels. - exit -2 -fi - -# create ROI mask from rtstruct -roi_mask=roi_${rtstruct_roi}.mhd -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 -fi - -# guarantees the same sampling for roi mask and vector image -roi_mask2=resampled_${roi_mask} -clitkResampleImage -i ${roi_mask} -o ${roi_mask2} --like ${vector_file} - -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/result.txt diff --git a/scripts/compress_mhd.sh b/scripts/compress_mhd.sh deleted file mode 100755 index f2e8912..0000000 --- a/scripts/compress_mhd.sh +++ /dev/null @@ -1,69 +0,0 @@ -#! /bin/bash - -compress_file() -{ - local f=$1 - if [ "$verbose" = "1" ]; then - echo "Compressing $f..." - fi - - local msg=`clitkImageConvert -i $f -o $f -c` - if [ -z "$msg" ]; then - raw=`echo $f | sed 's/\.mhd/\.raw/'` - if test -e $raw; then - rm $raw - fi - fi -} - -compress_directory() -{ - local input=$1 - - files=$(find $input -name "*.mhd" | sort) - for f in $files; do - if ! grep -iq 'LIST' $f; then - # process all except 4D files - compress_file $f - else - # process only 4D files (just update pointers to new zraw files) - # assumes each .RAW in the list has a corresponding .MHD, which - # must be compressed separately - sed -i 's/\.raw/\.zraw/g' $f - if grep -q "CompressedData" $f; then - sed -i 's/CompressedData.*/CompressedData = True/' $f - else - tmp=/tmp/$RANDOM.mhd - echo "CompressedData = True" > $tmp - cat $f >> $tmp - mv $tmp $f - fi - fi - done -} - -# main program - -if [ $# -lt 1 -o $# -gt 2 ]; then - echo "Invalid params. `basename $0` [-h | --help] for help" 1>&2 - exit -1 -fi - -if [ -n "`echo $@ | grep '\-h\|--help'`" ]; then - echo "Usage: `basename $0` {FILE | DIRECTORY | -h | --help | -v}" - exit 0 -fi - -if [ -n "`echo $@ | grep '\-v'`" ]; then - echo Verbose mode : ON - verbose=1 -fi - -input=$1 -if test -d $input; then - compress_directory $input -elif test -f $input; then - compress_file $input -else - echo "Unknow input file type." 1>&2 -fi diff --git a/scripts/create_mhd_3D.sh b/scripts/create_mhd_3D.sh deleted file mode 100755 index fbb9760..0000000 --- a/scripts/create_mhd_3D.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/sh - - -################################################################################# -# create_mhd_3D argument : {image data} raw_image nom_fichier_de_sortie # -################################################################################# -if [ $# -lt 1 ] -then - echo "Usage: create_mhd_3D.sh dimx dimy dimz spcx spcy spcz offx offy offz pixel_type raw_image_file output_file" - echo "dim*: dimensions of the image" - echo "spc*: pixel spacing along each dimension" - echo "off*: offset along each dimension" - echo "pixel_type: CHAR, UCHAR, SHORT, USHORT, FLOAT" - echo "raw_image_file: image to be referenced by the mhd file created" - echo "output_file: mhd to be created" - exit 1 -fi - -# can point to existing raw files -n=`ls ${11} | wc -l` -if [ $n -eq 0 ] -then - echo "${11} does not exist. Cannot create mhd file." - exit -fi - -# check if the raw file has the "raw" extension -n=`ls ${11} | grep .raw | wc -l` -if [ $n -eq 0 ] -then - # change extension to raw - raw_base=`echo ${11} | cut -d . -f 1`; - raw_file=$raw_base".raw"; - mv ${11} $raw_file -else - raw_file=${11} -fi - -# create file (with some default values...) -echo "NDims = 3" > ${12} -echo "TransformMatrix = 1 0 0 0 1 0 0 0 1" >> ${12} -echo "Offset = " $7 $8 $9 >> ${12} -echo "CenterOfRotation = 0 0 0" >> ${12} -echo "AnatomicalOrientation = RAI" >> ${12} -echo "ElementSpacing = " $4 $5 $6 >> ${12} -echo "DimSize = " $1 $2 $3 >> ${12} -echo "ElementType = MET_"${10} >> ${12} -echo "ElementDataFile = " $raw_file >> ${12} - - diff --git a/scripts/create_mhd_4D-2.0.sh b/scripts/create_mhd_4D-2.0.sh deleted file mode 100755 index c10e098..0000000 --- a/scripts/create_mhd_4D-2.0.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh +x - - -write_mhd_4D() -{ - cat $orig | sed "s/NDims = .*/NDims = 4/ - s/TransformMatrix = .*/TransformMatrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1/ - /Offset/ s/.*/& 0/ - /CenterOfRotation/ s/.*/& 0/ - s/AnatomicalOrientation = .*/AnatomicalOrientation = ????/ - /ElementSpacing/ s/.*/& 1/ - /DimSize/ s/.*/& $nbph/ - s/ElementDataFile = .*/ElementDataFile = LIST/" > "$1/$file_name_4D" - - for ph in $listph - do - phase=`basename $ph` - echo "$phase" >> "$1/$file_name_4D" - done -} - -################################################# -# create_mhd_4D argument : repertoire # -################################################# -if [ $# -lt 1 ] -then - echo "Usage: create_mhd_4D.sh DIRECTORY" - exit 1 -fi - -dirname=`dirname $1` -pattern=`basename $1` - -list_phase_file=`ls -1 $1*[0-9].mhd` -nb_phase_file=`ls -1 $1*[0-9].mhd | wc -l` -if [ $nb_phase_file = 0 ] -then - echo "Error: no phase found" - exit 1 -fi - -nbph=$nb_phase_file -orig=`echo $list_phase_file | cut -f 1 -d ' '` -listph=`echo $list_phase_file | sed 's:\.mhd:\.raw:g'` - -file_name_4D=`echo "${pattern}4D.mhd"` - -write_mhd_4D $dirname -echo "$dirname/$file_name_4D" diff --git a/scripts/create_mhd_4D.sh b/scripts/create_mhd_4D.sh deleted file mode 100755 index 24d3fd1..0000000 --- a/scripts/create_mhd_4D.sh +++ /dev/null @@ -1,154 +0,0 @@ -#!/bin/sh +x - - -write_mhd_4D() -{ - cat $orig | sed "s/NDims = .*/NDims = 4/ - s/TransformMatrix = .*/TransformMatrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1/ - /Offset/ s/.*/& 0/ - /CenterOfRotation/ s/.*/& 0/ - s/AnatomicalOrientation = .*/AnatomicalOrientation = ????/ - /ElementSpacing/ s/.*/& 1/ - /DimSize/ s/.*/& $nbph/ - s/ElementDataFile = .*/ElementDataFile = LIST/" > "$1/$file_name_4D" - - for ph in ${listph[@]} - do - ph=`basename $ph` - echo "$ph" >> "$1/$file_name_4D" - done - echo $1/$file_name_4D -} - -################################################# -# create_mhd_4D argument : repertoire # -################################################# -if [ $# -lt 1 ] -then - echo "Usage: create_mhd_4D.sh DIRECTORY" - exit 1 -fi - -nb_phase_file=`find $1 -maxdepth 1 -iname "*[0-9]*.mhd" | wc -l` -if [ $nb_phase_file = 0 ] -then - echo "No phase found in $1" - exit 1 -fi - - -list_prefix="" -list_phase_file=`find $1 -maxdepth 1 -iname "*[0-9]*.mhd"` -for phase_file in $list_phase_file -do - if grep -q "NDims = 4" $phase_file 2> /dev/null - then - #echo $phase_file is 4D - continue; - fi; - - phase_file_name=`basename $phase_file .mhd` -# if [[ ! -z `echo "$phase_file_name" | grep "__[0-9]"` ]] -# then -# prefix=`echo $phase_file_name | sed "s/__[0-9].*/__/"` -# else -# if [[ ! -z `echo "$phase_file_name" | grep "[0-9]-.*\]"` ]] -# then -# if [[ ! -z `echo "$phase_file_name" | grep "[0-9][0-9]-.*\]"` ]] -# then -# prefix=`echo $phase_file_name | sed "s/[0-9][0-9]-.*//"` -# else -# prefix=`echo $phase_file_name | sed "s/[0-9]-.*//"` -# fi -# else -# prefix="NONE" -# fi -# fi - - # preffix: grep sequence of characters followed by sequence of numbers and remove sequence of numbers - # prefix=`echo $phase_file_name | grep -o "\(^[[:alpha:][:punct:]]*\)\([[:digit:]\.\_\-]\+\)" | sed 's/\(^[[:alpha:][:punct:]]*\)\([[:digit:]\.\_\-]\+\)/\1/'` - prefix=`echo $phase_file_name | grep -o "\(^[[:alpha:][:punct:]]*\)\([[:digit:]]\+\)\([\.\_\-]*\)"` - prefix=`echo $prefix | sed 's/^\-\+//' | sed 's/\(^[[:alpha:][:punct:]]*\)\([[:digit:]]\+\)\([\.\_\-]*\)/\1/'` - if [[ -z $prefix ]] - then - prefix="NONE" - fi - - # register new preffix if not yet done - if [[ -z `echo "$list_prefix" | grep -w -- "$prefix"` ]] - then - list_prefix="$list_prefix $prefix" - fi -done - - -for prefix in $list_prefix -do - if [ "$prefix" = "NONE" ] - then - prefix="" - fi - - list_suffix="" - list_phase_file_prefix=`find $1 -maxdepth 1 -iname "${prefix}[0-9]*.mhd"` - for phase_file_prefix in $list_phase_file_prefix - do - if grep -q "NDims = 4" $phase_file_prefix 2> /dev/null - then - #echo $phase_file_prefix is 4D - continue; - fi; - phase_file_prefix_name=`basename $phase_file_prefix .mhd` -# if [[ ! -z `echo "$phase_file_prefix_name" | grep "__[0-9]"` ]] -# then -# suffix="NONE" -# else -# if [[ ! -z `echo "$phase_file_prefix_name" | grep "[0-9]-.*\]"` ]] -# then -# suffix=`echo $phase_file_prefix_name | sed "s/.*[0-9]-//;s/_\.mhd//;s/\.mhd//"` -# else -# suffix="NONE" -# fi -# fi -# - - # suffix: grep sequence of numbers followed by sequence of characters and remove sequence of numbers - # suffix=`echo $phase_file_prefix_name | grep -o "\([[:digit:]\.\_\-]\+\)\([[:alpha:][:punct:]]*$\)" | sed 's/\([[:digit:]\.\_\-]\+\)\([[:alpha:][:punct:]]*$\)/\2/'` - suffix=`echo $phase_file_prefix_name | grep -o "\([\.\_\-]*\)\([[:digit:]]\+\)\([[:alpha:][:punct:]]*$\)"` - if ! echo $suffix | grep -qo "^\-\+"; then - suffix=`echo $suffix | sed 's/\([\.\_\-]*\)\([[:digit:]]\+\)\([[:alpha:][:punct:]]*$\)/\3/'` - fi - suffix=`echo $suffix | sed 's/[\.\_\-]\+$//'` - if [[ -z $suffix ]] - then - suffix="NONE" - fi - - # register new suffix if not yet done - if [[ -z `echo "$list_suffix" | grep -w -- "$suffix"` ]] - then - list_suffix="$list_suffix $suffix" - fi - done - - - for suffix in $list_suffix - do - if [ "$suffix" = "NONE" ] - then - suffix="" - fi - orig=`find $1 -maxdepth 1 -iname "${prefix}[0-9]*${suffix}*.mhd" | grep "${prefix}[[:digit:][:punct:]]\+${suffix}[\.\_\-]*.mhd" | sort | head -n 1` - listph=( `find $1 -maxdepth 1 -iname "${prefix}[0-9]*${suffix}*.*raw" | grep "${prefix}[[:digit:][:punct:]]\+${suffix}[\.\_\-]*.z*raw" | sort` ) - nbph=${#listph[@]} - - # only create 4D file if potential number of phases is > 1 - if [ $nbph -gt 1 ]; then - file_name_4D="${prefix}4D${suffix}.mhd" - write_mhd_4D $1 - fi - - done - -done - diff --git a/scripts/create_mhd_4D_pattern.sh b/scripts/create_mhd_4D_pattern.sh deleted file mode 100755 index 9f7896a..0000000 --- a/scripts/create_mhd_4D_pattern.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/sh - -############################################################################### -# -# FILE: create_mhd_pattern.sh -# AUTHOR: Rômulo Pinho 05/08/2011 -# -# Similar to create_mhd_4D.sh, but receives a pattern as input. -# -# Example: -# create_mhd_pattern.sh "/all_my_phases_start_like_this_" -# -############################################################################### - -write_mhd_4D() -{ - cat $orig | sed "s/NDims = .*/NDims = 4/ - s/TransformMatrix = .*/TransformMatrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1/ - /Offset/ s/.*/& 0/ - /CenterOfRotation/ s/.*/& 0/ - s/AnatomicalOrientation = .*/AnatomicalOrientation = ????/ - /ElementSpacing/ s/.*/& 1/ - /DimSize/ s/.*/& $nbph/ - s/ElementDataFile = .*/ElementDataFile = LIST/" > "$dirname/$file_name_4D" - - for ph in $listph - do - phase=`basename $ph` - echo "$phase" >> "$dirname/$file_name_4D" - done -} - -################################################# -# create_mhd_4D argument : repertoire # -################################################# -if [ $# -lt 1 ] -then - echo "Usage: $0 PREFIX [SUFFIX]" - exit 1 -fi - -dirname=`dirname $1` -prefix=`basename $1` -suffix=$2 - -nbph=0 -list_phase_file=`ls -1 $dirname/$prefix*[0-9]*$suffix.mhd` -for ph in $list_phase_file -do - if [ -e `echo $ph | sed 's/mhd/raw/'` ] - then - listph="$listph $ph" - nbph=$(( nbph + 1 )) - fi -done - -nb_phase_file=`ls -1 $dirname/$prefix*[0-9]*$suffix.mhd | wc -l` -if [ $nb_phase_file = 0 ] -then - echo "Error: no phase found" - exit 1 -fi - -orig=`echo $list_phase_file | cut -f 1 -d ' '` -listph=`echo $listph | sed 's:\.mhd:\.raw:g'` - -file_name_4D=`echo "${prefix}4D${suffix}.mhd"` - -write_mhd_4D -echo "$dirname/$file_name_4D" diff --git a/scripts/create_midP-2.0.sh b/scripts/create_midP-2.0.sh deleted file mode 100755 index 4c2b41b..0000000 --- a/scripts/create_midP-2.0.sh +++ /dev/null @@ -1,461 +0,0 @@ -#!/bin/sh -x - -############################################################################### -# -# FILE: create_midP-2.0.sh -# AUTHOR: Rômulo Pinho 05/08/2011 -# -# Version 2.0 of the create_midP.sh script. The most relevant changes are: -# * receives a .conf file as input, with variables related to the registration -# parameters and to paramters of the script itself (see accompanying midp_template.conf) -# for details. -# * separates execution steps: it's now possible to choose which operation to execute -# (mask, registration, midp, or all). -# * some steps are now in different modules, to facilitate re-use (see "includes" section). -# * minor modifications on output file names. -# * attempt to simplify the code a bit. -# -############################################################################### - -######################### includes - -source `dirname $0`/create_midP_masks-2.0.sh using-as-lib -source `dirname $0`/registration.sh -source `dirname $0`/midp_common.sh - -registration() -{ - echo - echo "----------- Registration ------------" - start=`date` - echo "start: $start" - echo - - mkdir -p $vf_dir - mkdir -p $output_dir - - # banded images may be created as separate files, - # with the specified preffix, which is interesting for debugging. - # if blank, it means that the original images (those without bands) - # will be used (see create_midP_masks-2.0.sh for details). - banded="" - - # params read from conf file - use_coeffs=1 - params="$nb_iter $nb_samples $sampling_algo $nb_hist_bins $nb_levels $bspline_spacing $metric $optimizer $interpolator" - - # register all phases to the reference - for i in $( seq 0 $((${#phase_files[@]} - 1))); do - phase_file=${phase_files[$i]} - phase_nb=${phase_nbs[$i]} - - if [ "$phase_nb" != "$ref_phase_nb" ]; then - # inside params - reference_in=$mask_dir/${banded}inside_$ref_phase_nb.mhd - target_in=$mask_dir/${banded}inside_$phase_nb.mhd - mask_ref_in=$mask_dir/mask_inside_$ref_phase_nb.mhd - mask_targ_in=$mask_dir/mask_inside_$phase_nb.mhd - vf_in=$vf_dir/vf_inside_${ref_phase_nb}_$phase_nb.mhd - result_in=$output_dir/result_inside_${ref_phase_nb}_$phase_nb.mhd - log_in=$log_dir/log_inside_${ref_phase_nb}_$phase_nb.log - if [ $use_coeffs = 1 ]; then - init_coeff_in=$coeff_in # empty at first iteration - coeff_in=$vf_dir/coeff_inside_${ref_phase_nb}_$phase_nb.mhd - fi - - # outside params - reference_out=$mask_dir/${banded}outside_$ref_phase_nb.mhd - target_out=$mask_dir/${banded}outside_$phase_nb.mhd - mask_ref_out=$mask_dir/mask_outside_$ref_phase_nb.mhd - mask_targ_out=$mask_dir/mask_outside_$phase_nb.mhd - vf_out=$vf_dir/vf_outside_$ref_phase_nb\_$phase_nb.mhd - result_out=$output_dir/result_outside_$ref_phase_nb\_$phase_nb.mhd - log_out=$log_dir/log_outside_${ref_phase_nb}_$phase_nb.log - if [ $use_coeffs = 1 ]; then - init_coeff_out=$coeff_out # empty at first iteration - coeff_out=$vf_dir/coeff_outside_${ref_phase_nb}_$phase_nb.mhd - fi - - # registration - if [ "$method" == "blutdir" ]; then - registration_blutdir $reference_in $target_in $mask_ref_in $mask_targ_in $vf_in $result_in $params $log_in $coeff_in - registration_blutdir $reference_out $target_out $mask_ref_out $mask_targ_out $vf_out $result_out $params $log_out $coeff_out - elif [ "$method" == "elastix" ]; then - registration_elastix $reference_in $target_in $mask_ref_in $mask_targ_in $vf_in $result_in $params $log_in $coeff_in - registration_elastix $reference_out $target_out $mask_ref_out $mask_targ_out $vf_out $result_out $params $log_out $coeff_out - fi - - motion_mask=$mask_dir/${mask_type}_$phase_nb.mhd - - # combine in and out results - out_result=$output_dir/result_${ref_phase_nb}_$phase_nb.mhd - # clitkCombineImage -i $result_in -j $result_out -m $motion_mask -o $out_result - combine_image $result_in $result_out $out_result $motion_mask - abort_on_error registration $? clean_up_registration - - # combine in and out vf - vf_result=$vf_dir/vf_${ref_phase_nb}_$phase_nb.mhd - #clitkCombineImage -i $vf_in -j $vf_out -m $motion_mask -o $vf_result - combine_image $vf_in $vf_out $vf_result $motion_mask - abort_on_error registration $? clean_up_registration - - clitkZeroVF -i $vf_in -o vf_zero.mhd - abort_on_error registration $? clean_up_registration - - patient_mask=$mask_dir/patient_mask_$phase_nb.mhd - #clitkCombineImage -i $vf_result -j vf_zero.mhd -m $patient_mask -o $vf_result - combine_image $vf_result vf_zero.mhd $vf_result $patient_mask - abort_on_error registration $? clean_up_registration - - rm vf_zero.* - - # save for later... - vf_ref=$vf_in - fi - done - - # create (zero) vf from reference to reference and associated images - vf_in=$vf_dir/vf_inside_${ref_phase_nb}_${ref_phase_nb}.mhd - vf_out=$vf_dir/vf_outside_${ref_phase_nb}_${ref_phase_nb}.mhd - clitkZeroVF -i $vf_ref -o $vf_dir/vf_${ref_phase_nb}_${ref_phase_nb}.mhd - clitkZeroVF -i $vf_ref -o $vf_in - clitkZeroVF -i $vf_ref -o $vf_out - abort_on_error registration $? clean_up_registration - - motion_mask=$mask_dir/${mask_type}_${ref_phase_nb}.mhd - reference_in=$mask_dir/${banded}inside_${ref_phase_nb}.mhd - reference_out=$mask_dir/${banded}outside_$ref_phase_nb.mhd - out_result=$output_dir/result_${ref_phase_nb}_${ref_phase_nb}.mhd - #clitkCombineImage -i $reference_in -j $reference_out -m $motion_mask -o $out_result - combine_image $reference_in $reference_out $out_result $motion_mask - abort_on_error registration $? clean_up_registration - - # create 4D vf - create_mhd_4D_pattern.sh $vf_dir/vf_${ref_phase_nb}_ - create_mhd_4D_pattern.sh $vf_dir/vf_inside_${ref_phase_nb}_ - create_mhd_4D_pattern.sh $vf_dir/vf_outside_${ref_phase_nb}_ - - # create 4D coeffs - create_mhd_4D_pattern.sh $vf_dir/coeff_inside_${ref_phase_nb}_ _0 - create_mhd_4D_pattern.sh $vf_dir/coeff_outside_${ref_phase_nb}_ _0 - - # create 4D result image - create_mhd_4D_pattern.sh $output_dir/result_inside_${ref_phase_nb}_ - create_mhd_4D_pattern.sh $output_dir/result_outside_${ref_phase_nb}_ - create_mhd_4D_pattern.sh $output_dir/result_${ref_phase_nb}_ - - echo - echo "-------- Registration done ! --------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - -registration_no_motion_mask() -{ - echo - echo "----------- Registration Without Motion Masks ------------" - start=`date` - echo "start: $start" - echo - - mkdir -p $vf_dir - mkdir -p $output_dir - - # banded images may be created as separate files, - # with the specified preffix, which is interesting for debugging. - # if blank, it means that the original images (those without bands) - # will be used (see create_midP_masks-2.0.sh for details). - banded="" - - # params read from conf file - use_coeffs=1 - params="$nb_iter $nb_samples $sampling_algo $nb_hist_bins $nb_levels $bspline_spacing $metric $optimizer $interpolator" - - # register all phases to the reference - for i in $( seq 0 $((${#phase_files[@]} - 1))); do - phase_file=${phase_files[$i]} - phase_nb=${phase_nbs[$i]} - - if [ "$phase_nb" != "$ref_phase_nb" ]; then - # params - reference=$mask_dir/patient_$ref_phase_nb.mhd - target=$mask_dir/patient_$phase_nb.mhd - mask_ref=$mask_dir/patient_mask_$ref_phase_nb.mhd - mask_targ=$mask_dir/patient_mask_$phase_nb.mhd - vf=$vf_dir/vf_${ref_phase_nb}_$phase_nb.mhd - result=$output_dir/result_${ref_phase_nb}_$phase_nb.mhd - log=$log_dir/log_${ref_phase_nb}_$phase_nb.log - if [ $use_coeffs = 1 ]; then - init_coeff=$coeff_in # empty at first iteration - coeff=$vf_dir/coeff_${ref_phase_nb}_$phase_nb.mhd - fi - - # registration - if [ "$method" == "blutdir" ]; then - registration_blutdir $reference $target $mask_ref $mask_targ $vf $result $params $log $coeff - elif [ "$method" == "elastix" ]; then - registration_elastix $reference $target $mask_ref $mask_targ $vf $result $params $log - fi - - # save for later... - vf_ref=$vf - fi - done - - clitkZeroVF -i $vf_ref -o $vf_dir/vf_${ref_phase_nb}_${ref_phase_nb}.mhd - - # create 4D vf - create_mhd_4D_pattern.sh $vf_dir/vf_${ref_phase_nb}_ - - # create 4D coeffs - create_mhd_4D_pattern.sh $vf_dir/coeff_${ref_phase_nb}_ _0 - - # create 4D result image - create_mhd_4D_pattern.sh $output_dir/result_${ref_phase_nb}_ - - echo - echo "-------- Registration done ! --------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - -midp() -{ - echo - echo "----------- Mid-position ------------" - start=`date` - echo "start: $start" - echo - - mkdir -p $midp_dir - - ########### calculate the midp wrt the reference phase - phase_nb=$ref_phase_nb - echo "Calculating midp_$phase_nb.mhd..." - vf_midp=$midp_dir/vf_midp_$phase_nb.mhd - midp=$midp_dir/midp_$phase_nb.mhd - # average the vf's from reference phase to phase - #clitkAverageTemporalDimension -i $vf_dir/vf_${ref_phase_nb}_4D.mhd -o $vf_midp - average_temporal_dimension $vf_dir/vf_${ref_phase_nb}_4D.mhd $vf_midp - abort_on_error midp $? clean_up_midp - - # invert the vf (why?) - clitkInvertVF -i $vf_midp -o $vf_midp - abort_on_error midp $? clean_up_midp - - # create the midp by warping the reference phase with the reference vf - clitkWarpImage -i $ref_phase_file -o $midp --vf=$vf_midp -s 1 - abort_on_error midp $? clean_up_midp - - ref_vf_midp=$vf_midp - ref_midp=$midp - clitkImageConvert -i $ref_midp -o $ref_midp -t float - abort_on_error midp $? clean_up_midp - - ########### calculate the midp wrt the other phases - for i in $( seq 0 $((${#phase_files[@]} - 1))); do - phase_file=${phase_files[$i]} - phase_nb=${phase_nbs[$i]} - vf_midp=$midp_dir/vf_midp_$phase_nb.mhd - midp=$midp_dir/midp_$phase_nb.mhd - - if [ "$phase_nb" != "$ref_phase_nb" ]; then - echo "Calculating midp_$phase_nb.mhd..." - # calculate vf from phase to midp, using the vf from reference phase to midp (-i) - # and the vf from reference phase to phase (-j) - clitkComposeVF -i $ref_vf_midp -j $vf_dir/vf_$ref_phase_nb\_$phase_nb.mhd -o $vf_midp - abort_on_error midp $? clean_up_midp - - clitkWarpImage -i $phase_file -o $midp --vf=$vf_midp -s 1 - abort_on_error midp $? clean_up_midp - - clitkImageConvert -i $midp -o $midp -t float - abort_on_error midp $? clean_up_midp - fi - done - - # create 4D midp - create_mhd_4D_pattern.sh $midp_dir/midp_ - create_mhd_4D_pattern.sh $midp_dir/vf_midp_ - - echo "Calculating midp_avg.mhd..." - #clitkAverageTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd - average_temporal_dimension $midp_dir/midp_4D.mhd $midp_dir/midp_avg.mhd - abort_on_error midp $? clean_up_midp - - echo "Calculating midp_med.mhd..." - clitkMedianTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_med.mhd - abort_on_error midp $? clean_up_midp - - # clean-up - #rm $midp_dir/vf_* - - echo - echo "-------- Mid-position done ! --------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - -midp_in_out() -{ - echo - echo "----------- Mid-position ------------" - start=`date` - echo "start: $start" - echo - - mkdir -p $midp_dir - - ########### calculate the midp wrt the reference phase - phase_nb=$ref_phase_nb - phase_file=$ref_phase_file - echo "Calculating midp_$phase_nb.mhd..." - vf_midp_in=$midp_dir/vf_inside_midp_$phase_nb.mhd - vf_midp_out=$midp_dir/vf_outside_midp_$phase_nb.mhd - coeff_midp_in=$midp_dir/coeff_inside_midp_$phase_nb.mhd - coeff_midp_out=$midp_dir/coeff_outside_midp_$phase_nb.mhd - # average the vf's from reference phase to phase - average_temporal_dimension $vf_dir/coeff_inside_${ref_phase_nb}_4D_0.mhd $coeff_midp_in - abort_on_error midp $? clean_up_midp - average_temporal_dimension $vf_dir/coeff_outside_${ref_phase_nb}_4D_0.mhd $coeff_midp_out - abort_on_error midp $? clean_up_midp - - # invert the vf - clitkInvertVF -i $coeff_midp_in -o $vf_midp_in --type 1 --like $ref_phase_file - abort_on_error midp $? clean_up_midp - clitkInvertVF -i $coeff_midp_out -o $vf_midp_out --type 1 --like $ref_phase_file - abort_on_error midp $? clean_up_midp - - # combine in and out VF's - ref_vf_midp_in=$vf_midp_in - ref_vf_midp_out=$vf_midp_out - vf_midp=$midp_dir/vf_midp_$phase_nb.mhd - combine_image $vf_midp_in $vf_midp_out $vf_midp $mask_dir/${mask_type}_$phase_nb.mhd - clitkZeroVF -i $vf_midp -o vf_zero.mhd - combine_image $vf_midp vf_zero.mhd $vf_midp $mask_dir/patient_mask_$phase_nb.mhd - - # create the midp by warping the reference phase with the reference vf - midp=$midp_dir/midp_$phase_nb.mhd - clitkWarpImage -i $phase_file -o $midp --vf=$vf_midp -s 1 - abort_on_error midp $? clean_up_midp - - clitkImageConvert -i $midp -o $midp -t float - abort_on_error midp $? clean_up_midp - - ########### calculate the midp wrt the other phases - for i in $( seq 0 $((${#phase_files[@]} - 1))); do - phase_file=${phase_files[$i]} - phase_nb=${phase_nbs[$i]} - vf_midp_in=$midp_dir/vf_inside_midp_$phase_nb.mhd - vf_midp_out=$midp_dir/vf_outside_midp_$phase_nb.mhd - - if [ "$phase_nb" != "$ref_phase_nb" ]; then - echo "Calculating midp_$phase_nb.mhd..." - # calculate vf from phase to midp, using the vf from reference phase to midp (-i) - # and the vf from reference phase to phase (-j) - clitkComposeVF -i $ref_vf_midp_in -j $vf_dir/vf_inside_$ref_phase_nb\_$phase_nb.mhd -o $vf_midp_in - abort_on_error midp $? clean_up_midp - clitkComposeVF -i $ref_vf_midp_out -j $vf_dir/vf_outside_$ref_phase_nb\_$phase_nb.mhd -o $vf_midp_out - abort_on_error midp $? clean_up_midp - - # combine in and out VF's - vf_midp=$midp_dir/vf_midp_$phase_nb.mhd - combine_image $vf_midp_in $vf_midp_out $vf_midp $mask_dir/${mask_type}_$phase_nb.mhd - clitkZeroVF -i $vf_midp -o vf_zero.mhd - combine_image $vf_midp vf_zero.mhd $vf_midp $mask_dir/patient_mask_$phase_nb.mhd - - midp=$midp_dir/midp_$phase_nb.mhd - clitkWarpImage -i $phase_file -o $midp --vf=$vf_midp -s 1 - abort_on_error midp $? clean_up_midp - - clitkImageConvert -i $midp -o $midp -t float - abort_on_error midp $? clean_up_midp - fi - done - - rm vf_zero.* - - # create 4D midp - create_mhd_4D_pattern.sh $midp_dir/midp_ - create_mhd_4D_pattern.sh $midp_dir/vf_midp_ - create_mhd_4D_pattern.sh $midp_dir/vf_inside_midp_ - create_mhd_4D_pattern.sh $midp_dir/vf_outside_midp_ - - echo "Calculating midp_avg.mhd..." - average_temporal_dimension $midp_dir/midp_4D.mhd $midp_dir/midp_avg.mhd - abort_on_error midp $? clean_up_midp - - echo "Calculating midp_med.mhd..." - clitkMedianTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_med.mhd - abort_on_error midp $? clean_up_midp - - echo - echo "-------- Mid-position done ! --------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - - -######################### main - -if [ $# != 3 ]; then - echo "Usage: create_midP-2.0.sh CT_4D REF_PHASE CONF_FILE" - exit -1 -fi - -echo -echo "--------------- START ---------------" -begining=`date --rfc-3339=seconds` -# echo "beginning: $begining" -echo - -# variable declarations -mhd4d=$1 -ref_phase=$2 -conf=$3 -source $conf - -cd `dirname $mhd4d` - -mkdir -p $log_dir -mask_dir="MASK-${mask_interpolation_spacing}mm-$mask_interpolation_algorithm" - -extract_4d_phases_ref $mhd4d $ref_phase - -if [ "$step" == "mask" -o "$step" == "all" ]; then - motion_mask $mhd4d $mask_type $mask_interpolation_spacing $mask_interpolation_algorithm -fi - -if [ "$step" == "registration" -o "$step" == "all" ]; then - if [ "$mask_type" == "mm" ]; then - registration - elif [ "$mask_type" == "lungs" ]; then - registration - elif [ "$mask_type" == "patient" ]; then - registration_no_motion_mask - midp_combined_vf=1 - fi -fi - -if [ "$step" == "midp" -o "$step" == "all" ]; then - if [ $midp_combined_vf = 0 ]; then - midp_in_out - else - midp - fi -fi - -echo -echo "---------------- END ----------------" -terminating=`date --rfc-3339=seconds` -echo "beginning: $begining" -echo "terminating: $terminating" -echo diff --git a/scripts/create_midP.sh b/scripts/create_midP.sh deleted file mode 100755 index 7591efb..0000000 --- a/scripts/create_midP.sh +++ /dev/null @@ -1,535 +0,0 @@ -#!/bin/sh -x - -################################################################# -# create_MidP arguments : CT_4D.mhd ref_phase spacing # -################################################################# -source `dirname $0`/midp_common.sh - -extract_patient() -{ - echo "$image_name -> Extracting patient..." - $CLITK/clitkExtractPatient -i $image_name -o MASK/patient_$image_name --noAutoCrop - $CLITK/clitkBinarizeImage -i MASK/patient_$image_name -o MASK/patient_$image_name -l 1 -u 1 --mode=BG - $CLITK/clitkSetBackground -i $image_name -o MASK/patient_only_$image_name --mask MASK/patient_$image_name --outsideValue -1000 -} - -extract_bones() -{ - echo "$image_name -> Extracting bones..." - $CLITK/clitkImageConvert -i $image_name -o MASK/float_$image_name -t float - $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 - $CLITK/clitkMorphoMath -i MASK/bones_$image_name -o MASK/bones_$image_name --type 2 --radius 4,4,2 -} - -resample() -{ - echo "$image_name -> Resampling..." - $CLITK/clitkResampleImage -i MASK/patient_$image_name -o MASK/patient_$image_name --spacing $spacing - $CLITK/clitkResampleImage -i MASK/patient_only_$image_name -o MASK/patient_only_$image_name --spacing $spacing - #$CLITK/clitkResampleImage -i MASK/bones_$image_name -o MASK/bones_$image_name --like MASK/patient_only_$image_name -} - -compute_motion_mask() -{ -# $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; -$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; -} - -set_background() -{ - echo "$image_name -> Setting Background..." - $CLITK/clitkSetBackground -i MASK/patient_only_$image_name -o MASK/inside_$image_name --mask MASK/mm_$image_name --outsideValue -1200 - $CLITK/clitkSetBackground -i MASK/patient_only_$image_name -o MASK/outside_$image_name --mask MASK/mm_$image_name --outsideValue -1200 --fg -} - -create_registration_masks() -{ - echo "$image_name -> Creating registration masks..." - $CLITK/clitkMorphoMath -i MASK/mm_$image_name -o MASK/regmask_in_$image_name --type 1 --radius 8 - $CLITK/clitkExtractPatient -i MASK/outside_$image_name -o MASK/regmask_out_$image_name --noAutoCrop - $CLITK/clitkMorphoMath -i MASK/regmask_out_$image_name -o MASK/regmask_out_$image_name --type 1 --radius 8 -} - -remove_files() -{ - echo "Removing temporary files..." - image_name_base=`echo $image_name | sed 's/mhd//'` - case $1 in - 1) - rm MASK/float_$image_name_base*;; - 2) - rm MASK/bones_$image_name_base*;; - 3) - rm MASK/patient_only_$image_name_base*;; - 4) - #rm MASK/patient_$image_name_base* - #rm MASK/mm_$image_name_base* - rm -f $vf_dir/vf_tmp_in_${ref}_${phase}.* - rm -f $vf_dir/vf_tmp_out_${ref}_${phase}.* - #rm MASK/regmask_in_$image_name_base* - #rm MASK/regmask_out_$image_name_base* - ;; - 5) -# rm -f coeff_* - #rm $vf_dir/vf_in_* - #rm $vf_dir/vf_out_* -# rm MASK/regmask_* -# rm MASK/mm_* - ;; - 6) - ;; - #rm -f $vf_dir/_4D.* - #rm -f $vf_dir/vf_MIDP_${ref}.* - #rm -f $vf_dir/vf_${ref}_MIDP.*;; - 7) - ;; #rm $vf_dir/vf_MIDP_${phase}.*;; - 8) - rm $vf_dir/*.txt - rm $vf_dir/*.log - esac -} - -mm_preprocessing() -{ - extract_patient - #extract_bones - remove_files 1 - resample -} - -mm_postprocessing() -{ - remove_files 2 - set_background - remove_files 3 - create_registration_masks -} - -# mm_workflow() -# { -# extract_patient -# extract_bones -# remove_files 1 -# resample -# echo "$image_name -> Computing motion mask..." -# compute_motion_mask >> LOG/motion_mask_$image_name.log -# remove_files 2 -# set_background -# remove_files 3 -# create_registration_masks -# } - -motion_mask() -{ - echo - echo "------------ Motion mask ------------" - start=`date` - echo "start: $start" - echo - mkdir -p "MASK" - rm -f "LOG/motion_mask*.log" - regmask_in_list="" - regmask_out_list="" - reg_in_list="" - reg_out_list="" - - # multi-threaded pre-processing for motion mask calcs - for phase in $phase_list - do - image_name=`echo $phase | sed 's/raw/mhd/'` - check_threads $MAX_THREADS - #mm_preprocessing & - done - - # single-threaded motion mask calc - check_threads 1 - for phase in $phase_list - do - image_name=`echo $phase | sed 's/raw/mhd/'` - - echo "$image_name -> Computing motion mask..." - #compute_motion_mask >> LOG/motion_mask_$image_name.log - done - - # multi-threaded post-processing of motion mask calcs - for phase in $phase_list - do - image_name=`echo $phase | sed 's/raw/mhd/'` - check_threads $MAX_THREADS - #mm_postprocessing & - regmask_in_list="$regmask_in_list regmask_in_$image_name" - regmask_out_list="$regmask_out_list regmask_out_$image_name" - reg_in_list="$reg_in_list inside_$image_name" - reg_out_list="$reg_out_list outside_$image_name" - done - - wait - echo - echo "-------- Motion mask done ! ---------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - -compute_BLUTDIR() -{ - ########## register in ########## - for reg_in in $reg_in_list - do - if [ ! -z `echo $reg_in | grep "$phase"` ] - then - target_in=$reg_in - fi - done - echo "Computing BLUTDIR $reference_in -> $target_in ..." - #$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 - $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 - coeff_in_ini="--initCoeff=$coeff_dir/coeff_in_${ref}_${phase}.mhd" - ########## register out ########## - for reg_out in $reg_out_list - do - if [ ! -z `echo $reg_out | grep "$phase"` ] - then - target_out=$reg_out - fi - done - echo "Computing BLUTDIR $reference_out -> $target_out ..." - #$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 - $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 - coeff_out_ini="--initCoeff=$coeff_dir/coeff_out_${ref}_${phase}.mhd" - ################################## - $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 - $CLITK/clitkZeroVF -i $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_${ref}_${ref}.mhd - $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 - remove_files 4 -} - -compute_DEMONSDIR() -{ - ########## register in ########## - for reg_in in $reg_in_list - do - if [ ! -z `echo $reg_in | grep "_$phase"` ] - then - target_in=$reg_in - fi - done - echo "Computing DEMONSDIR $reference_in -> $target_in ..." - $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 - vf_in_ini="--init=$vf_dir/vf_in_${ref}_${phase}.mhd" - ########## register out ########## - for reg_out in $reg_out_list - do - if [ ! -z `echo $reg_out | grep "_$phase"` ] - then - target_out=$reg_out - fi - done - echo "Computing DEMONSDIR $reference_out -> $target_out ..." - $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 - vf_out_ini="--init=$vf_dir/vf_out_${ref}_${phase}.mhd" - ################################## - $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 - $CLITK/clitkZeroVF -i $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_${ref}_${ref}.mhd - $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 - remove_files 4 -} - -compute_ELASTIX() -{ - ########## register in ########## - for reg_in in $reg_in_list - do - if [ ! -z `echo $reg_in | grep "_$phase"` ] - then - target_in=$reg_in - fi - done - echo "Computing ELASTIX $reference_in -> $target_in ..." - exec_dir=`which elastix` - exec_dir=`dirname $exec_dir` - cat $exec_dir/params_BSpline.txt | sed -e "s++500+" \ - -e "s+++" \ - -e "s++25+" \ - -e "s++3+" \ - -e "s++2000+" \ - -e "s++Random+" \ - -e "s++32+" > params_BSpline.txt - elastix -f "MASK/$reference_in" -m "MASK/$target_in" -fMask "MASK/$reference_mask_in" -out $vf_dir -p params_BSpline.txt > /dev/null - transformix -tp $vf_dir/TransformParameters.0.txt -out $vf_dir -def all > /dev/null - mv $vf_dir/deformationField.mhd $vf_dir/vf_in_${ref}_${phase}.mhd - mv $vf_dir/deformationField.raw $vf_dir/vf_in_${ref}_${phase}.raw - sed -i "s:deformationField:vf_in_${ref}_${phase}:" $vf_dir/vf_in_${ref}_${phase}.mhd - mv $vf_dir/result.0.mhd MASK/reg_$target_in - targetraw=`echo reg_$target_in | sed 's:mhd:raw:'` - sed -i "s:result.0.mhd:$targetraw" MASK/reg_$target_in - mv $vf_dir/result.0.raw MASK/$targetraw - remove_files 8 - - ########## register out ########## - for reg_out in $reg_out_list - do - if [ ! -z `echo $reg_out | grep "_$phase"` ] - then - target_out=$reg_out - fi - done - echo "Computing ELASTIX $reference_out -> $target_out ..." - elastix -f "MASK/$reference_out" -m "MASK/$target_out" -fMask "MASK/$reference_mask_out" -out $vf_dir -p params_BSpline.txt > /dev/null - transformix -tp $vf_dir/TransformParameters.0.txt -out $vf_dir -def all > /dev/null - mv $vf_dir/deformationField.mhd $vf_dir/vf_out_${ref}_${phase}.mhd - mv $vf_dir/deformationField.raw $vf_dir/vf_out_${ref}_${phase}.raw - sed -i "s:deformationField:vf_out_${ref}_${phase}:" $vf_dir/vf_out_${ref}_${phase}.mhd - mv $vf_dir/result.0.mhd MASK/reg_$target_out - targetraw=`echo reg_$target_out | sed 's:mhd:raw:'` - sed -i "s:result.0.mhd:$targetraw" MASK/reg_$target_out - mv $vf_dir/result.0.raw MASK/$targetraw - remove_files 8 - - ################################## - $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 - $CLITK/clitkZeroVF -i $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_${ref}_${ref}.mhd - $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 - remove_files 4 -} - -registration() -{ - echo - echo "----------- Registration ------------" - start=`date` - echo "start: $start" - echo - - rm -f "LOG/registration*.log" - - # wait any unfinished threads - check_threads 1 - - for reg_in in $reg_in_list - do - if [ ! -z `echo $reg_in | grep "$ref"` ] - then - reference_in=$reg_in - fi - done - for reg_out in $reg_out_list - do - if [ ! -z `echo $reg_out | grep "$ref"` ] - then - reference_out=$reg_out - fi - done - for regmask_in in $regmask_in_list - do - if [ ! -z `echo $regmask_in | grep "$ref"` ] - then - reference_mask_in=$regmask_in - fi - done - for regmask_out in $regmask_out_list - do - if [ ! -z `echo $regmask_out | grep "$ref"` ] - then - reference_mask_out=$regmask_out - fi - done - - registration_parameters_BLUTDIR="--spacing=32,32,32 --interp=2 --metric=11 --bins=25 --samples=1 --levels=3 --verbose " #--coeffEveryN 5" - registration_parameters_DEMONSDIR="--demons=3 --levels=1" - registration_parameters_ELASTIX="--demons=3 --levels=1" - - coeff_in_ini="" - coeff_out_ini="" - vf_in_ini="" - vf_out_ini="" - - for phase in $list_phases - do - for img in $phase_list - do - if [ ! -z `echo $img | grep "$phase" | grep -v "[0-9]$phase"` ] - then - image_name=`echo $img | sed 's/raw/mhd/'` - fi - done - if [ $method = 1 ] - then - compute_BLUTDIR - elif [ $method = 2 ] - then - compute_DEMONSDIR - elif [ $method = 3 ] - then - compute_ELASTIX - fi - done - remove_files 5 - - echo - echo "-------- Registration done ! --------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - -calculate_vf_MIDP_REF() -{ - echo "Calculating vf_REF_MIDP.mhd..." - remove_files 6 - create_mhd_4D.sh $vf_dir #"vf_4D.mhd" - $CLITK/clitkAverageTemporalDimension -i $vf_dir/_4D.mhd -o $vf_dir/vf_${ref}_MIDP.mhd - $CLITK/clitkInvertVF -i $vf_dir/vf_${ref}_MIDP.mhd -o $vf_dir/vf_MIDP_${ref}.mhd -} - -calculate_CT_MIDP_REF() -{ - reference=`ls *.mhd | grep $ref #| grep -v "[0-9]$ref.mhd"` - echo "Calculating CT_MIDP_REF.mhd '$reference'..." - $CLITK/clitkWarpImage -i $reference -o CT_MIDP_REF.mhd --vf=$vf_dir/vf_MIDP_${ref}.mhd -s 1 -} - -calculate_CT_MIDP_PHASE() -{ - echo "Calculating CT_MIDP_${phase}.mhd..." - $CLITK/clitkComposeVF -i $vf_dir/vf_MIDP_${ref}.mhd -j $vf_dir/vf_${ref}_${phase}.mhd -o $vf_dir/vf_MIDP_${phase}.mhd - phase_img=`ls *.mhd | grep "${phase}" # | grep -v "[0-9]$ref.mhd"` - $CLITK/clitkWarpImage -i $phase_img -o MIDP/CT_MIDP_${phase}.mhd --vf=$vf_dir/vf_MIDP_${phase}.mhd -s 1 - $CLITK/clitkImageConvert -i MIDP/CT_MIDP_${phase}.mhd -o MIDP/CT_MIDP_${phase}.mhd -t float - remove_files 7 -} - -prepare_MIDP_images() -{ - echo "Preparing MIDP images..." - cp CT_MIDP_REF.mhd MIDP/CT_MIDP_${ref}.mhd - cp CT_MIDP_REF.raw MIDP/CT_MIDP_${ref}.raw - cat MIDP/CT_MIDP_${ref}.mhd | sed "s/ElementDataFile = .*/ElementDataFile = CT\_MIDP\_${ref}\.raw/" > MIDP/CT_MIDP_${ref}_tmp.mhd - rm MIDP/CT_MIDP_${ref}.mhd - mv MIDP/CT_MIDP_${ref}_tmp.mhd MIDP/CT_MIDP_${ref}.mhd - $CLITK/clitkImageConvert -i MIDP/CT_MIDP_${ref}.mhd -o MIDP/CT_MIDP_${ref}.mhd -t float - create_mhd_4D.sh MIDP #"CT_MIDP_4D.mhd" -} - -calculate_CT_MIDP_MOY() -{ - echo "Calculating CT_MIDP_MOY.mhd..." - $CLITK/clitkAverageTemporalDimension -i MIDP/_4D.mhd -o CT_MIDP_MOY.mhd -} - -calculate_CT_MIDP_MED() -{ - echo "Calculating CT_MIDP_MED.mhd..." - $CLITK/clitkMedianTemporalDimension -i MIDP/_4D.mhd -o CT_MIDP_MED.mhd -} - -calculate_CT_MIDP_MIP() -{ - echo "Calculating CT_MIDP_MIP.mhd..." - $CLITK/clitkMIP -i MIDP/_4D.mhd -o CT_MIDP_MIP.mhd -d 3 -} - -mid_position() -{ - echo - echo "----------- Mid-position ------------" - start=`date` - echo "start: $start" - echo - - mkdir -p "MIDP" - - calculate_vf_MIDP_REF - calculate_CT_MIDP_REF - for phase in $list_phases - do - check_threads $MAX_THREADS - calculate_CT_MIDP_PHASE & - done - wait - prepare_MIDP_images - calculate_CT_MIDP_MED & - calculate_CT_MIDP_MIP & - calculate_CT_MIDP_MOY & - wait - - echo - echo "-------- Mid-position done ! --------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - -################# -# main # -################# - -if [ $# -lt 1 ] -then - echo "Usage: create_midP.sh CT_4D.mhd ref_phase computation_spacing(mm) method(1:BSPLINE, 2:DEMONS)" - exit 1 -fi -CLITK=~/creatis/clitk3/build/bin -CT_4D_path=$1 -CT_4D=`basename $CT_4D_path` -work_dir=`dirname $CT_4D_path` -cd $work_dir -ref=$2 -spacing=$3 -method=$4 - -vf_dir="VF" -coeff_dir="$vf_dir/coeff" - -mkdir -p $vf_dir -mkdir -p $coeff_dir - -echo -echo "--------------- START ---------------" -begining=`date` -echo "start: $begining" -echo - -mkdir -p "LOG" -phase_list=`grep ".raw" $CT_4D` -echo "phases -> " $phase_list -nb_phases=`grep ".raw" $CT_4D | wc -l` -gt_ref="" -lt_ref="" - -phase_files=( `cat $CT_4D | grep ".raw" | sed 's:.raw:.mhd:'` ) -echo "Phase files are ${phase_files[@]}" - -phase_nbs=( `echo ${phase_files[@]} | grep -o '[[:alpha:][:punct:]][0-9]\{1,2\}[[:punct:]]' | grep -o '[0-9]\{1,2\}'` ) -#phase_nbs=( `echo ${phase_files[@]} | grep -o '[0-9]\{1,2\}'` ) -echo "Phase numbers are ${phase_nbs[@]}" - - -for ph in $phase_list -do - #ph_nb=`echo $ph | grep -o "[0-9][0-9]*\.raw" | sed -e 's/\.raw//'` - ph_nb=`echo $ph | grep -o "[0-9][0-9]"` - if [ $ph_nb -gt $ref ] - then - gt_ref="$gt_ref $ph_nb" - elif [ $ph_nb -lt $ref ] - then - lt_ref="$lt_ref $ph_nb" - fi -done -list_phases="$gt_ref $lt_ref" -echo list_phases $list_phases - -motion_mask -registration -mid_position - -echo -echo "---------------- END ----------------" -terminating=`date` -echo "start: $begining" -echo "end: $terminating" -echo diff --git a/scripts/create_midP_masks-2.0.sh b/scripts/create_midP_masks-2.0.sh deleted file mode 100755 index 10cd52a..0000000 --- a/scripts/create_midP_masks-2.0.sh +++ /dev/null @@ -1,369 +0,0 @@ -#! /bin/bash -x - -############################################################################### -# -# FILE: create_midP-2.0.sh -# AUTHOR: Rômulo Pinho 05/08/2011 -# -# Version 2.0 of the create_midP_masks.sh script. The most relevant changes are: -# * creation of bands around input and output image regions to try to improve -# the registration along lung boundaries (naturally, it depends on the quality -# of motion mask generation). -# * some steps are now in different modules, to facilitate re-use (see "includes" section). -# * minor modifications on output file names. -# * attempt to simplify the code a bit. -# -############################################################################### - -source `dirname $0`/midp_common.sh - -extract_patient() -{ - echo "$phase_file -> Extracting patient..." - clitkExtractPatient -i $phase_file -o $mask_dir_tmp/patient_mask_$phase_nb.mhd --noAutoCrop -a $afdb_file $ExtractPatientExtra -# abort_on_error clitkExtractPatient $? - - clitkSetBackground -i $phase_file -o $mask_dir_tmp/patient_$phase_nb.mhd --mask $mask_dir_tmp/patient_mask_$phase_nb.mhd --outsideValue -1000 -# abort_on_error clitkSetBackground $? -} - -extract_bones() -{ - if [ x = x$ExtractBonesLower1 ]; then - ExtractBonesLower1=120 - fi - if [ x = x$ExtractBonesLower2 ]; then - ExtractBonesLower2=80 - fi - echo "$phase_file -> Extracting bones..." - clitkImageConvert -i $phase_file -o $mask_dir_tmp/float_$phase_nb.mhd -t float - clitkExtractBones -i $mask_dir_tmp/float_$phase_nb.mhd -o $mask_dir_tmp/bones_$phase_nb.mhd -a $afdb_file --lower1 $ExtractBonesLower1 --upper1 2000 --lower2 $ExtractBonesLower2 --upper2 2000 --smooth --time 0.0625 --noAutoCrop -} - -extract_lungs() -{ - echo "$phase_file -> Extracting lungs..." - clitkExtractLung -i $phase_file -o $mask_dir_tmp/lungs_$phase_nb.mhd -a $afdb_file --noAutoCrop --doNotSeparateLungs --type 1 -} - - - -resample() -{ - echo "$phase_file -> Resampling..." - clitkResampleImage -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/patient_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo - clitkResampleImage -i $mask_dir_tmp/patient_mask_$phase_nb.mhd -o $mask_dir_tmp/patient_mask_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo - if [ "$mask_type" != "patient" ]; then - clitkResampleImage -i $mask_dir_tmp/lungs_$phase_nb.mhd -o $mask_dir_tmp/lungs_$phase_nb.mhd --like $mask_dir_tmp/patient_$phase_nb.mhd - fi - if [ "$mask_type" == "mm" ]; then - clitkResampleImage -i $mask_dir_tmp/bones_$phase_nb.mhd -o $mask_dir_tmp/bones_$phase_nb.mhd --like $mask_dir_tmp/patient_$phase_nb.mhd - fi -} - -compute_motion_mask() -{ - if [ x = x$MotionMaskOffsetDetect ]; then - MotionMaskOffsetDetect="0,-5,0" - fi - if [ x = x$FillingLevel ]; then - FillingLevel=94 - fi - - clitkMotionMask -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/mm_$phase_nb.mhd --featureLungs $mask_dir_tmp/lungs_$phase_nb.mhd --upperThresholdLungs -400 --featureBones $mask_dir_tmp/bones_$phase_nb.mhd --fillingLevel $FillingLevel --offsetDetect $MotionMaskOffsetDetect --pad --writeFeature $mask_dir_tmp/feature_$phase_nb.mhd $MotionMaskExtra - #--monitor=$mask_dir_tmp/monitor_$phase_nb.mhd -} - -create_banded_mask() -{ - input=$1 - input_mask=$2 - output=$3 - output_mask=$4 - radius=$5 - - input_dir=`dirname $input` - input_base=`basename $input` - - # first band - clitkMorphoMath -i $input_mask -o $input_dir/extra1_$input_base --type 1 --radius $radius - clitkImageArithm -i $input_dir/extra1_$input_base -j $input_mask -o $input_dir/band1_$input_base -t 7 - clitkBinarizeImage -i $input_dir/band1_$input_base -o $input_dir/band1_$input_base -l 1 -u 1 --fg 100 --mode both - clitkImageConvert -i $input_dir/band1_$input_base -o $input_dir/short_band1_$input_base -t short - - # second band - clitkMorphoMath -i $input_dir/extra1_$input_base -o $input_dir/extra2_$input_base --type 1 --radius $radius - clitkImageArithm -i $input_dir/extra2_$input_base -j $input_dir/extra1_$input_base -o $input_dir/band2_$input_base -t 7 - clitkBinarizeImage -i $input_dir/band2_$input_base -o $input_dir/band2_$input_base -l 1 -u 1 --fg 200 --mode both - clitkImageConvert -i $input_dir/band2_$input_base -o $input_dir/short_band2_$input_base -t short - - # combine bands with masks - clitkImageArithm -i $input_mask -j $input_dir/band1_$input_base -o $output_mask -t 0 - clitkImageArithm -i $output_mask -j $input_dir/band2_$input_base -o $output_mask -t 0 - # combine bands with image - combine_image $input_dir/short_band1_$input_base $input $output $input_dir/band1_$input_base - combine_image $input_dir/short_band2_$input_base $output $output $input_dir/band2_$input_base - - # clean-up - rm `echo $input_dir/extra?_$input_base | sed 's:.mhd:.*:g'` - rm `echo $input_dir/band?_$input_base | sed 's:.mhd:.*:g'` - rm `echo $input_dir/short_band?_$input_base | sed 's:.mhd:.*:g'` -} - -create_registration_masks() -{ - # extract inside and outside regions from the patient image, - # using the motion mask computed previously - echo "$phase_file -> Setting Background..." - clitkSetBackground -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/inside_$phase_nb.mhd --mask $mask_dir_tmp/${mask_type}_$phase_nb.mhd --outsideValue -1200 - clitkSetBackground -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/outside_$phase_nb.mhd --mask $mask_dir_tmp/${mask_type}_$phase_nb.mhd --outsideValue -1200 --fg - - # the registration masks for inside (and outside) region correspond - # to the motion mask (and its complement) plus extra grey value bands, - # obtained with morphological dilations. - # - echo "$phase_file -> Creating registration masks..." - # inside - clitkMorphoMath -i $mask_dir_tmp/${mask_type}_$phase_nb.mhd -o $mask_dir_tmp/mask_inside_$phase_nb.mhd --type 1 --radius 8 - create_banded_mask $mask_dir_tmp/inside_$phase_nb.mhd $mask_dir_tmp/${mask_type}_$phase_nb.mhd $mask_dir_tmp/banded_inside_$phase_nb.mhd $mask_dir_tmp/banded_mask_inside_$phase_nb.mhd 4 - # outside - clitkBinarizeImage -i $mask_dir_tmp/outside_$phase_nb.mhd -o $mask_dir_tmp/${mask_type}_outside_$phase_nb.mhd -l -999 -u 4000 --mode both - #clitkExtractPatient -i $mask_dir_tmp/outside_$phase_nb.mhd -o $mask_dir_tmp/${mask_type}_outside_$phase_nb.mhd --noAutoCrop - clitkMorphoMath -i $mask_dir_tmp/${mask_type}_outside_$phase_nb.mhd -o $mask_dir_tmp/mask_outside_$phase_nb.mhd --type 1 --radius 8 - create_banded_mask $mask_dir_tmp/outside_$phase_nb.mhd $mask_dir_tmp/${mask_type}_outside_$phase_nb.mhd $mask_dir_tmp/banded_outside_$phase_nb.mhd $mask_dir_tmp/banded_mask_outside_$phase_nb.mhd 4 -} - -mm_preprocessing() -{ - extract_patient - - if [ "$mask_type" != "patient" ]; then - extract_lungs - fi - - if [ "$mask_type" == "mm" ]; then - extract_bones - fi - - # remove_tmp_masks 1 - if [ $resample_spacing -ne 0 ] ; then - resample - fi -} - -mm_postprocessing() -{ - # remove_tmp_masks 2 - # remove_tmp_masks 3 - - create_registration_masks -# if [ "$mask_type" == "mm" ]; then -# create_registration_motion_masks -# elif [ "$mask_type" == "lungs" ]; then -# create_registration_lung_masks -# fi -} - -wait_mm_creation() -{ - # the motion masks are first created in a tmp directory. this directory is - # later going to be renamed to the final motion mask directory. concurrent - # executions trying to create the same set of masks will be blocked until - # the first execution finishes. naturally, these other executions will not - # recreate the masks. so first we try to create the tmp directory. - # if the creation fails, it means that another execution is - # already creating the masks, so this execution will be blocked. the - # execution is unblocked only when the creation of masks is finished and - # the mask directory is renamed. - # - do_mm=1 - if [ -e $mask_dir ]; then - # check that the final mask dir exists and that it contains all files it needs. - # the check assumes that the inside and outside masks are the key files to exist. - do_mm=0 - nb_phases=${#phase_nbs[@]} - if [ "$mask_type" == "patient" ]; then - nb_masks=`ls $mask_dir/lungs_*.mhd | wc -l` - if [ $nb_masks != $nb_phases ]; then - # if the mask dir is invalid, remove it and recreate all masks, just in case. - rm -fr $mask_dir 2> /dev/null - do_mm=1 - fi - else - nb_mm_masks=`ls $mask_dir/${mask_type}_outside*.mhd | wc -l` - nb_in_masks=`ls $mask_dir/mask_in*.mhd | wc -l` - nb_out_masks=`ls $mask_dir/mask_out*.mhd | wc -l` - if [ $nb_mm_masks != $nb_phases -o $nb_in_masks != $nb_phases -o $nb_out_masks != $nb_phases ]; then - # if the mask dir is invalid, remove it and recreate all masks, just in case. - rm -fr $mask_dir 2> /dev/null - do_mm=1 - fi - fi - fi - - if [ $do_mm = 1 ]; then - if ! mkdir $mask_dir_tmp 2> /dev/null; then - if [ ! -e $mask_dir_tmp ]; then - # if the temp dir couldn't be created, but it doesn't exist, abort - abort_on_error wait_mm_creation $? clean_up_masks - else - # assumes another process is creating the maks in the temp dir. - # now we need to wait until the masks are complete or until the - # time limit is reached. - interval=10 - sleeping=0 - max_wait=3600 # one hour - nb_files0=`ls $mask_dir_tmp/* | wc -l` - while [ ! -e $mask_dir -a $sleeping -le $max_wait ]; do - echo "waiting creation of motion masks..." - sleep $interval - sleeping=$(( $sleeping + $interval )) - nb_files1=`ls $mask_dir_tmp/* | wc -l` - if [ $nb_files1 != $nb_files0 ]; then - nb_files0=$nb_files1 - sleeping=0 - fi - done - - if [ $sleeping -gt $max_wait ]; then - abort_on_error wait_mm_creation -1 clean_up_masks - else - echo "finished waiting" - do_mm=0 - fi - fi - fi - fi -} - -motion_mask() -{ - #set cmd line variables - local mhd4d=`basename $1` - mask_type=$2 - if [ $# -eq 4 ] ; then - resample_spacing=$3 - resample_algo=$4 - else - resample_spacing=0 - resample_algo=0 - fi - - dir=`dirname $1` - cd $dir - - # import variables specific to each patient - if test -e ./variables; then - source ./variables - fi - - #set other global variables - if [ $resample_spacing -ne 0 ] ; then - mask_dir="MASK-${resample_spacing}mm-$resample_algo" - else - mask_dir="MASK" - fi - mask_dir_tmp="tmp.$mask_dir" - extract_4d_phases $mhd4d - - echo - echo "------------ Motion mask from create_midP_masks.sh ------------" - start=`date` - echo "start: $start" - echo - - wait_mm_creation - -# do_mm=1 -# mask_dir_tmp=$mask_dir - if [ $do_mm == 1 ]; then - mask_log_dir=$mask_dir_tmp/LOG - mkdir -p $mask_log_dir - - # multi-threaded pre-processing for motion mask calcs - pids=( ) - for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do - phase_nb=${phase_nbs[$i]} - phase_file=${phase_files[$i]} - afdb_file=`echo $phase_file | sed 's/mhd/afdb/'` - - check_threads $MAX_THREADS - mm_preprocessing & - pids=( "${pids[@]}" "$!" ) - done - - wait_pids ${pids[@]} - for ret in $ret_codes; do - abort_on_error mm_preprocessing $ret clean_up_masks - done - - if [ "$mask_type" == "mm" ]; then - # single-threaded motion mask calc - for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do - phase_nb=${phase_nbs[$i]} - phase_file=${phase_files[$i]} - - check_threads 1 - echo "$phase_file -> Computing motion mask..." - compute_motion_mask > $mask_log_dir/motion_mask_$phase_file.log - abort_on_error compute_motion_mask $? clean_up_masks - done - fi - - # multi-threaded post-processing of motion mask calcs - if [ "$mask_type" != "patient" ]; then - pids=( ) - for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do - phase_nb=${phase_nbs[$i]} - phase_file=${phase_files[$i]} - - check_threads $MAX_THREADS - mm_postprocessing & - pids=( "${pids[@]}" "$!" ) - done - - wait_pids ${pids[@]} - for ret in $ret_codes; do - abort_on_error mm_postprocessing $ret clean_up_masks - done - fi - - # rename tmp mask directory after mask creation - check_threads 1 - mv -f $mask_dir_tmp $mask_dir - fi - - echo - echo "-------- Motion mask done ! ---------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - - -################# -# main # -################# - -if [ $# -ne 4 -a $# -ne 2 -a $# -ne 1 ]; then - echo "Usage: $0 CT_4D TYPE [RESAMPLE_SPACING RESAMPLE_ALGORITHM]" - echo " TYPE: \"mm\" (traditional motion masks); \"lungs\" (lung masks); \"patient\" (patient mask only)" - exit -1 -fi - -# -# variables exported in this scope -# -# mask_dir: directory where all masks are kept -# - -if [ $1 != "using-as-lib" ]; then - if [ $# -eq 4 ] ; then - motion_mask $1 $2 $3 $4 - elif [ $# -eq 2 ] ; then - motion_mask $1 $2 - else - motion_mask $1 all - fi -fi diff --git a/scripts/create_midP_masks.sh b/scripts/create_midP_masks.sh deleted file mode 100755 index 845237d..0000000 --- a/scripts/create_midP_masks.sh +++ /dev/null @@ -1,253 +0,0 @@ -#! /bin/bash -x - -source `dirname $0`/midp_common.sh - -extract_patient() -{ - echo "$image_name -> Extracting patient..." - clitkExtractPatient -i $image_name -o $mask_dir_tmp/patient_$image_name --noAutoCrop -a $afdb_name $ExtractPatientExtra - clitkBinarizeImage -i $mask_dir_tmp/patient_$image_name -o $mask_dir_tmp/patient_$image_name -l 1 -u 1 --mode=BG - clitkSetBackground -i $image_name -o $mask_dir_tmp/patient_only_$image_name --mask $mask_dir_tmp/patient_$image_name --outsideValue -1000 -} - -extract_bones() -{ - if [ x = x$ExtractBonesLower1 ]; then - ExtractBonesLower1=120 - fi - if [ x = x$ExtractBonesLower2 ]; then - ExtractBonesLower2=80 - fi - echo "$image_name -> Extracting bones..." - clitkImageConvert -i $image_name -o $mask_dir_tmp/float_$image_name -t float - #clitkExtractBones -i $mask_dir_tmp/float_$image_name -o $mask_dir_tmp/bones_$image_name --lower1 120 --upper1 2000 --lower2 70 --upper2 2000 --smooth --time 0.0625 --noAutoCrop - clitkExtractBones -i $mask_dir_tmp/float_$image_name -o $mask_dir_tmp/bones_$image_name -a $afdb_name --lower1 $ExtractBonesLower1 --upper1 2000 --lower2 $ExtractBonesLower2 --upper2 2000 --smooth --time 0.0625 --noAutoCrop - #clitkMorphoMath -i $mask_dir_tmp/bones_$image_name -o $mask_dir_tmp/bones_$image_name --type 2 --radius 4,4,2 -} - -extract_lungs() -{ - echo "$image_name -> Extracting lungs..." - clitkExtractLung -i $image_name -o $mask_dir_tmp/lungs_$image_name -a $afdb_name --noAutoCrop -} - -resample() -{ - echo "$image_name -> Resampling..." - clitkResampleImage -i $mask_dir_tmp/patient_$image_name -o $mask_dir_tmp/patient_$image_name --spacing $resample_spacing --interp $resample_algo - clitkResampleImage -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/patient_only_$image_name --spacing $resample_spacing --interp $resample_algo - clitkResampleImage -i $mask_dir_tmp/bones_$image_name -o $mask_dir_tmp/bones_$image_name --like $mask_dir_tmp/patient_only_$image_name --interp $resample_algo - clitkResampleImage -i $mask_dir_tmp/lungs_$image_name -o $mask_dir_tmp/lungs_$image_name --like $mask_dir_tmp/patient_only_$image_name -} - -compute_motion_mask() -{ - if [ x = x$MotionMaskOffsetDetect ]; then - MotionMaskOffsetDetect="0,-5,0" - fi - if [ x = x$FillingLevel ]; then - FillingLevel=94 - fi - clitkMotionMask -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/mm_$image_name --featureBones=$mask_dir_tmp/bones_$image_name --featureLungs=$mask_dir_tmp/lungs_$image_name --upperThresholdLungs -400 --fillingLevel $FillingLevel --offsetDetect 0,-5,0 --pad --writeFeature=$mask_dir_tmp/feature_$image_name $MotionMaskExtra --monitor=$mask_dir_tmp/monitor_$image_name -} - -set_background() -{ - echo "$image_name -> Setting Background..." - clitkSetBackground -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/inside_$image_name --mask $mask_dir_tmp/mm_$image_name --outsideValue -1200 - clitkSetBackground -i $mask_dir_tmp/patient_only_$image_name -o $mask_dir_tmp/outside_$image_name --mask $mask_dir_tmp/mm_$image_name --outsideValue -1200 --fg -} - -create_registration_masks() -{ - echo "$image_name -> Creating registration masks..." - clitkMorphoMath -i $mask_dir_tmp/mm_$image_name -o $mask_dir_tmp/regmask_in_$image_name --type 1 --radius 8 - clitkExtractPatient -i $mask_dir_tmp/outside_$image_name -o $mask_dir_tmp/regmask_out_$image_name --noAutoCrop - clitkMorphoMath -i $mask_dir_tmp/regmask_out_$image_name -o $mask_dir_tmp/regmask_out_$image_name --type 1 --radius 8 -} - -remove_tmp_masks() -{ - echo "Removing temporary files..." - image_name_base=`echo $image_name | sed 's/mhd//'` - case $1 in - 1) - #rm $mask_dir_tmp/float_$image_name_base* - ;; - 2) - #rm $mask_dir_tmp/bones_$image_name_base* - ;; - 3) - #rm $mask_dir_tmp/patient_only_$image_name_base* - ;; - esac -} - -mm_preprocessing() -{ - extract_patient - extract_bones - extract_lungs - remove_tmp_masks 1 - resample -} - -mm_postprocessing() -{ - remove_tmp_masks 2 - set_background - remove_tmp_masks 3 - create_registration_masks -} - -mm_workflow() -{ - extract_patient - extract_bones - remove_tmp_masks 1 - resample - echo "$image_name -> Computing motion mask..." - compute_motion_mask >> $mask_log_dir/motion_mask_$image_name.log - remove_tmp_masks 2 - set_background - remove_tmp_masks 3 - create_registration_masks -} - -motion_mask() -{ - #set cmd line variables - echo "4D CT -> "$1 - phases=`grep ".raw" $1` - echo "mask phases -> " $phases - - dir=`dirname $1` - cd $dir - - # import variables specific to each patient - source variables - - resample_spacing=$2 - resample_algo=$3 - - - #set other global variables - mask_dir="MASK-$resample_spacing""mm""-$resample_algo" - mask_dir_tmp="tmp."$mask_dir - - echo - echo "------------ Motion mask from create_midP_masks.sh ------------" - start=`date` - echo "start: $start" - echo - - # the motion masks are first created in a tmp directory. this directory is - # later going to be renamed to the final motion mask directory. concurrent - # executions trying to create the same set of masks will be blocked until - # the first execution finishes. naturally, these other executions will not - # recreate the masks. so first we try to create the tmp directory. - # if the creation fails, it means that another execution is - # already creating the masks, so this execution will be blocked. the - # execution is unblocked only when the creation of masks is finished and - # the mask directory is renamed. - # - # ATTENTION: RP - 08/02/2011 - # robustness issue: tmp directory may exist but may be empty or - # incomplete. the solution is to check per file, but I'll leave it like - # this for the moment. - do_mm=0 - if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then - mkdir $mask_dir_tmp 2> /dev/null - return_mkdir=$? - if [ $return_mkdir == 0 ]; then - do_mm=1 - else - while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do - echo "waiting creation of motion masks..." - sleep 2 - done - echo "finished waiting" - fi - fi - - regmask_in_list="" - regmask_out_list="" - reg_in_list="" - reg_out_list="" - - # multi-threaded pre-processing for motion mask calcs - if [ $do_mm == 1 ]; then - mask_log_dir=$mask_dir_tmp/LOG - mkdir -p $mask_log_dir - - for phase in $phases - do - image_name=`echo $phase | sed 's/raw/mhd/'` - afdb_name=`echo $phase | sed 's/raw/afdb/'` - check_threads $MAX_THREADS - mm_preprocessing & - done - - # single-threaded motion mask calc - for phase in $phases - do - image_name=`echo $phase | sed 's/raw/mhd/'` - - check_threads 1 - echo "$image_name -> Computing motion mask..." - compute_motion_mask >> $mask_log_dir/motion_mask_$image_name.log - done - fi - - # multi-threaded post-processing of motion mask calcs - for phase in $phases - do - image_name=`echo $phase | sed 's/raw/mhd/'` - if [ $do_mm = 1 ]; then - check_threads $MAX_THREADS - mm_postprocessing & - fi - - regmask_in_list="$regmask_in_list regmask_in_$image_name" - regmask_out_list="$regmask_out_list regmask_out_$image_name" - reg_in_list="$reg_in_list inside_$image_name" - reg_out_list="$reg_out_list outside_$image_name" - done - - # rename tmp mask directory after mask creation - check_threads 1 - mv $mask_dir_tmp $mask_dir - - wait - - echo - echo "-------- Motion mask done ! ---------" - end=`date` - echo "start: $start" - echo "end: $end" - echo -} - - -################# -# main # -################# - -if [ $# != 3 ]; then - echo "Usage: create_midP_masks.sh 4D_CT.mhd resample_spacing resample_algorithm" - exit -1 -fi - -# -# variables exported in this scope -# -# mask_dir: directory where all masks are kept -# regmask_in_list: list of registration mask files (inside lungs) -# regmask_out_list: list of registration mask files (outised lungs) -# reg_in_list: list of registration image files (inside lungs) -# reg_out_list: list of registration image files (outside lungs) -# - -if [ $1 != "using-as-lib" ]; then - motion_mask $1 $2 $3 -fi diff --git a/scripts/dicom_info.sh b/scripts/dicom_info.sh deleted file mode 100755 index b18d7a5..0000000 --- a/scripts/dicom_info.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -#Get info about the first dicom image in the directory -find . -iname "*.dcm" -type f | head -n1 | xargs clitkDicomInfo | less diff --git a/scripts/midp_common.sh b/scripts/midp_common.sh deleted file mode 100755 index e120168..0000000 --- a/scripts/midp_common.sh +++ /dev/null @@ -1,281 +0,0 @@ -#! /bin/sh -x - -############################################################################### -# -# FILE: midp_common.sh -# AUTHOR: Rômulo Pinho 05/08/2011 -# -# Helper file with many functions used in the midP scripts. -# -############################################################################### - -bin=`dirname $0` -source $bin/tools.sh - -# -# check return value passed and abort if it represents an error (ie, ret != 0) -# optionally, a function can be passed as a 3rd parameter, to be called just -# before exiting. this is useful for cleaning up, for example. -# -abort_on_error() -{ - if [ $2 != 0 ]; then - echo Aborted at $1 with code $2 - #if [ $# = 3 ]; then - # eval $3 - #fi - - exit $2 - fi -} - -# -# wait for all processes in the list and return their exit codes -# in the ret_codes variable. -# -# OBS: this function must always be called in the same shell -# that launched the processes. -# -wait_pids() -{ - local pids=$* - local ret= - local rets= -# echo PIDS: $pids - for p in $pids; do -# echo waiting $p - wait $p > /dev/null 2> /dev/null - ret=$? - if [ ret != 127 ]; then - rets=$rets" "$ret - else - rets=$rets" "0 - fi - - done - - ret_codes=$rets -} - -# -# clean-up functions for maks, registration, and midp -# -clean_up_masks() -{ - rm -fr $mask_dir_tmp -} - -clean_up_midp() -{ - rm -fr $midp_dir -} - -clean_up_registration() -{ - rm -fr $vf_dir - rm -fr $output_dir -} - - -# -# block execution untill the number of threads (jobs) launched by the -# current process is below the given number of threads. -MAX_THREADS=2 -check_threads() -{ - nbth=$1 - while [[ $(jobs -p | wc -l) -ge $nbth ]]; do - jobs - sleep 10 - done -} - -# -# receive a 4D file and and extract the corresponding phase numbers -# export the variables containing each of the extracted data -# -extract_4d_phase_numbers() -{ - mhd4d=$1 - - nb_phases=${#phase_files[@]} - - # get everything except numbers and punctuation - 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 ".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 ".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 ".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 ".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 ".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 - - done - fi - - echo "Phase numbers are ${phase_nbs[@]}" - rm /tmp/patterns.txt -} - -# -# receive a 4D file and extract the corresponding phase files, -# and phase numbers. -# export the variables containing each of the extracted data -# -extract_4d_phases() -{ - mhd4d=$1 - - echo "4D file is $mhd4d" - - # array of phase files - phase_files=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:'` ) - echo "Phase files are ${phase_files[@]}" - - extract_4d_phase_numbers $mhd4d -} - - -# -# receive a 4D file and the reference phase number as input -# and extract the corresponding phase files, phase numbers, -# and reference phase file. -# -# export the variables containing each of the extracted data -# -extract_4d_phases_ref() -{ - extract_4d_phases $1 - - # reference phase file - 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 - for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do - ref_phase_nb=`echo ${phase_nbs[$i]} | grep $2` - if [ -n "$ref_phase_nb" ]; then - echo "Reference phase number is $ref_phase_nb" - break - fi - done -} - -# -# replacement for clitkCombineImage -combine_image() -{ -# eg: -i $result_in -j $result_out -o $out_result -m $motion_mask - local tmp1=$RANDOM - local tmp2=$RANDOM - - clitkSetBackground -i $1 -o $tmp1.mhd -m $4 - clitkSetBackground -i $2 -o $tmp2.mhd -m $4 --fg - - clitkImageArithm -i $tmp1.mhd -j $tmp2.mhd -o $3 - rm $tmp1.* $tmp2.* -} - -# -# replacement for clitkAverageTemporalDimension -average_temporal_dimension() -{ - # eg: -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd - local tmp=$RANDOM - local tot=$tmp.mhd - - local dir=`dirname $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 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_gui $roi_list - else - rtstruct_roi_name=$roi - fi;; - 1) - if zenity --question --text="Do you really wish to quit?" - then - exit - else - select_contour_gui $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=( `get_dicom_field_value "3006|0026" ${rtstruct_file} all | sed 's/ /_/g'` ) - rtstruct_roi_number_list=( `get_dicom_field_value "3006|0022" ${rtstruct_file} all | sed 's/ //g'` ) - 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 -} diff --git a/scripts/midp_template.conf b/scripts/midp_template.conf deleted file mode 100644 index be3e0aa..0000000 --- a/scripts/midp_template.conf +++ /dev/null @@ -1,76 +0,0 @@ -############################################################################### -# -# FILE: midp_template.conf -# AUTHOR: Rômulo Pinho 05/08/2011 -# -# Template configuration file for create_midP-2.0.sh. Values between <> must be -# changed according to the methods. -# -############################################################################### - -# output directories -vf_dir="" -midp_dir="" -output_dir="" -log_dir="" - -# script step to be executed (mask, registration, midp, all) -# midp depends on registration, which depends on mask -step="all" - -# indicate if midp images are computed with the in and out -# VFs combined -midp_combined_vf=0 - -# indicate which masks are used in the registration (mm, lungs, patient). if -# "patient", only the patient mask is used, and midp_combined_vf is -# set to 1. -mask_type="" - -# mask parameters -# -# interpolation algorithm for resampling (see clitkResampleImage) -mask_interpolation_algorithm="" -# interpolation spacing (in mm) -mask_interpolation_spacing= - -# registration method (blut, elastix) -method="" - -# registration parameters (depend on registration method - see descriptions) -# - -# b-spline spacing (blut, elastix) -bspline_spacing= - -# multi-resolution levels -nb_levels= - -# number of histogram bins (blut, elastix) -nb_hist_bins= - -# number of points (samples) to calculate metric -# (blut: percentage of image size; elastix: absolute number) -nb_samples= - -# algorithm used to sample points for the metric -# (elastix: see docs) -sampling_algo="" - -# number of iterations (blut, elastix) -nb_iter= - -# tolerance (stop condition for the metric) (blut) -tolerance= - -# metric used in the registration -# (blut: see clitkBLUTDIR; elastix: see docs) -metric="" - -# interpolation type -# (blut: see clitkBLUTDIR; elastix: see docs) -interpolator="" - -# optmizer -# (blut: see clitkBLUTDIR; elastix: see docs) -optimizer="" diff --git a/scripts/pts_to_landmarks.sh b/scripts/pts_to_landmarks.sh deleted file mode 100755 index 5f02900..0000000 --- a/scripts/pts_to_landmarks.sh +++ /dev/null @@ -1,32 +0,0 @@ -#! /bin/sh - -############################################################################### -# -# FILE: pts_to_landmarks -# AUTHOR: Vivien Delmon -# -# Conversion from landmarks in the format used in clitkCalculateTRE (.pts) -# to the format used in VV (.txt). -# -############################################################################### - -if [ $# -ne 2 ]; then - echo "Usage: $0 input.pts output.txt" 1>&2 - exit 1 -fi - -to_append=`mktemp` -to_prepend=`mktemp` -pts_file_spaced=`mktemp` - -for i in $(seq 0 $((`cat $1 | wc -l` - 1))); -do - echo "0 0" >> $to_append - echo $i >> $to_prepend -done - -echo "LANDMARKS1" > $2 -cat $1 | sed -e 's/\s/ /g' >> $pts_file_spaced -paste -d ' ' $to_prepend $pts_file_spaced $to_append >> $2 - -rm $to_append $to_prepend $pts_file_spaced diff --git a/scripts/registration.sh b/scripts/registration.sh deleted file mode 100755 index 8f6a5f2..0000000 --- a/scripts/registration.sh +++ /dev/null @@ -1,162 +0,0 @@ -#! /bin/sh - -############################################################################### -# -# FILE: registration.sh -# AUTHOR: Rômulo Pinho 05/08/2011 -# -# Helper file with registration functions using different methods. -# Each function receives a set of parameters that overall apply to any -# registration algorithm, as follows: -# -# reference=$1 : reference (fixed) image -# target=$2 : target (moving) image -# mask_ref=$3 : mask for the reference image -# mask_targ=$4 : mask for the moving image -# vf=$5 : output vector field representing the registration -# result=$6 : result image after applying the vector field -# nb_iter=$7 : maximum number of iterations -# nb_samples=$8 : number of image samples used in the metric calulcation -# sampling_algo=$9 : algorithm used in the selection of image samples -# hist_bins=${10} : number of histogram bins used in the metric calculation -# nb_levels=${11} : number of image resolutions -# spacing=${12} : spacing of the b-spline grid of the fines resolution -# metric=${13} : metric algorithm -# optimizer=${14} : optimizer -# interpolator=${15} : image interpolator -# log=${16} : log file -# -# New registration functions may be added to this file at any moment, -# respecting the interface defined above. -# -############################################################################### - -source `dirname $0`/midp_common.sh - - -################# BLUTDIR ##################### -registration_blutdir() -{ - local reference=$1 - local target=$2 - local mask_ref=$3 - local mask_targ=$4 - local vf=$5 - local result=$6 - local nb_iter=$7 - local nb_samples=$8 - local sampling_algo=$9 - local hist_bins=${10} - local nb_levels=${11} - local spacing=${12} - local metric=${13} - local optimizer=${14} - local interpolator=${15} - local log=${16} - local coeff=${17} - local init_coeff=${18} - - echo "Computing BLUTDIR $reference -> $target ..." - blutdir_params="--levels $nb_levels --metric $metric --optimizer $optimizer --samples $nb_samples --spacing $spacing,$spacing,$spacing --bins $hist_bins --maxIt $nb_iter --interp $interpolator --centre --verbose" - - if [ -n "$coeff" ]; then - coeff="--coeff $coeff" - fi - - if [ -n "$init_coeff" ]; then - init_coeff="--initCoeff $init_coeff" - fi - - cmd="clitkBLUTDIR -r $reference -t $target -m $mask_ref --targetMask $mask_targ --vf $vf $coeff $init_coeff -o $result $blutdir_params" - $cmd > $log - - abort_on_error registration_blutdir $? clean_up_registration -} - -################# ELASTIX ##################### -registration_elastix() -{ - local reference=$1 - local target=$2 - local mask_ref=$3 - local mask_targ=$4 - local vf=$5 - local result=$6 - local nb_iter=$7 - local nb_samples=$8 - local sampling_algo=$9 - local hist_bins=${10} - local nb_levels=${11} - local spacing=${12} - local metric=${13} - local optimizer=${14} - local interpolator=${15} - local log=${16} - local coeff=${17} - local init_coeff=${18} - -# RP 25/06/2012: unused variables (for now at elast) -# local log=${16} -# local coeff=${17} -# local init_coeff=${18} - - ########## register in ########## - for reg_in in $reg_in_list - do - if [ ! -z `echo $reg_in | grep "_$phase"` ] - then - target_in=$reg_in - fi - done - echo "Computing ELASTIX $reference -> $target ..." - exec_dir=`which elastix` - exec_dir=`dirname $exec_dir` - suffix=${nb_samples}_${nb_iter}_${nb_levels} - cat $exec_dir/params_elastix_template.txt | sed -e "s++$nb_iter+" \ - -e "s++$hist_bins+" \ - -e "s++$nb_levels+" \ - -e "s++$nb_samples+" \ - -e "s++$sampling_algo+" \ - -e "s++$metric+" \ - -e "s++$optimizer+" \ - -e "s++$interpolator+" \ - -e "s++$spacing+" > params_elastix_${suffix}.txt - - # set elastix to write out the coefficient images and corresponding DVFs - echo "(WriteCoefficientImage \"true\")" >> params_elastix_${suffix}.txt - echo "(WriteDVFFromCoeff \"true\")" >> params_elastix_${suffix}.txt - - vf_dir=`dirname $vf` - vf_base=`basename $vf .mhd` - coeff_dir=`dirname $coeff` - coeff_base=`basename $coeff .mhd` - result_dir=`dirname $result` - result_base=`basename $result .mhd` - - # image registration - cmd="elastix -f $reference -m $target -fMask $mask_ref -mMask $mask_targ -out $result_dir -p params_elastix_${suffix}.txt" - $cmd > /dev/null - abort_on_error registration_elastix $? clean_up_registration - - # generate vector field - # cmd="transformix -tp $result_dir/TransformParameters.0.txt -out $vf_dir -def all" - # $cmd > /dev/null - # abort_on_error registration_elastix $? clean_up_registration - - # post-processing - local level=$(($nb_levels - 1)) - mv $result_dir/dvf.0.R$level.mhd $vf - mv $result_dir/dvf.0.R$level.raw `echo $vf | sed 's/mhd/raw/'` - sed -i "s+dvf.0.R$level+$vf_base+" $vf - - mv $result_dir/coeff.0.R$level.mhd $coeff_dir/${coeff_base}_0.mhd - mv $result_dir/coeff.0.R$level.raw $coeff_dir/${coeff_base}_0.raw - sed -i "s+coeff.0.R$level+$coeff_base+" $coeff_dir/${coeff_base}_0.mhd - - mv $result_dir/result.0.mhd $result - mv $result_dir/result.0.raw `echo $result | sed 's/mhd/raw/'` - sed -i "s+result.0+$result_base+" $result - - mv $result_dir/elastix.log $log - mv $result_dir/TransformParameters.0.txt $result_dir/${result_base}_TransformParameters.0.txt -} \ No newline at end of file -- 2.45.1