\chapter{Classification and Prediction}

Thus far in the book, the term \emph{information}
has been used sparingly and when
it has been used, we have purposely been imprecise as to its meaning.
Although, everyone  has an intuitive feeling for what information is,
it is difficult to attach
a meaningful quantitative definition to the term. In the context of
communication systems, Claude Shannon was able to do exactly this,
and as a result, opened up an entirely new view of communication systems
analysis and design \cite[p.~123]{Albano1991}.
The principal contribution of Shannon's information theory to date has been
to allow communication theorists to establish absolute
bounds on communication systems performance that cannot be exceeded no matter
how ingeniously designed or complex our communication systems are.
Fundamental physical limitations on communication systems performance is
another topic that has been largely ignored in the preceding chapters,
but it is a subject of exceptional practical importance.
For example, for any of the numerous communication systems developed
thus far in the book, we could decide to design
a new system that would outperform the accepted standard for a particular
application. The first question that we should ask is: how close is the
present system to achieving theoretically optimum performance?
If the existing communication system operates at or near the fundamental
physical limit on performance, our task may be difficult or impossible.
However, if the existing system is far away from the absolute
performance bound, this might be an area for fruitful work.

\section{What is Classification? What is Prediction?}
\label{ch01.sec11.1}

Of course, in specifying the particular communication system under
investigation, we must know the important physical parameters,
such as transmitted
power, bandwidth, type(s) of noise present, and so on,
and information theory allows these constraints to be incorporated.
However, information theory does not provide a way for communication system
complexity to be explicitly included.
Although, this is something of a drawback, information theory itself provides
a way around this difficulty, since it is generally true that as we approach
the fundamental limit on the performance of a communication system,
the system complexity increases, sometimes quite drastically.
Therefore, for a simple communication system operating far
from its performance bound, we may be able to improve the performance
with a relatively modest increase in complexity.
On the other hand, if we have a rather complicated communication system
operating near its fundamental limit, any performance improvement may
be possible only with an extremely complicated system.

In this chapter we are concerned with the rather general block diagram
shown in Figure~\ref{ch01.fig11.1.1}. Most of the early work by
Shannon and others ignored the source  encoder/decoder blocks and
concentrated  on bounding the performance of the channel
encoder/decoder pair. Subsequently, the source  encoder/decoder blocks
have attracted much research attention.  In this chapter we consider
both topics and expose the reader to the nomenclature used in the
information theory literature.
Quantitative definitions of information are presented in
Sec.~\ref{ch01.sec11.2} that lay the foundation for the remaining
sections. In Secs.~\ref{ch01.sec11.2} and~\ref{ch01.sec11.2} we present
the fundamental source and channel coding theorems, give some examples,
and state the implications of these theorems.
Section~\ref{ch01.sec11.2} contains a brief development of rate
distortion theory,
which is the mathematical basis for data compression.
A few applications of the theory in this chapter are presented
in Sec.~\ref{ch01.sec11.2}, and a technique for variable-length
source coding is given in Sec.~\ref{ch01.sec11.2}.
\begin{equation}
x = a + b - \sqrt{q}
\end{equation}
Of course, in specifying the particular communication system under
investigation, we must know the important physical parameters,
such as transmitted
power, bandwidth, type(s) of noise present, and so on,
and information theory allows these constraints to be incorporated.
However, information theory does not provide a way for communication system
complexity to be explicitly included.
Although, this is something of a drawback, information theory itself provides
a way around this difficulty, since it is generally true that as we approach
the fundamental limit on the performance of a communication system,
the system complexity increases, sometimes quite drastically.
Therefore, for a simple communication system operating far
from its performance bound, we may be able to improve the performance
with a relatively modest increase in complexity.
On the other hand, if we have a rather complicated communication system
operating near its fundamental limit, any performance improvement may
be possible only with an extremely complicated system.

\subsection{And Yet More of the Same}

\noindent In this chapter we are concerned with the rather general block diagram
shown in Figure~\ref{ch01.fig11.1.1}. Most of the early work by
Shannon and others ignored the source  encoder/decoder blocks and
concentrated  on bounding the performance of the channel
encoder/decoder pair. Subsequently, the source  encoder/decoder blocks
have attracted much research attention.  In this chapter we consider
both topics and expose the reader to the nomenclature used in the
information theory literature.
Quantitative definitions of information are presented in
Sec.~\ref{ch01.sec11.2} that lay the foundation for the remaining
sections. In Secs.~\ref{ch01.sec11.2} and~\ref{ch01.sec11.2} we present
the fundamental source and channel coding theorems, give some examples,
and state the implications of these theorems.
Section~\ref{ch01.sec11.2} contains a brief development of rate
distortion theory,
which is the mathematical basis for data compression.
A few applications of the theory in this chapter are presented
in Sec.~\ref{ch01.sec11.2}, and a technique for variable-length
source coding is given in Sec.~\ref{ch01.sec11.2}.
\begin{itemize}
\item This is a bullet list with a short item.
\item And another item that is much longer so that we can make sure it is
formatted correctly and so forth and so on.
\item And a final short item.
\end{itemize}

\begin{figure}
\includegraphics[width=24pc]{sample-figure}
\caption{This is a caption for this figure. It is fairly long so we can make
sure it looks good when occupying more than one line. Here is one more
sentence to make it longer.}
\label{ch01.fig11.1.1}
\label{fig:query-specification}
\end{figure}

In this chapter we are concerned with the rather general block diagram
shown in Figure~\ref{ch01.fig11.1.1}. Most of the early work by
Shannon and others ignored the source  encoder/decoder blocks and
concentrated  on bounding the performance of the channel
encoder/decoder pair. Subsequently, the source  encoder/decoder blocks
have attracted much research attention.  In this chapter we consider
both topics and expose the reader to the nomenclature used in the
information theory literature.
\begin{align}
a& = b + c\\
x&= \frac{1}{2} a
\end{align}
Quantitative definitions of information are presented in
Sec.~\ref{ch01.sec11.2} that lay the foundation for the remaining
sections. In Secs.~\ref{ch01.sec11.2} and~\ref{ch01.sec11.2} we present
the fundamental source and channel coding theorems, give some examples,
and state the implications of these theorems.
Section~\ref{ch01.sec11.1} contains a brief development of rate
distortion theory,
which is the mathematical basis for data compression.
A few applications of the theory in this chapter are presented
in Sec.~\ref{ch01.sec11.2}, and a technique for variable-length
source coding is given in Sec.~\ref{ch01.sec11.2}.

Only the binary Huffman procedure has been described here,
but nonbinary codes can be designed using the Huffman method.
The details are somewhat more complicated and nonbinary codes
are less commonly encountered than binary ones,
so further discussion is left to the problems and the literature.

\section{Case Study}
\label{sec:case-study}

In this section, we exemplify how the 5S extensions for content-based image retrieval can be explored to define an image search service in the context of the CTRnet project.
The Crisis, Tragedy, and Recovery Network (CTRnet)~\cite{Balzer2007} objectives include to develop better
approaches toward making technology useful for archiving information about such events, and to support
analysis of rescue, relief, and recovery, from a digital library  perspective. CTRnet has
several modules, including crawling, filtering, a Facebook application, visualization, metadata
search, and Content-Based Image Retrieval (CBIR).

The CBIR module
builds upon the EVA tool for evaluating image descriptors for content-based image retrieval \cite{Bierman2005}. Eva integrates the most common stages of an image retrieval process and provides functionalities to facilitate the comparison of image descriptors in the context of content-based image retrieval.

In this case study, we consider the scenario in which a user is interested in finding images in the CTRnet collection that are similar to a particular photo provided as example. The objective is to identify images that could be used in a report on damages caused by an earthquake.
In this example, the query specification $q$ would be a tuple $q = (H_q, Contents_q, P_q)$, where $q$ is an image (see Figure~\ref{fig:query-specification})
 Thus, $q =
((V_q, E_q), L_q, F_q), Contents_q, P_q)$, where $V_q = {v_1}$; $E_q =\emptyset$; $L_q={' Cathedral\_P\_A\_P.jpg'}$; $F_q: V_q\cup E_q
\rightarrow L_q$, $Contents_q$ is the stream of the query image; and $P_q: V_q
\rightarrow Contents_q$.

\begin{table}
\caption{This is a little table here.}
\label{tab:coordinates}
\begin{tabular}{ccccc}
\hline
Foo& Bar& Zoo& Snork& Quux\\
\hline
 0 & 0 & 1 & 2 & 4\\
 0 & 0 & 3 & 2 & 4\\
 3 & 3 & 1 & 0 & 1\\
 3 & 1 & 4 & 2 & 1\\
 3 & 4 & 4 & 2 & 2\\
\end{tabular}
\end{table}

In this chapter we are concerned with the rather general block diagram
shown in Figure~\ref{ch01.fig11.1.1}. Most of the early work by
Shannon and others ignored the source  encoder/decoder blocks and
concentrated  on bounding the performance of the channel
encoder/decoder pair. Subsequently, the source  encoder/decoder blocks
have attracted much research attention.  In this chapter we consider
both topics and expose the reader to the nomenclature used in the
information theory literature.

\section{Entropy and Average Mutual Information}
\label{ch01.sec11.2}

Consider a discrete random variable $U$ that takes on
the values $\{u_1, u_2, \dots, u_M\}$, where the set of possible
values of $U$ is often called the \textit{alphabet} and the elements
of the set are called \textit{letters} of the alphabet. Let $P_U(u)$
denote the probability  assignment  over the alphabet, then we can
define the \textit{self-information} of the event $ u = u_j $ by
\begin{equation}
  I_U \left( u_j \right) = \log \frac{1}{P_U (u_j)} = - \log P_U
    \left( u_j \right)~.
\label{ch01.eq11.2.1}
\end{equation}
The quantity $I_U (u_j) $ is a measure  of the information  contained
in the event $ u = u_j$. Note that the base of the logarithm in
Eq.~\eqref{ch01.eq11.2.1} is unspecified. It is common
to use base $e$, in which case $I_U (\cdot) $ is in natural units (nats),
or base 2,  in which case $I_U(\cdot)$ is in binary units (bits).
Either base is acceptable since the difference in the two bases is just a
scaling operation. We will use base 2 in all of our work,
and hence $I_U(\cdot)$ and related quantities will be in bits.
The average or expected value of the self-information is called the
\textit{entropy,} also discrete entropy or absolute entropy, and is given by
\begin{equation}
 \displaystyle H(U) = - \sum^M_{j=1} P_U \left(u_j\right)
    \log P_U \left(u_j \right)~.
\label{ch01.eq11.2.2}
\end{equation}
The following example illustrates the calculation of entropy and how it is
affected by probability assignments.

\section{Exercises and Projects}
\label{cbir:exercises}

\begin{enumerate}
\item
How might CBIR be applied so teachers with a computer and connected camera can be reminded of the names of students in their class?

\item Consider the two colourful images (Image A and Image B) showed below, represented in the RGB color space. Suppose that the intensity values of each pixel in all bands (R, G, and B) are the same. Furthermore, each $(R,G,B)$ triplet is represented by a single intensity value. For example the triplet $(R,G,B)= (2,2,2)$ is represented by the intensity value $2$.

Suppose also that the colour space was quantized in five colors with intensity values 0, 1, 2, 3, and 4.

\item Compute the $L_1$ between the {\em Color Histograms} (5
  bins) of the two images. The $L_1$ distance between two color histograms $H_A$ and $H_B$ is computed as follows: $L_1(H_A, H_B) = \sum_{i=1}^{K}|H_A[i]- H_B[i]|$, where $K$ is the size of both histograms (5, in the case).

\item By considering both the feature vector extraction function and the distance function defined of the descriptor {\em Color Coherence Vector -- CCV}~\cite{Burns2006}, compute the distance
  $\delta_{CCV}(A,B)$ between the two images.

\item Consider the existence of two classes ({\em class 1} and {\em
class 2}) composed of five images each. Consider the existence of three different descriptors ({\em descriptor 1}, {\em descriptor 2}, and {\em descriptor 3}), whose feature vector extraction functions extract vectors belonging to the $R^2$ space.
Table~\ref{tab:coordinates} shows the coordinate of each image of each class, considering the three descriptors.
\end{enumerate}


\chapter{Illumination optics}\label{chap:Illumination optics}
This chapter provides some concepts of illumination optics used in this thesis. We start explaining the difference between radiometry and photometry.
In particular, we focus on the photometric variables, defining them both in three and two dimensions. The reflection and refraction laws and the phenomenon of total internal reflection are explained next. The last paragraph of the chapter gives a brief introduction to Fresnel reflection.
\section{Radiometric and photometric variables}\label{sec:photometry}
Radiometry concerns with the measurement of electromagnetic radiation across the entire electromagnetic spectrum. Photometry is the sub-field of radiometry that takes into account only part of the electromagnetic spectrum corresponding to the visible light \cite{zalewski1995radiometry}. Radiometry deals with radiometric quantities. An important radiometric quantity  is the radiant flux $\Phi_{\textrm{r}}$ (unit watt \textrm{W}) which is the total energy emitted from a source or received by a target per unit time:
\begin{equation}
\Phi_{\textrm{r}} = \frac{\textrm{d}Q}{\textrm{d}\mytime}\,,
\end{equation}
where $Q$ is the energy and $\mytime$ the time.\\
\indent In illumination optics the measurement of light is given in terms of the impression that it gives on the human eye. Therefore, illumination optics deals with photometric variables rather than with radiometric variables. The most important photometric variables are defined in the following using the notation adopted by Chaves in \cite{chaves2015introduction}. The luminous flux $\Phi$ (unit lumen, \textrm{lm}) is defined as the \textit{perceived} power of light by the human eye.
 The radiant and the luminous flux are related by the luminous efficacy function $y$ (unit \textrm{lm}/\textrm{W}) which defines how many lumen correspond to one Watt of power at a given wavelength.
 The luminous efficacy function reaches its maximum  at a wavelength of $555$ $\textrm{nm}$ where it is equal to $683$ $\textrm{lm}/\textrm{W}$.
  We may normalize the luminous efficacy function with its maximum value of $683$.
  The normalized function $\bar{y}(\lambda)$ is the luminosity function shown in Figure $\ref{fig:luminosityfunction}$ where $\lambda$ is the wavelength. It is a \textit{dimensionless} quantity with a range between $0$ and $1$ \cite{schubert2005light}.
\begin{figure}[t]
%\label{fig:luminousfunction}
  \begin{center}
  \includegraphics[width=7cm]{CIELuminosity}
  \end{center}
  \caption{\textbf{Luminosity function $\bar{y}(\lambda)$}. Relation between the eye's sensitivity and the wavelength of light. The luminosity function is dimensionless  \cite{wiki}.}
  \label{fig:luminosityfunction}
  \end{figure}
\\ \indent The luminous flux corresponding to one Watt of radiation power at any wavelength is given by the product of $683$ $\textrm{lm/W}$ and the luminosity function at the same wavelength,
i.e., $683 \, \bar{y}(\lambda)$. Hence, the total luminous flux $\Phi$ is defined as:
\begin{equation}\label{eq:luminous_flux}
\Phi = 683 \int_0^\infty \Psi_\textrm{r}(\lambda) \bar{y}(\lambda)\textrm{d}\lambda,
\end{equation}
where $\Psi_\textrm{r}(\lambda)$ is the spectral radiant flux, i.e., the power (in Watt) per unit wavelength (unit \textrm{W}/\textrm{m}). The corresponding photometric variable is the spectral luminous flux $\Psi(\lambda)$ (unit \textrm{lm}/\textrm{m}), i.e., the flux as perceived by the human eye as a function of the wavelength.
Using the following relation between $\Psi_\textrm{r}$ and $\Psi$:
\begin{equation}
683 \bar{y}(\lambda) = \frac{\Psi(\lambda)}{\Psi_\textrm{r}(\lambda)},
\end{equation}
(\ref{eq:luminous_flux}) can be written as:
\begin{equation}
\Phi = \int_0^\infty \Psi(\lambda)\textrm{d}\lambda.
\end{equation}
%% Explain while integrals between 0 and infinity
%The luminous emittance $M = M(\vect{x}, \myangle)$ is the total flux emitted in all direction from a unit area. It is measured in lumens pr square meters (\textrm{lm}/$\textrm{m}^2$).
\\ \indent Geometric optics describes a beam of light as a collection of parallel light rays, where a light ray can be interpreted as a line or curve along which the electromagnetic energy travels. A ray is always direct perpendicular to the light's wavefront.
The infinitesimal luminous flux $\textrm{d}\Phi$ incident on an infinitesimal surface $\textrm{d}A$ is called illuminance $E$ (unit $\textrm{lm}/\textrm{m}^2$)
and is defined as:
\begin{equation}
 E=E(\vect{x}) = \frac{\textrm{d}\Phi}{\textrm{d}A}\;.
 \end{equation}
The corresponding radiometric variable is called \textit{irradiance}, we indicate it with $P$. The density of light emitted by a point source in a given direction depends on the solid angle.\\ \indent
The solid angle in a given direction is expressed by a cone of rays emitted in that particular direction by a point source located at the center of the unit sphere \cite{koshel2012illumination}. Let us now consider a finite source $\textrm{d}A$ and let $\textrm{d}S$ be the area on the unit sphere subtended by the cone,
the infinitesimal solid angle $\textrm{d}\Omega$ is given by:
\begin{equation}\label{solid_angle}
\textrm{d}\Omega = \textrm{d}S= \sin(\theta)\textrm{d}\theta \textrm{d}\varphi,
\end{equation}
 where $\myangle$ and $\varphi$ are the polar and the azimuthal angle, respectively, which the normal $\boldsymbol{\nu}$ to the infinitesimal area $\textrm{d}A$ makes with the direction of the central line of $\textrm{d}\Omega$ (see Figure \ref{fig:rad}).
The solid angle on the entire unit sphere is $\Omega = 4\pi$ and its unit is steradian ($\textrm{sr}$) \cite{arecchi2007field}.
The luminous intensity $I$ (unit candela, $\textrm{cd}=\textrm{lm}/\textrm{sr}$) is defined as the luminous flux $\textrm{d}\Phi$ per solid angle
$\textrm{d}\Omega$ and is given by:
\begin{equation}\label{intensity}
I = I(\myangle, \varphi) = \frac{\textrm{d}\Phi}{\textrm{d}\Omega}\;.
\end{equation}
 \begin{figure}[t]
%\label{fig:cup}
  \begin{center}
  \includegraphics[width=6 cm]{SolidAngle}
  \end{center}
  \caption{\textbf{Solid angle}. $\textrm{d}\Omega$ is in a given direction $\myangle$ and $\varphi$.}
  \label{fig:rad}
  \end{figure}
\\ \indent
The luminance $L = L(\vect{x}, \myangle, \varphi)$ (unit $\textrm{cd} / \textrm{m}^2$) depends both on the position and the direction, it is the luminous flux per unit solid angle $\textrm{d}\Omega$ and  per unit projected area $\cos\myangle \textrm{d}A$.  $L$  is given by:
\begin{equation}\label{luminance1}
  L=L(\vect{x}, \myangle, \varphi) = \frac{\textrm{d}\Phi}{\cos\myangle\textrm{d}A\textrm{d}\Omega}\,.
\end{equation}
\noindent Note that from ($\ref{intensity}$) and ($\ref{luminance1}$) we can derive a relation between the intensity and the luminance.
The intensity $I$ emitted by the infinitesimal area $\textrm{d}A$ is given by:
\begin{equation}\label{eq:int_lum}
I = \frac{\textrm{d}\Phi}{\textrm{d}\Omega}= L(\vect{x},\myangle, \varphi)\cos\myangle\textrm{d}A \,.
\end{equation}
When the luminance is uniform over a finite area $A$, the luminous intensity emitted in the direction $\myangle$ and $\varphi$ is:
\begin{equation}
I(\myangle, \varphi) = L(\myangle, \varphi) A \cos\myangle\,.
\end{equation}
Further, when $L(\vect{x},\myangle, \varphi)$ does not depend on the position and the direction (i.e. $L(\vect{x},\myangle, \varphi)=L$), we obtain Lambert's cosine law:
\begin{equation}
I(\myangle) = I_0\cos\myangle ,
\end{equation}
where $I_0 = I(\myangle = 0) = LA$. Light sources emitting light with a constant luminance are called \textit{Lambertian} sources.\\
\indent Finally, we give a definition of the \'{e}tendue $U$ (unit $\textrm{m}^2\textrm{sr}$).
\'{E}tendue is a french word that means extent or spread. In geometric optics, it is a quantity to describe how light is spread out in terms of area and solid angle \cite{lerner2006etendue, zhu2011etendue}.
The quantity $ \textrm{d}U $ of a source $\textrm{d}A$ is defined as:
\begin{equation}\label{etendue}
\textrm{d}U = n^2  \frac{1}{L}\textrm{d}\Phi = n^2 \cos\myangle\textrm{d}A\textrm{d}\Omega,
\end{equation}
where $n$ is the index of refraction of the medium in which $\textrm{d}A$ is immersed. In phase space optics the \'{e}tendue is considered to be a volume in phase space (or an area for two-dimensional systems). This concept will be clarified in Chapter \ref{chap:PS} in which we treat the phase space in more detail.
An important property is conservation of \'{e}tendue within an optical system in the absence of absorption.
In the following we show, using the approach of Chaves in \cite{chaves2015introduction},
how conservation of \'{e}tendue in a lossless system can be derived.
%For the optical systems we will consider in this work, the source and the target are located in the same medium (air) with $n=1$, so the luminance $L$ equals the basic luminance $L^* = L/n^2$ at the source and the target of the system.
Consider a light ray emitted from an infinitesimal area $\textrm{d}A_1$ to the area $\textrm{d}A_2$. Suppose that the centers of $\textrm{d}A_1$ and $\textrm{d}A_2$
are located at a distance \variabile{d} from each other and that both the surfaces are located in the same medium, see Figure \ref{fig:etendue_conservation}.
\begin{figure}[t]
 \label{fig:etendue_conservation}
     \begin{center}
     \includegraphics[width=10cm]{areas.pdf}
     \end{center}
     \caption{\textbf{Transfer of flux from the source $\textrm{d}A_1$ to the target $\textrm{d}A_2$.} $\textrm{d}A_1$ and $\textrm{d}A_2$ are two surfaces with normals $\nu_1$ and $\nu_2$, respectively. Their centers are located at a distance \variabile{d}.
$\myangle_1$ and $\myangle_2$ are the angles made by the central ray with the normals $\nu_1$ and $\nu_2$, respectively.}
\label{fig:etendue_conservation}
 \end{figure}
We derive \'{e}tendue conservation for the case in which $\textrm{d}A_1$ and $\textrm{d}A_2$ are located in the same medium (see \cite{chaves2015introduction, koshel2012illumination} for the general case).
We indicate with $\boldsymbol{\nu}_1$ and $\boldsymbol{\nu}_2$ the normals on the surfaces $\textrm{d}A_1$ and $\textrm{d}A_2$, and with $\myangle_1$ and $\myangle_2$ the angles that the ray connecting the centers of $\textrm{d}A_1$ and $\textrm{d}A_2$ forms with $\boldsymbol{\nu}_1$ and $\boldsymbol{\nu}_2$. The infinitesimal solid angle $\textrm{d}\Omega_1$ subtended by $\textrm{d}A_2$ at the center of $\textrm{d}A_1$ and the flux $\textrm{d}\Phi_1$ passing through $\textrm{d}A_2$ emitted from $\textrm{d}A_1$ are defined as:
\begin{subequations}
\begin{align}
\label{eq:omega1}
\textrm{d}\Omega_1 &= \frac{\textrm{d}A_2\cos(\myangle_2)}{\variabile{d}^2}\,,\\
\textrm{d}\Phi_1 &= L_1 \cos\myangle_1 \textrm{d}A_1 \textrm{d}\Omega_1. \label{eq:phi1}
\end{align}
\end{subequations}
Similarly, the infinitesimal solid angle $\textrm{d}\Omega_2$ subtended by $\textrm{d}A_1$ at the center of $\textrm{d}A_2$ and the flux $\textrm{d}\Phi_2$ passing through $\textrm{d}A_1$ emitted from $\textrm{d}A_2$ are equal to:
\begin{subequations}\begin{align}\label{eq:omega2}
\textrm{d}\Omega_2 &= \frac{\textrm{d}A_1\cos\myangle_1}{\variabile{d}^2}\,,\\
\textrm{d}\Phi_2 &= L_2 \cos\myangle_2 \textrm{d}A_2 \textrm{d}\Omega_2.\label{eq:phi2}
\end{align}
\end{subequations}
Then from (\ref{etendue}) we obtain the following relations:
\begin{subequations}
\begin{align}
\textrm{d}U_1 &= n^2\cos\myangle_1 \textrm{d}A_1\textrm{d}\Omega_1= \frac{n^2\cos\myangle_1 \textrm{d}A_1\textrm{d}A_2\cos\myangle_2}{\variabile{d}^2},\\
\textrm{d}U_2 &= n^2 \cos\myangle_2\textrm{d}A_2\textrm{d}\Omega_2= \frac{ n^2 \cos\myangle_2\textrm{d}A_2\textrm{d}A_1\cos\myangle_1}{\variabile{d}^2}, \label{eq:dU2}
%\end{split}
\end{align}
\end{subequations}
for $\textrm{d}A_1$ and $\textrm{d}A_2$, respectively.
%From equation ($\ref{etendue1}$) and ($\ref{etendue2}$)
From the previous equations we can conclude that $\textrm{d}U_1=\textrm{d}U_2$ and therefore the \'{e}tendue is conserved along a beam of light.
Since the system is a lossless system, energy is conserved from the source to the target ($\textrm{d}\Phi_1= \textrm{d}\Phi_2$), therefore the \'{e}tendue conservation implies:
\begin{equation}\label{basicluminance}
L_1 = n^2 \frac{\textrm{d}\Phi_1}{\textrm{d}U_1} = n^2 \frac{\textrm{d}\Phi_2}{\textrm{d}U_2} = L_2\,,
\end{equation}
where the first equality comes from (\ref{luminance1}) combined with (\ref{etendue}).
\\ \indent In this thesis we consider two-dimensional optical systems.
 Hence, the definitions of the photometric parameters have to be adapted to two-dimensions. An infinitesimal line segment of length $\textrm{d}\variabile{a}$ emitting a ray that makes an angle $\myangle$ with the normal $\boldsymbol{\nu}$ are considered, see Figure \ref{fig:2Dsolidangle}.
\begin{figure}[t]
 \label{fig:2Dsolidangle}
     \begin{center}
     \includegraphics[width=7cm]{solidangle2D.pdf}
     \end{center}
     \caption{\textbf{Ray emitted by an infinitesimal line segment.} $\textrm{d}a$ makes an angle $\myangle$ with respect to the line normal $\boldsymbol{\nu}$.}
\label{fig:2Dsolidangle}
 \end{figure}
The two-dimensional illuminance (unit $\textrm{lm}/\textrm{m}$) denotes the luminous flux received by an infinitesimal line segment of length $\textrm{d}\variabile{a}$
and it is given by:
 \begin{equation}
 E=\frac{\textrm{d}\Phi}{\textrm{d}\variabile{a}}\;.
 \end{equation}
%The luminous emittance $M = M(\variabile{x}, \myangle)$ is the total flux emitted in all direction from a line segment.
The luminous intensity \big(unit $\big[\textrm{lm}/\textrm{rad}\big]$\big) is the luminous flux per angle $\textrm{d}\myangle$:
 \begin{equation}
 I=\frac{\textrm{d}\Phi}{\textrm{d}\myangle}\;.
 \end{equation}
 The two-dimensional luminance (unit $\textrm{lm}/(\textrm{rad}\,\textrm{m})$) is given by:
 \begin{equation}
 L= \frac{\textrm {d}\Phi}{\cos\myangle\,\textrm{d}\variabile{a} \,\textrm{d}\myangle}.
 \end{equation}
 Thus the following relation holds:
 \begin{equation}
 I = L(x, \myangle)\cos\myangle\,\textrm{d}\variabile{a},
 \end{equation}
where $x$ is a certain position at the light source $\textrm{d}\variabile{a}$.
 Finally, the \'{e}tendue $\textrm{d}U $ (unit $\textrm{m}\;\textrm{rad}$) in two-dimensions is given by:
\begin{equation}\label{etendue2d}
\textrm{d}U = \n\cos\myangle\textrm{d}\variabile{a}\,\textrm{d}\myangle.
\end{equation}
An overview of the photometric variables used in this thesis is given in Table \ref{tab:photometric_variables}
\begin{table}[t]
\centering
\caption{\bf Photometric variables}
\begin{tabular}{lllll}
 \hline   \\
Name  & Symbol & Unit ($3D$) & Unit ($2D$) \\
  \hline
Luminous flux & $\Phi$   & $\textrm{lm}$   &  $\textrm{lm}$ \\
%Spectral luminous flux & $\Phi_\lambda$   & $$   &  $1.75\cdot10^{-4}$ \\
Illuminance/emittance  & $E$    & $\textrm{lm}/{\textrm{m}^2} $ & $\textrm{lm}/{\textrm{m}}$  \\
Intensity  & $I$    & $\textrm{lm}/{\textrm{sr}} = \textrm{cd}$  & $\textrm{lm}/\textrm{rad}$ \\
Luminance  & $L$  & $ \textrm{cd}/{\textrm{m}^2}$   & $\textrm{lm}/(\textrm{rad} \,\textrm{m})$ \\
\'{E}tendue & $U$  & $\textrm{m}^2\, \textrm{sr}$   & $\textrm{m}\, \textrm{rad}$ \\
 \hline
 \end{tabular}
\label{tab:photometric_variables}
 \end{table}
\\ \indent In order to determine the light distribution on a surface and to compute the photometric variables on that surface, we need to understand how the light emitted from a source propagates. In the field of geometric optics the light propagation is described by light rays.
The propagation of a light ray traveling through different media is determined by the reflection and refraction law.
In the following we introduce these two laws and we explain the total internal reflection phenomenon.
\section{Reflection and refraction law}\label{sec:reflection}
A light ray is described by a position vector \vect{x} on a surface and a direction vector \vect{t} and can be parametrized by the arc length \variabile{s}.
Light rays travel in a homogeneous medium along straight lines, once they hit a reflective surface their direction changes.
 Denoting with $\vect{t}_\textrm{i}$ the direction of the incident ray and with $\boldsymbol{\nu}$ the unit normal to the surface at the location of incidence, the direction $\vect{t}_\textrm{r}$ of the reflected ray is given by:
 \begin{equation}\label{Reflection}
  \vect{t}_\textrm{r} = \vect{t}_\textrm{i}-2 (\vect{t}_\textrm{i}\boldsymbol{\cdot}\boldsymbol{\nu})\boldsymbol{\nu},
\end{equation}
where the vectors $\vect{t}_\textrm{i}$ and $\boldsymbol{\nu}$ are unit vectors and $\vect{t}_\textrm{i}\boldsymbol{\cdot}\boldsymbol{\nu}$ indicates the scalar product of
$\vect{t}_\textrm{i}$ and $\boldsymbol{\nu}$.
From the previous equation it follows that the vector  $\vect{t}_\textrm{r}$ is a unit vector too, indeed considering the scalar product $\vect{t}_\textrm{r}\boldsymbol{\cdot}\vect{t}_\textrm{r}$ we conclude:
\begin{equation}\label{unit_vector}
\vect{t}_\textrm{r}\boldsymbol{\cdot}\vect{t}_\textrm{r} = \vect{t}_\textrm{i}\boldsymbol{\cdot}\vect{t}_\textrm{i}
- 4(\vect{t}_\textrm{i}\boldsymbol{\cdot}\boldsymbol{\nu})(\vect{t}_\textrm{i}\boldsymbol{\cdot}\boldsymbol{\nu})+
4(\vect{t}_\textrm{i}\boldsymbol{\cdot}\boldsymbol{\nu})^2(\boldsymbol{\nu}\boldsymbol{\cdot}\boldsymbol{\nu})=1 .
\end{equation}
Note from (\ref{Reflection}) that the vectors $\vect{t}_\textrm{i}$, $\vect{t}_\textrm{r}$ and $\boldsymbol{\nu}$ are coplanar.
Indicating with $\myangle_\textrm{i}$ the incident angle and with $\myangle_\textrm{r}$ the reflective angle such that $\myangle_\textrm{i}$, $\myangle_\textrm{r} \in[0, \pi/2)$,
the reflection law states that $\myangle_\textrm{i}=\myangle_\textrm{r}$, see Figure \ref{fig:Snell}.
\begin{figure}[t]
 \label{fig:Snell}
     \begin{center}
     \includegraphics[width=8cm]{reflection}
     \end{center}
     \caption{\textbf{Propagation of a ray.} The ray travels through two materials with index of refraction $\n_\textrm{i}$ and $\n_\textrm{t}$.}% \bolsymbol{$\nu$} is the normal to the surface $A$ that the incident ray hits.
%$\vect{t}_i$, $\vect{t}_r$ and $\vect{t}_t$ are the direction vectors of the incident, reflected and refracted (or transmitted) ray, respectively. }}
%\myangle$_{i}$, \myangle$_{r}$ and \myangle$_{t}$ are the angles between \bolsymbol{$\nu$} and the incident, reflected and transmitted ray, respectively.}}
\label{fig:Snell}
 \end{figure}
\\ \indent When a ray propagates through two different media, its direction changes according to the law of refraction.
Indicating with $n_\textrm{i}$ the index of refraction of the medium in which the incident ray travels and with
$n_\textrm{t}$ the index of refraction of the medium of the transmitted ray, the direction of the transmitted ray is given by:
\begin{equation}\label{Refraction}
\vect{t}_\textrm{t} = n_{\textrm{i},\textrm{t}}\,\vect{t}_\textrm{i}-
\Big[\sqrt{1-n_{\textrm{i},\textrm{t}}^2+
n_{\textrm{i},\textrm{t}}^2(\boldsymbol{\nu}\boldsymbol{\cdot}\vect{t}_\textrm{i})^2}
+n_{\textrm{i},\textrm{t}}(\boldsymbol{\nu}\boldsymbol{\cdot}\vect{t}_\textrm{i}) \Big]\boldsymbol{\nu}\,,
\end{equation}
where $n_{\textrm{i},\textrm{t}}=\n_\textrm{i}/\n_\textrm{t}$ \cite{chaves2015introduction}.
While the direction of the normal $\boldsymbol{\nu}$ to the surface is not relevant for the computation of the direction of the reflected ray, in fact:
\begin{equation}
\vect{t}_\textrm{r} = \vect{t}_\textrm{i}-2(\vect{t}_\textrm{i}\boldsymbol{\cdot}\boldsymbol{\nu})\boldsymbol{\nu}= \vect{t}_\textrm{i}-2(\vect{t}_\textrm{i}\boldsymbol{\cdot}(-\boldsymbol{\nu}))(-\boldsymbol{\nu}),
\end{equation}
for computing the direction of the refracted ray, we need to specify the direction of $\boldsymbol{\nu}$ which is usually chosen so that the scalar product between the incident ray and the normal is negative $\vect{t}_{\textrm{i}}\boldsymbol{\cdot}\boldsymbol{\nu}<0$.
Considering the cross product of both the terms in Equation (\ref{Refraction}) with the normal $\boldsymbol{\nu}$ of the incident surface we obtain:
\begin{equation}
\vect{t}_\textrm{t}\times\boldsymbol{\nu} = n_{\textrm{i},\textrm{t}}(\vect{t}_\textrm{i}\times\boldsymbol{\nu}),
\end{equation}
which leads to the Snell's law:
\begin{equation}\label{eq:snell}
n_\textrm{t}\sin(\myangle_\textrm{t}) = \n_\textrm{i}\sin(\myangle_\textrm{i}).
\end{equation}
Note that (\ref{Refraction}) is only valid for
\begin{equation}\label{tir}
1-\n_{\textrm{i},\textrm{t}}^2+\n_{\textrm{i},\textrm{t}}^2(\boldsymbol{\nu}\boldsymbol{\cdot}\vect{t}_\textrm{i})^2\geq 0,
\end{equation} which implies that
\begin{equation}
\frac{\n_\textrm{t}}{\n_\textrm{i}}\geq \sqrt{1-(\boldsymbol{\nu}\boldsymbol{\cdot}\vect{t}_\textrm{i})^2},
\end{equation}
from which we obtain:
\begin{equation}
 %n_\textrm{t}\geq n_\textrm{i}\sqrt{1-\cos^2\myangle_\textrm{i}}=
 n_\textrm{t}\geq \n_\textrm{i} \sin\myangle_\textrm{i}\,.
\end{equation}
 The angle $\myangle_{\textrm{c}}$ for which the equality holds is
\begin{equation}\label{critical}
\myangle_{\textrm{c}} = \arcsin\Big(\frac{n_\textrm{t}}{\n_\textrm{i}}\Big),
\end{equation} and it is called the critical angle \cite{chaves2015introduction}.
%Note that the condition $\frac{n_t}{n_i}<1$ is verified as in this case $\sin(\myangle_\textrm{i})<1$.
When the incident angle $\myangle_{\textrm{i}}$ is exactly equal to the critical angle $\myangle_{\textrm{c}}$, the square root in (\ref{Refraction}) is zero and $\vect{t}_\textrm{t}\boldsymbol{\cdot}\boldsymbol{\nu}=0$, hence the transmitted ray propagates parallel to the refractive surface.
When $\myangle_{\textrm{i}}>\myangle_{\textrm{c}}$ the light ray is no longer refracted but is only reflected by the surface. This phenomenon is called total internal reflection (TIR). When TIR occurs, $100\%$ of the incident light is reflected and there is no refraction. Therefore, optical systems designed such that rays are reflected by TIR are very efficient. \\ \indent
In general, light that hits an ordinary refractive surface can be both reflected and refracted. Every incident ray generates two rays when interacting with a surface. Each of them carries a fraction of the total energy of the incident ray. Obviously, the sum of the reflected and transmitted energy equals the incident power.
The amount of energy transported by the reflected and the refracted ray is determined by the Fresnel equations. In the next paragraph an overview of the Fresnel coefficients is given based on the theory in \cite{hecht1998hecht} (Section $4.6$).
\section{Fresnel's coefficients for reflection and refraction}\label{sec:fresnel}
In order to derive Fresnel's equations we need to describe light as an electromagnetic wave.
It is therefore useful to study light propagation from the perspective of electromagnetic theory which gives information about the incident, reflected and transmitted radiant flux density, denoted with $P_\textrm{i}$, $P_\textrm{r}$ and $P_\textrm{t}$, respectively.
The electric field $\boldsymbol{\mathcal{E}}$ can be written as:
\begin{equation}
\boldsymbol{\mathcal{E}}(\vect{x}, \mytime) = \boldsymbol{\mathcal{E}}_0(\vect{x} )e^{i( \vect{k}\boldsymbol{\cdot}\vect{x}-\omega \mytime)},
\end{equation}
where \vect{k} is the vector in the direction of the field propagation with modulus
$|\vect{k}| = k$, \vect{x} is the position vector and $\mytime$ is the time. The amplitude $\boldsymbol{\mathcal{E}}_0(\vect{x})$ is constant in time and $\omega = c\,k/ \n$ is the value of the angular frequency with $c$ the velocity of light in vacuum and $\n$ the index of refraction in which the wave is traveling. We recall that $\n = c/v$ with $v$ the speed of light in the material. Note that the angular frequency can be also written as $\omega = v\,k$ (in vacuum $\n=1$ and $\omega=c\,k$). The parameter
$k =2\,\pi/\lambda$ is the wave number in vacuum, with $\lambda$ the wavelength. \\ \indent Similarly, the magnetic field has the form:
\begin{equation}
\boldsymbol{\mathcal{B}}(\vect{x}, \mytime) = \boldsymbol{\mathcal{B}}_0(\vect{x}) e^{i( \vect{k}\boldsymbol{\cdot} \vect{x}-\omega \mytime )},
\end{equation}
where $\boldsymbol{\mathcal{B}}_0(\vect{x})$ is the amplitude of the magnetic field.
The electric and magnetic fields satisfy the following relations:
\begin{subequations}\label{eq:electric_magnetic}
\begin{align}
\frac{\vect{k}}{k} \boldsymbol{\times} \boldsymbol{\mathcal{E}} & = v \, \boldsymbol{\mathcal{B}}, \\
\frac{\vect{k}}{k} \boldsymbol{\cdot}\boldsymbol{\mathcal{E}} &=0.
\end{align}
\end{subequations}
% Say right hand system
\\ \indent Light can be considered as an electromagnetic wave, consisting of an electric field $\boldsymbol{\mathcal{E}}$ and a magnetic field $\boldsymbol{\mathcal{B}}$, which propagates always perpendicular to $\boldsymbol{\mathcal{E}}$ and $\boldsymbol{\mathcal{B}}$ (see Equations (\ref{eq:electric_magnetic})). By convention, the direction of the electric field $\boldsymbol{\mathcal{E}}$ with respect to the incident plane defines the \textit{polarization} of an electromagnetic wave \cite{feynman1964feynman} (see Figure \ref{fig:planeofincidence}). \\
%The direction of $\boldsymbol{\mathcal{E}}$ is given by the incident and reflected rays as is shown in Figure \ref{fig:planeofincidence}. \\
\indent Fresnel's equations were introduced to describe the effect of an incident wave when encountering an interface located between two media having different indexes of refraction. In particular, the Fresnel coefficients determine the fractions of transmitted and reflected energy.
In the following we provide the Fresnel coefficients and we briefly explain their physical interpretation.
We refer the reader to \cite{hecht1998hecht, feynman2011feynman, born2013principles} for more details. To derive Fresnel's equations the polarization of light must be taken into account.
\\ \indent Light is said to be \textit{polarized} if the electric field oscillates in a single plane. Light is \textit{unpolarized} when the direction of this electric field changes randomly in time.
\begin{figure}[t]
 \label{fig:planeofincidence}
     \begin{center}
     \includegraphics[width=10cm]{plane_of_incidence1}
     \end{center}
     \caption{\textbf{Light ray that hits a mirror located on the reflecting plane.} The incident and the reflected ray are coplanar with the normal to the mirror. The plane of incidence is spanned by the reflected and the refracted rays. The plane of interface is perpendicular to the plane of incidence.}
\label{fig:planeofincidence}
 \end{figure}
The light polarization can be classified into three different kinds of polarization:
\begin{itemize}
\item \textit{Linear polarization}: The electric filed is confined to a single plane perpendicular to the direction of propagation;
\item \textit{Circular polarization}: The electric field describes a circle around the direction of propagation;
\item \textit{Elliptic polarization}: The electric field describes an ellipse around the direction of propagation.
\end{itemize}
Any form of light can be defined by two orthogonal linear polarizations.
Hence, the following two cases of light polarization are considered:
\begin{enumerate}
\item $\boldsymbol{\mathcal{E}}$ is perpendicular to the plane of incidence, and therefore $\boldsymbol{\mathcal{B}}$ is parallel to it (see Figure \ref{fig:electric_field}). In this case light is said to be \textit{s-polarized} (from the German word \textit{senkrecht}).
\item $\boldsymbol{\mathcal{E}}$ is parallel to the plane of incidence and, therefore $\boldsymbol{\mathcal{B}}$ is perpendicular to it (see Figure \ref{fig:electric_field_p}). In this case light is said to be \textit{p-polarized} (from the German word \textit{parallel}).
\end{enumerate}
\begin{figure}[t]
 \label{fig:electric_field}
     \begin{center}
     \includegraphics[width=8cm]{electric_field}
     \end{center}
     \caption{\textbf{Propagation of an electromagnetic wave for s-polarized light.} The components of $\boldsymbol{\mathcal{E}}$ are indicated with the green circles.
The components of $\boldsymbol{\mathcal{B}}$ are indicated with red arrows.}
\label{fig:electric_field}
 \end{figure}
\begin{figure}[t]
 \label{fig:electric_field_p}
     \begin{center}
     \includegraphics[width=8cm]{electric_field_p}
     \end{center}
 \caption{\textbf{Propagation of an electromagnetic wave for p-polarized light.} The components of $\boldsymbol{\mathcal{B}}$ are indicated with the red circle.
%, they are in the $(\variabile{x},\variabile{z})$ plane and they are oriented in the positive direction of $\variabile{z}$.
The components of $\boldsymbol{\mathcal{E}}$ are indicated with green arrows.}
%and they are located in the $(\variabile{x}, \variabile{z})$ plane.}
\label{fig:electric_field_p}
 \end{figure}
The amplitude Fresnel coefficient $r$ is defined by the ratio between the amplitudes of the reflected and the incident electric field. Similarly, $t$ is the ratio between the amplitudes of the transmitted and the incident electric field. They are defined by:
\begin{equation}
\begin{split}
r & =\frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{r}}|}{|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|}, \\
t & =\frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{t}}|}{|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|}.
\end{split}
\end{equation}
The Fresnel equations provide the perpendicular $r_\textrm{s}$ and $t_\textrm{s}$ and parallel $r_\textrm{p}$ and $t_\textrm{p}$ versions of $r$ and $t$, respectively.
Energy conservation gives the boundary conditions of the electromagnetic field at the plane of the interface (perpendicular to the incident plane), from which the Fresnel coefficients are derived. They are defined in both case $1$ and case $2$. Since the mathematical formulation is similar for the two cases, in the following we explain in detail the computation of the Fresnel coefficients only for s-polarized light (case $1$).\\
\indent For s-polarized light, the component tangential to the interface of $\boldsymbol{\mathcal{E}}$ and $\boldsymbol{\mathcal{B}}/\mu$ are continuous across the boundary between the two different media, where $\mu$ is the permeability of the material in which the electromagnetic field is located. From now on, all quantities defined in the medium of the incident, reflective and transmitted light are indicated with the subscripts \textrm{i}, \textrm{r} and \textrm{t}, respectively. The continuity of the tangential component of $\boldsymbol{\mathcal{E}}$ leads to:
\begin{equation}\label{Econservation}
|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|+|\boldsymbol{\mathcal{E}}_{0\textrm{r}}|= |\boldsymbol{\mathcal{E}}_{0\textrm{t}}|,
\end{equation}
%where we have indicated with $\mathcal{E}_{0\textrm{i}}$, $\mathcal{E}_{0\textrm{r}}$ and $\mathcal{E}_{0\textrm{t}}$
%the magnitudes of $\boldsymbol{\mathcal{E}}_{0\textrm{i}}$, $\boldsymbol{\mathcal{E}}_{0\textrm{r}}$ and $\boldsymbol{\mathcal{E}}_{0\textrm{t}}$, respectively.
while the continuity of the tangential component of $\boldsymbol{\mathcal{B}}/\mu$ gives:
\begin{equation}\label{Bconservation}
-\frac{|\boldsymbol{\mathcal{B}}_{0\textrm{i}}|}{\mu_\textrm{i}}\cos\myangle_{\textrm{i}}+\frac{|\boldsymbol{\mathcal{B}}_{0\textrm{r}}|}{\mu_\textrm{r}}\cos\myangle_{\textrm{r}} =
-\frac{|\boldsymbol{\mathcal{B}}_{0\textrm{t}}|}{\mu_\textrm{t}}\cos\myangle_{\textrm{t}},
\end{equation}
where the sign convention is chosen as illustrated in Figure \ref{fig:electric_field}.
%where we have indicated with $\mathcal{B}_{\textrm{i}}$, $\mathcal{B}_{\textrm{r}}$ and $\mathcal{B}_{\textrm{t}}$
%the absolute values of $\boldsymbol{\mathcal{B}}_{\textrm{i}}$,$\boldsymbol{\mathcal{B}}_{\textrm{r}}$ and $\boldsymbol{\mathcal{B}}_{\textrm{t}}$, respectively.
Since $|\boldsymbol{\mathcal{B}}| = |\boldsymbol{\mathcal{E}}|/v$, (\ref{Bconservation}) can be written as
\begin{equation}
\frac{1}{\mu_{\textrm{i}}v_{\textrm{i}}}(|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|-|\boldsymbol{\mathcal{E}}_{0\textrm{r}}|)\cos\myangle_{\textrm{i}} = \frac{1}{\mu_{\textrm{t}}v_{\textrm{t}}}|\boldsymbol{\mathcal{E}}_{0\textrm{t}}|\cos\myangle_{\textrm{t}},
\end{equation}
where we employed the fact that $v_{\textrm{i}}= v_{\textrm{r}}$, and $\myangle_{\textrm{i}}= \myangle_{\textrm{r}}$.
Since $n = c/v$, the previous equation becomes:
\begin{equation}
\frac{n_{\textrm{i}}}{\mu_{\textrm{i}}}(|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|-|\boldsymbol{\mathcal{E}}_{0\textrm{r}}|)\cos\myangle_{\textrm{i}} = \frac{n_{\textrm{t}}}{\mu_{\textrm{i}}}|\boldsymbol{\mathcal{E}}_{0\textrm{t}}|\cos\myangle_{\textrm{t}}.
\end{equation}
Most often used optical materials are non-magnetic, hence we assume $\mu_{\textrm{i}}=\mu_{\textrm{t}}=\mu_{0}$ \cite{lvovsky2013fresnel}. Employing (\ref{Econservation}) we arriving at the Fresnel coefficients for s-polarized light:
\begin{equation} \label{Fresnel_perpendicular}
\begin{split}
r_{\textrm{s}} & =\frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{r}}|_{\textrm{s}}}{|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|_\textrm{s}} =
\frac{n_\textrm{i}\cos\myangle_\textrm{i}-n_\textrm{t} \cos\myangle_\textrm{t}}{n_\textrm{i}
\cos\myangle_\textrm{i}+n_\textrm{t}\cos\myangle_\textrm{t}},\\
t_{\textrm{s}} & = \frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{t}}|_\textrm{s}}{|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|_\textrm{s}}
=\frac{2\n_\textrm{i}\cos\myangle_\textrm{i}}{n_\textrm{i}\cos\myangle_\textrm{i}+\n_\textrm{t}\cos\myangle_\textrm{t}},\\
\end{split}
\end{equation}
where the subscript $\textrm{s}$ is used to remind the reader that we are considering s-polarized light.
Using Snell's law (Equation (\ref{eq:snell})) the first pair of the Fresnel equations restricted to the case $\myangle_\textrm{i}\leq\myangle_\textrm{c}$ becomes:
\begin{equation}
\fbox{\begin{minipage}{18em}
\begin{equation*} \label{simple_Fresnel}
\begin{split}
r_{\textrm{s}} & = -\frac{\sin(\myangle_\textrm{i}-\myangle_\textrm{t})}{\sin(\myangle_\textrm{i}+\myangle_\textrm{t})}\\
t_{\textrm{s}} & = \frac{2\sin \myangle_\textrm{t} \cos \myangle_\textrm{i}}{\sin(\myangle_\textrm{i}+\myangle_\textrm{t})}
\end{split}
\end{equation*}
\end{minipage}}
\end{equation}

A similar argument for p-polarized light leads to the calculation of the parallel versions $r_\textrm{p}$ and $t_\textrm{p}$ of $r$ and $t$.
The boundary conditions for the electric and magnetic field read:
\begin{subequations}
\begin{align}
|\boldsymbol{\mathcal{E}}_{0 \textrm{i}}|\cos\myangle_{\textrm{i}} - |\boldsymbol{\mathcal{E}}_{0 \textrm{r}}|\cos\myangle_{\textrm{r}} &=
|\boldsymbol{\mathcal{E}}_{0 \textrm{t}}|\cos\myangle_{\textrm{t}}\\
|\boldsymbol{\mathcal{B}}_{0 \textrm{i}}| +
|\boldsymbol{\mathcal{B}}_{0 \textrm{r}}| &=
|\boldsymbol{\mathcal{B}}_{0 \textrm{t}}|,
\end{align}
\end{subequations}
leading to the second pair of Fresnel's equations:
\begin{equation}\label{Fresnel_parallel}
\begin{split}
r_{\textrm{p}} & = \frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{r}}|_{\textrm{p}}}{|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|_\textrm{p}} = \frac{\n_\textrm{t}\cos\myangle_\textrm{i}-n_\textrm{i} \cos\myangle_\textrm{t}}{n_\textrm{i} \cos\myangle_\textrm{t}+n_\textrm{t}\cos\myangle_\textrm{i}},\\
t_{\textrm{p}} & =\frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{t}}|_{\textrm{p}}}{|\boldsymbol{\mathcal{E}}_{0\textrm{i}}|_\textrm{p}} =  \frac{2\n_\textrm{i}\cos\myangle_\textrm{i}}{n_\textrm{i}\cos\myangle_\textrm{t}+n_\textrm{t}\cos\myangle_\textrm{i}}.
\end{split}
\end{equation}
%where $\myangle_\textrm{i}\leq\myangle_\textrm{c}$.
Similarly to the $s$-polarized case, using Snell's law, the previous equations when $\myangle_\textrm{i}\leq\myangle_\textrm{c}$ reduce to:
\begin{equation}
\fbox{\begin{minipage}{18em}
\begin{equation*} \label{simple_Fresnel_p}
\begin{split}
r_{\textrm{p}} & =  \frac{\tan(\myangle_\textrm{i}-\myangle_\textrm{t})}{\tan(\myangle_\textrm{i}+\myangle_\textrm{t})}\\
t_{\textrm{p}} & = \frac{2\sin \myangle_\textrm{t} \cos \myangle_\textrm{i}}{\sin(\myangle_\textrm{i}+\myangle_\textrm{t})\cos(\myangle_\textrm{i}- \myangle_\textrm{t})}
\end{split}
\end{equation*}
\end{minipage}}
\end{equation}
Furthermore, it can be checked that
 \begin{equation}
t_\textrm{s}-r_\textrm{s} = 1
\end{equation}
holds for every $\myangle_{\textrm{i}}$, while
\begin{equation}
t_\textrm{p}+r_\textrm{p} =  1
\end{equation}
is valid only at normal incidence ($\myangle_{\textrm{i}} = 0$).
\\ \indent The amplitude coefficients are shown in Figure \ref{fig:coefficients} for the case in which light travels from a less dense to a more dense medium, i.e., $\n_\textrm{i}<n_\textrm{t}$ where $\n_{\textrm{i}}= 1$ and $\n_{\textrm{t}}=1.5$, thus $\myangle_\textrm{i}>\myangle_\textrm{t}$.
In Figure \ref{fig:coefficients2} the amplitude coefficients are shown for the case in which $\n_\textrm{i}>n_\textrm{t}$ with $\n_{\textrm{i}}= 1.5$ and $\n_{\textrm{t}}=1$ ($\myangle_\textrm{i}<\myangle_\textrm{t}$). Note from Figure \ref{fig:coefficients} that $r_\textrm{p}$ approaches $0$ when $\myangle_\textrm{i}$ approaches $\myangle_\textrm{p}$ and it gradually decreases, reaching $-1$ for an incident angle $\myangle_\textrm{i}=90^\circ$. The angle $\myangle_\textrm{p}$ is called \textit{Brewster's angle} or polarization angle as only the component perpendicular to the incident plane is reflected at that angle and therefore light is perfectly polarized. Similarly, Figure \ref{fig:coefficients2} shows that $r_\textrm{p}=0$ for $\myangle_\textrm{i}= \myangle_{\textrm{p}\prime}$. It can be show that $\myangle_\textrm{p}+ \myangle_{\textrm{p}\prime}= 90^\circ$. Both $r_\textrm{p}$ and $r_\textrm{s}$ reach $1$ when $\myangle_\textrm{i}~=~\myangle_\textrm{c}$. $\myangle_\textrm{c}$ is called the critical angle. Light that hits the incident plane with an incident angle equal to or greater than $\myangle_{\textrm{c}}$ is totally reflected back and no transmitted light is observed. This phenomenon is called total internal reflection.
\begin{figure}[t]
  \begin{minipage}[h]{0.48\textwidth}
    \includegraphics[width=\textwidth]{amplitude_coefficients}
    \caption{\textbf{Amplitude coefficients of reflection and transmission.} $n_\textrm{i}<n_\textrm{t}$
($n_\textrm{i} = 1$ and $\n_\textrm{t}=1.5$). $\myangle_\textrm{p} ~=~ 56.3^\circ$ is the polarization angle.}
    \label{fig:coefficients}
  \end{minipage}\hfill
  \begin{minipage}[h]{0.48\textwidth}
    \includegraphics[width=\textwidth]{amplitude_coefficients3}
    \caption{\textbf{Amplitude coefficients.} $n_\textrm{i}>n_\textrm{t}$
($n_\textrm{i} = 1.5$ and $n_\textrm{t}=1$). $\myangle_{p\prime}~=~ 33.7^\circ$ is the polarization angle and $\myangle_\textrm{c}= 41.8^\circ$ is the critical angle.}
   \label{fig:coefficients2}
 \end{minipage}
\end{figure}\\
%\indent The  we introduce the Poynting vector \vect{P} that defines the energy flux of an electromagnetic field.
%It is measured in $[\textrm{W}/\textrm{m}^2]$, and it is given by:
%\begin{equation}
%\vect{P} = \frac{1}{\mu}\Big(\boldsymbol{\mathcal{E}}\boldsymbol{\times} \boldsymbol{\mathcal{B}}\Big),
%\end{equation}
%where $\mu = \frac{1}{\varepsilon v^2}$ is the permeability and $\varepsilon$ the permittivity of the medium.
% In the following, the parameters for vacuum are indicated with the subscript $0$. Optical rays are perpendicular to the wave front of an electromagnetic wave and parallel to the Poynting vector \cite{jones2015optical}.
\indent To calculate the amount of energy reflected and transmitted, it is necessary to define the reflection and transmission coefficients in terms of the irradiance (or radiant flux density) $P$ rather than in terms of amplitudes of the electric field. For a wave of amplitude $|\boldsymbol{\mathcal{E}}_0|$ propagating in a non-magnetic medium with a refractive index $n$, the energy flux $P$ is given by:
%Therefore, defining the average of the vector \vect{P} over the time as:
%\begin{equation}
%\langle \vect{P} \rangle_T = \frac{1}{T}\int_0^T \vect{P}\textrm{d}T
%\end{equation}
%we can write the irradiance $E$ as:
\begin{equation}
P = \frac{c\, n \, \varepsilon_0}{2}|\boldsymbol{\mathcal{E}}_0|^2,
\end{equation}
\cite{hecht1998hecht}. For a beam of light that hits a surface such that an area $A$ is illuminated,
$P$ is the average energy that crosses in unit time a unit area $A$ perpendicular to the direction of the energy flow.
We indicate with $P_{\textrm{i}}$, $P_{\textrm{r}}$ and $P_{\textrm{t}}$ the incident, reflected and transmitted flux densities, respectively.
The energy per unit time for the incident, reflected and transmitted beams are
$P_\textrm{
i} A\cos\myangle_\textrm{i}$, $P_\textrm{r} A\cos\myangle_\textrm{r}$, and
$P_\textrm{t} A\cos\myangle_\textrm{t}$, respectively. % as is shown in Figure \ref{}
The reflectance $\mathcal{R}$ is the ratio of the reflected power to the incident power:
\begin{equation}\label{reflectance}
\mathcal{R} = \frac{P_\textrm{r}\cos\myangle_\textrm{r}}{P_\textrm{i}\cos\myangle_\textrm{i}} = \frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{r}}|^2}{|\boldsymbol{\mathcal{E}}_{0 \textrm{i}}|^2} = r^2,
\end{equation}
where the second equality holds because $\n_{\textrm{i}}= \n_{\textrm{r}}$, $\varepsilon_{\textrm{i}} = \varepsilon_{\textrm{r}}$ and $\myangle_{\textrm{i}} = \myangle_{\textrm{r}}$.
Similarly, the transmittance $\mathcal{T}$ is the ratio between the transmitted to the incident power:
\begin{equation}\label{transmittance}
\mathcal{T} = \frac{P_\textrm{t} \cos\myangle_\textrm{t}}{P_\textrm{i}\cos\myangle_\textrm{i}} = \frac{n_\textrm{t} \cos\myangle_\textrm{t}}{n_\textrm{i} \cos\myangle_\textrm{i}}\frac{|\boldsymbol{\mathcal{E}}_{0 \textrm{t}}|^2}{\boldsymbol{|\mathcal{E}}_{0 \textrm{i}}|^2} = \frac{n_\textrm{t} \cos\myangle_\textrm{t}}{n_\textrm{i} \cos\myangle_\textrm{i}} t^2\,.
\end{equation}
Employing total energy conservation, that is:
\begin{equation}
P_\textrm{i} A\cos\myangle_\textrm{i} = P_\textrm{r} A\cos\myangle_\textrm{r}+P_\textrm{t} A\cos\myangle_\textrm{t},
\end{equation}
we can easily prove that:
\begin{equation}
\fbox{\begin{minipage}{10em}
\begin{equation*}
\mathcal{R}+\mathcal{T}=1\,.
\end{equation*}
\end{minipage}}
\end{equation}
 The perpendicular and parallel versions of $\mathcal{R}$ and $\mathcal{T}$ are given by:
\begin{equation}
\fbox{\begin{minipage}{15em}
\begin{equation*}\label{Fresnel_pands}
\begin{split}
\mathcal{R}_\textrm{s} &=  r_\textrm{s}^2\\
\mathcal{T}_\textrm{s} &= \frac{n_\textrm{t} \cos\myangle_\textrm{t}}{n_\textrm{i} \cos\myangle_\textrm{i}}t_\textrm{s}^2\\
\mathcal{R}_\textrm{p}& =  {r_\textrm{p}^2}\\
\mathcal{T}_\textrm{p} &=  \frac{n_\textrm{t} \cos\myangle_\textrm{t}}{n_\textrm{i} \cos\myangle_\textrm{i}}t_\textrm{p}^2\\
\end{split}
\end{equation*}
\end{minipage}}
\end{equation}
It can be shown that
%\begin{equation}
%\fbox{\begin{minipage}{15em}
\begin{equation}
\begin{split}
\mathcal{R}_\textrm{s}+\mathcal{T}_\textrm{s} &= 1,\\
\mathcal{R}_\textrm{p}+\mathcal{T}_\textrm{p} &=1\,.
\end{split}
\end{equation}
%\end{minipage}}
%\end{equation}
For normal incidence, i.e., $\myangle_\textrm{i} = 0$, the incident plane is not defined and there is no distinction between the perpendicular and the parallel versions of $\mathcal{R}$ and $\mathcal{T}$. As a consequence, (\ref{Fresnel_pands}) combined with (\ref{Fresnel_perpendicular}) and (\ref{Fresnel_parallel}) leads to:
\begin{equation}\label{eq:fresnel_pands2}
\begin{split}
\mathcal{R} &= \mathcal{R}_\textrm{p} = \mathcal{R}_\textrm{s} = \Bigg(\frac{n_\textrm{i}-n_\textrm{t}}{n_\textrm{t}+n_\textrm{i}}\Bigg)^2, \\
\mathcal{T} &= \mathcal{T}_\textrm{p} = \mathcal{T}_\textrm{s} = \frac{4n_\textrm{i} n_\textrm{t}}{(n_\textrm{t}+n_\textrm{i})^2}\,.
\end{split}
\end{equation}
%In two dimensions light hits lines instead of surfaces. Therefore only the plane of incidence is defined and it has no sense to consider separately the parallel and the perpendicular polarization.
%If $\mathcal{R}_\textrm{p} = \mathcal{R}_\textrm{s}$ and $\mathcal{T}_\textrm{p} = \mathcal{T}_\textrm{s}$, then the amount of energy in both polarization states is the same \cite{hecht1998hecht}.\\ \indent

Many common light sources such as the sun, halogen lighting, LED spotlights, and incandescent bulbs produce unpolarized light.
In this case, light can be represented by the sum of two orthogonal states. For unpolarized light, the convention is to take as reflectance and transmittance the averages:
\begin{equation}
\fbox{\begin{minipage}{15em}
\begin{equation*}\begin{split}
\mathcal{R} &= \frac{\mathcal{R}_\textrm{p}+ \mathcal{R}_\textrm{s}}{2}\\
\mathcal{T} &= \frac{\mathcal{T}_\textrm{p}+ \mathcal{T}_\textrm{s}}{2}
\end{split}
\label{eq:RandTin2D}
\end{equation*}
\end{minipage}}
\end{equation}
where $\mathcal{R}_\textrm{p}$, $\mathcal{R}_\textrm{s}$, $\mathcal{T}_\textrm{p}$ and $\mathcal{T}_\textrm{s}$ are obtained from (\ref{Fresnel_pands}). The reflectance $\mathcal{R}$ and the transmittance $\mathcal{T}$ give the fraction of the power of energy reflected and transmitted at every interaction of a light ray with a Fresnel surface.

With this overview we conclude this chapter. The notions given in Section \ref{sec:photometry} will be used in the entire thesis as our goal is to study the distribution of light at the target of some optical systems. In particular, we will focus on the computation of the output intensity distribution. The reflection and refraction laws explained in Section \ref{sec:reflection} are needed to determine how the optical system changes the ray's direction every time that it hits a surface (or a line in the two-dimensional case). In Chapters \ref{chap:raytracing}-\ref{chap:raymapping2} we restrict ourselves to pure reflection and refraction; the Fresnel reflection and refraction are ignored. Systems with Fresnel reflection is postponed until Chapter \ref{chap:fresnel}. The amount of reflected and transmitted light is calculated using (\ref{eq:RandTin2D}) and (\ref{Fresnel_pands}) where $r_\textrm{s}$ and $t_\textrm{s}$ are obtained from (\ref{Fresnel_perpendicular}) and $r_\textrm{p}$ and $t_\textrm{p}$ from (\ref{Fresnel_parallel}). In our simulations we consider the general case of unpolarized light.


\chapter{\proj Object Detection}
\label{ch_mvm}
\index{detection}

\markright{Object detection}

\section{Multiscale Vision Model}
\label{sect_detect}
\subsection{Introduction}
After applying the wavelet transform on the image, we have to
detect, to extract, to measure and to recognize the significant
structures. The wavelet space of a 2D direct space
is a 3D one. An object has to be defined in
this space.  A general idea for object definition lies in the connectivity
property. An object occupies a physical region, and in this region we
can join any pixel to other ones. The connectivity in the direct space
has to be transported to the wavelet transform space (WTS). In order to
define the objects we have to identify the WTS pixels we can attribute to
the objects.

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=fig_detect.ps,bbllx=4cm,bblly=4cm,bburx=18cm,bbury=21.cm,width=12cm,height=12cm}
}}
\label{fig_detect_graph}
\caption{Example of connectivity in the wavelet space: contiguous significant wavelet
coefficients
form a structure, and following an interscale relation, a set of structures form
an object. Two structures $S_{j},S_{j+1}$ at two successive scales
belong to the same object if the position pixel of the maximum wavelet
coefficient value of $S_j$ is  included in $S_{j+1}$.}
\end{figure}

Figure~\ref{fig_detect_graph} shows an example of connectivity between
detected structures at different scales.

\subsection{Definition}

The Multiscale Vision Model (MVM) \cite{ima:bijaoui95,ima:rue97}
described  an object as a hierarchical set of structures.
It uses the following definitions:
\begin{itemize}
\item {\bf Structure}: a structure ${\cal S}_j$ is a set of significant
 connected wavelet coefficients at the same scale $j$. \\
\item {\bf object}: an object is a set of structures.\\
\item {\bf object scale}: the scale of an object is given by the scale of the
maximum of its wavelet coefficients.\\
\item {\bf interscale-relation}: the rule which
allows us to connect two structures into a single object
is called ``interscale relation''. Let us consider two structures at two
successive scales, $S^1_j$ and  $S^2_{j+1}$. Each
structure is located in one of the individual images of the
decomposition and corresponds to a region in this image where the
signal is significant. Noting $p_m$ the pixel position of the maximum
wavelet coefficient value of $S^1_j$, $S^1_j$ is said to be connected to
$S^2_{j+1}$ if $S^2_{j+1}$ contains the pixel position $p_m$ (i.e. the maximum
position of the structure $S^1_j$ must also be contained in the structure
$S^2_{j+1}$). Several structures
appearing in successive wavelet coefficient images can be connected in
such a way, which we call an object in the interscale connectivity
graph.
\item {\bf sub-object}: a sub-object is a part of an object. It appears when
an object has a local wavelet maximum. Hence, an object can be composed of
several sub-objects. Each sub-object can also be analysed.
\end{itemize}

\subsection{Reconstruction}
The problem of reconstruction \cite{ima:bijaoui95,ima:rue97}
consists of searching for a signal $O$ such that
its wavelet coefficients are the same as those of the detected
structures. If $\cal T$ describes the wavelet transform operator, and $P_w$ the
projection operator in the subspace of the detected coefficients
(i.e.\ all coefficients set to zero at scales and positions where
nothing was detected), the solution is found by minimization of
\begin{eqnarray*}
J(O) = \parallel W - (P_w \circ {\cal T}) O  \parallel
\end{eqnarray*}
where $W$ represents the detected wavelet coefficients of the data.

The MVM presents many advantages compared to the standard approach:
\begin{itemize}
\item faint extended objects can be detected as well as point sources,
\item the analysis does not require background estimation,
\item the Point Spread Function (PSF) is not needed.
\end{itemize}
The second point is relatively important. Indeed, if the background varies
spatially, its estimation becomes a non-trivial task, and may produce
large errors in the object photometry.

The last point is an advantage when the PSF is unknown, or difficult to
estimate, which occurs relatively often when it is space variant.
However, when the PSF is well determined, it becomes a drawback because
known information is not used for the object reconstruction.
Such a situation leads to
systematic errors in the photometry, which depends on PSF and on the source
signal to noise ratio.
In order to correct this error, a kind of calibration must be performed using
simulations \cite{starck:sta99_4}. The
next section shows how the PSF can be used
in the MVM, leading to a deconvolution.

\section{Detection and Deconvolution}
\subsection{Object reconstruction using the PSF}
A reconstructed and deconvolved object can be obtained
by searching for a signal $O$ such that
the wavelet coefficients of $P*O$ are the same as those of the detected
structures. If $\cal T$ describes the wavelet transform operator,
and $P_w$ the projection operator in the subspace of the detected coefficients,
 the solution is found by minimization of
\begin{eqnarray}
J(O) = \parallel W - (P_w \circ {\cal T}) P * O  \parallel
\end{eqnarray}
where $W$ represents the detected wavelet coefficients of the data, and
$P$ is the point spread function. By this approach, each object is deconvolved
separately. The flux related to the ring of the PSF will be taken into
account. For a point source, the solution will be close to that obtained by
PSF fitting. This problem is different from global deconvolution
in the sense that it is well-constrained. Exept for the
positivity of the solution
which is always true and must be used, no other constraint needs to
be introduced. This is due to the fact that the reconstruction is performed
from a small set of wavelet coefficients (those above a detection limit).
The number of objects are the same as those obtained by the MVM, but
the photometry and the morphology are different. The position may also change
a little.

\subsection{The algorithm}
Any minimizing method can be used to obtain the solution $O$. As we did not
meet any problem of convergence, noise amplification, or ringing effect,
we choose the Van Cittert method, which is certainly the simplest one.
For each detected object, we apply the following algorithm:

\begin{eqnarray}
O^{n+1} = O^{n} + {\cal T}^{-1}(W - (P_w \circ {\cal T}) P * O^{n})
\end{eqnarray}
where ${\cal T}^{-1}$ is the inverse wavelet transform.

\begin{enumerate}
\item Set $n$ to $0$.
\item Find the initial estimation $O^{n}$ by applying an inverse
wavelet transform to the set $W$ corresponding to the detected wavelet
coefficients in the data.
\item Convolve $O^{n}$ with the PSF $P$: $I^n = P*O^{n}$.
\item Determine the wavelet transform $W(I^n)$ of $I^n$.
\item Threshold all wavelet coefficients in $W(I^n)$ at position
and scales where nothing has been detected (i.e. $P_w$ operator). We get
$W_t(I^n)$.
\item Determine the residual $W(R) = W -  W_t(I^n)$.
\item Reconstruct the residual image $R^n$ applying an inverse wavelet
 transform.
\item Add the residual to the solution: $O^{n+1} = O^{n} + R^n$.
\item Threshold negative values in $O^{n+1}$.
\item if $\sigma(R^n) / \sigma(O^0) < \epsilon$ then $n = n + 1$ and goto step 3.
\item $O^{n+1}$ contains the deconvolved reconstructed object.
\end{enumerate}
In practice, the convergence is very fast (less than 20 iterations).
The reconstructed image (not deconvolved) can also be obtained, just by
reconvolving the solution with the PSF.

\clearpage
\newpage

\section{Object Detection:  mr\_detect}
\index{mr\_detect}
Program {\em mr\_detect} detects all objects present in
an image \cite{ima:bijaoui95,ima:rue97}.
Several output files are created:
\begin{itemize}
\item name\_out.tex: LaTeX file which contains a table describing all detected
objects. Values given in the
table are respectively the object number, the coordinates in pixel units,
the standard deviation in $x$ and $y$, the orientation, the maximum value,
the flux and the signal to noise ratio of
 the maximum wavelet coefficient of the object. This file
can be compiled by the LaTeX command. The pixel coordinates are given
using the first pixel of the image as the reference pixel (0,0).
The rotation angle gives the angle between the main axis of the object
and the x-axis following the trigonometric convention. The object number is
defined by numbers: the scale number which indicates at which  scale
the object is detected, and a number which indicates the object number
at this scale.
\item name\_out\_obj.ps: a Postscript file giving the position
of all objects in the image.
\item name\_out.fits: this image contains the sum of all objects. It looks
like a filtered image, without any background.
\item name\_out.mes: ASCII file describing all detected objects. Each object
is described by five lines:
\begin{itemize}
\item the first line contains the scale where the maximum of the object has
been detected, and the object number at this scale.
\item the second line contains respectively the coordinates
in pixel units ($x$ and $y$), and the standard deviation in $x$ and $y$.
\item the third line contains the orientation, the maximum value, the flux,
and the magnitude.
\item the fourth line contains the flux error, the signal to noise
ratio (SNR) of the
maximum of the wavelet coefficient, and the SNR of the object.
The SNR of the wavelet coefficient gives the ratio between the maximum
of the wavelet coefficients belonging to the object, and the standard deviation
of the noise at the scale of this maximum. The SNR of the object is obtained
by calculating the standard deviation in a box containing 90\% of the flux of
the object in the reconstructed image, and by taking the  ratio between
this standard deviation and
the noise standard deviation. In the case of Poisson
noise with few events, the SNR of the wavelet coefficient is not calculated,
but instead the probability that the wavelet coefficient is not due to noise
(i.e.\ due to signal), and the SNR of the object, is calculated by
taking the ratio between  90\% of the flux of the source and the square
root of the flux contained in the same box in the original data.
\item the last line contains the position of the maximum wavelet coefficient.
\end{itemize}
\end{itemize}
Furthermore, if the option ``-w 1'' is set, an image is created for each
object individually.
For a FITS image, if the ``-C'' option is set, another TeX table is created
containing the object coordinates (RA, Dec), the flux and the SNR.

If the Point Spread Function (PSF) is available, it can be given to the
program using the ``-P'' option. It generally improves the object photometry.
Furthermore an object deconvolution can be performed using the
``-D'' option. In
this case, each object is also deconvolved.

The photometry is by default estimated by integrating the flux in the
reconstructed object. An alternative is to use an
aperture photometry (``-a'' option). In this case, a background image is needed.
It can be:
\begin{itemize}
\item automatically calculated using pyramidal transform (PMT) (``-a 1'').
It is found by interpolating
 the last scale of the PMT  to the input image size.
 The number of scales is derived from the
 input image size and the number of pixels in the last scale (default is 16,
 and can be changed with the ``-b'' option).
 The background image size must be large enough to take into account
 background variations, and small enough to no take into account the
 information relative to the significant signal.
\item  a flat image (``-a 3''), with a value equal to
{\em BgrValue}.
\item an image given by the user (``-a 2'' + ``-B'' option).
\end{itemize}
An object is characterized by its second order moments $\sigma_x,\sigma_y$ in
the two principal directions. The aperture photometry is done in a box
of size $k MAX(\sigma_x,\sigma_y)$, where k is defaulted to 3. $k$ can
be modified by the ``-l'' option.

{\bf
\begin{center}
 USAGE: mr\_detect option image\_in name\_out
\end{center}}
{\bf image\_in} is the input image and {\bf name\_out} the name which will
be used for the output files. This name must not contain any suffix.
\\
Options are:
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item {\bf [-t type\_of\_multiresolution\_transform ]}
\begin{enumerate}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item B-spline wavelet transform: \`a trous algorithm
\item Half-pyramidal transform
\item Pyramidal B-spline wavelet transform
\item Mixed WT and PMT method (WT-PMT)
\item Mixed Half-pyramidal WT and Median method (WT-HPMT)
\end{enumerate}
Default is 1.
\item {\bf [-V Multiscale\_Vision\_Model]}
\begin{enumerate}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item No vision model.
\item Blinded Objects.
\item Rue-Bijaoui Vision Model for blinded + embedded Objects.
\end{enumerate}
Default is Rue-Bijaoui Vision Model for blinded + embedded Objects.
\item {\bf [-n number\_of\_scales]} \\
Number of scales used in the multiresolution transform. Default is 5.
\item {\bf [-m type\_of\_noise]} \\
Description in section~\ref{sect_filter}.
\item {\bf [-g SigmaNoise]} \\
SigmaNoise = noise standard deviation. Default is automatically estimated.
\item {\bf [-c gain,sigma,mean]} \\
Description in section~\ref{sect_support}.
\item {\bf [-s NSigma]} \\
Thresholding at Nsigma * SigmaNoise. Default is 3.
\item {\bf [-E Epsilon]} \\
Epsilon = precision for computing thresholds. (Only used in the case of
Poisson noise with few events). Default is 1e-03.
\item {\bf [-e minimum\_of\_events ]} \\
Minimum number of events for a detection. Default is 4. \\
For Poisson noise with few events only (-m 9).
\item {\bf [-S SizeBlock]} \\
Size of the  blocks used for local variance estimation. Default is 7.
\item {\bf [-N NiterSigmaClip]} \\
Iteration number used for local variance estimation. Default is 1.
\item {\bf [-F first\_detection\_scale]} \\
First scale used for the detection. Default is 1.
\item {\bf [-R RMS\_Map\_File\_Name]} \\
RMS Map.  If this option is set, the noise model is automatically fixed to:\\
Non-stationary additive noise
\item {\bf [-L last\_detection\_scale]} \\
Last scale used for the detection.
\item {\bf [-i number\_of\_iterations]} \\
Iteration number per object reconstruction. Default is 10.
\item {\bf [-u object\_reconstruction\_error]} \\
Default is 1e-5.
\item {\bf [-k]} \\
Keep isolated objects. Default is no.
\item {\bf [-K]} \\
Keep objects which touch the border. Default is no.
\item {\bf [-A FluxMult]} \\
 Flux in TeX tables are multiplied by {\em FluxMul}. Default is 1.
\item {\bf [-w writing\_parameter]}
\begin{enumerate}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item  Write each object separately in an image. \\
The image file name of the object will be:
\begin{center}
                      ima\_obj\_xx\_yy.fits
\end{center}
\item  Two synthetic images \\
xx\_ellips.fits: an ellipse is drawn around each object \\
xx\_simu.fits: image created only from the morphological parameters
\item  equivalent to 1 and 2 together
\end{enumerate}
\item {\bf [-U]} \\
Sub-segmentation. If two objects are close, and detected as a single object,
sub-segmentation will try to separate them (deblending) into two objects.
\item {\bf [-p]} \\
Detect also negative structures. Default is no.
\item {\bf [-q]} \\
Define the root of an object from the maximum position and its value.
\item {\bf [-d DistMax]} \\
Maximum distance between two max positions
of the same object at two successive scales. \\
Default is 1.
\item {\bf [-o Sub-object analysis]} \\
If set, sub-objects are also analysed. Several files are created:
\begin{itemize}
\baselineskip=0.4truecm
\item name\_out\_sub\_obj.ps: a Postscript file giving the position
of all sub-objects in the image.
\item name\_out\_sub.mes: ASCII file describing all detected sub-objects.
The syntax is the same as for the object ASCII file.
\item name\_out\_sub.tex: LaTeX file which contains a table describing all detected
sub-objects.  The syntax is the same as for the object TeX file.
\item name\_out\_subobj\_radec: sub-object coordinates (only
if ``-C'' option is set).
\end{itemize}
\item {\bf [-D]} \\
Perform a deconvolution. Default is no.
\item {\bf [-P PsfFileName]} \\
PSF file name.
\item {\bf [-f Fwhm]} \\
Full Width at Half Maximum.
\item {\bf [-O PSF\_Sampling]} \\
PSF over-sampling value.
\item {\bf [-a BgrMethod] } \\
 Aperture photometry:
\begin{enumerate}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item Aperture photometry using a background image model
\item Aperture photometry using an estimated background image
\item Aperture photometry using a constant background
\end{enumerate}
Default is no aperture photometry.
\item {\bf [-B BgrFileName]  } \\
Background image file name.
\item {\bf [-G BgrValue] } \\
Constant background value. Default is: 0.
\item {\bf  [-b BGR\_Size] } \\
Background image size for automatic background estimation.
Default is 16.
\item {\bf [-l KSigmaAperture]  } \\
Aperture photometry size parameter. Default is 3.
\item {\bf [-M object\_reconstruction\_method]}
\begin{enumerate}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item reconstruction from the fixed step gradient method
\item reconstruction from the optimum step gradient method
\item reconstruction from the conjugate gradient method
\item reconstruction using the PSF
\end{enumerate}
Default is: reconstruction from the conjugate gradient method
\item {\bf [-C RADEC\_Table\_Order]}
\begin{enumerate}
\baselineskip=0.4truecm
\itemsep=0.1truecm
  \item TeX table ordered by object number.
  \item TeX table ordered by the right ascension.
  \item TeX table ordered by object SNR.
\end{enumerate}
A TeX table of name {\bf name\_out\_radec.tex} is created, which contains
the object number, the right ascension (in degrees and in HH MN SEC), the
declination in degrees and in DEG MN SEC), the flux, and the SNR. The object
order depends on the {\em RADEC\_Table\_Order} parameter.
\item {\bf [-v]} \\
Verbose. Default is no.
\end{itemize}

\section{Examples and Strategies}

\begin{figure}[htb]
\centerline{
\vbox{
\hbox{
\psfig{figure=fig_bert_org.ps,bbllx=1.9cm,bblly=12.6cm,bburx=14.6cm,bbury=25.4cm,width=8cm,height=8cm,clip=}
\psfig{figure=fig_bert_imag.ps,bbllx=1.9cm,bblly=12.6cm,bburx=14.6cm,bbury=25.4cm,width=8cm,height=8cm,clip=}}
\hbox{
\psfig{figure=fig_bert_detect.ps,bbllx=1.9cm,bblly=12.6cm,bburx=14.6cm,bbury=25.4cm,width=8cm,height=8cm,clip=}
\psfig{figure=fig_bert_ellips.ps,bbllx=1.9cm,bblly=12.6cm,bburx=14.6cm,bbury=25.4cm,width=8cm,height=8cm,clip=}}
}}
\caption{Top left, original simulated image (stars + galaxies). Top right,
same image plus Gaussian noise. Bottom left, output image produced by
{\em mr\_detect}. Bottom right, noise image, and ellipses overplotted.}
\label{fig_bert}
\end{figure}

\begin{itemize}
\item  mr\_detect -v -w 2 field\_g10.fits detect\_field \\
Figure~\ref{fig_bert} presents the result of applying such a treatment.
The input image (top right)  has been obtained by adding
Gaussian noise to a simulated image (top left). The output image shows
that all objects (even very faint objects) have been detected.
The file ``detect\_field.tex'' contains Table~\ref{tabdetect}, and
can be compiled by the simple command ``latex detect\_field''.
\item mr\_detect -P PsfFileName field\_g10.fits detect\_field \\
Apply the detection with the same parameters, but using the PSF.
\item mr\_detect -D -P PsfFileName field\_g10.fits detect\_field \\
This time, a deconvolution is performed on each extracted object.
\item mr\_detect -D -P -u 0 -i 20 PsfFileName field\_g10.fits detect\_field \\
Force the number of iterations for each object reconstruction to be equal to 20.
\item  mr\_detect -v -m 2 -n 7 field\_g10.fits detect\_field \\
Assume Poisson noise, and use more wavelet scales.
\item  mr\_detect -t 5 -q field\_g10.fits detect\_field \\
The fifth transform type is used.
This means that the reconstruction is not iterative
(it runs faster). When two objects are blended,
it may not reconstruct them as
well as with other transforms.
The Postscript file ``detect\_field\_obj.ps'' (see Figure \ref{fig_ima_obj})
shows the position of each object in the map.

\end{itemize}

\subsection{Choice of multiscale transform}
\subsubsection*{Linear versus nonlinear transform}
Five multiscale transforms are available. The first three are wavelet
transforms, while the last two are nonlinear transform. Wavelet transforms
should normally be preferred to nonlinear transforms, but in some cases,
non-linear transforms can be useful. The main difference between the two
types of transform  is that nonlinear transforms allow us to have very
fast object reconstruction, without iterating. But the separation between
two blended objects is better when we iterate.
The PSF can also not be used when
using nonlinear methods, and the consequence is that the photometry may
be systematically underestimated.

\subsubsection*{Decimation or not?}
The default transform (transform 1) produces cubes, while transforms
2 and 5 produce half pyramidal data structures, and 3 and 4 pyramidal
data structures. It is clear that in using pyramidal transforms we save
computation time and memory space, but the quality is less good than with
the two other sets of transforms. Half pyramidal transforms may be
a good compromise between quality and both computation time and memory.

\subsubsection*{Photometry}
By default, {mr\_detect} estimates the object photometry without any
information about the Point Spread Function (PSF). If the PSF presents
a tail, it may not be detected in the wavelet space and the photometry
may have a systematic error which depends on the PSF and
on the flux of the source. If objects are sparse, an aperture photometry
will resolve this problem (``-a'' option). In the case
where the PSF is known, a more accuracy photometry
can also be obtained using the ``-P'' option. The PSF knowledge can be used
for the object reconstruction, and the tail arround the objects is correctly
restored. This approach produces normally goor results, even for crowded
fields, where an aperture photometry produces poor results.

\begin{table}[h]
\begin{center}
\begin{tabular}{||c||c|c|c|c|c|c|c|c||}
\hline
Object & x & y & $\sigma_x$ & $\sigma_y$ & $\theta$ &
	$I_{max} $ & Flux & SNR\_WaveCoef\\
\hline
\hline
3-1 & 166.78 & 171.85 & 3.90 & 3.54 & 6.4 & 64.62 & 3155.54  & 26.88\\
\hline
3-2 & 167.23 & 47.96 & 2.16 & 1.59 & 23.1 & 26.30 & 533.53  & 24.34  \\
\hline
3-3 & 230.21 & 83.97 & 4.34 & 4.78 & -14.6 & 84.77 & 6107.79 & 10.59  \\
\hline
3-4 & 129.85 & 88.28 & 4.47 & 6.89 & 15.0 & 81.99 & 8839.90 & 22.36 \\
\hline
3-5 & 63.78 & 185.28 & 5.49 & 2.85 & 2.4 & 91.62 & 6830.69 & 32.21 \\
\hline
2-1 & 119.96 & 17.71 & 1.93 & 3.12 & -18.0 & 85.43 & 2910.22 & 14.89 \\
\hline
2-2 & 129.27 & 29.65 & 4.54 & 2.33 & -5.5 & 398.30 & 16277.82  & 60.05 \\
\hline
2-3 & 154.99 & 145.35 & 2.66 & 2.91 & -35.5 & 181.33 & 6070.46 & 31.37  \\
\hline
2-4 & 16.67 & 160.69 & 2.78 & 2.40 & -11.6 & 55.63 & 1562.33 & 8.37 \\
\hline
2-5 & 103.16 & 75.34 & 2.98 & 2.12 & -11.6 & 47.76 & 1368.29 & 12.32 \\
\hline
2-6 & 114.21 & 231.37 & 2.35 & 3.13 & -41.1 & 72.64 & 2538.67 & 12.43 \\
\hline
2-7 & 233.17 & 101.44 & 2.91 & 3.02 & 0.5 & 79.43 & 3552.28  & 8.54 \\
\hline
2-8 & 101.13 & 98.60 & 2.80 & 3.51 & 42.9 & 79.17 & 3228.94 & 9.36 \\
\hline
2-9 & 231.91 & 127.28 & 3.15 & 2.74 & 5.7 & 56.52 & 2015.41 & 8.71 \\
\hline
2-10 & 36.05 & 153.08 & 2.33 & 3.03 & -39.5 & 32.58 & 1162.67  & 15.26 \\
\hline
2-11 & 227.54 & 207.51 & 3.36 & 2.98 & -0.8 & 54.97 & 2549.64 & 12.13 \\
\hline
2-12 & 152.97 & 209.01 & 3.54 & 3.09 & 35.1 & 95.40 & 4358.75  & 10.36 \\
\hline
1-1 & 176.37 & 131.80 & 1.48 & 1.37 & 2.4 & 133.07 & 1653.93 & 5.66 \\
\hline
1-2 & 154.00 & 146.00 & 1.01 & 1.01 & 0.0 & 122.96 & 1004.52 & 13.05 \\
\hline
1-3 & 13.92 & 145.08 & 1.38 & 1.42 & -9.0 & 299.54 & 3488.15 & 5.54  \\
\hline
\end{tabular}
\label{tabdetect}
\caption{Detection table.}
\end{center}
\end{table}

\begin{figure}[htb]
\centerline{
\hbox{
\psfig{figure=fig_bert_obj.ps,bbllx=2cm,bblly=6cm,bburx=19cm,bbury=23.5cm,height=14cm,width=14cm,clip=}
}}
\caption{Position of each object in the image.}
\label{fig_ima_obj}
\end{figure}

\clearpage


\section{Method}\label{sec:yourmethod}
We present our two different approaches to parallelize the algorithm and their major optimizations. Our implementations use MPI (``Message Passing Interface''). We also discuss a vectorization optimization that can be applied to both versions. Our source code and test cases are publicly available \cite{our_source}.

% Now comes the ``beef'' of the report, where you explain what you did.
% Again, organize it in paragraphs with titles. As in every section
% you start with a very brief overview of the section.
% In this section, structure is very important so one can follow the technical content.
% Mention and cite any external resources that you used including libraries or other code.

\mypar{Row-wise algorithm}
The design of our algorithm is intentionally simple. We divide each row of our pyramid-shaped DP table into evenly spaced contiguous segments and assign exactly one to each worker. As the number of cells in each row is increased by one we increase the size of a segment by one. To ensure a balanced workload we increase each segment size in a round robin fashion.

Each worker calculates the cells in its segment from left to right. But in order to calculate the leftmost element the worker may depend on the rightmost element of the previous row from the left neighboring worker. Thus a worker has to wait for the neighboring worker to complete its segment in the previous row before the worker can start the next row. This can lead to an imbalance. In order to overcome this issue we prioritize the leftmost and rightmost elements and calculate them first and send them right away before calculating any other elements. This ensures that the waiting time is minimal.

The number of messages sent in each row is constant in the number of workers (up to two receives and one send per worker) independent of the size of the rows. This means that with a small work size per worker we get a significant communication overhead. We solve this by first increasing the segment of a worker until it reaches a certain threshold before assigning any work to the next worker. In experiments we found that a threshold of $200$ cells works well.

We can calculate the edit script in a similar fashion as a sequential algorithm would. But due to the quadratic memory consumption we did not benchmark this.

\mypar{Dynamic priority algorithm}
In our row-wise algorithm a worker will not calculate any elements of the next row until it receives all required elements of the previous row. This blocking wait is usually not necessary - it is possible to calculate some elements of the next rows without waiting for other workers. We designed the dynamic priority algorithm to always perform some calculation instead of a blocking wait if possible. Additionally, the algorithm prioritizes calculations that will be sent to other workers, so that they are quickly unblocked.

\begin{figure}[hbt]\centering
  \includegraphics[width=0.85\linewidth]{images/dphpc-dynamic-priority-diagram.pdf}
  \caption{Visualization of the internal state of the dynamic priority algorithm. The circles are cells in the DP table (colored by worker). There are two workers (red and green). The state is from the perspective of the red worker. The arrows represent (past or future) sends and receives. Every cell on or above the \emph{frontier} is known to have been calculated by some worker. Every cell on or below the \emph{limiting line} cannot be calculated by the red worker at the moment.}
  \label{priority_state}
\end{figure}

In order to perform calculations whenever possible, it is necessary to determine which calculations are possible in a given state. Figure \ref{priority_state} shows the internal state that our algorithm keeps for this purpose. Every cell on the solid jagged line (\emph{frontier}) has been calculated by some worker, and every cell on or below the dotted line (\emph{limiting line}) can not be calculated at the moment. This means that all cells below the frontier and above the limiting line can be calculated without receiving any further messages from other workers. The frontier and the limiting lines fully determine the currently possible calculations (darker red).

The shape of the frontier is a direct consequence of the dependencies in our DP table. Before a DP cell can be calculated, the cells above it (left and right) must be calculated (see arrows in figure \ref{dp_table}). Because of transitivity, this means that all DP cells in the ``upside down pyramid'' ending in a cell must have been calculated before it. The frontier is the boundary of a union of such pyramids. Whenever a worker learns that a cell was calculated (because it calculated the value itself or received it from another worker) it extends its frontier by union with the pyramid ending in that cell.

Every worker has up to 2 limiting lines at any given time. These diagonal lines go through the next cell that the worker expects to receive. The worker shouldn't calculate any cells on or below a limiting line, since these cells will either be calculated by another worker (e.g. the cell that it expects to receive) or depend on values in such cells.

Once it is clear which cells are possible to calculate, it is necessary to choose a calculation order. One interesting case is outlined with a red circle in figure \ref{priority_state}. This cell has not been calculated yet, and another worker expects to receive its value. To prevent other workers from being blocked, such cells (and their dependencies) will be calculated before cells that do not result in a send. If there are no such cells to prioritize, cells near the middle of the DP pyramid are calculated first, since this order results in a frontier with few bends, which makes planning calculations faster.

\mypar{SIMD Optimization}
The amount of work needed per DP cell dynamically depends on the input, since the number of loop iterations in the while-loop is determined by the number of equal elements after a change. Therefore, the workload per worker might be imbalanced. %, especially for inputs with high similarity.
To mitigate these imbalances and hence, reduce the time workers are waiting, we use x86's SIMD\footnote{``Single instruction stream, multiple data streams'' by Flynn's Taxonomy \cite{flynns_taxonomy}} vector extensions to compare multiple elements at once.

In many cases we need to compare only one value, because the first value is already unequal. In these cases we would not benefit from using SIMD instructions, they might even be more expensive. Therefore, we have a preliminary scalar check for inequality of the first element. Only if this check fails, we will use SIMD instructions.

%To force the compiler to use SIMD instructions we use the vector intrinsics from the \texttt{immintrin.h} %TODO: ref needed?) -> Intel intrinsics guide as footnote?
%header.
%If there are at least eight elements left in both input sequences, we will use the AVX\footnote{Advanced Vector Extensions and} instructions which operate on 8-way vectors of 32-bit integers. For the case, in which less than eight elements but at least four are left, we make use of SSE\footnote{Streaming SIMD Extensions are extensions to the x86 instruction set architecture} instructions once to compare 4-way vectors.

For the vectorized comparison we first load unaligned 8-way or 4-way vectors from both input sequences (the loads need to be unaligned, since we do not know the position in the vector in advance and we compare the input sequences at different offsets). Then we use a \texttt{cmpeq} intrinsic to compare the two vectors for equality. The result is then checked for an unequal element by using the negation of the \texttt{testc} intrinsic. If there is no inequality, the next vector will be checked. Otherwise, the exact location of the inequality is determined by a scalar loop over the last up to eight elements using the same loop as in the original algorithm.