include(../../../m4-dip-html-macros.m4)

LESSON(Correlation)

Correlation in 1D is defined by the following equation

\[
g(x) = f(x) \circ h(x) = \sum_{all \; k} f^{\ast}(k) h(x+k) 
\]

It is very similar to the convolution equation, and in practice it can
be seen as the convolution of the complex conjugate of {\bf f} (image)
with a kernel {\bf h} (filter).  Note that the kernel is not 
reflected as in convolution.  This can be extended to the 2D case.

\[
\begin{array}{ccc}
p(x,y) \circ q(x,y) & \Longleftrightarrow & P^{\ast}(u,v) Q(u,v) \\
                    &    \mbox{or}        &                      \\
p^{\ast}(x,y) q(x,y)& \Longleftrightarrow & P(u,v) \circ Q(u,v)
\end{array}
\]

Similar to convolution, correlation also has a theorem that relates
spatial domain processing with frequency domain processing via the
Discrete Fourier Transform (DFT).  Thus, correlation can be performed either
in the spatial domain or frequency domain.  Correlation is used to determine
the degree of similarity between data sets.

SUBLESSON(Experiment: Template Matching by Normalized Correlation)

One application of correlation is to perform template matching.
The idea is to find the position where a best match exists between 
a small pattern (template) and a set of patterns in a larger image.

In this experiment we will use the gull image as the larger image and
a small portion (10x10) around its eye as the pattern image or template.

HTMLTWOFIGWIDTHLINK(gull,,, gull-roi,ORIG,,
	            larger image, 
		    zoomed pattern or template (region of the eye))

The correlation of the image with the 10x10 template does not yield
the expected result.  The maximum points occur at the location width=199
and height=100 and do not correspond to the position where the eye is.
Note that the template behaves as a lowpass filter blurring the image.
Also, the maximum point will depend on the amount of white (high pixel
values) present. You can see the output of the correlation in the image
below:

HTMLONEFIG(gull-normal-correlation,
	   Correlation of the gull image and its eye template)

To solve this problem we have to use the {\bf normalized correlation}
procedure defined below for the 1D case 

\[
g_c(x) = \frac{f(x) \circ h(x)}{\sqrt{f^2(x) \circ \imath}}
\]

where {\bf i} is a constant kernel where each pixel has value 1 and
with the same size as the correlation pattern.  The denominator
is known as the normalization factor for correlation.  

Now, the maximum of the normalized correlation is at the gull's eye.
You can also see that the detected point is not very robust as there
are many other high value points that correlates with the template.

HTMLTWOFIG(gull-root-square,gull-normalized-correlation,
	   normalized factor for correlation, 
           normalized correlation: maximum at the eye)

To help you determine exactly the location of the maximum points use the
statistic operators.

BUILDINGWORKSPACE

LABPURPOSE(`The purpose of this lab exercise is to experiment with
template matching, correlation, and normalized correlation using
spatial convolution.')

BEGINLIST %1

ITEM Read and display the image IMAGEDIR(gull.viff). Convert it to
floating point using the operator CONVERTTYPE.

BEGINLIST %2

ITEM USERDEFINEDMENU

ITEM DISPLAYIMAGEMENU

ITEM CONVERTTYPEMENU

ENDLIST %2

ITEM Extract a small (10x10) pattern from the image using the EXTRACT
operator. Determine the coordinates of the region you wish to
extract by moving the cursor over the pattern in the displayed image.
Display the extracted region (first expand it by a factor of 10).

BEGINLIST %2

ITEM EXTRACTMENU

ITEM EXPANDMENU

ITEM DISPLAYIMAGEMENU

ENDLIST %2

ITEM Perform the correlation between the image and the pattern using 
the LINEAROPERATOR. In the LINEAROPERATOR pane, explicitly set the
kernel origin to be in the lower right hand corner.  Also set the
"Kernel reflection" option to "Do not flip kernel". Display the
result. To better visualize the maximum value, you may want to pseudo
color the result using the "Disjoint Greycode" option in the AUTOCOLOR 
operator.

BEGINLIST %2

ITEM LINEAROPERATORMENU

ITEM DISPLAYIMAGEMENU

ITEM AUTOCOLORMENU

ENDLIST %2

ITEM Use the STATISTICS operator to find the coordinates of the
maximum point of the resulting image.  Open the STATISTICS pane and
select the "maximum", and the width and height coordinates of the
maximum.

BEGINLIST %2

ITEM STATISTICSMENU

	Notice that the maximum does not occur where you would expect
it to, which is over the pattern that you extracted.

ENDLIST %2

ITEM Perform the normalized correlation.

BEGINLIST %2

ITEM Calculate the square of image using the SQUARE operator.

BEGINLIST %3

ITEM SQUAREMENU

ENDLIST %3

ITEM Create a constant kernel of value 1 that is the same size as the
pattern (10x10) using the CONSTANT operator. Perform correlation
between the squared image and this kernel. Set up the LINEAROPERATOR
as you did in step 3.

BEGINLIST %3

ITEM CONSTANTMENU

ITEM LINEAROPERATORMENU

ENDLIST %3

ITEM Take the square root of the result of the correlation in step B,
using the SQUAREROOT operator.

BEGINLIST %3

ITEM SQUAREROOTMENU

ENDLIST %3

ITEM Divide the original correlation image (step 3) by the normalization
factor image just created, and display the result. To better visualize
the maximum value, pseudo color the result using the "Disjoint
Greycode" option in the AUTOCOLOR operator.

BEGINLIST %3

ITEM DIVIDEMENU

ITEM AUTOCOLORMENU

ITEM DISPLAYIMAGEMENU

ENDLIST %3

ITEM Find the maximum of the normalized correlation using the STATISTICS 
operator as you did in step 4.

BEGINLIST %3

ITEM STATISTICSMENU

ENDLIST %3

ENDLIST %2

ENDLIST %1

EXERCISES

BEGINLIST %1

ITEM Perform the same experiment, but in the frequency domain.

ENDLIST %1

WORKSPACE(correlation.wk)

ENDLESSON
