\documentclass[10pt,a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,hyperref,graphicx,booktabs,float}

% Paragraph indentation
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}

\title{Image processing 4: Local Structure}
\author{Sander van Veen \& Tadde\"us Kroes \\ 6167969 \& 6054129}

\begin{document}

\maketitle

\section{Analytical Local Structure}

\subsection{Derivatives}
\label{sub:derivatives}

We have been given the following function:
$$f(x, y) = A sin(Vx) + B cos(Wy)$$

The partial derivatives $f_x, f_y, f_{xx}, f_{xy}$ and $f_{yy}$ can be
derived as follows:

\begin{table}[H]
	\begin{tabular}{rl}
		$f_x$    & $= \frac{\delta f}{\delta x}$ \\
		         & $= A \frac{\delta}{\delta x} sin(Vx) + B \frac{\delta}{\delta x} cos(Wy)$ \\
	    	     & $= A cos(Vx) \cdot V + B \cdot 0$ \\
	    	     & $= AV cos(Vx)$ \\
	    	     & \\
		$f_y$    & $= \frac{\delta f}{\delta y}$ \\
		         & $= A \frac{\delta}{\delta y} sin(Vx) + B \frac{\delta}{\delta y} cos(Wy)$ \\
	    	     & $= A \cdot 0 - B sin(Wy) \cdot W$ \\
	    	     & $= -BW sin(Wy)$ \\
	    	     & \\
		$f_{xx}$ & $= \frac{\delta f_x}{\delta x}$ \\
		         & $= AV \frac{\delta}{\delta x} cos(Vx)$ \\
		         & $= -AV^2 sin(Vx)$ \\
	    	     & \\
		$f_{xy}$ & $= \frac{\delta f_x}{\delta y} = AV \frac{\delta}{\delta y} cos(Vx) = 0$ \\
	    	     & \\
		$f_{yy}$ & $= \frac{\delta f_y}{\delta y}$ \\
		         & $= -BW \frac{\delta}{\delta y} sin(Wy)$ \\
		         & $= -BW^2 cos(Wy)$ \\
	\end{tabular}
\end{table}

\pagebreak

\subsection{Plots}

The following plots show $f(x, y)$ and its first and second derivatives. The
image on the left shows $f_x$ and $f_y$. The image on the right shows $f_{xx}$
and $f_{yy}$ in a quiver plot over $f(x, y)$. The arrows point towards the
largest increase of gray value, which means that the derivations in chapter
\ref{sub:derivatives} are correct.

\begin{figure}[H]
	\hspace{-2cm}
	\includegraphics[scale=.8]{samples.pdf}
	\caption{Plots of $f(x, y)$ and its first and second derivatives.}
\end{figure}

\section{Gaussian Convolution}

\subsection{Implementation}

All Gaussian functions are implemented in the file \emph{gauss.py}. The
\texttt{Gauss} function fills a 2D array with the values of the 2D Gaussian
function\footnote{\label{footnote:gaussian-filter}
\url{http://en.wikipedia.org/wiki/Gaussian\_filter}}:

$$g_{2D}(x, y) = \frac{1}{2 \pi \sigma^2} e^{-\frac{x^2 + y^2}{2 \sigma^2}}$$

This function converges to zero, but never actually equals zero. The filter's
size is therefore chosen to be $\lceil 6 * \sigma \rceil$ in each direction by
convention (since values for $x,y > 3 * \sigma$ are negligible). Finally,
because the sum of the filter should be equal to 1, it is divided by its own
sum.

The result of the \texttt{Gauss} function is shown in figure
\ref{fig:gauss-2d}. The subplots respectively show the original image, the
Gaussian kernel and the convolved image.

\begin{figure}[H]
	\hspace{-3cm}
	\includegraphics[scale=.6]{gauss_2d_5.pdf}
	\caption{The result of \texttt{python gauss.py 2d 5}.}
	\label{fig:gauss-2d}
\end{figure}

\subsection{Measuring Performance}

We've timed the runtime of the \texttt{Gauss} function for
$\sigma = 1,2,3,5,7,9,11,15,19$, the results are in figure
\ref{fig:times-2d}. The graph shows a computational complexity of
$\mathcal{O}(\sigma^2)$.

\begin{figure}[H]
	\center
	\includegraphics[scale=.5]{gauss_times_2d.pdf}
	\caption{The result of \texttt{python gauss.py timer 2d 5} (so, each
	         timing has been repeated 5 times and then averaged).}
	\label{fig:times-2d}
\end{figure}

\section{Separable Gaussian Convolution}

\subsection{Implementation}

The \texttt{Gauss1} function uses the 1D Gaussian
function\ref{footnote:gaussian-filter}:

$$g_{1D}(x) = \frac{1}{\sqrt{2 \pi} \cdot \sigma} e^{-\frac{x^2}{2 \sigma^2}}$$

This function returns a 1D array of kernel values, which is used by the
function \texttt{convolve1d}. Using the separability property, the following
code snippets produce the same result:

\begin{verbatim}
W = Gauss1(s)
G = convolve1d(F, W, axis=0, mode='nearest')
G = convolve1d(G, W, axis=1, mode='nearest')
\end{verbatim}

as opposed to:

\begin{verbatim}
G = convolve(F, Gauss(s), mode='nearest')
\end{verbatim}

The timing results of the first code snippet are displayed in figure
\ref{fig:times-1d}. The graph shows that the 1D convolution has a
computational complexity of $\mathcal{O}(\sigma)$, which is much faster than
the 2D convolution (certainly for higher scales).

\begin{figure}[H]
	\center
	\includegraphics[scale=.5]{gauss_times_1d.pdf}
	\caption{The result of \texttt{python gauss.py timer 1d 50}.}
	\label{fig:times-1d}
\end{figure}

\section{Gaussian Derivatives}

\subsection{Separability}

We can show analytically that all derivatives of the 2D Gaussian function
are separable as well:

\begin{table}[H]
	\begin{tabular}{rll}
		$ \frac{\delta}{\delta x} \frac{\delta}{\delta y} G_{2D}(x, y)$
		& $= \frac{\delta}{\delta x} (\frac{\delta}{\delta y} (G_{1D}(x)
            \cdot G_{1D}(y)))$ & $G_{2D}(x, y) = G_{1D}(x) \cdot G_{1D}(y)$ is
            given \\
		& $= \frac{\delta}{\delta x} (G_{1D}(x) \cdot
		    \frac{\delta}{\delta y} G_{1D}(y))$ & because $G_{1D}(x)$ is
		    constant with respect to $y$ \\
		& $= \frac{\delta}{\delta x} G_{1D}(x) \cdot
            \frac{\delta}{\delta y} G_{1D}(y)$ & \\
	\end{tabular}
\end{table}

The separability property is used in the \texttt{gD} function, by calling the
\texttt{convolve1d} function separately for each direction. This implementation
yields the 2-jet of scale 4 of the cameraman image in figure \ref{fig:jet}.

\begin{figure}[H]
	\center
	\includegraphics[scale=.4]{jet_4.pdf}
	\caption{The result of \texttt{python gauss.py jet 4}.}
	\label{fig:jet}
\end{figure}

\section{Canny Edge detector}

The Canny Edge Detector is implemented in the file \emph{canny.py}. For the
algorithm, we used the Wiki
page\footnote{\url{http://en.wikipedia.org/wiki/Canny\_edge\_detector}}. The
different Wiki sections are marked by comments with similar descriptions. Since
the Wiki page is self-explanatory, we will not discuss the algorithm itself in
this report.

The program usage is as follows:

\begin{verbatim}
python canny.py SCALE [ LOWER_THRESHOLD HIGHER_THRESHOLD ]
\end{verbatim}

The scale is obviously used for finding the intensity gradient, and the
thresholds are used in the "Hysterisis thresholding" part. Note that the
thresholds are optional, because the assignment instructs to create a function
\texttt{canny(F, s)} without any arguments for thresholds. Therefore, we were
not sure whether to implement this section. If the thresholds are specified,
the resulting plot will contain an additional image containing a binary image
of edges. The thresholds can be specified in the range 0-255, they are scaled
down by the program to match the image's color range.  An example execution of
edge detection on the cameraman image using a scale of 2, a lower threshold of
50 and a higher threshold of 70, can be viewed in figure \ref{fig:canny}.

\begin{figure}[H]
	\hspace{-5cm}
	\includegraphics[scale=.6]{canny_2_50_70.pdf}
	\caption{The result of \texttt{python canny.py 2 50 70}.}
	\label{fig:canny}
\end{figure}

\end{document}