\documentclass[10pt]{article}
\usepackage[left=2.5cm,top=2cm, textwidth=16cm, textheight=24.cm, headheight=0cm,headsep=0.5cm,foot=1cm]{geometry}                % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper}                   % ... or a4paper or a5paper or ... 
%\geometry{landscape}                % Activate for for rotated page geometry
%\usepackage[parfill]{parskip}    % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx}
\usepackage{dsfont}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}
\usepackage{array}
\usepackage{graphicx}     
\usepackage{pgfplots}  
\usepackage{pgf,tikz}

\usepackage{epstopdf}
\DeclareGraphicsRule{.tif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .tif`.png}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[parfill]{parskip}
\usepackage{pifont}
\usepackage{marvosym}

\title{\textbf{TP 1 : Basic tools for image processing}}
\date{}                                           % Activate to display a given date or no date


\usepackage{xstring}

\hyphenation{op-tical net-works semi-conduc-tor}



\newcommand{\code}[1]{
\begin{center}
\colorbox{black!10}{\begin{minipage}{0.9\textwidth}
\tt{#1}
\end{minipage}}
\end{center}
\medskip}




\newcounter{ManipCounter}

\newcommand{\Manipulation}[1]{%
\addtocounter{ManipCounter}{1}%
\medskip
\textbf{Manipulation \arabic{ManipCounter}~}%
-----------------------------------------------------------------------------------------------------------------\\ 
#1\\
-----------------------------------------------------------------------------------------------------------------------------------------
\medskip
}

\newcommand{\ManipulationDix}[1]{%
\addtocounter{ManipCounter}{1}%
\medskip
\textbf{Manipulation \arabic{ManipCounter}~}%
---------------------------------------------------------------------------------------------------------------\\ 
#1\\
-----------------------------------------------------------------------------------------------------------------------------------------
\medskip
}


\newcounter{PrepaCounter}

\newcommand{\Preparation}[1]{%
\addtocounter{PrepaCounter}{1}%
\textbf{Preparation \arabic{PrepaCounter}~}------------------------------------------------------------------------------------------------------------------\\
#1\\
-----------------------------------------------------------------------------------------------------------------------------------------\\
}

\def\chevrons{>\! \!>~}



\begin{document}
\maketitle
\vspace{-1cm}




This first tutorial aims at introducing the various basic tools available in the Matlab \textit{image processing} library. You are invited to regularly consult the documentation of the different Matlab functions to understand the operations they perform: \\

\code{
\chevrons help function~~~$\%$ short description \\
\chevrons doc function~~~~$\%$ longer description and detailed examples. 
}

A certain number of tools are identical to those you used in digital signal processing labs and random signal processing labs, so don't hesitate to consult your previous codes to remember how to use the functions. 





\section{Visualization}
The image 'Emphysema$\_$H$\_$and$\_$E.jpg’ is an image of haematoxylin and eosin stained lung tissue sample taken from an end-stage emphysema patient. Cell nuclei appear in blue-purple, red blood cells are red, other cell bodies and extracellular material are pink, and air spaces are white. The original image comes from "T Cells Cause Lung Damage in Emphysema", PLoS Med 1(1): e25, 2004, https://doi.org/10.1371/journal.pmed.0010025". 

\begin{enumerate}
	\item Load the image 'Emphysema$\_$H$\_$and$\_$E.jpg’ with $\tt{imread}$, and display it with $\tt{imshow}$. 
	\item Convert the color image to grayscale with $\tt{rgb2gray}$, then display the result.
	\item Display each of the red, green and blue components of the color image on separated figures. Display the corresponding colorbar. \begin{enumerate}
		\item Can you make the connection between the information carried by the three components and the color image displayed in question 1 ? 
		\item Can you make the connection between the information carried by the three components and the different biological objects ?
	\end{enumerate}
	\item Display the components of hue, saturation and value of the color image, computed with $\tt{rgb2hsv}$. To display the hue as a color image, we can set to 1 the saturation and the value of the HSV image, then convert it into RGB image with the function hsv2rgb.
	\item Display the image converted into grayscale with a false color representation: \\
	$\tt{imshow(myImage, 'Colormap', jet)}$. Display the corresponding colorbar.
	\item Convert the grayscale image in double. Display the converted image by specifying the intensity range in imshow. Test successively: [0,1], [0,100], [100,255] and $[ ]$. What is the effect of using $[ ]$ on the image display? 
\end{enumerate}

\section{Load and read different image formats}
A digital image is represented by a matrix or an array whose dimension depends on the nature of the image (grayscale, color, multispectral, etc). The format of the image (jpeg, png, tif, nifti, fits, mat, etc) depends on the way the image is stored in memory and the software used to produce/store/read the image.

\begin{enumerate}
	\item Load the image 'Emphysema$\_$H$\_$and$\_$E.jpg’ with $\tt{imread}$.  How the intensities are coded ?
	\item Load the image 'fractured$\_$spine.tif’ with $\tt{imread}$. How the intensities are coded ?
	\item Load the computed tomography scan contained in file 'CT.mat' with function $\tt{load}$. 
		\begin{enumerate}
		\item Look carefully at how the image is stored in this Matlab structure and how you can access it.
		\item How the intensities are coded ?
		\item Display the image with $\tt{imshow}$ with the corresponding colorbar.
		\item Convert the image in double and display the image with $\tt{imshow}$ with the corresponding colorbar.
		\item Calculate the min and max intensities of the image. What is the problem on the two previous displayed image ? 
		\item How to set up the $\tt{imshow}$ function so that the display of the intensities is correct? 
		\item What is the default setting to use if you don't know the image intensity range in advance? 
	\end{enumerate}
	File with .mat extension are Matlab structure used to store different types of data like images, metadata, etc. Other formats like .nifti, .fits, etc offer also the possibility to store images and metadata. Specific functions must be used to open these images. The $\tt{imread}$ function is dediacted to load images stored in classical image formats (.png, .tif, .jpeg) whose intensities are coded on integers. 
	
\end{enumerate}



\section{Histogram and image enhancement}
\begin{enumerate}
	\item Load the image contained in file 'CT.mat'.
	\item When interpreting the images, the radiologist chooses a visualization dynamic (color scale) adapted to the organs he wants to analyze. Test successively : $[ ]$, $[-150, 250]$ and $[-1400, 200]$  and analyze the differences.
	\item Knowing the min and max intensities of this image, then convert it into an image with gray levels between 0 and 255 (once the scaling is done, take care to convert the image into uint8). Display the histogram of this converted image with $\tt{imhist}$ function. We will note thereafter this image $\tt{I_{norm}}$.
	\item We now want to display the histogram of the original image. In this case, $\tt{imhist}$ does not work because the image is not coded on 256 gray levels. Use the $\tt{hist}$ function for this. This function requires a vector as input. We can convert the image into a vector using the syntax $\tt{I(:)}$. Test the influence of the choice of the number of bins (default value, Sturges rule : 1 + ln(n) with n the number of pixels in the image, max-min, ...). 
	\item On the $\tt{I_{norm}}$ image, apply the following algebraic operations: $\tt{I_{norm}^2}$, $\tt{I_{norm}^4}$, $\tt{log(I_{norm})}$. Visualize and comment on their effects. What happens if we don't take care of converting the $\tt{I_{norm}}$ image into double beforehand?
	\item Perform a histogram equalization (histeq) of the Inorm image. Is the objective of the method achieved?
	
	
\end{enumerate}

\section{Fourier Transform}
\begin{enumerate}
	\item Load the image sin32.png.
	\item Compute its discrete Fourier transform (DFT) with the $\tt{fft2}$ function (after converting the image into double).
	\item Display the modulus and the angle of the DFT of the image. The DFT is usually represented with the low frequencies in the center of the image, unlike the result calculated by $\tt{fft2}$. To move the low frequencies back to the center, use $\tt{fftshift}$ on the result of $\tt{fft2}$.
	\item How is the modulus of the DFT interpreted?
	\item Perform the same operations with the images sin16.png and sin8.png. What do you conclude?
\end{enumerate}

\section{High and low frequencies of an image}

\begin{enumerate}
	\item  Load the image contained in file 'CT.mat'.
	\item Compute its discrete Fourier transform (DFT) with the function fft2 and fftshift. Should the Fourier transform be calculated on the color image or on the grayscale transformed image ?
	\item Multiply its Fourier transform with a rectangle function of width $\Delta f$ in order to recover only the low frequencies (we will build the rectangle function by using the zeros function, then by setting to 1 the values at the center of the image).
	\item Calculate the inverse Fourier transform of the result ($\tt{fftshift}$ then $\tt{fft2}$) in order to visualize the corresponding low frequency image.
	\item Repeat the same operations but this time recovering the high frequencies.
	\item Vary $\Delta f$ and watch the influence on the corresponding high and low frequency images.
\end{enumerate}

\section{Aliasing phenomenon}
The aliasing is a phenomenon that appears when the image is undersampled without respecting the Shannon theorem. This phenomenon can be avoided by filtering the image properly before sampling. The purpose of this exercise is to compare subsampling without a filter, with a Gaussian filter and then with an ideal filter.

\begin{enumerate}
	\item Load and display the image 'onion$\_$cells.jpg’ and convert it to grayscale and double.
	\item Subsample the image by a factor of two, this can be done with the instruction $\tt{I(1:2:end,1:2:end)}$. How does the aliasing phenomenon translate visually? 
	\item Display the modulus of the Fourier transform of the subsampled image and compare it to the modulus of the Fourier transform of the original image. How does the aliasing phenomenon translate visually on the Fourier transform? Look at the top left edge of the image and the edge at the bottom right of the image.
	\item By subsampling the image, is the sampling rate changed? Is Shannon's theorem respected ? What is the one-dimensional equivalent of aliasing called?
	\item Apply a Gaussian filter ($\tt{imgaussfilt}$ function with 'circular' option) to the image before subsampling. Adjust the filter parameters to minimize the aliasing. Depending on the value of sigma, choose a sufficiently large value for the parameter $\tt{FilterSize}$.
	\item Apply an ideal filter to the image before subsampling. Choose the filter cutoff frequencies appropriately to reduce aliasing without altering the image. An ideal low-pass filter completely removes frequencies above the cutoff frequency without altering the frequencies below. To implement the ideal filter, it is easiest to work in the Fourier domain to define the ideal filter.
\end{enumerate}



\end{document}