\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{float}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{subcaption}
\usepackage{url}
\usepackage{array}

\usepackage{listings}
\lstset{%
%backgroundcolor=\color{cyan!10},
basicstyle=\ttfamily,
numbers=left,numberstyle=\scriptsize
}

% adjust margins
\addtolength{\oddsidemargin}{-.5in}
\addtolength{\evensidemargin}{-.5in}
\addtolength{\textwidth}{1in}

\addtolength{\topmargin}{-.5in}
\addtolength{\textheight}{1in}

\renewcommand{\arraystretch}{2}


\title{Introduction to Digital Images}
\author{EE 224: Signals and Systems I}
\date{}

\begin{document}

\maketitle

\section{Overview}

In this lab we introduce digital images as a new higher dimensional signal type. Digital images are written as two-dimensional matrices of numbers that can be manipulated to enhance contrast, invert images, and highlight objects.

\section{Learning Objectives}

By the end of this lab, students will be able to:
\begin{enumerate}
\item Describe digital image types, including monochrome and color.
\item Display images in MATLAB.
\item Perform basic pixel manipulation including clearing, copying, inverting, and thresholding.
\item Perform grsyscale brightening and contrast-stretching.
\end{enumerate}

\section{Pre-Lab Reading}
\label{sec:prelab}

\subsection{Monochrome Images}
\label{sec:monochrome}

An image can be represented as a function $J(x, y)$ of two continuous variables representing the horizontal ($x$) and vertical ($y$) coordinates of a point in space. The variables $x$ and $y$ represent spatial dimensions. Thus, their units would be inches or some other unit of length. Moving images (such as video) add a time variable to the two spatial variables.  Digital images sample the signal $J(x,y)$ at uniform intervals:
\begin{align*}
J[m,n] &= J(mT_1, nT_2) &1 \leq m \leq M \textrm{ and } 1\leq n \leq N
\end{align*}
where $T_1$  and $T_2$  are the sample spacings in the horizontal and vertical directions. In MATLAB, we can represent an image as a matrix which consists of M rows and N columns. The matrix entry at $(m, n)$ is the sample value $J[m, n]$—called a \emph{pixel} (short for picture element).

Monochrome images are displayed using black and white and shades of gray, so they are called grayscale images.  In this lab, we will consider only sampled grayscale still images.  A sampled grayscale still image would be represented as a two-dimensional array of numbers.  An important property of light images such as photographs and TV pictures is that their values are always non-negative and finite in magnitude; i.e.,
\begin{align*}
0 \leq J[m,n] \leq J_{max} < \infty
\end{align*}
This is because light images are formed by measuring the intensity of reflected or emitted light which must always be a positive, finite quantity.  When stored in a computer or displayed on a monitor, the values of $J[m, n]$ have to be scaled relative to a maximum value $J_{max}$ . Usually, an eight-bit integer representation is used. With 8-bit integers, the maximum value (in the computer) would be $J_{max}  = 2^8 -1 = 255$, and there would be $2^8  = 256$ different gray levels for the display, from 0 to 255. Images in this format have type uint8 (unsigned 8-bit integer) in MATLAB. However, in image recognition applications, binary images ($J_{max}=1$) are often used to separate parts of the image that are of interest from areas that are not. A simple example is the application of zip code recognition on letters. The algorithm would first search for the zip code, then filter out the rest of the image.

\subsection{Displaying and Exporting Images in MATLAB}
\label{sec:display}

Most of the lab exercises in this class will require you to produce one or more output images. These will need to be incorporated into your report. Since many exercises will use MATLAB, we will describe here some basic I/O and display commands in the MATLAB environment.
\begin{itemize}
\item{Reading Images: You can read an image file, \emph{img.tif} or \emph{image.jpg}, into the MATLAB workspace using the command:
\begin{lstlisting}[language=MATLAB]
IMG=imread(’pout.tif’);
\end{lstlisting}
This will produce an image matrix IMG of data type uint8. 
}
\item{Displaying Images: You can display the image array \texttt{IMG} with the following commands.
\begin{lstlisting}[language=MATLAB] 
colormap(gray(256));  % only needed for grayscale image
imshow(IMG)
truesize; 
\end{lstlisting}
If \texttt{IMG} is a grayscale image, the colormap function is needed to tell MATLAB which color to display for each possible pixel value. The \texttt{truesize} command, which is included in the image processing toolbox, maps each image pixel to a single display pixel to avoid any interpolation on the display. This will be important in future labs.
}
\item{Writing Images: When producing a lab report document, you should strive to present the best representation of your output images. Therefore, it is best to export your images to a lossless file format, such as TIFF or BMP, which can then be imported into your lab report document. You can write the image array \texttt{X} to a file using the \texttt{imwrite} function: 
\begin{lstlisting}[language=MATLAB]
imwrite(x,’img_out.tif’)
\end{lstlisting}
Note that if \texttt{X} is of type uint8, the \texttt{imwrite} function assumes a dynamic range of $[0,255]$, and will clip any values outside that range. If \texttt{X} is of type double, then \texttt{imwrite} assumes a dynamic range of $[0,1]$, and will linearly scale to the range $[0,255]$, clipping values outside that range, before writing out the image to a file. To convert the image to type uint8 before writing, you can use 
\begin{lstlisting}[language=MATLAB]
imwrite(uint8(x),’img_out.tif’)
\end{lstlisting}
If your image is in color, such as RGB (Red-Green-Blue), then you will need to convert it to grayscale using the \texttt{rgb2gray} command in MATLAB:
\begin{lstlisting}[language=MATLAB]
RGB = imread(`peppers.png'); imshow(RGB)
I = rgb2gray(RGB);
figure; imshow(I)
\end{lstlisting}
}
\end{itemize}

\subsection{Prelab Questions}
\label{sec:pre}

Open Matlab and run the following command:
\begin{lstlisting}[language=MATLAB]
moon = imread(`moon.tif');
\end{lstlisting}
\begin{enumerate}
\item What is the size of the image example? NxM pixels
\item What is the variable type? 
\item Write out one line of Matlab code for seeing the intensity value of any given pixel in the image.
\end{enumerate}

\section{Image Manipulation}
\label{sec:manipulate}

The goal of image manipulation is to improve the image in ways that increase the performance of a system for human viewing or computer vision applications.

\subsection{Histogram Computation}

\begin{figure}[h]
\centering
\includegraphics[width=\textwidth]{tireHistogram.png}
\caption{Image of a tire with a histogram of intensity values. Most of the pixels have low intensities.}
\label{fig:tirehist}
\end{figure}


Image histograms are a simple but very useful method to analyze images and to enhance the quality of the image (at least for a human observer) by performing point transformations. In grayscale images, the histogram looks at the distribution of the intensity values of the image. For each range of grayscale values, the number of pixels in that range are counted. To see the distribution of intensities in the image, create a histogram by calling the \texttt{imhist} function. Generally, a good contrast image will have values spread out across the range of the intensity values from $0$ to $N$. Figure~\ref{fig:tirehist} shows the MATLAB image tire.tif and its histogram. Note that most of the pixels have low values that are in the range of 0-25 in intensity.

\subsection{Contrast Stretching and Intensity Level Slicing}

\begin{figure}[h!]
\centering
\includegraphics[width=0.3\textwidth]{contrastMap.png}
\caption{Example contrast mapping $s = T(r)$. The function $T$ is applied to each pixel.}
\label{fig:conmap}
\end{figure}

Low contrast images result from poor illumination or lack of response from the imaging sensor. The idea behind contrast stretching is to increase the dynamic range of the gray levels in the image being processed. This is implemented by a mapping function, $s = T(r)$. Figure~\ref{fig:conmap} shows a typical transformation for contrast stretching. The locations of points $(r1, s1)$ and $(r2,s2)$ control the shape of the contrast function. If $r1=r2$ and $s1=s2$, then the transformation is a linear function. If $r1=r2$, $s1=0$, and $s2=L-1$, then the transformation is a thresholding function. Intermediate values produce different types of spread. The mapping is a piecewise linear interpolation.

\begin{figure}[h!]
\centering
\includegraphics[width=0.6\textwidth]{levelSlice.png}
\caption{Example mapping functions for Intensity Level Slicing. The mapping on the left zeros out all pixels that have intensities below $A$ ($r<A$) and above $B$ ($r>B$). The mapping on the right brightens pixels with intensities between $A$ and $B$ ($A\leq r \leq B$).}
\label{fig:ils}
\end{figure}

A related transformation is Intensity Level Slicing which can highlight or delete a specific range of gray levels. Applications include enhancing features such as water in satellite images or the tire in the image above. There are two main approaches for this: display a high value for all of the pixels in a desired range and zero out the others or brighten the pixels in the range and leave the other pixels the same. Levels can be deleted using a similar approach. Examples are shown in Figure~\ref{fig:ils}.

\subsection{Histogram Equalization}

\begin{figure}[h!]
\centering
\includegraphics[width=\textwidth]{tireEqual.png}
\caption{Tire image after histogram equalization with a histogram of its intensities.}
\label{fig:tireEq}
\end{figure}

The histogram equalization operation tries to change the pixel distribution so that it is as close as possible to a uniform distribution. This means that all gray levels from $0$ to $2^{B}-1$ (where $B$ is the number of bits) are approximately equally likely. The effect is to widen the effective dynamic range of the image intensity. The results for this operation are shown in Figure~\ref{fig:tireEq} for the tire.tif image. Compare this to the original image in Figure~\ref{fig:tirehist}. Note that the histogram extends across most gray values with approximately equal numbers of occurrences. MATLAB code for this operation is (\texttt{X} is the original image): 
\begin{lstlisting}[language=MATLAB]
Xeq = histeq(X);
figure; subplot(121); imshow(Xeq)
subplot(122); imhist(Xeq)
\end{lstlisting}


\section{Lab Exercises}
\label{sec:exercises}

\begin{enumerate}
\item{Load the image ``pout.tif'' into MATLAB and compute its histogram. Include the histogram and image in your lab report.
\begin{enumerate}
\item What is the range of the pixel intensity values in the original image?
\item{Give two methods for increasing the range of the image intensity from Section~\ref{sec:manipulate} above. Describe and implement the methods and show the results of the operations in your lab report. Compare the results in both cases, does one method work better than the other?}
\end{enumerate}
}
\item{Load the built-in MATLAB image ``coloredChips.png.'' You will need to convert the image to grayscale. Use the methods described in Section~\ref{sec:manipulate} to isolate the round chips in the image by filtering the image intensity. The goal is to pre-process the image to remove the background so that a machine vision system can easily count the chips on the table.  
In your lab report, describe what steps you took to analyze the image and subtract the background. Include your MATLAB script and the stages of your processing pipeline. Display the image after each processing step.
}
\end{enumerate}

\subsection{Report Checklist}

Be sure the following are included in your report.
\begin{enumerate}
\item Section~\ref{sec:pre}: answer the three pre-lab questions
\item Section~\ref{sec:exercises}: answer the questions and complete the exercises in 1(a) and 1(b)
\item Section~\ref{sec:exercises}: complete and describe your approach to isolating the chips in 2
\end{enumerate}

\begin{thebibliography}{9}
\bibitem{gonzalez}
R.C. Gonzalez and R.E. Woods
\textit{Digital Image Processing},
Addison Wesley, 1993

 
\bibitem{matlabIPT}
``MATLAB Image Processing Toolbox Documentation'','
\textit{The Mathworks},
\\\url{https://www.mathworks.com/help/images/index.html?s_tid=CRUX_lftnav}


\bibitem{jackson}
Jackson,Jeff
Notes from ECE 482, University of Alabama,
\\\url{http://jjackson.eng.ua.edu/courses/ece482/lectures/LECT01-2.pdf}
\\\url{http://jjackson.eng.ua.edu/courses/ece482/lectures/LECT05-2.pdf}


\end{thebibliography}



%\end{thebibliography}





\end{document} 