\documentclass[11pt,a4paper]{article}
\usepackage{algorithm, algorithmic, listings} % Code
\usepackage{amsmath,mathtools,amssymb,amsfonts,dsfont,cancel} % Math
\usepackage{amstext}
\usepackage{color, xcolor} % Color
\usepackage{diagbox, tabularx} % Table
\usepackage{enumerate} % List
\usepackage{epsfig, epstopdf, graphicx, multicol, multirow, palatino, pgfplots, subcaption, tikz} % Image.
\usepackage{fancybox}
\usepackage{verbatim}

\usepackage[font=footnotesize]{caption} % labelfont=bf
\usepackage[margin=1in]{geometry}
\usepackage[hidelinks]{hyperref}
\epstopdfsetup{outdir=./Figure/Converted/}
\graphicspath{{./Figure/}}

\makeatletter
\def\input@path{{./Figure/}}
\makeatother

% MATLAB code settings
\lstset{extendedchars=false, % Shutdown no-ASCII compatible
basicstyle=\normalsize\tt, % the size of the fonts that are used for the code
language=Matlab, tabsize=4, numbers=left, numberstyle=\small, stepnumber=1, numbersep=8pt, keywordstyle=\color[rgb]{0,0,1}, commentstyle=\color[rgb]{0.133,0.545,0.133}, stringstyle=\color[rgb]{0.627,0.126,0.941}, backgroundcolor=\color{white}, showspaces=false, showstringspaces=false, showtabs=false, frame=single, captionpos=t, breaklines=true, breakatwhitespace=false, morekeywords={break, case, catch, continue, elseif, else, end, for, function, global, if, otherwise, persistent, return, switch, try, while}, title=\lstname,
mathescape=true,escapechar=? % escape to latex with ?..?
escapeinside={\%*}{*)}, % if you want to add a comment within your code
%morestring=[m]', % strings
%columns=fixed, % nice spacing
}

\begin{document}
\title{\sc\vspace{3cm}\hrule\vspace{0.3cm}{\LARGE DD2423}\\\vspace{0.1cm}{\Large Image Analysis and Computer Vision}\vspace{0.3cm}\hrule\vspace{1.5cm}{\Large Laboratory Report}\\{\large Lab 2: Edge Detection \& Hough Transform}}
\author{Jiang, Sifan\\sifanj@kth.se}
\maketitle
\newpage

\newcounter{Counter}
\setcounter{Counter}{0}

\section*{1\hspace{0.5cm}Difference operators}
	\begin{itemize}
		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} What do you expect the results to look like and why? Compare the size of \texttt{dxtools} with the size of \texttt{tools}. Why are these sizes different?
			\par The $x$-wise derivative is expected to be an image with the edge of the original image in the $x$ direction. The $y$-wise derivative is expected to be an image with the edge of the original image in the $y$ direction. The result is shown in Figure \ref{fig:Question_1}.
			\begin{figure}[!ht]
				\centering
				\includegraphics[width=\columnwidth]{Question_1.eps}
				%\scalebox{1}{\input{Question_1.tex}}
				\caption{The first row of images is applied by simple difference operator. The second row is applied by central differences. The third row is applied by Robert's diagonal operator. The last row is applied by the Sobel operator.}
				\label{fig:Question_1}
			\end{figure}
			\par The sizes of the derivative images are different from each other or from the original image because of the difference of the kernel sizes in each method. The image size is shown in Table \ref{tab:Image_Size}.
			\begin{table}[!ht]
				\centering
				\caption{Image size.}
				\label{tab:Image_Size}
				\begin{tabular}{ccc}
					\hline
					Image & $x$ direction ($y \times x$) & $y$ direction ($y \times x$) \\
					\hline
					Original & $256 \times 256$ & $256 \times 256$ \\
					Simple difference operator & $256 \times 254$ & $254 \times 256$ \\
					Central difference operator & $256 \times 254$ & $254 \times 256$ \\
					Roberts cross edge operator & $255 \times 255$ & $255 \times 255$ \\
					Sobel operator & $254 \times 254$ & $254 \times 254$ \\
					\hline
				\end{tabular}
			\end{table}
			\par The reason why these sizes are different is because of the difference between the kernel size of each difference operators. If the image is of size $N \times M$ and in the case of the simple difference operator, the kernel when considering the $x$-direction has size of $1 \times 3$. Since all the elements in the kernel should be multiplied by a element in the image, the kernel will fit the image $N$ times in the $y$ direction, but $M-2$ times in the $x$ direction.
	\end{itemize}
\section*{2\hspace{0.5cm}Point-wise thresholding of gradient magnitudes}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_2_Tools_Gradient_Magnitude.eps}
		\caption{Gradient magnitude of the derivative images for \texttt{few256}.}
		\label{fig:Question_2_Tools_Gradient_Magnitude}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_2_Tools_Hist.eps}
		\caption{Histogram of gradient magnitude of the derivative images for \texttt{few256}.}
		\label{fig:Question_2_Tools_Hist}
	\end{figure}
		\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.85\columnwidth]{Question_2_Tools_Threshold.eps}
		\caption{Gradient magnitude of the derivative images with different threshold for \texttt{few256}. The threshold in the case of simple difference operator is 27.2. 13.6 for central difference operator. 32.7 for Roberts cross edge operator. 92.7 for Sobel operator.}
		\label{fig:Question_2_Tools_Threshold}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_2_Godthem_Gradient_Magnitude.eps}
		\caption{Gradient magnitude of the derivative images for \texttt{godthem256}.}
		\label{fig:Question_2_Godthem_Gradient_Magnitude}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_2_Godthem_Hist.eps}
		\caption{Histogram of gradient magnitude of the derivative images for \texttt{godthem256}.}
		\label{fig:Question_2_Godthem_Hist}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.85\columnwidth]{Question_2_Godthem_Threshold.eps}
		\caption{Gradient magnitude of the derivative images with different threshold for \texttt{godthem256}. The threshold in the case of simple difference operator is 29.1. 14.6 for central difference operator. 35.2 for Roberts cross edge operator. 103 for Sobel operator.}
		\label{fig:Question_2_Godthem_Threshold}
	\end{figure}
	\begin{itemize}
		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} Is it easy to find a threshold that results in thin edges? Explain why or why not!
			\par It is not easy to find a threshold that results in thin edges at the most time.
			\begin{itemize}
				\item Applying a threshold to the whole image where the brightness for all edges are different would obtain thin edges for some objects while other edges could disappear or be too thick.
				\item Different operator would give different result as illustrated in Figure \ref{fig:Question_2_Tools_Gradient_Magnitude}. So the threshold for each operator should be different.
				\item Noise with big magnitude could also make it hard to find a threshold that results in thin edges.
			\end{itemize}
			\par However, in the case of \texttt{few256}, the histogram, Figure \ref{fig:Question_2_Tools_Hist}, accumulated to the left border of the histogram pretty well, so we can set the threshold based on the bin edges of the first cluster of the histogram. The threshold in the case of simple difference operator is 27.2. 13.6 for central difference operator. 32.7 for Roberts cross edge operator. 92.7 for Sobel operator.
		\begin{figure}[!ht]
			\centering
			\includegraphics[width=0.9\columnwidth]{Question_2_Tools_Gaussian_2-25.eps}
			\caption{Smoothed gradient magnitude using Gaussian filter with $\sigma^{2}=2.25$ for \texttt{few256}.}
			\label{fig:Question_2_Tools_Gaussian_2-25}
		\end{figure}
		\begin{figure}[!ht]
			\centering
			\includegraphics[width=0.9\columnwidth]{Question_2_Tools_Gaussian_2-25_Hist.eps}
			\caption{Histogram for smoothed gradient magnitude using Gaussian filter with $\sigma^{2}=2.25$ for \texttt{few256}.}
			\label{fig:Question_2_Tools_Gaussian_2-25_Hist}
		\end{figure}
		\begin{figure}[!ht]
			\centering
			\includegraphics[width=0.85\columnwidth]{Question_2_Tools_Gaussian_2-25_Threshold.eps}
			\caption{Smoothed gradient magnitude using Gaussian filter with $\sigma^{2}=2.25$ and different threshold for \texttt{few256}. The threshold in the case of simple difference operator is 25.4. 12.7 for central difference operator. 16 for Roberts cross edge operator. 89.9 for Sobel operator.}
			\label{fig:Question_2_Tools_Gaussian_2-25_Threshold}
		\end{figure}
		\begin{figure}[!ht]
			\centering
			\includegraphics[width=0.9\columnwidth]{Question_2_House_Gaussian_2-25.eps}
			\caption{Smoothed gradient magnitude using Gaussian filter with $\sigma^{2}=2.25$ for \texttt{godthem256}.}
			\label{fig:Question_2_House_Gaussian_2-25}
		\end{figure}
		\begin{figure}[!ht]
			\centering
			\includegraphics[width=0.9\columnwidth]{Question_2_House_Gaussian_2-25_Hist.eps}
			\caption{Histogram for smoothed gradient magnitude using Gaussian filter with $\sigma^{2}=2.25$ for \texttt{godthem256}.}
			\label{fig:Question_2_House_Gaussian_2-25_Hist}
		\end{figure}
		\begin{figure}[!ht]
			\centering
			\includegraphics[width=0.85\columnwidth]{Question_2_House_Gaussian_2-25_Threshold.eps}
			\caption{Smoothed gradient magnitude using Gaussian filter with $\sigma^{2}=2.25$ and different threshold for \texttt{godthem256}. The threshold in the case of simple difference operator is 23.4. 11.7 for central difference operator. 16 for Roberts cross edge operator. 93.4 for Sobel operator.}
			\label{fig:Question_2_House_Gaussian_2-25_Threshold}
		\end{figure}
		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} Does smoothing the image help to find edges?
			\par Yes, smoothing the image helps to find edges. Smoothing would remove noise from the image, thus making the detected ``edges'' are more likely to be real edges.
			\par However, smoothing the image with Gaussian filter could blur the edges, thus making it difficult to detect the edges with difference operators and find a threshold. This can also be seen from the histograms of the images after smoothing which are more shewed when compared to the gradient magnitude with no Gaussian smoothing.
	\end{itemize}

\section*{4\hspace{0.5cm}Computing differential geometry descriptors}
	\par The images after applying Gaussian filter to \texttt{godthem256} with different value of $\sigma^{2}$ is illustrated in Figure \ref{fig:Question_4_House_Gaussian}. The second order derivative of \texttt{godthem256} after Gaussian smoothing is shown in Figure \ref{fig:Question_4_House_Second_Order} and the third order derivative is shown in Figure \ref{fig:Question_4_House_Third_Order}. Also, the third order derivative of \texttt{few256} after Gaussian smoothing is shown in Figure \ref{fig:Question_4_Tools_Third_Order}.
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_4_House_Gaussian.eps}
		%\scalebox{0.9}{\input{Question_4_House_Gaussian.tex}}
		\caption{Gaussian smoothing for \texttt{godthem256} with different scale.}
		\label{fig:Question_4_House_Gaussian}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_4_House_Second_Order.eps}
		%\scalebox{0.9}{\input{Question_4_House_Second_Order.tex}}
		\caption{Second order derivative for \texttt{godthem256} after Gaussian smoothing.}
		\label{fig:Question_4_House_Second_Order}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_4_House_Third_Order.eps}
		%\scalebox{0.9}{\input{Question_4_House_Third_Order.tex}}
		\caption{Third order derivative for \texttt{godthem256} after Gaussian smoothing.}
		\label{fig:Question_4_House_Third_Order}
	\end{figure}
	\begin{figure}[!ht]
		\centering
		\includegraphics[width=0.9\columnwidth]{Question_4_Tools_Third_Order.eps}
		%\scalebox{0.9}{\input{Question_4_Tools_Third_Order.tex}}
		\caption{Third order derivative for \texttt{few256} after Gaussian smoothing.}
		\label{fig:Question_4_Tools_Third_Order}
	\end{figure}
	\begin{itemize}
		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} What can you observe? Provide explanation based on the generated images.
			\par From Figure \ref{fig:Question_4_House_Gaussian}, we can observe that, the larger the scale is (which means the larger the variance of the Gaussian white noise is), the more blurry and the less noisy the image becomes. This is also true when comes to the result of the edge detection.
			\par However, higher variance of Gaussian filter also blurs the edges and making it harder to determine the real position of the edges. Also, from Figure \ref{fig:Question_4_House_Second_Order}, \ref{fig:Question_4_House_Third_Order}, and \ref{fig:Question_4_Tools_Third_Order}, we can see that Gaussian filter with higher variance would make the edges seem to be thickened thus losing the accuracy.

		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} Assemble the results of the experiment above into an illustrative collage with the \texttt{subplot} command. Which are your observations and conclusions?
			\par As stated at the beginning of this section, the images are shown in Figure \ref{fig:Question_4_House_Gaussian}, \ref{fig:Question_4_House_Second_Order}, \ref{fig:Question_4_House_Third_Order}, and \ref{fig:Question_4_Tools_Third_Order}, where the conclusion could be extracted: the bigger the variance of the Gaussian filter applied to the image is, the less noise will appear in the high order derivative while the less accurate the edges are found.

		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} How can you use the response from $\widetilde{L}_{vv}$ to detect edges, and how can you improve the result by using $\widetilde{L}_{vvv}$?
			\par The local maximum and local minimum of the gradient magnitude would be reached when $\widetilde{L}_{vv}=0$, however, if we want to find edges more accurately, we only want the local maximum be reserved. So when $(\widetilde{L}_{vv}=0 \cap \widetilde{L}_{vvv}<0)$, the local maximum would be reached. Based on such idea, we can improve the response from $\widetilde{L}_{vv}$ by combining the result of $\widetilde{L}_{vv}$ and $\widetilde{L}_{vvv}$ above. The combining result is shown in Figure \ref{fig:Question_6_House_Lvv_Lvvv} and \ref{fig:Question_6_Tools_Lvv_Lvvv}.
			\begin{figure}[!ht]
				\centering
				\includegraphics[width=0.9\columnwidth]{Question_6_House_Lvv_Lvvv.eps}
				%\scalebox{0.9}{\input{Question_6_House_Lvv_Lvvv.tex}}
				\caption{Combination of $\widetilde{L}_{vv}=0$ and $\widetilde{L}_{vvv}<0$ of \texttt{godthem256}.}
				\label{fig:Question_6_House_Lvv_Lvvv}
			\end{figure}
			\begin{figure}[!ht]
				\centering
				\includegraphics[width=0.9\columnwidth]{Question_6_Tools_Lvv_Lvvv.eps}
				%\scalebox{0.9}{\input{Question_6_House_Lvv_Lvvv.tex}}
				\caption{Combination of $\widetilde{L}_{vv}=0$ and $\widetilde{L}_{vvv}<0$ of \texttt{few256}.}
				\label{fig:Question_6_Tools_Lvv_Lvvv}
			\end{figure}
	\end{itemize}

\section*{5\hspace{0.5cm}Extraction of edge segments}
	\begin{itemize}
		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} Present your best results obtained with \texttt{extractedge} for \texttt{house} and \texttt{tools}.
			\par The best results obtained for \texttt{house} and \texttt{tools} are shown in Figure \ref{fig:Question_7_House_Overlay_Curves} and \ref{fig:Question_7_Tools_Overlay_Curves} respectively. The \texttt{godthem256} is applied by Gaussian smoothing with variance $\sigma^{2}=4$ and threshold $4$. The \texttt{few256} is applied by Gaussian smoothing with variance $\sigma^{2}=4$ and threshold $6$.
			\begin{figure}[!ht]
				\centering
				\includegraphics[width=0.9\columnwidth]{Question_7_House_Overlay_Curves.eps}
				%\scalebox{0.9}{\input{Question_6_House_Lvv_Lvvv.tex}}
				\caption{Best result for \texttt{godthem256}.}
				\label{fig:Question_7_House_Overlay_Curves}
			\end{figure}
			\begin{figure}[!ht]
				\centering
				\includegraphics[width=0.9\columnwidth]{Question_7_Tools_Overlay_Curves.eps}
				%\scalebox{0.9}{\input{Question_6_House_Lvv_Lvvv.tex}}
				\caption{Best result for \texttt{few256}.}
				\label{fig:Question_7_Tools_Overlay_Curves}
			\end{figure}
	\end{itemize}

\section*{6\hspace{0.5cm}Hough transform}
\subsection*{6.1\hspace{0.5cm}Hints and practical advice}
	\begin{itemize}
		\item\addtocounter{Counter}{1}\textbf{????? Question \arabic{Counter}:} Identify the correspondences between the strongest peaks in the accumulator and line segments in the output image. Doing so convince yourself that the implementation is correct. Summarize the results in one or more figures.
			\par The strongest peaks in the accumulator should be the longest and the most obvious line segments in the edge plot.

		\item\addtocounter{Counter}{1}\textbf{Question \arabic{Counter}:} How do the results and computational time depend on the number of cells in the accumulator?
			\par The accuracy of the results depends on the resolution of the accumulator since the larger of \texttt{ntheta} and \texttt{nrho}, the more accurate the points and curves would be presented in the accumulator thus the intersection points would be easier to find its real position in accumulator thus finding accurate line in spatial domain. However, high resolution could also lead to more local maximum, thus giving multiple lines for a single edge we would like to obtain from the image.
			\par When the resolution is higher, it would take much more computational time. The time complexity could be $O(n^{2})$, where $n$ is the resolution in $x$ or $y$ direction.
	\end{itemize}

\subsection*{6.2\hspace{0.5cm}Choice of accumulator incrementation function}
	\begin{itemize}
		\item\addtocounter{Counter}{1}\textbf{????? Question \arabic{Counter}:} How do you propose to do this? Try out a function that you would suggest and see if it improves the results. Does it?
			\par I would like to try some monotonically increasing function like $\log$ or square root functions. The increasing rate of such functions becomes smaller when the magnitude becomes larger.
	\end{itemize}

% Template
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{figure}[H]
%	\centering
%	\scalebox{0.9}{\input{test.tex}}
%	\caption{Test.}
%	\label{fig:Test}
%\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{figure}[!ht]
%	\footnotesize
%	\centering
%	\begin{subfigure}[t]{.32\linewidth} % .32 for three polts .49 for two plots
%	\includegraphics[width=\columnwidth]{Linearity_F.eps}
%	\caption{Image F}
%	\label{fig:F}
%	\end{subfigure}
%	\begin{subfigure}[t]{.32\linewidth} % .32 for three polts
%	\includegraphics[width=\columnwidth]{Linearity_G.eps}
%	\caption{Image G = F'}
%	\label{fig:G}
%	\end{subfigure}
%	\begin{subfigure}[t]{.32\linewidth} % .32 for three polts
%	\includegraphics[width=\columnwidth]{Linearity_H.eps}
%	\caption{Image H = F + 2 * G}
%	\label{fig:H}
%	\end{subfigure}
%	\caption{Origin images.}
%	\label{fig:origin}
%\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{align*}
%	var_{t = 0.1} &= \begin{bmatrix} 0.0133 & 0.0000 \\ 0.0000 & 0.0133 \end{bmatrix} \\
%	var_{t = 0.3} &= \begin{bmatrix} 0.2811 & 0.0000 \\ 0.0000 & 0.2811 \end{bmatrix} \\
%	var_{t = 1.0} &= \begin{bmatrix} 1.0000 & 0.0000 \\ 0.0000 & 1.0000 \end{bmatrix} \\
%	var_{t = 10.0} &= \begin{bmatrix} 10.0000 & 0.0000 \\ 0.0000 & 10.0000 \end{bmatrix} \\
%	var_{t = 100.0} &= \begin{bmatrix} 100.0000 & 0.0000 \\ 0.0000 & 10.0000 \end{bmatrix}
%\end{align*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \lstinputlisting{x.m}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fphw Assignment
% LaTeX Template
% Version 1.0 (27/04/2019)
%
% This template originates from:
% https://www.LaTeXTemplates.com
%
% Authors:
% Class by Felipe Portales-Oliva (f.portales.oliva@gmail.com) with template
% content and modifications by Vel (vel@LaTeXTemplates.com)
%
% Template (this file) License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%    PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[
    12pt, % Default font size, values between 10pt-12pt are allowed
    %letterpaper, % Uncomment for US letter paper size
    %spanish, % Uncomment for Spanish
]{fphw}

% Template-specific packages
\usepackage[utf8]{inputenc} % Required for inputting international characters
\usepackage[T1]{fontenc} % Output font encoding for international characters
\usepackage{fontspec,unicode-math} % Required for using utf8 characters in math mode
\usepackage{parskip}  % To add extra space between paragraphs
% \usepackage{mathpazo} % Use the Palatino font
\usepackage{graphicx} % Required for including images
\usepackage{booktabs} % Better horizontal rules in tables
\usepackage{hyperref} % For links (both internal and external)
% \usepackage{listings} % Required for insertion of code
\usepackage{enumerate}% To modify the enumerate environment
\usepackage{cleveref} % Better \ref command -> \cref
\usepackage{import}   % This 4 packages and the command allow importing pdf
\usepackage{xifthen}  % figures generated with inkscape
\usepackage{pdfpages} % Source: https://castel.dev/post/lecture-notes-2/
\usepackage{mathtools}
\usepackage{wrapfig}
\usepackage{cancel}
\usepackage{transparent}
\newcommand{\incfig}[1]{%
    \def\svgwidth{0.95\columnwidth}
    \small
        \import{./images/}{#1.pdf_tex}
}

\setlength{\parindent}{15pt}
\setlength{\headheight}{22.66pt}

%----------------------------------------------------------------------------------------
%    ASSIGNMENT INFORMATION
%----------------------------------------------------------------------------------------

\title{Task 3 \\ Convenient Coordinates} % Assignment title

\author{Emilio Domínguez Sánchez} % Student name

\date{March 16th, 2021} % Due date

\institute{University of Murcia \\ Faculty of Mathematics} % Institute or school name

\class{Geometría Global de Superficies} % Course or class name

\professor{Dr. Luis J. Alías Linares} % Professor or teacher in charge of the assignment

%----------------------------------------------------------------------------------------
%    Definitions
%----------------------------------------------------------------------------------------

\usepackage{physics}
\newcommand{\R}{\mathbb{R}}
\newcommand{\inner}[2]{\left\langle #1, \; #2 \right\rangle}

\DeclareDocumentCommand\covariantderivative{ s o m g d() }
{ % Covariant derivative
	% s: star for \flatfrac flat covariant derivative
	% o: optional n for nth covariant derivative
	% m: mandatory (x in Df/dx)
	% g: optional (f in Df/dx)
	% d: long-form D/dx(...)
	\IfBooleanTF{#1}
	{\let\fractype\flatfrac}
	{\let\fractype\frac}
	\IfNoValueTF{#4}
	{
		\IfNoValueTF{#5}
		{\fractype{D \IfNoValueTF{#2}{}{^{#2}}}{\diffd #3\IfNoValueTF{#2}{}{^{#2}}}}
		{\fractype{D \IfNoValueTF{#2}{}{^{#2}}}{\diffd #3\IfNoValueTF{#2}{}{^{#2}}} \argopen(#5\argclose)}
	}
	{\fractype{D \IfNoValueTF{#2}{}{^{#2}} #3}{\diffd #4\IfNoValueTF{#2}{}{^{#2}}}}
}
\DeclareDocumentCommand\cdv{}{\covariantderivative} % Shorthand for \covariantderivative

\begin{document}

\maketitle % Output the assignment title, created automatically using the information in the custom commands above

%----------------------------------------------------------------------------------------
%    ASSIGNMENT CONTENT
%----------------------------------------------------------------------------------------

\section*{Problem}

\begin{problem}
    Let $p \in S$ be a point of a regular surface $S$
    and $\qty{e_1, e_2}$ an orthonormal basis of $T_pS$.
    Consider $X(u, v) = \exp_p(ue_1 + ve_2)$,
    a normal coordinate system centered in $p$
    that is defined over a neighbourhood $U$ of $(0, 0) \in \R^2$.
    Prove that the following statements are true.

    \begin{enumerate}
        \item For any $v = ae_1 + be_2 \in T_pS$, $v \ne 0$,
        the coordinate expression of the maximal geodesic $γ_v(t)$ is
        $(u(t), v(t)) = (at, bt)$.

        \item The Christoffel symbols $\qty{Γ_{ij}^k : 1 \le i,j,k \le 3}$
        and the partial derivatives of $E$, $F$ and $G$ are zero in $(0, 0)$.
    \end{enumerate}
\end{problem}

%----------------------------------------------------------------------------------------

\subsection*{Answer}

    By the homogenity of geodesics and the definition of the exponential function,
$γ_v(t) = γ_{tv}(1) = \exp_p(tv) = \exp_p(tae_1 + tbe_2)$.
Because $X$ has to be inyective,
$(u(t), v(t)) = t(a, b)$ is the coordinates expression of $γ_v$.
Thanks to this simple expression,
the differential equation for a geodesic\footnote{
    For simplicity,
    we have omitted the point of evaluation $(u, v)$ of the Christoffel symbols.
},
\begin{equation*}
    \begin{cases}
        u'' + (u')^2Γ^1_{11} + 2u'v'Γ^1_{12} + (v')^2Γ^1_{22} = 0 \\
        v'' + (u')^2Γ^2_{11} + 2u'v'Γ^2_{12} + (v')^2Γ^2_{22} = 0
    \end{cases},
\end{equation*}
becomes
\begin{equation*}
    \begin{cases}
        a^2Γ^1_{11} + 2abΓ^1_{12} + b^2Γ^1_{22} = 0 \\
        a^2Γ^2_{11} + 2abΓ^2_{12} + b^2Γ^2_{22} = 0
    \end{cases}.
\end{equation*}
Given that $v$ is arbitrary and that
$X(0, 0) = p = γ_v(0)$ is a point common to any $γ_v$,
we can conclude that all the Christoffel symbols are $0$ in $(0, 0)$.
That is,
substituting with $v = e_1$ gives $Γ^1_{11}(0, 0), Γ^2_{11}(0, 0) = 0$,
substituting with $v = e_2$ gives $Γ^1_{22}(0, 0), Γ^2_{22}(0, 0) = 0$,
and knowing that,
substituting with $v = e_1 + e_2$ gives $Γ^1_{12}(0, 0), Γ^2_{12}(0, 0) = 0$.
Ultimately, writing
\begin{equation*}
    \mqty(
        \frac{E_u}{2} & \frac{E_v}{2} & F_v - \frac{G_u}{2} \\
        F_u - \frac{E_v}{2} & \frac{G_u}{2} & \frac{G_v}{2}
    ) =
    \mqty(E & F \\ F & G)
    \mqty(Γ^1_{11} & Γ^1_{12} & Γ^1_{22} \\ Γ^2_{11} & Γ^2_{12} & Γ^2_{22}) = 0,
\end{equation*}
we can conclude that the partials of $E$, $F$ and $G$ are $0$ in $(0, 0)$ as well.

%----------------------------------------------------------------------------------------

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Infinite Offset Paradox with \texttt{tikz}}
\author{Leonard Kleinhans}
\date{January 2017}
\usepackage[a4paper, left=2cm, right=2cm, top=3cm]{geometry}

\usepackage{tikz}
\usepackage{ifthen}
\usepackage{float}

\usetikzlibrary{decorations.pathreplacing}

\newcommand{\infiniteoffsetparadox}[3]{
   \begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!40,minimum size=20}]
% number of rectangles
\def \count {#1}
% print fraction for the first \print rectangles
\def \print {#2}
% color overlap for first \overlap rectangles
\def \overlap {#3}
% rectangle width
\def \a {4}
% rectangle height
\def \b {1}
% rectangle start offset in grid
\def \s {1}
% offset of nth rectangle
\def \o {0}
\pgfmathsetmacro{\c}{\count-1}
\pgfmathsetmacro{\overlap}{\overlap-1}
\pgfmathsetmacro{\ocolor}{0}
% calculate offset for coloring
\foreach \cy in {0,...,\overlap} {
    \pgfmathsetmacro{\oocolor}{\ocolor+((2*(\cy+1))^(-1)*\a)}
    \global\let\ocolor=\oocolor
}
\foreach \y in {0,...,\c} {
    \draw
        (\s+\o,    \s-\y*\b) rectangle
        (\s+\a+\o, \s-\b-\y*\b);
    % calculate new offset
    \pgfmathsetmacro{\oo}{\o+((2*(\y+1))^(-1)*\a)}
    \pgfmathsetmacro{\f}{2*\y+2}
    % print fraction with curly brace
    \ifthenelse{\y<\print}{
    \draw [decorate, line width=.5pt, decoration={brace, mirror}] (\s+\o, \s-\y*\b-1.1*\b) --  (0.99*\s+0.99*\oo, \s-\y*\b-1.1*\b) node [midway, below=3pt]
    {\footnotesize$\frac{1}{\pgfmathprintnumber\f}$};
    };{};

    % add color
    \ifthenelse{\NOT{\y>\overlap}}{
        \pgfmathsetmacro{\rectend}{min(\s+\ocolor,\s+\o+\a)}
        % for overlap
        \fill [overlapcolor] (\s+\o,    \s-\y*\b) rectangle (\rectend, \s-\b-\y*\b);
        % for setoff
        \fill [setoffcolor] (\rectend,    \s-\y*\b) rectangle (\s+\o+\a, \s-\b-\y*\b);
    };{};
    % update offset
    \global\let\o=\oo
}

\end{tikzpicture}
}

\begin{document}

\maketitle

\section{Introduction}
This document contains a fully customizable function that generates illustrations of the Infinite Offset Paradox. The figures are generated using \texttt{tikz}. The provided command \texttt{infiniteoffsetparadox} takes three arguments, that is first the number of rectangles to draw, the number of labels to generate and the number of rectangles that get colored regarding overlap and offset. The next section provides example outputs.

\section{Examples}
% define color
\definecolor{overlapcolor}{HTML}{801A15}
\definecolor{setoffcolor}{HTML}{537614}

\begin{figure}[H]
    \centering
    \infiniteoffsetparadox{3}{2}{2}
    \caption{Illustration of the Infinite Offset Paradox with parameters $(3,2,2)$}
\end{figure}

% use different colors for second figure
\definecolor{overlapcolor}{HTML}{A5CA00}
\definecolor{setoffcolor}{HTML}{085286}

\begin{figure}[H]
    \centering
    \infiniteoffsetparadox{5}{1}{4}
    \caption{Illustration of the Infinite Offset Paradox with parameters $(5,1,4)$}
\end{figure}
\end{document}


\section{PI for the man in the road}
\subsection{}

\begin{frame}
\frametitleTC{Foreword}
\framesubtitleTC{PI (Proportional plus Integral) control as an intuitive idea}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item We now introduce PI control in a totally intuitive manner, ie., the way\\
       ``the man in the road'' would do --- and incidentally, the way somebody\\
       says it was initially invented (we are not discussing this).
 \item The purpose of this part of our activity is twofold:
       \begin{itemize}[<+-| alert@+>]
       \item show that PI control is intuitive indeed,
       \item but also that intuition without theory is often misleading.
       \end{itemize}
 \item Exercise: as we proceed, try to spot the flaws (not necessarily true ``errors''\\
       but symptoms of a partial viewpoint) in the reasoning by the man\\
       in the road and take note of them...
 \item[] \vspace{-0.75mm}...and then, when we re-visit the matter formally later on, check\\
       your notes to see if you spotted \underline{all} such flaws.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Intuition \# 1}
\framesubtitleTC{Proportional control}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The man in the road says: \blue{``the farther the controlled variable $y$ is\\
       from the reference $w$, the more intense the control action $u$ has to be''}.
 \item \vfill Naming $e$ the error $w-y$, this introduces the \TC{Proportional (P) action}
       \begin{displaymath}
        u_P(k) = K_P \, e(k),
       \end{displaymath}
       where $K_P$ is a configuration parameter.
 \item Seems definitely a good idea.
 \item However $u_P$ is nonzero only if so is $e$, hence with P action alone
       \begin{itemize}[<+-| alert@+>]
       \item either $y$ can stay at ANY value ($w$ can be anything) with $u=0$,
       \item or one has to accept an error to have a control action.
       \end{itemize}
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Intuition \# 2}
\framesubtitleTC{Introducing an automatic bias}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item Alternatively, a bias can be included in $u$ so as to zero the error:
       \begin{displaymath}
        u(k) = u_P(k)+ u_{bias}.
       \end{displaymath}
 \item But how to compute $u_{bias}$?
 \item The man in the road says: \blue{``automatically; if the error is zero keep it constant\\
       because you found the right value, otherwise increase or decrease it at each step,\\
       proportionally to the error''}.
 \item This means
       \begin{displaymath}
        u_{bias}(k) = u_{bias}(k-1)+ K_{bias} e(k),
       \end{displaymath}
       where $K_{bias}$ is another configuration parameter.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{Intuition \# 3}
\framesubtitleTC{Toward integral control}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The main in the road continues: \blue{``the automatic bias is also consistent with the\\
       idea that if the error does not diminish this means that the system is reluctant\\
       to obey to the control action, hence the said action has to become stronger\\
       and stronger''}.
 \item In fact, $u_{bias}(k)$ as just defined, sums each new error weighed by $K_{bias}$, which comes
       to determine the ratio of the control variation \TC{rate} to the value of the error.
 \item We can say that this is \TC{integrating} the error, and name the corresponding control component
       the \TC{Integral (I) action}
       \begin{displaymath}
        u_I(k) = u_I(k-1)+ K_I e(k),
       \end{displaymath}
        with $K_I$ as the second controller configuration parameter besides $K_P$.
 \end{itemize}
\end{frame}

\begin{frame}
\frametitleTC{The PI control law}
\framesubtitleTC{}
\myPause
 \begin{itemize}[<+-| alert@+>]
 \item The control signal is the sum of the P and the I action:
       \begin{displaymath}
        \left\{\begin{array}{rcl}
         u_P(k) &=& K_P \, e(k) \\
         u_I(k) &=& u_I(k-1)+ K_I e(k) \\
         u(k)   &=& u_P(k)+u_I(k)
        \end{array}\right.
       \end{displaymath}
 \item \vfill Intuition basically stops here.
 \item To understand how to give a value to $K_P$ and $K_I$ (or to the more\\
       comfortable equivalent parameters we shall define in the following)\\
       we must resort to theory.
 \end{itemize}
\end{frame}


\documentclass[epsfig,10pt,fullpage]{article}

\newcommand{\LabNum}{3}
\newcommand{\CommonDocsPath}{../../common/docs}
\input{\CommonDocsPath/preamble.tex}

\begin{document}

\centerline{\huge OpenCL}
~\\
\centerline{\huge Laboratory Exercise \LabNum}
~\\
\centerline{\large Lane Detection for Autonomous Driving}
~\\

This exercise introduces you to the Hough transform and how it can be used for detecting lanes in an autonomous driving application.
The exercise uses the Canny edge detector that you implemented in Exercise 2.

\section*{Lane Detection Using Line Detection}

Lane detection is a technique that uses a vehicle's sensor data to
determine the vehicle's position relative to the lanes and/or road boundaries.
The position information is used to make steering adjustments and to change lanes, which
makes lane detection a fundamental part of an autonomous vehicle.

~\\
There are many different lane detection schemes that make use a variety of different sensor combinations.
In this exercise, we will implement a simple scheme that uses a camera attached to the front of the vehicle.
Since lane and road boundaries tend to appear as lines in the camera feed, we will accomplish lane detection
by detecting the lines in the image.
Once we have the positions and angles of the lines, we can use our knowledge of the perspective of the camera
to infer the vehicle's position relative to the lane boundaries.
To detect the lines, we will employ edge detection and a technique called the {Hough transform}.
Figures~\ref{fig:road},~\ref{fig:road_edges}, and~\ref{fig:road_lines} show the application of edge detection
followed by the Hough transform to detect lines in an image.

~\\
\begin{figure}[h]
\centering
\begin{minipage}[b]{0.32\textwidth}
	\includegraphics[width=\textwidth]{figures/fig_road.png}
	\caption{Original image.}
	\label{fig:road}
\end{minipage}
\hfill
\begin{minipage}[b]{0.32\textwidth}
	\includegraphics[width=\textwidth]{figures/fig_road_edges.png}
	\caption{Edge-detected image.}
	\label{fig:road_edges}
\end{minipage}
\hfill
\begin{minipage}[b]{0.32\textwidth}
	\includegraphics[width=\textwidth]{figures/fig_road_lines.png}
	\caption{Line-detected image.}
	\label{fig:road_lines}
\end{minipage}
\end{figure}

\section*{The Hesse Normal Representation for Lines}

Before starting our discussion of line detection, we must first consider the system we will use to describe lines in an image.
For efficiency, we want to describe a line using the fewest parameters possible.
A commonly used line representation is $y = mx + b$ which describes lines using two parameters \textit{m} and \textit{b}.
The problem with this system is that it cannot describe vertical lines as m would assume an undefined value.
The line representation that we will use is known as the Hesse normal form. This system uses
two parameters ($\rho$, $\theta$) to describe a line \textbf{A} as follows:

\begin{enumerate}
\item $\rho$ is the distance from the origin (the center of the image) to the closest point on A.
\item $\theta$ is the angle between the x axis and the normal line connecting the origin to the closest point on A.
\end{enumerate}

To account for all possible lines in an image, the parameters range: $0 \leq \theta < 180^\circ$ and

$-\sqrt(image\_height^2+image\_width^2)/2 \leq \rho \leq \sqrt(image\_height^2+image\_width^2)/2$

Examples of lines and their ($\rho$, $\theta$) parameters are shown in Figure~\ref{fig:hesse_normal_examples}.
The line being described is displayed in red, and the normal line is displayed in magenta.
Take note that $\rho$ can be a negative value which extends the normal line in the reverse direction from the angle
denoted by $\theta$, as shown in Examples 2, 3, and 4. The line shown in Example 3 is
parameterized as $(-7, 0^\circ)$ and not $(7, 180^\circ)$, due to the valid range of theta: $0 \leq \theta < 180^\circ$.
For the same reason, the line in Example 4 is parameterized as $(-7, 90^\circ)$ and not $(7, 270^\circ)$.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.78]{figures/fig_hesse_normal_examples}
   \end{center}
   \caption{Lines (in red) and their ($\rho$, $\theta$) parameters.}
	\label{fig:hesse_normal_examples}
\end{figure}

Lines described in ($\rho$, $\theta$) form can be mapped to pixels in (x,y) coordinates and vice-versa using the equation $\rho = x*cos(\theta) + y*sin(\theta)$.
Note that a pixel can lie on an infinite number of lines, as there are the infinite $\theta$ values from $0^\circ$ to $180^\circ$.
Figure~\ref{fig:pixel_to_lines} shows a pixel at (x,y) coordinate (8,5) and some of the lines that it is positioned upon.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.95]{figures/fig_pixel_to_lines}
   \end{center}
   \caption{A pixel in an image can lie on an infinite number of lines, including the four lines shown.}
	\label{fig:pixel_to_lines}
\end{figure}

\pagebreak

\section*{The Hough Transform}

The Hough transform takes an edge-detected input image and transforms it to a 2-dimensional integer array called
the accumulator.
The two dimensions of the accumulator are indexed by $\rho$ and $\theta$ respectively,
and each element stores how many edge pixels in the input image
are positioned along the corresponding line ($\rho$, $\theta$).
%Effectively, the accumulator values measure the length of each line that is present in the input image.
The most prominent lines in the input image can then be inferred by finding the lines with the
highest values in the accumulator.
The process by which the Hough transform calculates the values in the accumulator is described below.

~\\
The accumulator elements are first initialized to zero, then \textit{accumulate} in value as the Hough transform
algorithm sweeps across the pixels of the input image.
Each edge pixel encountered by the algorithm contributes +1 to the accumulator values
of all lines on which it is positioned.
Since there are theoretically an infinite number of such lines, we must select a resolution for $\rho$ and $\theta$ to
limit the number of lines being tracked by the algorithm.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.83]{figures/fig_line_20x14}
   \end{center}
   \caption{Applying the Hough transform ($\theta$ resolution = $45^\circ$) on a 20x14 image with five edge pixels.}
	\label{fig:hough_demonstration}
\end{figure}

As a demonstration, let us choose a $\theta$ resolution of $45^\circ$ and a $\rho$ resolution of 1 pixel to
transform a 20x14 image. This requires an accumulator array, \texttt{accum[RHOS][THETAS]}, with dimensions
RHOS = 24, and THETAS = 4. Integer values of $\rho$ range $-12 \leq \rho < 12$,
and $\theta$ values range $0^\circ, 45^\circ, 90^\circ,$ and $135^\circ$.
Figure~\ref{fig:hough_demonstration} shows the operations involved in
transforming a 20x14 edge-detected image with five edge pixels in the top-right corner.
The five pixels form the line $(8, 45^\circ)$, which is shown as the bold red line.
Each pixel contributes +1 to the four accumulator indices corresponding to the four possible $\theta$ values.
The $\rho$ index corresponding to each $\theta$ is calculated using the formula $\rho = x*cos(\theta) + y*sin(\theta)$.
After the Hough transform completes, the accumulator is as shown in Figure~\ref{fig:accumulator}. As
expected, \texttt{accum[8][45$^\circ$]} has the highest value of 5, since there were five pixels along it in the input image.

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.8]{figures/fig_accumulator}
   \end{center}
   \caption{The accumulator resulting from the Hough transform of the image in Figure~\ref{fig:hough_demonstration}.}
	\label{fig:accumulator}
\end{figure}

\subsection*{Extracting Lines from Local Maxima in the Accumulator}

Upon completion of the Hough transform we are left with the task of extracting prominent lines from the accumulator.
The simplest method for extracting the N most prominent lines from the accumulator is to
find the N highest values (global maxima). However, this approach is prone to issues which we will discuss below.

~\\
A source of error when using the Hough transform for line detection is the potential for
the pixels of a single "real" line in an image contributing to multiple adjacent elements in
the accumulator. This could happen if the line was not perfectly straight due to noise, or if
our resolution for $\rho$ or $\theta$ was inadequate. For example, consider transforming an image with the line
(11,$7.5^\circ$) using a $\theta$ resolution of $1^\circ$. During the transform some of the pixels in the line may
contribute to \texttt{accum[11][7]} while others may contribute to \texttt{accum[11][8]} resulting in
multiple nearby accumulator elements with similarly high values.
After the transform if we simply searched for the global maxima to detect lines,
we could falsely detect two or more lines resulting from the single real line.

~\\
To eliminate false duplicates, we will extract lines from local maxima in the accumulator rather than global maxima.
Whenever there are multiple high accumulator values in a vicinity we will only consider the one whose
value is highest, and eliminate those neighboring. This approach allows us to detect the single line which is likely the best
fit to the real line in the input image.
Figures~\ref{fig:global_maxima} and \ref{fig:local_maxima} illustrate the differences between global maxima and
local maxima extraction. Note that this approach runs the risk of elliminating lines that actually did exist near eachother
in the input image, but for the purposes of lane detection we can assume lane markings are sufficiently spaced apart for
this to not be an issue.

~\\
Searching for local minima also allows us to search for a fewer lines in total while cover all of the lanes in the image.
Consider an image with two lane markings where one was significantly longer than the other. In this case,
the longer lane may yield multiple accumulator values that are greater than any of the values
for the shorter lane. If we then searched for two global maxima with the expectation of detecting the two lanes,
we would detect the longer lane twice and the shorter lane not at all. Searching for local maxima means that we would
only extract one line corresponding to the longer lane, allowing for the detection of the second lane as long as it was the
second highest local maximum in the accumulator.

\begin{figure}[h]
\centering
\begin{minipage}[b]{0.47\textwidth}
	\includegraphics[width=\textwidth]{figures/fig_road_lines_global_maxima.png}
	\caption{Lines extracted from global maxima.}
	\label{fig:global_maxima}
\end{minipage}
\hfill
\begin{minipage}[b]{0.47\textwidth}
	\includegraphics[width=\textwidth]{figures/fig_road_lines_local_maxima.png}
	\caption{Lines extracted from local maxima.}
	\label{fig:local_maxima}
\end{minipage}
\end{figure}

\subsection*{Elliminating the Horizon and Horizontal Lines}

To improve the accuracy of our lane detector, we will make two simple optimizations to the Hough transform and
line extraction.

~\\
The first optimization aims to prevent detecting lines from objects far in the horizon (such as those detected from hills or clouds). Figure~\ref{fig:local_maxima} shows an example where the upper portion of the image contains the horizon and
the sky. When implementing the Hough transform we will elliminate the horizon by
only going through the bottom half of the edge-detected image when incrementing the accumulator.
%limiting the Y coordinate range of pixels that we sweep through the pixels of the input image during the Hough transform.
Not only will this optimization potentially prevent false lines, it will reduce the runtime of the Hough transform by
skipping over half of the input pixels.

~\\
The second optimization elliminates horizontal lines from being extracted from the accumulator.
This optimization is based on the knowledge that lanes boundaries
will tend to point outward from the camera's perspective (from the bottom of the image towards the top),
meaning that vertical and slanted lines are the ones likely to correspond to lanes.
Figure~\ref{fig:bus} shows a scenario where this optimization is particularly beneficial, as the merging bus contains features
that would result in prominent horizontal lines being detected.
We can implement this optimization in our line extraction stage by ignoring the lines in the accumulator whose $\theta$
indicates a horizontal line ($80^\circ < \theta < 100^\circ$).

\begin{figure}[H]
   \begin{center}
       \includegraphics[scale = 0.3]{figures/fig_bus}
   \end{center}
   \caption{A merging bus contains prominent horizontal lines that may be detected instead of the lane markings.}
	\label{fig:bus}
\end{figure}

\section*{Part 1}

Write a C-language program to implement the Hough transform.
Start with the skeleton code provided in \textit{/design\_files/part1/hough.c}.
Your program should load a 720x540 BMP image, apply edge detection, apply the Hough transform, extract the lines,
overlay the lines onto the original image, then save the resulting image as a BMP file.

~\\
Your Hough transform should use a $\theta$ resolution of $1^\circ$ and a $\rho$ resolution of 2 pixels.
Ellminate the horizon and horizontal lines using the optimizations described in the previous section.
Extract five lines from the five highest local maxima in the accumulator.
A value is considered the local maximum if it is the highest value in the 5x5 square around it (+/- 2 to the $\rho$ and $\theta$ indices).
Overlay the extracted lines onto the original image (as shown in Figure~\ref{fig:local_maxima}), then save it as a BMP file.
Use the images provided in \textit{/design\_files/test\_images/} to test your program.
How closely do the detected lines align to the lane boundaries? For images
where lane detection was less successful, explain potential source(s) of error.

~\\
Record the runtime of the Hough transform across the test images.
Why does the Hough transform runtime vary by image?
Using the average total runtime across the images, calculate framerate at which you processed them.
The total runtime includes edge detection, Hough transform, and line extraction, but not operations
involved with loading and saving the BMP images.
Determine the upper bound on the runtime of the Hough transform and calculate the minimum framerate.

\section*{Part 2}

Create an OpenCL kernel for the Hough transform. Start with the
the skeleton code provided in \textit{/design\_files/part2/}.
Use the same parameters as in Part 1.
To improve performance, use local memory to hold the accumulator array and unroll critical loop(s).
Instantiate your edge detection kernel from Exercise 2 to compile a single \textit{.aocx} image that
contains both the edge detector and the hough transform.

~\\
Skeleton host code is provided in \textit{/design\_files/part2/host/}. You must add to the code so that
the host program first calls the edge detection kernel, waits for it to complete, then feeds the edge detected data
buffer to the hough transform kernel. As in Part 1, your host program should extract five lines from the five
highest local maxima in the accumulator, overlay them onto the original image, and save it as a BMP file.

~\\
Record the runtime of the Hough transform across the test images. How does the runtime compare to your
implementation in Part 1? Using the average total runtime across the images, calculate framerate at which you processed them.
Determine the upper bound on the runtime of the Hough transform and calculate the minimum framerate.

~\\
When implementing the Hough transform kernel, you can use the pragma \texttt{\#pragma ivdep}
to tell the OpenCL compiler that accesses to \texttt{accum} in subsequent loop iterations are free of data dependencies for
better pipelining of the circuit.
The OpenCL compiler would otherwise have difficulty in determining the lack of dependency, as subsequent indices calculated by
the formula $\rho = x*cos(\theta) + y*sin(\theta)$ are seemingly unpredictable.
We as designers however, know that the formula will ensure subsequent accesses to \texttt{accum} will be at different indices,
as we sweep $\theta$ from $0^\circ$ to $179^\circ$ for each edge pixel.

\section*{Part 3}

Improve the performance of your design from Part 2 by using OpenCL pipes to connect the output of your edge-detection kernel
to the input of your Hough transform kernel. Doing this allows your kernels to share data completely inside the FPGA,
rather than using slow external memory as intermediary storage. Use a blocking pipe with a depth of 1.
Compile your piped kernels using the \texttt{aoc} flag \texttt{-profile=all} to enable profiling.
Make the necessary modifications to your host program to support your newly piped kernels.

~\\
After running your application, use the profile monitor to check for kernel stalls.
Do your kernels stall as a result of the blocking pipe? What do the stalls (or lack of stalls)
indicate about the throughput of your edge detection kernel compared to your Hough transform kernel?

~\\
Record the runtime of your piped implemention across the test images. How does the runtime compare to your
implementation in Part 2? Using the average total runtime across the images, calculate framerate at which you processed them.
Determine the upper bound on the runtime of the Hough transform and calculate the minimum framerate.

\input{\CommonDocsPath/copyright.tex}
\end{document}


\documentclass[12pt]{article}
\usepackage[usenames]{color} %used for font color
\usepackage{amsmath, amssymb, amsthm}
\usepackage{wasysym}
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{float}
\usepackage{mathtools}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
\MakeOuterQuote{"}
\graphicspath{ {./} }
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\prob}{\mathbb{P}}
\newcommand{\degrees}{^{\circ}}
\DeclarePairedDelimiter\ceil{\lceil}{\rceil}
\DeclarePairedDelimiter\floor{\lfloor}{\rfloor}

\author{Tianshuang (Ethan) Qiu}
\begin{document}
\title{Math 104, HW10}
\maketitle
\newpage

\section{Q1: Ross 33.7}
\subsection{a}
Let $P$ be an arbitrary partition such that $P=\{a=t_1 < t_2 < ... < t_n = b\}$. Now consider
$$U(f^2,P) - L(f^2,P) = (M[t_1,t_2]-m[t_1,t_2])(t_2-t_1) + ... + (M[t_{n-1},t_n]-m[t_{n-1},t_n])(t_{n}-t_{n-1})$$

Since RHS and LHS has the same partition $P$, each $t_k$ is also the same on the RHS. We consider just $P_1=[t_1, t_2]$:
\newline
Let the $M(f^2,P_1)=f(x_0)^2, m(f^2,P_1)=f(x_1)^2$
$$U(f^2,P_1) - L(f^2,P_1) = (f(x_0)^2-f(x_1)^2)(t_2-t_1) = (f(x_0)+f(x_1))(f(x_0)-f(x_1))(t_2-t_1)$$
Now consider the same partition for $f$:
$$U(f,P_1) - L(f,P_1) =(M(f,P_1)-m(f,P_1))(t_2-t_1)$$
Since $B \geq f(x)$ for all $x \in [a,b]$, we have $2B \geq (f(x_0)+f(x_1))$. Since $M(f,P_1)$ is the maximum of the function over this interval and $m(f,P_1)$ is the minimum, their difference is greater than any other differences in this interval $P_1$, namely $M(f,P_1)-m(f,P_1) \geq (f(x_0)-f(x_1))$.
\newline
Therefore we have $U(f^2,P_1) - L(f^2,P_1) \leq 2B(U(f,P_1) - L(f,P_1))$. Now we can repeat this with all intervals of $[t_k, t_k-1]$ where $2 \leq k \leq n$, thus we have shown that $$U(f^2,P) - L(f^2,P) \leq 2B(U(f,P) - L(f,P))$$ for any partition $P$

\subsection{b}
Since $f$ is integrable, for any $\epsilon > 0$, there exists a partition $P$ such that $U(f,P)-L(f,P)<\epsilon$. Now for any $\epsilon > 0$, choose $\epsilon_0 = \epsilon \times 4B$ where $B$ is the absolute bound for $f$, since $f$ is integrable we find $P_0$ that the difference between the Darboux sums is less than $\epsilon_0$
\newline
Now consider $U(f^2,P_0)-L(f^2,P_0)$, from part(a) we know that $U(f^2,P_0)-L(f^2,P_0) \leq 2B(U(f,P_0)-L(f,P_0)) \leq \frac{\epsilon}{2}<\epsilon$
\newline
Therefore $f^2$ is integrable.

\section{Q1, Ross 33.8}
By our theorem we know that the sum(difference) of two integrable functions is integrable. Therefore we know that $(f+g)$ and $(f-g)$ are integrable. By 33.7 we know that $(f+g)^2$, $(f-g)^2$ are integrable. Now we simply take the difference: $(f+g)^2-(f-g)^2 = 4fg$. We apply the integrability theorem again and we know that this is integrable as well. Thus $fg$ must be integrable.
\newpage

\section{Q2}
\subsection{a}
The function is continuous at decreasing intervals as $|x|$ decreases, it is also continuous at $x=0$. Since $-1 \leq \text{sgn}(x) \leq 1$, $-x \leq f(x)\leq x$ therefore the function converges to $0$ at $x=0$ by squeeze theorem.
\newline
Now for any other point, the continuity breaks when $\sin \frac{1}{x}$ is 0 since the whole expression which was not 0 before suddenly "drops" or "rises" to 0. More rigorously, if $\sin \frac{1}{x} \not = 0$, then sgn$(x)=1$ or $-1$, then $f(x)=x$ or $-x$, which does not have the value $0$ unless $x=0$. Therefore the function is discontinuous at all points where $\sin(\frac{1}{x})=0$, or $\frac{1}{x} = n \pi$ where $n$ is an integer.
Since it is continuous everywhere else, we have that the function is continuous on $[-1,-\frac{1}{\pi}), (-\frac{1}{\pi},-\frac{1}{2\pi})... (\frac{1}{2\pi},\frac{1}{\pi}), (\frac{1}{\pi},1]$

\subsection{b}
Even though $f$ is not piecewise continuous on all of $[-1,1]$, the discontinuities increase near 0. We claim that it is piecewise continuous on $[-1,0)$ and $(0,1]$. Let $a_n$ be the sequence of postive discontinuous points: $a_n = \frac{1}{n\pi}$, and let $b_n=-a_n$. Since $0<a_n < 1/n$ we know that it converges to $0$, by similar logic so does $b_n$.
Let $0<x_0\leq 1$, we know that between each $a_n$ the function is either $x$ or $-x$ which is uniformly continuous. Moreover, we know that since $a_n \to 0$, there is $n \in \N$ such that $a_n < x_0$. Let $n_0$ be the smallest such $n$. Let the first partition be $[x_0, a_{n_0-1}]$, and the last $[a_1,1]$.
The same is true if $-1 \leq x_1 < 0$, let $n_1$ be the smallest $n \in \N$ such that $a_{n} > x_1$. We define our first partiton as $[-1,a_1]$, and the last $[a_{n},x_1]$, between each closed interval the function is uniformly continuous.
\newline
Now for any $\epsilon > 0$, choose $u = \sqrt{\epsilon}/4, v = - u$. As we have shown above the function is integrable in $[-1,v]$ and $[u,1]$. Therefore we only need to consider the interval $[v,u]$. In this interval the greatest value $f$ can take is $u$ when $\text{sgn}(\sin(\frac{1}{x}))=1$ and $f(x)=x$
Similarly the least value it can take is $v$ when $f(x)=-x$
$$U(f,[v,u]) = u(u-v)$$
$$L(f, [v,u]) = v(u-v)$$
Thus $U(f,[v,u])-L(f, [v,u]) = (u-v)^2 = \frac{\epsilon}{16} < \epsilon$. Therefore the function is integrable.
\newpage

\section{Ross 34.2}
\subsection{a}
We first assumes that the function $e^{t^2}$ is the derivative of a function $F(t)$, so by the Fundamental Theorem of Calculus, we simplify the expression into $\lim_{x \to 0} \frac{F(x)-F(0)}{x}$.
Since the denominator approaches 0, we apply L'Hospital's rule and the limit is equal to $\lim_{x \to 0} \frac{F'(x)-F'(0)}{1} = \frac{e^{x^2}(x')-e^0(0')}{1} = 1$
\newline
In the last step, we needed to apply the chain rule and take the derivative of the function inputs, ending with $1-0=1$

\subsection{b}
We first assumes that the function $e^{t^2}$ is the derivative of a function $F(t)$, so by the Fundamental Theorem of Calculus, we simplify the expression into $\lim_{x \to 0} \frac{F(3+h)-F(3)}{h}$.
Since the denominator approaches 0, we apply L'Hospital's rule and the limit is equal to $\lim_{x \to 0} \frac{F'(3+h)-F'(3)}{1} = \frac{e^{(3+h)^2}(3+h)'-e^3(0')}{1} = e^9$
\newpage

\section{Q4}
\subsection{a}
For $x < 0$, $F(x)=0$ since $f(x)=0$
\newline
For $0 \leq x \leq 1$, $F(x)=\frac{1}{2}x^2$ since we have proven the power rule and $\frac{1}{x}x^2$ has a derivative of
$x$.
\newline
For $x > 1$, $F(x) = \frac{1}{2} + 4(x-1)$. The one half comes from $F(1)-F(0)$, and since the function is constant, the upper and lower Darboux sums will be the same for any partition.

\subsection{b}
$F$ is continuous everywhere. In the intervals $x<0, 0<x<1, x>1$ we know $F$ is continuous since their functions are continuous. At $x=0$, let $\epsilon_0 > 0$, pick $\delta_0 = \sqrt{\epsilon_0}$
Let $|x-0|<\delta$, then if $x<0$, $|F(x)-F(0)|=0$, otherwise $|F(x)-F(0)| < \epsilon_0/2-0 < \epsilon$, therefore $F$ is continuous at 0
\newline
At $x=1$, let $\epsilon_1 > 0$, pick $\delta_1 = \sqrt{\epsilon_0}$. If $x>1$

\subsection{c}
$F$ is differentiable at $(-\infty, 1)$. $F'(x)$ for $x < 0$ is $0$ since it is constant. $F'(x)$ on $[0,1]$ is $x$ and since $F'(x)=0$ on both negative and positive sides of $x=0$, therefore $F$ is differentiable at $0$
\newline
For $x>1$, $F'(x)= 4$. Therefore it is also differentiable at $(1, \infty)$. It is not differentiable at $1$ since on the negative side it is $1$, but on the positive it is $4$
\newpage

\section{Ross 34.5}
For each $x \in \R$, limit both $F,f$ to $[x-1,x+2]$. Since $f$ is continuous on this interval and $F$ is its integral, $F$ is differentiable at $x$ by the Fundamental Theorem of Calculus.
\newline
Since the upper bound of the integral computed in $F$ has an upper bound of $x+1$, by the same theorem we know that it is equal to $f(x+1)$ .
\newpage

\section{Ross 34.7}
Let $J$ be the interval $(-\infty, \infty)$, $u:J \to \R $ is defined as $u=x^2$ and $u' = 2x$, and $f: \R \to \R$, $f = -\frac{2}{3}(1-a)^{3/2}$, by the power rule $f' = \sqrt{1-a}$. Now since $U(J) \subseteq \R$, we can apply u-subsitution here.
\newline
Let the integral equal $I$, we know that $2I = \int_0^1 2x \sqrt{1-x^2} dx= \int_0^1 u'(f'\circ u) = \int_{u(0)}^{u(1)}f' = f(1)-f(0) = \frac{2}{3}$
\newline
Now we have $2I = \frac{2}{3}$, so our original integral $I = \frac{1}{3}$
\newpage

\section{Ross 34.12}
Assume that there exists some $f$ where it is not $0$ everywhere that satisfies this integrala. Since $g$ is an arbitrary continuous function and $f$ is continuous, let $g=f$. Now our integral becomes $\int_a^b f^2(x)dx = 0$
Now since $f^2(x) \geq 0$, we know that $f^2(x) = 0$ everywhere in our interval.
\newline
To see that it is true, we let $f^2(x_0) > 0$, then by continuity there exists $\delta > 0$ such that $|f^2(x)-f^2(x_0)|< f^2(x_0)/2$, then in this interval the integral is at least $\delta f^2(x_0)/2$, thus the whole integral must be greater than or equal to it. Since the expression is positive, there is no way for the integral to be 0. Thus $x_0$ does not exist and $f^2(x)=0$ in our interval.
\newline
From our conclusion above it is simple to see that $f(x)=0$ everywhere for $f^2(x)=0$, therefore we have reached a contradiction, and $f(x)=0$ for all $x \in [a,b]$
\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Random variables}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Random variables}

\begin{itemize}

\item A \hl{random variable} is a numeric quantity whose value depends on the outcome of a random event
\begin{itemize}
\item We use a capital letter, like $X$, to denote a random variable
\item The values of a random variable are denoted with a lowercase letter, in this case $x$
\item For example, $P(X = x)$
\end{itemize}

\item There are two types of random variables:
\begin{itemize}
\item \hl{Discrete random variables} often take only integer values
\begin{itemize}
\item Example: Number of credit hours, Difference in number of credit hours this term vs last
\end{itemize}
\item \hl{Continuous random variables} take real (decimal) values
\begin{itemize}
\item Example: Cost of books this term, Difference in cost of books this term vs last
\end{itemize}
\end{itemize}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Expectation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Expectation}

\begin{itemize}

\item We are often interested in the average outcome of a random variable.

\item We call this the \hl{expected value} (mean), and it is a weighted average of the possible outcomes
\formula{\[\mu = E(X) = \sum_{i = 1}^k x_i ~ P(X = x_i)\]}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Expected value of a discrete random variable}

\dq{In a game of cards you win \$1 if you draw a heart, \$5 if you draw an ace (including the ace of hearts), \$10 if you draw the king of spades and nothing for any other card you draw. Write the probability model for your winnings, and calculate your expected winning.}

\pause

\begin{center}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{l | c | c | c }
Event		& $X$ 		& $P(X)$        		& $X ~ P(X)$ \\
\hline
Heart (not ace)	& $1$		& $\frac{12}{52}$	& $\frac{12}{52}$ \\
Ace			& $5$		& $\frac{4}{52}$	& $\frac{20}{52}$ \\
King of spades	& $10$		& $\frac{1}{52}$	& $\frac{10}{52}$ \\
All else		& $0$		& $\frac{35}{52}$	& $0$ \\
\hline
Total			&			&				& $E(X) = \frac{42}{52} \approx 0.81$
\end{tabular}

\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Expected value of a discrete random variable (cont.)}

Below is a visual representation of the probability distribution of winnings from this game:

\begin{center}
\includegraphics[width=0.8\textwidth]{3-4_random_variables/figures/card_game/card_game}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Variability in random variables}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Variability}

We are also often interested in the variability in the values of a random variable.

\formula{
\[ \sigma^2 = Var(X) = \sum_{i = 1}^k (x_i - E(X))^2 P(X = x_i) \]
\[ \sigma = SD(X) = \sqrt{Var(X)} \]
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Variability of a discrete random variable}

\dq{For the previous card game example, how much would you expect the winnings to vary from game to game?}

\vspace{2mm}
\only<2->{
{\footnotesize
\begin{center}
\renewcommand{\arraystretch}{2}
\begin{tabular}{c | c | c | l | p{4cm}}
$X$ & $P(X)$         & $X ~ P(X)$      & \multicolumn{1}{c|}{$(X - E(X))^2$}  & \multicolumn{1}{c}{$P(X) ~ (X - E(X))^2$}  \\
\hline
1 & $\frac{12}{52}$  & $1 \times \frac{12}{52} = \frac{12}{52}$ & $(1 - 0.81)^2 = 0.0361$ &  $\frac{12}{52} \times 0.0361 = 0.0083$ \\
\hline
5 & $\frac{4}{52}$   & $5 \times \frac{4}{52} = \frac{20}{52}$ & $(5 - 0.81)^2 = 17.5561$  & $\frac{4}{52} \times 17.5561 = 1.3505$ \\
\hline
10  & $\frac{1}{52}$ & $10 \times \frac{1}{52} = \frac{10}{52}$  & $(10 - 0.81)^2 = 84.4561$   & $\frac{1}{52} \times 84.0889 = 1.6242$ \\
\hline
0 & $\frac{35}{52}$  & $0 \times \frac{35}{52} = 0$  & $(0 - 0.81)^2 = 0.6561$ & $\frac{35}{52} \times 0.6561 = 0.4416$ \\
\hline
  &       & $E(X) = 0.81$ & & \soln{\only<3->{$V(X) = 3.4246$}} \\
 &       &                                                         & & \soln{\only<4>{$SD(X) = \sqrt{3.4246} = 1.85$}} \\
\end{tabular}
\end{center}
}
}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Linear combinations of random variables}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Linear combinations}

\begin{itemize}

\item A \hl{linear combination} of random variables $X$ and $Y$ is given by

\[ aX + bY \]

where $a$ and $b$ are some fixed numbers.

\pause

\item The average value of a linear combination of random variables is given by
\formula{\[ E(aX + bY) = a \times E(X) + b \times E(Y) \]}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Calculating the expectation of a linear combination}

\dq{On average you take 10 minutes for each statistics homework problem and 15 minutes for each chemistry homework problem. This week you have 5 statistics and 4 chemistry homework problems assigned. What is the total time you expect to spend on statistics and chemistry homework for the week?}

\soln{
\pause
\begin{align*}
E(S + S + S + S + S + C + C + C + C) &= 5 \times E(S) + 4 \times E(C) \\
&= 5 \times 10 + 4 \times 15 \\
&= 50 + 60 \\
&= 110~min
\end{align*}
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Variability in linear combinations of random variables}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Linear combinations}

\begin{itemize}

\item The variability of a linear combination of two independent random variables is calculated as
\formula{\[ V(aX + bY) = a^2 \times V(X) + b^2 \times V(Y) \]}

\pause

\item The standard deviation of the linear combination is the square root of the variance.

\end{itemize}

\pause
\vfill

\Note{If the random variables are not independent, the variance calculation gets a little more complicated and is beyond the scope of this course.}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Calculating the variance of a linear combination}

\dq{The standard deviation of the time you take for each statistics homework problem is 1.5 minutes, and it is 2 minutes for each chemistry problem. What is the standard deviation of the time you expect to spend on statistics and physics homework for the week if you have 5 statistics and 4 chemistry homework problems assigned? Suppose that the time it takes to complete each problem is independent of another.}

\soln{
\pause
\begin{align*}
V(S + S + S + S + S + C + C + C + C) &= V(S) + V(S) + V(S) + V(S) + V(S) + V(C) + V(C) + V(C) + V(C) \\
&= 5 \times V(S) + 4 \times V(C) \\
&= 5 \times 1.5^2 + 4 \times 2^2 \\
&= 27.25
\end{align*}
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Recap}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Practice}

\pq{A casino game costs \$5 to play. If the first card you draw is red, then you get to draw a second card (without replacement). If the second card is the ace of clubs, you win \$500. If not, you don't win anything, i.e. lose your \$5. What is your expected profits/losses from playing this game? {\small Remember: profit/loss = winnings - cost.}}

\begin{multicols}{2}
\begin{enumerate}[(a)]
\item A profit of 5\textcent
\solnMult{A loss of 10\textcent}
\item A loss of 25\textcent
\item A loss of 30\textcent
\end{enumerate}
\end{multicols}

\soln{
\only<2>{
{\small
\renewcommand\arraystretch{1.25}
\begin{tabular}{l c c c r}
Event				& Win	& Profit: $X$	& $P(X)$	& $ X \times P(X)$	\\
\hline
\orange{Red}, {A}{$\clubsuit$}		& 500		& 500 - 5 = 495	& $\frac{26}{52} \times \frac{1}{51} = 	0.0098$ & 	 $495 \times 0.0098 = 4.851$ \\
Other	& 0 			& 0 - 5 = -5	& $1 - 0.0098 = 0.9902$ & $-5 \times 0.9902 = -4.951$ \\
\hline
					&			&			& 			& $E(X) = -0.1$
\end{tabular}
}
}
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Fair game}

A \hl{fair} game is defined as a game that costs as much as its expected payout, i.e. expected profit is 0.

\pause

$\:$

\dq{Do you think casino games in Vegas cost more or less than their expected payouts?}

\soln{
\pause
\begin{columns}[c]
\column{0.6\textwidth}
If those games cost less than their expected payouts, it would mean that the casinos would be losing money on average, and hence they wouldn't be able to pay for all this:
\column{0.4\textwidth}
\includegraphics[width=\textwidth]{3-4_random_variables/figures/bellagio.jpg}
\end{columns}
\ct{Image by Moyan\_Brenn on Flickr \webURL{http://www.flickr.com/photos/aigle\_dore/5951714693}.}
}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Simplifying random variables}

Random variables do not work like normal algebraic variables:
\[ X + X \ne 2X \]

\pause

{\small
\twocol{0.45}{0.45}
{
\begin{align*}
E(X + X) &= E(X) + E(X) \\
&= 2 E(X) \\
&~  \\
E(2X) &= 2 E(X) \\
&~
\end{align*}
}
{
\begin{align*}
Var(X + X) &= Var(X) + Var(X)~{\scriptsize \text{(assuming independence)}} \\
&= 2~Var(X) \\
&~  \\
Var(2X) &= 2^2~Var(X) \\
&= 4~Var(X)
\end{align*}
}
}

\pause

\vspace{3mm}

\mathhl{E(X + X)  = E(2X)}, but \mathhl{Var(X + X) \ne Var(2X)}.

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}
\frametitle{Adding or multiplying?}

\dq{A company has 5 Lincoln Town Cars in its fleet. Historical data show that annual maintenance cost for each car is on average \$2,154 with a standard deviation of \$132. What is the mean and the standard deviation of the total annual maintenance cost for this fleet?}

\pause

Note that we have 5 cars each with the given annual maintenance cost $(X_1 + X_2 + X_3 + X_4 + X_5)$, not one car that had 5 times the given annual maintenance cost $(5X)$.

\pause

{\small
\begin{eqnarray*}
E(X_1 + X_2 + X_3 + X_4 + X_5) &=& E(X_1) + E(X_2) + E(X_3) + E(X_4) + E(X_5) \\
\pause
&=& 5 \times E(X) = 5 \times 2,154 = \$ 10,770 \\
\pause
Var(X_1 + X_2 + X_3 + X_4 + X_5) &=& Var(X_1) + Var(X_2) + Var(X_3) + Var(X_4) + Var(X_5) \\
\pause
&=& 5 \times V(X) = 5 \times 132^2 = \$ 87,120 \\
\pause
SD(X_1 + X_2 + X_3 + X_4 + X_5) &=& \sqrt{87,120} =  295.16
\end{eqnarray*}
}

\end{frame}


\chapter{Preliminary Investigation}
\label{ch:investigation}
One of the most recurrent subject in talks regarding Android, is how much the platform does not help developers write secure code. Speakers usually make examples of how developers forget to make usage of private mode when saving preferences or either resorting in saving important documents on the SD card, where are publicly available\cite{codemotion}. Moreover, resources such as Stack Overflow, where developers can share solutions to common problems, do not really help when the security of the published code is often of no importance. Therefore, before starting this work, I identified common vulnerabilities which I thought could be fixed at the compiler level and each of them is here presented with an appropriate threat model\footnote{To prevent malicious uses of this work, threat models are explained in as much details as possible, but no code will be provided.}. Furthermore, the fact that these problems were reported did not mean that they were present in top Android apps, hence, before actually creating \emph{DevArtist} to solve these security issues, an investigation was necessary to determine whether they were present and how wide-spread they were.

\section{Hashing}
The term is used to identify the process of transforming data of arbitrary length to data of fixed size, which can be obtained with so called \emph{Hash functions}. Many fields of computer science use these functions to achieve a variety of results, ranging from memory optimization to algorithms which produce randomness or, as analyzed in this thesis, to verify data integrity. However, the possibility to use such functions in the latter field, exists due to another property: collision-resistance. In fact, any secure hash function is collision-resistant in the sense that it is computationally hard to find two inputs which produce the same hash, otherwise it is said that such function is not cryptographically secure in respect to collisions. Furthermore, due to the fact that on Android the full Java cryptography package is available, a developer must decide which hashing algorithm has to use, depending on its use case because not all of the already existing ones are cryptographically secure.

\subsection{The MD5 Case}
Message Digest 5, hence the MD5 acronym, was developed by Ronald Rivest in 1991 and it was formalized on RFC 1321. However, since Dobbertin found a collision\cite{dobbertin} in 1996, many other publications reported cases against the algorithm, culminating in 2012 with Marc Stevens, who published in \cite{stevens}, an algorithm and sources to execute a collision attack on MD5 with the use of a normal laptop. Due to these reasons, the cryptography community suggested to move on to more secure hashing methods such as SHA-1. It is worth to mention that the algorithm could still be used safely to verify data integrity when there is no reason to believe that intentional corruption, by a malicious third-party, took place.

\subsection{The SHA1 Case}
Secure Hashing Algorithm 1, \enquote{SHA1} in short, was developed by the United States Security Agency in 1995. Ten years later Rijmen and Oswald published an initial collision attack\cite{sha1b1}, which was followed in 2005 by another paper from Marc Stevens\cite{sha1b2}, who estimated that an attack could be made by investing maximum 2.77 million U.S. dollars in virtualized computing power. More recently, Google's employees achieved and showed in \cite{sha1b3} how it was possible to create collisions for SHA1. Therefore, cryptography researchers and companies suggest to move on more secure hashing methods, such as SHA-256, whenever the computational power of the device allows.

\subsection{Threat Model}
Attacks on Android apps which make use of hashing functions are difficult to report, due to the fact it depends on how the app uses the function. The most common attack vector for this kind of vulnerability is a Man-In-The-Middle (MITM) attack for apps that download from the Internet a package and verify its integrity with MD5 or SHA1. In this case, the attacker can use a proxy between the app and the outside network to change the package with a malicious one which has the same hash as the original one. Of course, being able to install a proxy is an intermediate level of difficulty, whilst creating a malicious package which has the same hash as the original one is definitively harder, but it is indeed possible and it is not too far dissimilar from what happened with the Swift keyboard app shipped within the Galaxy Samsung S6 in 2015 (CVE-2015-4640 and CVE-2015-4641)\footnote{https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2015-4641}, which was downloading language packs from the Internet. Although, \emph{DevArtist} wouldn't have voided completely the above mentioned attack, due to its sophistication, SHA1 was still in use to verify the downloaded zip and, if the checksum field in the json file was transferred over \texttt{HTTP} instead of \texttt{HTTPS}, someone with the right amount of money and time would have performed an attack similar to the one described above. This example and many other apps in the Play Store, such the ones which download wallpapers or themes to style the interface of Android, show that such functionality exists and could be exploited.

\section{SQL and Injections}
As the definition applies: a database is a collection of data and the software which manages it is called Database Management System or, in short, DBMS. Many kinds of DBMS are being developed since 1960 but the most popular one, still in 2017 (the year of writing), as shown in Fig: \ref{appendix:dbmarketshare} by solid-IT in their DB-Engine ranking\footnote{https://db-engines.com/en/ranking} is a direct descendant of the relational database described in \cite{dbrelational} by Edgar F. Codd in 1970. Probably for this reason and for the possibility to have a database in one single file, Google, on its Android, decided to provide developers with a relational database named SQLite as their only DBMS option. Therefore, developers are allowed to create as many databases as they want inside their own, hopefully private, folders. However, by doing this, it left an open margin for SQL Injections (SQLi), which are particular code injections techniques with the aim of stealing user's data from any SQL-enabled database. In fact, the main concept behind SQLi is the ability to write valid SQL code inside an input field, which modifies the behavior of the original one to extract protected information, thus showing the private ones instead of the expected information. These kind of attacks were really \emph{popular} during the early PHP era and they allowed massive breaches, which produced incredible amounts of stolen credit cards, password and other user information\footnote{Examples of data breaches with SQL Injection can be found extensively on-line.}.

\subsection{The Case of the \emph{RawQuery} Method}
Android developers interact with the underlying SQLite within the class \texttt{SQliteDatabase}, which provides all the methods to perform CRUD operations. However, both for performances and incrementation of the capability offered by the system, a way to execute queries in a non constructed manner is provided as API, leaving the responsibility of proper usage to developers. That API is the \emph{rawQuery()} method. In fact, if a developer is unaware of the danger which such API exposes to, malicious attackers could use it to perform SQL injections and extract user data. The code in listing \ref{lst:injection} shows an example of possible Android code, supposedly returning only the passwords for the defined account id and URL. However, the value of the variable \emph{url} can be manipulated in such a way which makes the query return all the passwords also for other accounts.\newline\newline

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt}
\begin{lstlisting}[language=Java, label={lst:injection}, caption="SQL injection example", captionpos=b]
public List<String> foo(int id, String url){
	String sql = "SELECT password FROM passwords "+
	   			" WHERE accountID="+id+" AND URL='"+url"'";
	SQliteDatabase db = getReadableDatabase();
	Cursor c = db.rawQuery(sql, null);
	List<String> foos = new ArrayList<>();
	while(c.hasNext()){
		foos.add(c.getString(0));
	}
	return foos;
}
\end{lstlisting}

% \subsection{Threat Model}
% There are several Attack models for SQL injection on Android and they are here described on the level of difficulty which is required to successfully take advantage of them.

% \subsubsection{Easy attack}
% This threat model envisions that a malicious person (Eve) steals shares, even for a short period of time, the mobile phone of another person (Alice). If this event occurs and Eve knows that an application (A) is subject to an SQL Injection it can simply open the app and run the SQL Injection. This attack might seem ephemeral at first, but take as example an app that shows contacts based on the account in use by the Android system or, as mentioned before, a password manager. In these cases, even if the attack is easy and requires manual intervention, the danger of exposing unwanted information to the wrong person is high.

\subsubsection{Advanced Attack}
As mentioned in section \ref{sc:exportedcomponents}, certain components in the Android's SDK  are allowed to be accessed publicly by other applications. One possible case of such components is the \texttt{ContentProvider}, which can offer data from a SQLite database owned by the app (A) to another app (B), if exported, through an interface. In this case, if the interface is using string composition as showed earlier and the method \texttt{rawQuery}, an attacker could install a rogue app which exploits this vulnerability and leak the information contained in A. Installing such application is not that hard, one can do it manually if physical access to the device is granted or remotely by inducing the victim (with a Whatsapp message if the number of the victim is known, or other social engineering means) in downloading an app that pretends to do one thing and, in the meanwhile, exploits the vulnerability.

\newpage
\section{Randomness}
\label{sec:randominvestigation}
In Computer Science, it is often necessary to produce random data and, although generating real randomness is still an open problem, Java provides two classes to produce pseudo-random data: \emph{Random} and \emph{SecureRandom}. The former class, should not be employed in a hardened environment due to the fact that it is using a Linear Congruential Generator (LCG) which has been demonstrated to be broken in \cite{lcgbreak} by Hugo Krawczyk. On the other hand, \emph{SecureRandom}, if used correctly, draws its randomness from \emph{/dev/urandom}, which is considered more secure by the cryptographic community as discussed in \cite{secrandom} and \cite{urandom}. However, as reported by BBC\cite{bbc} and other news publishers, the algorithm in use by \emph{SecureRandom} could be broken by the U.S National Security Agency (NSA). To avoid philosophical discussions over what is trusted or not in this work, we assume that \emph{SecureRandom} provides a better security over the \emph{Random} class. The only exception to this assumption is on Android Jellybean, where \texttt{SecureRandom} was vulnerable, as described in \cite{bitcoinalert} and \cite{randomalert}, but since this project is built for Android 7 \enquote{Nougat}, the case is not a concern.

\subsection{Threat Model}
Since describing meaningful attack vectors of apps which contains an insecure usage of random is difficult due to their dependency on the purpose of their target, I will make use of an example: a poker app which needs to shuffle the deck and allows the player to buy virtual coins in exchange of real coins. In this case, the developer of the app might have used the class \texttt{Random} instead of \texttt{SecureRandom} and, if the player manages to guess the seed it can reproduce the shuffle of the deck and win every match. Although the difficulty of performing such an attack is hard, it might still be perpetrated by a resolute attacker especially due to the fact that money is involved, thus showing the importance of patching randomness.

\section{Preliminary Analysis}
\label{sc:preliminaryanalysis}
To investigate on the severity of the above issues, it was necessary to check if applications already published in the Play Store contained the signatures of the Java methods used to produce them. Therefore, I developed an \emph{Artist} module which detects the signatures of methods listed below and extended Monkey Troop to re-compile applications with my module, allowing me to count the number of usages of those calls.\newpage Moreover, the full source code of this module and the Monkey Troop extension is available on Github\footnote{\url{https://www.github.com/jibbo/master_thesis}}.

\begin{itemize}
	\label{it:signs}
	\item{For Random: java.util.Random.\textless init\textgreater}
	\item{For SecureRandom: java.security.SecureRandom.\textless init\textgreater}
	\item{For MD5, SHA-1, SHA-256: java.security.MessageDigest.getInstance(java.lang.String)}
	\item{For rawQuery():\newline android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])}
\end{itemize}

\subsection{The List of Apps}
The evaluation was conducted on the top 500 apps of the Play Store, available in Germany as the 1st of January 2017, the full list can be found in \ref{appendix:appslist}, ordered alphabetically by their package name, which is unique. Therefore, popular apps such as Facebook, Telegram, WhatsApp, Instagram, Snapchat, Pinterest (to name a few) are included in the list. It is a general belief that the most downloaded apps are either social networks or games and that, especially the latter category, these apps are less secure. However, it is worth to mention that top apps are computed over the most downloaded apps in every every category available on the Store, such as: Finance, Productivity, News, Photography and so on \footnote{Full list available in appendix \ref{appendix:categorieslist}}.

\subsection{Monkey Troop Extension}
The first change to this automated system was to specify, through a script, the commands needed to be executed and they can be found below. Since every signature could be checked through static analysis when compiling, these commands do not need to be many and follow every execution path, but they need to ensure that any app can at least be launched without crashing and provide an extensive log for deeper investigation. Therefore, I instructed the system with the commands below and then extended the analyzer class (\texttt{ResultAnalyzer.py}) to keep track of the specific log outputs of my module. These logs get stored inside a SQLite Database only when the above mentioned class reported that all the steps had been executed successfully. Moreover, the database was composed of only one table which contained two fields: the package-name of the apps and a specific line of the Logcat designed for the purpose. In fact, to detect the hashing algorithm, my module logged a line with the following shape: \enquote{[DC][HASHING][ALGORITHM\_NAME]}, whilst for all the other cases, the form \enquote{[DC][SIGNATURE] usage found} was used. In this way, counting only the the needed signatures or specific hashing function could be achieved by filtering the log line through standard SQL queries.

\begin{itemize}
	\item{Download app from the Play Store}
	\item{Install the app on the device}
	\item{Run the app}
	\item{Re-Compile the app with the module}
	\item{Run the instrumented app}
	\item{Copy the full Logcat in a file}
	\item{Repeat with another app from the list}
\end{itemize}

\subsection{Results}
Monkey Troop makes usage of a third party tool to download application from the Play Store, but, due to authentication problems between the third-party tool and the Play Store, happening systematically only when downloading certain apps, the population shrunk from 500 apps to 492.  In addition, although the remaining part of the apps could be run, the actual population shrunk again to 392 due to \emph{Artist} itself, which (in my own tests) has a success rate in instrumenting of only 85,40\%. From a preliminary analysis, these apps seem to be using APIs which are not supported by \emph{Artist} as also mentioned in \cite{artist}. Moreover, taking in consideration apps below the top 500 line, to increment the number of examined apps, would have decreased too much the quality of the population, thus risking worse results. Every number reported in table \ref{tb:respreliminary} was extracted from the SQL database, filtered by the signature and grouped by the app package-name when necessary.

\begin{table}[H]
	\vspace{1.5cm}
	\centering
	\begin{tabular}{|c|c|c|}
		\hline
		Subject & Found in \# of apps  & Times used \\
		\hline
		Random & 261 & 2909 \\
		\hline
		SecureRandom & 244 & 1023\\
		\hline
		rawQuery & 296 & 737\\
		\hline
		MD5 & 189 & 2743\\
		\hline
		SHA-1 & 119 & 1091\\
		\hline
		SHA-256 & 29 & 339\\
		\hline
	\end{tabular}
	\caption{Number of method signatures found}
	\label{tb:respreliminary}
	\vspace{1.5cm}
\end{table}

\subsection{Interpretation}
Table \ref{tb:respreliminary} shows that MD5 is used in more than 48\% of the cases alone, making it the most widely used hash function. Moreover, even SHA-1 reaches a remarkable 30\% of usage, making it clear that the usage of broken hash functions is very common and it should be the first danger addressed by this work. Furthermore, the usage of the insecure \texttt{rawQuery()} method is also astonishingly wide-spread, since the method is present in more than the 75,5\% of the apps. In addition, although the class \texttt{SecureRandom} and \texttt{Random} are present in almost the same amount of apps, the insecure one is used almost twice as many times. Therefore, this data suggested that there is a real need in addressing the issue.


%!TEX root = ../../dissertation.tex

\subsection{Cellular Automaton} % (fold)
\label{sub:cellular_automaton}

A cellular Automaton is a model of a system of cells within a grid with a given shape, each of this cells can be on one of a finite set of states. It evolves during a finite amount of time steps with a set of simple rules according to each state of the neighboring cells.
The neighborhood of the cell can be defined in many different ways, the most common is the use of the adjacent cells.

This models have various applications, such as modeling of nature aspects (Figure~\ref{fig:CArule30shell}), textures, and as inspiration to architecture (Figure~\ref{fig:CAarchitecture}).

\begin{figure}
        \centering
        %\begin{subfigure}[b]{0.5\textwidth}
                \includegraphics[width=0.45\textwidth]{images/Theory/Cellular_A/dome1.jpg}
        %        \caption{}
  %              \label{fig:CAdome}
        %\end{subfigure}%
        %~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
          %(or a blank line to force the subfigure onto a new line)
          ~~
        %\begin{subfigure}[b]{0.5\textwidth}
                \includegraphics[width=0.45\textwidth]{images/Theory/Cellular_A/main1.jpg}
        %        \caption{}
   %             \label{fig:CArule30}
        %\end{subfigure}
        \caption{Examples of cellular automata applied in architecture}
        \label{fig:CAarchitecture}
\end{figure}

The case where each cell have two possible states and the next generation state depends only on the previous state of the cell and the two immediate neighbors is called an \emph{elementary cellular automaton}. In this case we have $2^3 = 8$ possible patterns for a neighborhood and $2^8 = 256$ sets of possible different rules. This rules are referred by their \emph{Wolfram code} \cite{CellularAutWOLFRAM}.

A common initial state for this elementary cellular automata is a random line. But to be able to compare the results between rules and get clean results another option is to start with a line with zeros except the middle cell that is initialized with the value one. Applying this second option and the set of rules in Figure~\ref{fig:CArule} (the rule 30), we get the pattern in the Figure~\ref{fig:resultCA} that represents the evolution of a cellular automaton over a few generations.

\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.85\textwidth]{images/Theory/Cellular_A/Rules.png}
	\caption{Example Production Rules\cite{Shiffman2012}}
	\label{fig:CArule}
\end{figure}

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.75\textwidth]{images/Theory/Cellular_A/Result.png}
    \caption{Sierpiński Triangle, rule 90}
    \label{fig:resultCA}
\end{figure}

In Figure~\ref{fig:resultCA} each line represents an iteration of the system with the application of the rules. With this set of rules a Sierpiński triangle is reproduced.

Cellular automata are used mainly to model phenomena that occur in the physical world, most of them can only express the basic idea of a phenomenon, but some are accurate enough to be able to make predictions.

In this context, cellular automata are used to model natural shapes and textures, Figure~\ref{fig:CArule30shell} shows on the left, a natural texture on the shell of a \emph{Textile Cone Snail}, that looks like the patterns formed with the cellular automaton on the right.

\begin{figure}
        \centering
        %\begin{subfigure}[b]{0.3\textwidth}
                \includegraphics[width=0.45\textwidth]{images/Theory/Cellular_A/shell.jpeg}
        %        \caption{a)}
		%		\label{fig:CAshell}
        %\end{subfigure}%
        %~ %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
          %(or a blank line to force the subfigure onto a new line)
          ~~
        %\begin{subfigure}[b]{0.5\textwidth}
                \includegraphics[width=0.45\textwidth]{images/Theory/Cellular_A/Rule30.png}
		%		\caption{b)}
		%		\label{fig:CArule30}
        %\end{subfigure}
        \caption{Example of the representation of natural patterns with cellular automata. On the left, a Natural Shell \cite{Shiffman2012} and on the right a Pattern formed with the rule 30}
		\label{fig:CArule30shell}
\end{figure}

% subsection cellular_automaton (end)