
There are two somewhat similar concepts that must be understood clearly when performing linear spatial filtering. One is correlation and the other convolution. Correlation, speaking from an image processing point of view, is when one moves a filtering mask across an image and computes the sum of products at each location. Convolution is similar, but the filtering mask is first rotated 180 degrees. This is best shown with a trivial example of 1-D filter on a 1-D image. See Figure \ref{correlation1d}.

\begin{figure}[h]
\centering
\includegraphics[width=14cm]{report/background/correlation1d.png}
\caption{Image illustrating Convolution in 1D. Inspired by an example in \cite{Gonzales}}
\label{correlation1d}
\end{figure}

The first thing one notices is that if one is to overlap the filter on all the value in the image, i.e. move each value of the filter on every pixel in the image, then we would have to pad the image with zeros such that it is possible. This means that if the filter is of size m, then we need a padding of size m-1 to the left and right of the image. In this case a padding of size four is added on each side of the image since the filter contains five values. Another aspect worth noticing here is that correlation and convolution are functions of displacement. Where one starts at a displacement of zero and increases by one as one filters the image. 

\subsection*{Convolution}
From a mathematical point of view, convolution is an operation involving two functions that produces a new function. This new function reflects to which extent the original functions match if their graphs were aligned with each other. Convolution is mathematically defined as Equation \ref{convolution1} \cite{Gonzales}.

\begin{equation}
(f \star g)(t) = \int_{-\infty}^\infty \! f(a)g(t-a) \, da
\label{convolution1}
\end{equation}

\begin{equation}
w(x,y,z) \star f(x,y,z) = \sum_{s=-a}^a \sum_{t=-b}^b \sum_{r=-c}^c w(s,t,r) f(x - s, y - t, z - r)
\label{convolution}
\end{equation}

The continuous mathematical definition is not that useful in our case since we would like to deal with discrete cases in programming. To convert the preceding in to a discrete function we would have to add discrete convoluted values from each function. If we have that $w(x,y)$ is the filter of size $m \times n$ that will be convoluted with an image $f(x,y)$ , denoted as $w(x,y) \star f(x,y)$. This gives the Equation \ref{convolution} \cite{Gonzales}, which is a discrete sumation equivalent to the continous mathematical definition of convolution.
