%! TeX program = lualatex
\documentclass[a4paper]{article} 

% packages
\usepackage{microtype}      % Slightly tweak font spacing for aesthetics
\usepackage[english]{babel} % Language hyphenation and typographical rules
\usepackage[final, colorlinks = true, urlcolor = black, linkcolor = black]{hyperref} 
\usepackage{changepage}     % adjust margins on the fly

\usepackage{fontspec}
\setmainfont{EB Garamond}
\setmonofont[Scale=MatchLowercase]{Deja Vu Sans Mono}

\usepackage{minted}
\usemintedstyle{algol_nu}
\usepackage{xcolor}

\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.9}

\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing}}{}
\captionsetup[listing]{skip=0pt}
\setlength{\abovecaptionskip}{5pt}
\setlength{\belowcaptionskip}{5pt}

\usepackage[yyyymmdd]{datetime}
\renewcommand{\dateseparator}{--}

\usepackage{titlesec}
% \titleformat{\section}{\LARGE\bfseries}{}{}{}[\titlerule]
% \titleformat{\subsection}{\Large\bfseries}{}{0em}{}
% \titlespacing{\subsection}{0em}{-0.7em}{0em}
%
% \titleformat{\subsubsection}{\large\bfseries}{}{0em}{$\bullet$ }
% \titlespacing{\subsubsection}{1em}{-0.7em}{0em}

% margins
\addtolength{\hoffset}{-2.25cm}
\addtolength{\textwidth}{4.5cm}
\addtolength{\voffset}{-3.25cm}
\addtolength{\textheight}{5cm}
\setlength{\parskip}{0pt}
\setlength{\parindent}{0in}
% \setcounter{secnumdepth}{0}

\begin{document}
\hrule \medskip
\begin{minipage}{0.295\textwidth} 
    \raggedright
    \footnotesize 
    \begin{tabular}{@{}l l}
        Name: & Andrew Hayes \\
        Student ID: & 21321503 \\
        E-mail: & \href{mailto://a.hayes18@universityofgalway.ie}{a.hayes18@universityofgalway.ie} \\
    \end{tabular}
\end{minipage}
\begin{minipage}{0.4\textwidth} 
    \centering 
    \vspace{0.4em}
    \LARGE
    \textsc{ct404} \\ 
\end{minipage}
\begin{minipage}{0.295\textwidth} 
    \raggedleft
    \today
\end{minipage}
\medskip\hrule 
\begin{center}
    \normalsize
    Assignment 2: Image Processing \& Analysis
\end{center}
\hrule

\section{A Morphological Image Processing Pipeline for Medical Images}
\begin{figure}[H]
    \centering
    \includegraphics[width=0.7\textwidth]{../Task1.jpg}
    \caption{Original Skin Biopsy Image}
\end{figure}

\subsection{Conversion to A Single-Channel Image}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{python}{../code/task1/1_single_channel_conversion.py}
\caption{\mintinline{python}{1_single_channel_conversion.py}}
\end{code}

Since the image has predominant hues of pink-purple, we would expect the green-channel-only image to be the one that yields the highest contrast, as pink \& purple colours are made up primarily by the blue \& red channels: the dominance of these channels results in little variance in intensity within these channels, and therefore green will have the highest intensity variance.
This is proven true by the text output of the above code, where the standard deviation of the greyscale image based off the green channel alone is by far the highest:
\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{./images/1_single_channel_conversion_output.png}
    \caption{Output of \mintinline{python}{1_single_channel_conversion.py}}
\end{figure}

\noindent
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../Task1.jpg}
    \captionof{figure}{Original image}
    \includegraphics[width=\textwidth]{../code/task1/output/greyscale.jpg}
    \captionof{figure}{Greyscale original}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/b_channel.jpg}
    \captionof{figure}{B-Channel}
    \includegraphics[width=\textwidth]{../code/task1/output/b_channel_greyscale.jpg}
    \captionof{figure}{B-Greyscale}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/g_channel.jpg}
    \captionof{figure}{G-Channel}
    \includegraphics[width=\textwidth]{../code/task1/output/g_channel_greyscale.jpg}
    \captionof{figure}{G-Greyscale}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/r_channel.jpg}
    \captionof{figure}{R-Channel}
    \includegraphics[width=\textwidth]{../code/task1/output/r_channel_greyscale.jpg}
    \captionof{figure}{R-Greyscale}
\end{minipage}

My selected single-channel image is the greyscale version of the green-channel-only image, as it yields the greatest contrast:
\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{../code/task1/output/g_channel_greyscale.jpg}
    \caption{Selected single-channel image: greyscale green-channel-only}
\end{figure}

\subsection{Image Enhancement}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{python}{../code/task1/2_image_enhancement.py}
\caption{\mintinline{python}{2_image_enhancement.py}}
\end{code}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{./images/2_image_enhancement_output.png}
    \caption{Output of \mintinline{python}{2_image_enhancement.py}}
\end{figure}

I chose to use the histogram equalisation technique as it gave the best contrast, as seen from the calculated standard deviation in contrast above and in the output images below.

\begin{minipage}{0.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/histogram_equalised.jpg}
    \captionof{figure}{Histogram-equalised image}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/contrast_stretched.jpg}
    \captionof{figure}{Contrast-stretched image}
\end{minipage}

\subsection{Thresholding}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{python}{../code/task1/3_thresholding.py}
\caption{\mintinline{python}{3_thresholding.py}}
\end{code}

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{./images/3_thresholding_output.png}
    \caption{Output of \mintinline{python}{3_thresholding.py}}
\end{figure}

I used Otsu's algorithm to find the optimal threshold value that best separated the foreground (objects of interest) from the background.
As can be seen from the above output, the optimal value chosen was 129.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{../code/task1/output/otsu.jpg}
    \caption{Image with Otsu thresholding}
\end{figure}


\subsection{Noise Removal}
\begin{code}
    \inputminted[linenos, breaklines, frame=single]{python}{../code/task1/4_noise_removal.py}
    \caption{\mintinline{python}{4_noise_removal.py}}
\end{code}

\noindent
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_1.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 1}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_3.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 3}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_5.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 5}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_7.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 7}}
\end{minipage}

\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_9.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 9}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_11.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 11}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_13.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 13}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_15.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 15}}
\end{minipage}

\noindent
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_17.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 17}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_19.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 19}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_21.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 21}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_23.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 23}}
\end{minipage}

\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_25.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 25}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_27.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 27}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_29.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 29}}
\end{minipage}
\hfill
\begin{minipage}{0.24\textwidth}
    \centering
    \includegraphics[width=\textwidth]{../code/task1/output/kernel_size_31.jpg}
    \captionof{figure}{\mintinline{python}{kernel_size = 31}}
\end{minipage}

I chose to go with \mintinline{python}{kernel_size = 25} as it seemed to give the optimal balance between removing noise without significantly reducing the size of the remaining fat globules .

\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{../code/task1/output/kernel_size_25.jpg}
    \caption{Chosen noise threshold: \mintinline{python}{kernel_size = 25}}
\end{figure}

\subsection{Extraction of Binary Regions of Interest / Connected Components}



\end{document}
