#!/bin/sh
## \section{segment (shell script)}
## 
## \subsection*{Purpose}
## Perform a complete filtering, neural network training and classification
## in order to test and evaluate classification by a specific method on a
## specific textured image.
##
## \subsection*{Description}
## For large images, run:"
## \begin{verbatim}
##   setenv TMPDIR <dir>"
## \end{verbatim}
## first, where {\tt <dir>} is a directory with plenty of free space."
## 
## \subsection*{Usage}
## \begin{verbatim}
## segment [-dec]
##    [-sn structure number <integer>]
##    [-f filter name <string>] [-symm]
##    [-ref reference image <filename>]
##    [-class classifier <string>]
##    [-classes number of classes <integer>]
##    [-cut border width <integer>] 
##    [-oimg output image <filename>]
##    [-ostat output statistics <filename>]
## \end{verbatim}
## \begin{description}
##   \item[-U]
## 	Print usage information
##   \item[-sn]
## 	filter structure number
##   \item[-f]
## 	filter name (default i_2_1_09). Options: 
##	Farro: Farrokhnia's (90) method, Gabor: Gabor filter with structure 
##	given by -sn, f..:  FIR QMF PR filters, i..: IIR QMF PR filters
##	and w..: Wavelet QMF PR filters
##   \item[-dec]
## 	decimate the filter output?
##   \item[-symm]
## 	symmetrically extend prior to filtering?
##   \item[-ref]
## 	reference (tag) image
##   \item[-mask]
## 	mask file (mask out non-training regions) (HIPS image)
##   \item[-class]
## 	classifier (lvq/som/k-means) (def. lvq)
##   \item[-classes]
## 	number of classes (default 2)
##   \item[-cut]
## 	 cut away a border to avoid edge effects (default 0)
##   \item[-oimg]
## 	output classified image (default segment.hips)
##   \item[-ostat]
## 	output statistics file (default segment.stats)
## \end{description}
## 
## \subsection*{Date created}
## March 1994

. ${HOME}/set_vars

# Default parameters:
SYMM=FALSE		# don't use symmetric extension
DEC=FALSE		# don't use decimation of QMF filter
CLASSIFIER=lvq		# classification algorithm
CUT=0			# no edge cu away
STRUCT=8
OUT_STAT=segment.stats
OUT_IMG=segment.hips
FILTER=i_2_1_09
CLASSES=2
MASK_FILE=""

# Temporary files:
FEATURE=`tempnam`
FILT_STAT=`tempnam`
CLASS_STAT=`tempnam`
IMAGE=`tempnam`




print_usage ()
{
  # Print usage
  echo "\nsegment [options] texture"
  echo "\tOptions:"
  echo "\t  -U		-- print usage"
  echo "\t  -sn ..  	-- filter structure number"
  echo "\t  -f @@@  	-- filter name (default ${FILTER})"
  echo "\t 		   Farro: Farrokhnia's (90) method"
  echo "\t 		   Gabor: Gabor filter with structure given by -sn "
  echo "\t 		   f..:  FIR QMF PR filters"
  echo "\t 		   i..: IIR QMF PR filters"
  echo "\t 		   w..: Wavelet QMF PR filters"
  echo "\t  -dec  	-- decimate the filter output?"
  echo "\t  -symm	-- symmetrically extend prior to filtering?"
  echo "\t  -ref @@@	-- reference (tag) image"
  echo "\t  -mask @@@	-- mask file (mask out non-training regions)"
  echo "\t  -class @@@	-- classifier (lvq/som/k-means) (def. ${CLASSIFIER})"
  echo "\t  -classes ##	-- number of classes (default ${CLASSES})"
  echo "\t  -cut ##  	-- cut away a border to avoid edge effects" \
			   "(default ${CUT})"
  echo "\t  -oimg @@@ 	-- output classified image (default ${OUT_IMG})"
  echo "\t  -ostat @@@	-- output statistics file (default ${OUT_STAT})"
  echo ""
  exit 1
}



clean ()
{
  rm -f $FEATURE $FILT_STAT $CLASS_STAT $IMAGE
}



summary ()
{
  echo "+-----------------------------------------------------------------+"
  echo "| Create the summary file and present the results                 |"
  echo "+-----------------------------------------------------------------+"

  echo "Segmentation results" >  ${OUT_STAT}
  echo "	${IMAGE}" >>  ${OUT_STAT}
  cat $FILT_STAT  >> ${OUT_STAT}
  cat $CLASS_STAT >> ${OUT_STAT}
  if [ "${REFERENCE}" != "" ]; then
    ACCURACY=`diffseq ${REFERENCE} ${OUT_IMG} | abspix | blackpst`
    echo "	Accuracy: 			${ACCURACY}"  >> ${OUT_STAT}
  fi

  cat ${OUT_STAT}
}


# M A I N   P R O G R A M


until [ $# -eq 0 ]; do
  case "${1}" in
    -U		) shift; print_usage ;;
    -ref	) shift; REFERENCE="${1}"; shift;;
    -mask	) shift; MASK_FILE="${1}"; shift;;
    -sn		) shift; STRUCT="${1}"; shift;;
    -f		) shift; FILTER="${1}"; shift;;
    -class	) shift; CLASSIFIER="${1}"; shift;;
    -classes	) shift; CLASSES=${1}; shift;;
    -symm	) shift; if [ "${1}" = "TRUE" -o "${1}" = "FALSE" ] ; then
			   SYMM=${1}; shift ;
			 else
			   SYMM=TRUE
			 fi;;
    -dec	) shift; if [ "${1}" = "TRUE" -o "${1}" = "FALSE" ] ; then
			   DEC=${1}; shift ;
			 else
			   DEC=TRUE
			 fi;;
    -cut	) shift; CUT="${1}"; shift;;
    -oimg	) shift; OUT_IMG="${1}"; shift;;
    -ostat	) shift; OUT_STAT="${1}"; shift;;
    -*		) shift; print_usage ;;
    *		) INPUT_IMAGE=${1}; shift;;
  esac
done

tohips $INPUT_IMAGE $IMAGE
filter -symm $SYMM -dec $DEC -sn $STRUCT -f $FILTER \
 -if $IMAGE -ff $FEATURE -sf $FILT_STAT
# cut_border
if [ ${CLASSIFIER} = lvq ] ; then
  if [ "${MASK_FILE}" = "" ] ; then
    lvq_classify -dec $DEC -sn $STRUCT -ff $FEATURE -tf $REFERENCE \
	-cf $OUT_IMG -sf $CLASS_STAT
  else
    lvq_classify -dec $DEC -sn $STRUCT -ff $FEATURE -tf $REFERENCE \
	-cf $OUT_IMG -sf $CLASS_STAT -mf $MASK_FILE -perc 100
  fi
elif [ ${CLASSIFIER} = som ] ; then
  som_classify -dec $DEC -sn $STRUCT -ff $FEATURE \
	-cf $OUT_IMG -sf $CLASS_STAT -classes $CLASSES
elif [ ${CLASSIFIER} = k-means ] ; then
  kmeans_classify -dec $DEC -sn $STRUCT -ff $FEATURE \
	-cf $OUT_IMG -sf $CLASS_STAT -classes $CLASSES
fi
summary
clean
