#!/bin/bash
#
# Developed by Fred Weinhaus 8/25/2009 .......... revised 8/27/2013
#
# USAGE: normcrosscorr [-s] [-p] [-m mode] [-c color] smallfile largefile corrfile [matchfile]
# USAGE: normcrosscorr [-h or -help]
#
# OPTIONS:
#
# -s                stretch the correlation surface to full dynamic range so 
#                   that the best match is full white; default is unstretched
# -p                apply a pseudocolor to the correlation surface image; 
#                   default is no pseudocoloring
# -m      mode      mode for matchfile output; choices are: draw or overlay;
#                   draw colored box at best match location or
#                   overlay the small image at match location on a one half 
#                   transparent large image; default=draw
# -c      color     color to use for drawing box on large image where best 
#                   matched subsection was found; default=black
#
###
#
# NAME: NORMCROSSCORR 
# 
# PURPOSE: To compute the normalized cross correlation surface to find where 
# a small image best matches within a larger image.
# 
# DESCRIPTION: NORMCROSSCORR computes the normalized cross correlation surface 
# (image) to find where a small (first) image best matches within a larger 
# (second) image. Since the matching may differ for each channel, the two 
# input images will be converted to grayscale. Any alpha channel on either 
# image will be removed automatically before processing. Values in the 
# correlation surface can vary between +1 and -1, with a perfect match 
# being +1. If the correlation surface result is saved to an image format 
# that does not support negative values, the correlation surface will be 
# clamped so that all negative values are zero.
# 
# 
# OPTIONS: 
# 
# -s ... Stretch the normalized cross correlation surface image to full 
# dynamic range. Default is no stretch.
# 
# -p ... Apply a pseudocoloring to the normalized cross correlation surface 
# image where red corresponds to the highest values and purple to the lowest 
# values. Default is no pseudocoloring.
# 
# -m mode ... MODE is the layout mode for the optional matchfile image.
# Choices are draw (or d) or overlay (or o). Draw simply draws a colored box 
# outline at the best match subsection in the larger image. Overlay inserts 
# the small image at the match location of a 30% opaque version of the larger
# image. The default="draw". Ignored if no matchfile specified.
# 
# -c color ... COLOR is the color to use to draw the outline of the best 
# matching subsection in the larger image when mode=draw. Any valid IM color 
# specification may be used. The default=black.
# 
# REQUIREMENTS: IM version 6.5.4-7 or higher, but compiled with HDRI enabled 
# in any quantum level of Q8, Q16 or Q32. Also requires the FFTW delegate 
# library.
# 
# See http://www.fmwconcepts.com/imagemagick/fourier_transforms/fourier.html 
# for more details about the Fourier Transform with ImageMagick.
# 
# CAVEAT: No guarantee that this script will work on all platforms, 
# nor that trapping of inconsistent parameters is complete and 
# foolproof. Use At Your Own Risk. 
# 
# 
######
#

# set default values
stretch="no"		#yes or no
pseudocolor="no"    #yes or no
mode="draw"			#draw or overlay
color="black"		#any valid IM color
transp=0.5			#transparency of large image in mode=overlay

# set directory for temporary files
tmpdir="/tmp"

# set up functions to report Usage and Usage with Description
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
usage1() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -n '/^###/q;  /^#/!q;  s/^#//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}
usage2() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -n '/^######/q;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}


# function to report error messages
errMsg()
	{
	echo ""
	echo $1
	echo ""
	usage1
	exit 1
	}

# function crossCorr to compute IFT of complex product of (A*)x(B), 
# where A* is complex conjugate
# A*=a1-ia2; B=b1+ib2
# (A*)x(B)=(a1xb1+a2*b2) + i(a1xb2-a2xb1)
crossCorr()
	{
	img1=$1
	img2=$2
	# note both images contain 2 frames
	convert $img1 $img2 \
		\( -clone 0 -clone 2 -compose multiply -composite \) \
		\( -clone 1 -clone 3 -compose multiply -composite \) \
		\( -clone 4 -clone 5 -compose plus -composite \) \
		\( -clone 0 -clone 3 -compose multiply -composite \) \
		\( -clone 1 -clone 2 -compose multiply -composite \) \
		\( -clone 7 -clone 8 +swap -compose minus -composite \) \
		-delete 0-5,7,8 +ift "$dir/tmp0.mpc"
	}

# function to test for minus at start of value of second part of option 1 or 2
checkMinus()
	{
	test=`echo "$1" | grep -c '^-.*$'`   # returns 1 if match; 0 otherwise
    [ $test -eq 1 ] && errMsg "$errorMsg"
	}

# test for correct number of arguments and get values
if [ $# -eq 0 ]
	then
	# help information
   echo ""
   usage2
   exit 0
elif [ $# -gt 10 ]
	then
	errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
	while [ $# -gt 0 ]
		do
			# get parameter values
			case "$1" in
		  -h|-help)    # help information
					   echo ""
					   usage2
					   exit 0
					   ;;
				-s)    # get stretch
					   stretch="yes"
					   ;;
				-p)    # get pseudocolor
					   pseudocolor="yes"
					   ;;
			   	-m)    # mode
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID MODE SPECIFICATION ---"
					   checkMinus "$1"
					   mode=`echo "$1" | tr "[:upper:]" "[:lower:]"`
					   case "$mode" in
					   		draw|d) mode="draw" ;;
					   		overlay|o) mode="overlay" ;;
					   		*) errMsg "--- MODE=$mode IS NOT A VALID CHOICE ---" ;;
					   esac
					   ;;
			   	-c)    # get color
					   shift  # to get the next parameter
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID COLOR SPECIFICATION ---"
					   checkMinus "$1"
					   color="$1"
					   ;;
				 -)    # STDIN and end of arguments
					   break
					   ;;
				-*)    # any other - argument
					   errMsg "--- UNKNOWN OPTION ---"
					   ;;
		     	 *)    # end of arguments
					   break
					   ;;
			esac
			shift   # next option
	done
	#
	# get infile, filtfile and outfile
	smallfile=$1
	largefile=$2
	corrfile=$3
	matchfile=$4
fi

# test that infile provided
[ "$smallfile" = "" ] && errMsg "NO SMALL INPUT FILE SPECIFIED"

# test that filtfile provided
[ "$largefile" = "" ] && errMsg "NO LARGE INPUT FILE SPECIFIED"

# test that outfile provided
[ "$corrfile" = "" ] && errMsg "NO CORRELATION FILE SPECIFIED"



# Setup directory for temporary files
# On exit remove ALL -- the whole directory of temporary images
dir="$tmpdir/$PROGNAME.$$"
trap "rm -rf $dir; exit 0" 0
trap "rm -rf $dir; exit 1" 1 2 3 15
mkdir "$dir" || {
  echo >&2 "$PROGNAME: Unable to create working dir \"$dir\" -- ABORTING"
  exit 10
}

# read the input image and filter image into the temp files and test validity.
convert -quiet -regard-warnings "$smallfile" -alpha off +repage +write "$dir/tmpA4.mpc" -colorspace gray "$dir/tmpA1.mpc" ||
	errMsg "--- FILE $smallfile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO SIZE  ---"

convert -quiet -regard-warnings "$largefile" -alpha off +repage +write "$dir/tmpA3.mpc" -colorspace gray "$dir/tmpA2.mpc" ||
	errMsg "--- FILE $largefile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO SIZE  ---"


# test for valid version of IM
im_version=`convert -list configure | \
	sed '/^LIB_VERSION_NUMBER /!d;  s//,/;  s/,/,0/g;  s/,0*\([0-9][0-9]\)/\1/g' | head -n 1`
[ "$im_version" -lt "06050407" ] && errMsg "--- REQUIRES IM VERSION 6.5.4-7 OR HIGHER ---"

# test for hdri enabled
hdri_on=`convert -list configure | grep "enable-hdri"`
[ "$hdri_on" = "" ] && errMsg "--- REQUIRES HDRI ENABLED IN IM COMPILE ---"


# get image dimensions to be sure that infile1 is smaller than infile2
ws=`identify -ping -format "%w" "$dir/tmpA1.mpc"`
hs=`identify -ping -format "%h" "$dir/tmpA1.mpc"`
wlo=`identify -ping -format "%w" "$dir/tmpA2.mpc"`
hlo=`identify -ping -format "%h" "$dir/tmpA2.mpc"`

[ $ws -gt $wlo ] && errMsg "--- SECOND IMAGE MUST BE WIDER THAN FIRST IMAGE ---"
[ $hs -gt $hlo ] && errMsg "--- SECOND IMAGE MUST BE TALLER THAN FIRST IMAGE ---"


# make large image even dimensions and square
wl=`convert xc: -format "%[fx:2*ceil($wlo/2)]" info:`
hl=`convert xc: -format "%[fx:2*ceil($hlo/2)]" info:`
# test if large image is square and if not, then pad with the mean
test1=`convert xc: -format "%[fx:($wl==$hl)?1:0]" info:`
if [ $test1 -eq 0 ]; then
	# not square so get larger dimension
	maxdim=`convert xc: -format "%[fx:max($wl,$hl)]" info:`
	wl=$maxdim
	hl=$maxdim
fi
mean=`convert "$dir/tmpA2.mpc" -format "%[fx:100*mean]" info:`
convert "$dir/tmpA2.mpc" -background "gray($mean%)" -extent ${wl}x${hl} "$dir/tmpA2.mpc"
#echo "ws=$ws; hs=$hs; wlo=$wlo; hlo=$hlo; wl=$wl; hl=$hl;"


: '
C = ((S-Ms) X (L-Ml))/(sigmaS*sqrt(Nx(U X L^2) - (U X L)^2)
where
A X B = I(F(A*)F(B)] and A* is complex conjugate of A, F=FFT and I=IFT
L is large image.
L-Ml is the mean subtracted large image. 
S-Ms is the mean subtracted small image. 
U is a unit image (value=1) 
Both S-Ms and U are padded at right and bottom to size of L. 
'

# compute N=wsxhs = total pixels in small image
Ns=`convert xc: -format "%[fx:$ws*$hs]" info:`
Nl=`convert xc: -format "%[fx:$wl*$hl]" info:`

# get quantumrange
qrange=`convert xc: -format "%[fx:quantumrange]" info:`
qrangesqr=`convert xc: -format "%[fx:sqrt(quantumrange)]" info:`
qscale=`convert xc: -format "%[fx:100*quantumscale]" info:`

# get factors
fact1=`convert xc: -format "%[fx:1/$Ns]" info:`
fact2=`convert xc: -format "%[fx:$qrange/($Ns*$Ns)]" info:`

#echo "Ns=$Ns; Nl=$Nl fact1=$fact1; fact2=$fact2; qrange=$qrange; qrangesqr=$qrangesqr;"


# get mean and std of small image
mean=`convert "$dir/tmpA1.mpc" -format "%[mean]" info:`
std=`convert "$dir/tmpA1.mpc" -format "%[standard-deviation]" info:`

# get mean of large image
meanl=`convert "$dir/tmpA2.mpc" -format "%[mean]" info:`
#echo "mean=$mean; std=$std; meanl=$meanl"

# ncc has range -1 to +1, so need to scale that to range 0 to quantumrange
# thus we scale by dividing std by quantumrange and 
# note that negatives are clipped by PNG output.
if [ "$im_version" -lt "06050410" ]; then
	#HDRI was unscaled by quantumrange
	# so need the extra factor of quantumrange?
	# not sure why a perfect match is considerably less that quantumrange?
	std=`convert xc: -format "%[fx:$std/(quantumrange)]" info:`
fi
#echo "std=$std"

# get square of large image and take FFT
# correct for IM normalization by multiplying by quantumrange
convert "$dir/tmpA2.mpc" "$dir/tmpA2.mpc" -compose multiply -composite -evaluate multiply $qrange +fft "$dir/tmpL2.mpc"

# take FFT of large image
convert "$dir/tmpA2.mpc" +fft "$dir/tmpL.mpc"

# subtract mean of large image and take FFT
convert "$dir/tmpA2.mpc" -evaluate subtract $meanl +fft "$dir/tmpLM.mpc"

# subtract mean from small image and pad and take FFT
# unnormalize FFT by multiplying by Nl
convert "$dir/tmpA1.mpc" -evaluate subtract $mean \
	-background black -extent ${wl}x${hl} +fft -evaluate multiply $Nl "$dir/tmpS.mpc"

# create identity U image (value=1) and pad and take FFT
# unnormalize FFT by multiplying by Nl
convert -size ${ws}x${hs} xc:white -background black -extent ${wl}x${hl} +fft -evaluate multiply $Nl "$dir/tmpU.mpc"

# create (S-Ms) X (L-Ml)/Ns
crossCorr  "$dir/tmpS.mpc"  "$dir/tmpLM.mpc"
convert "$dir/tmp0.mpc" -evaluate multiply $fact1 "$dir/tmpSL.mpc"

# create (U X L^2)/Ns
crossCorr "$dir/tmpU.mpc" "$dir/tmpL2.mpc"
convert "$dir/tmp0.mpc" -evaluate multiply $fact1 "$dir/tmpL2.mpc"

#create (U X L)^2/Ns^2
crossCorr  "$dir/tmpU.mpc"  "$dir/tmpL.mpc"
# multiply by qrange to account for IM normalization in multiply
# so final multiply is qrange/(Ns*Ns)
convert "$dir/tmp0.mpc" "$dir/tmp0.mpc" -compose multiply -composite -evaluate multiply $fact2 "$dir/tmpL.mpc"


# compute the std of L denominator image
# IM normalizes sqrt in range 0 to 1, then multiplies by qrange
# thus need to divide by sqrt(qrange) to compensate
# make white image
# trap values for std of large image if too close to 0 (when sub region is too constant a color), so no divide by zero
# replacing the use of -fx "(abs(u)<0.002)?1:u"
convert \( "$dir/tmpL2.mpc" "$dir/tmpL.mpc" +swap -compose minus -composite \
		-evaluate pow 0.5 -evaluate divide $qrangesqr \) \
	\( -size ${wl}x${hl} xc:white \) \
	\( -clone 0 -clone 0 -compose multiply -composite -evaluate pow 0.5 \
		-threshold 0.2% -negate \) \
	-compose over -composite "$dir/tmpL.mpc"


#evaluate normalize cross correlation image
# multiply by qrange to convert ncc result from 1 to qrange so that normalized value is 1
convert "$dir/tmpSL.mpc" -evaluate multiply $qrange \
	\( "$dir/tmpL.mpc" -evaluate multiply $std \) \
	+swap -compose over -compose divide -composite -crop ${wlo}x${hlo}+0+0 +repage \
	"$dir/tmp0.mpc"



# setup pseudocolor lut
if [ "$pseudocolor" = "yes" ]; then
	convert xc:blueviolet xc:blue xc:cyan xc:green1 \
		xc:yellow xc:orange xc:red +append \
		-filter cubic -resize 256x1 "$dir/tmpP.mpc"
	colorize="$dir/tmpP.mpc -clut"
else
colorize=""
fi

# setup stretch
if [ "$stretch" = "yes" ]; then
#	max=`convert "$dir/tmp0.mpc" -format "%[max]" info:`
#	min=`convert "$dir/tmp0.mpc" -format "%[min]" info:`
	#echo "max=$max; min=$min"
#	convert \( "$dir/tmp0.mpc" -level 0x$max \) $colorize $corrfile
	convert \( "$dir/tmp0.mpc" -auto-level \) $colorize $corrfile
else
	convert "$dir/tmp0.mpc" $colorize $corrfile
fi

: <<COMMENT
#old slow method 
echo "get match"
# convert to txt format
# skip to second line
# remove all non numeric characters
# sort in reverse order according to field 3 
# keep 1 result with highest value in field 3
# for that result get fields 1 and 2 for location
# convert space to + sign
match=`convert  "$dir/tmp0.mpc" txt:- | tail -n +2 | tr -cs '0-9\012' ' ' | \
sort -n -r -k 3 | head -n 1 | cut -d\  -f 1-3`
echo ""
echo "Match Coords And Score: $match"
echo ""
# compute subsection
ulx=`echo $match | cut -d\  -f 1`
uly=`echo $match | cut -d\  -f 2`
subsection="${ws}x${hs}+$ulx+$uly"
echo "$subsection"
COMMENT


#echo "get match"
max=`convert "$dir/tmp0.mpc" -format "%[fx:maxima]" info:`
str=`convert "$dir/tmp0.mpc" -fx "u>=($max-quantumscale)?debug(u):0" null: 2>&1`
coords=`echo "$str" | sed -n 's/^.*\[\([0-9]*,[0-9]*\)\]\.red.*$/\1/p'`
echo ""
echo "Match Coords: ($coords) And Score In Range 0 to 1: ($max)"
echo ""

# compute subsection
ulx=`echo $coords | cut -d,  -f 1`
uly=`echo $coords | cut -d,  -f 2`
subsection="${ws}x${hs}+$ulx+$uly"
#echo "$subsection"


if [ "$matchfile" != "" -a "$mode" = "draw" ]; then
	lrx=$(($ulx+$ws))
	lry=$(($uly+$hs))
#echo "ulx=$ulx; uly=$uly; lrx=$lrx; lry=$lry"
	convert $dir/tmpA3.mpc[${wlo}x${hlo}+0+0] -fill none -stroke "$color" \
		-draw "rectangle $ulx,$uly $lrx,$lry" $matchfile
elif [ "$matchfile" != "" -a "$mode" = "overlay" ]; then
	convert \( "$dir/tmpA3.mpc" -alpha on -channel a -evaluate set 30% +channel \) "$dir/tmpA4.mpc" \
		-geometry "$subsection" -compose over -composite $matchfile
fi
exit 0



