Image Processing Lab


Introduction to Image Processing Using MATLAB

MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and visualization. It integrates numerical analysis, matrix computation, signal processing, and graphics in an easy-to- use environment. MATLAB also features a family of application-specific solutions called toolboxes. Toolboxes are comprehensive collections of MATLAB functions that extend its environment in order to solve particular classes of problems.

MATLAB Image Processing Toolbox provides a comprehensive set of reference- standard algorithms and graphical tools for image processing, analysis, visualization, and algorithm development. You can perform image enhancement, image deblurring, feature detection, noise reduction, image segmentation, spatial transformations, and image registration. Many functions in the toolbox are multithreaded to take advantage of multicore and multiprocessor computers.

IP Toolbox supports a diverse set of image types, including high dynamic range, gigapixel resolution, ICC-compliant color, and tomographic images. Graphical tools let you explore an image, examine a region of pixels, adjust the contrast, create contours or histograms, and manipulate regions of interest (ROIs). With the toolbox algorithms you can restore degraded images, detect and measure features, analyze shapes and textures, and adjust the color balance of images.

The Image Processing Toolbox software is a collection of functions that extend the capability of the MATLAB numeric computing environment. The toolbox supports a wide range of image processing operations, including:

 Image enhancement, filtering, and deblurring

 Image analysis, including segmentation, morphology, feature extraction, and measurement

 Spatial transformations and image registration

 Image transforms, including FFT, DCT, Radon, and fan-beam projection

 Workflows for processing, displaying, and navigating arbitrarily large images

 Modular interactive tools, including ROI selections, histograms, and distance measurements

 ICC color management

 Multidimensional image processing

 Image-sequence and video display

 DICOM import and export

Many of the toolbox functions are MATLAB files with a series of MATLAB statements that implement specialized image processing algorithms.

MATLAB Image Processing Programming

When working with images in Matlab, there are many things to keep in mind such as loading an image, using the right format, saving the data as different data types, how to display an image, conversion between different image formats, etc. This worksheet presents some of the commands designed for these operations. Most of these commands require the Image processing tool box installed with Matlab in your computer. To find out if it is installed, type the command “ver” at the Matlab prompt. This gives you a list of what tool boxes that are installed on your system.


A digital image is composed of pixels which can be thought of as small dots on the screen. A digital image is an instruction of how to color each pixel. We will see in detail later on how this is done in practice. A typical size of an image is 512-by-512 pixels. It is convenient to let the dimensions of the image to be a power of 2. For example, 29=512. In the general case we say that an image is of size m-by-n if it is composed of m pixels in the vertical direction and n pixels in the horizontal direction.

Let us say that we have an image on the format 512-by-1024 pixels. This means that the data for the image must contain information about 524288 pixels, which requires a lot of memory! Hence, compressing images is essential for efficient image processing. You will later on see how Fourier analysis and Wavelet analysis can help us to compress an image significantly. There are also a few "computer scientific" tricks (for example entropy coding) to reduce the amount of data required to store an image.

Image formats supported by Matlab

The following image formats are supported by Matlab:

Most images you find on the Internet are JPEG-images which is the name for one of the most widely used compression standards for images. If you have stored an image you can usually see from the suffix what format it is stored in. For example, an image named myimage.jpg is stored in the JPEG format and we can very well load an image of this format into Matlab.

Working formats in Matlab

If an image is stored as a JPEG-image on your disc we first read it into Matlab. However, in order to start working with an image, for example perform a wavelet transform on the image, we must convert it into a different format. This section explains four common formats.

Intensity image (gray scale image)

This is the equivalent to a "gray scale image" and this is the image we will mostly work with in this course. It represents an image as a matrix where every element has a value corresponding to how bright/dark the pixel at the corresponding position should be colored. There are two ways to represent the number that represents the brightness of the pixel: The double class (or data type). This assigns a floating number ("a number with decimals") between 0 and 1 to each pixel. The value 0 corresponds to black and the value 1 corresponds  to  white.  The  other  class  is called uint8 which assigns an integer between 0 and 255 to represent the brightness of a pixel. The value 0 corresponds to black and 255 to white. The class uint8 only requires roughly 1/8 of the storage compared to the class double. On the other hand, many mathematical functions can only be applied to the double class. We will see later how to convert between double and uint8.

Binary image

This image format also stores an image as a matrix but can only color a pixel black or white (and nothing in between). It assigns a 0 for black and a 1 for white.

Indexed image

This is a practical way of representing color images. (In this course we will mostly work with gray scale images but once you have learned how to work with a gray scale image you will also know the principle how to work with color images.) An indexed image stores an image as two matrices. The first matrix has the same size as the image and one number for each pixel. The second matrix is called the color map and its size may be different from the image. The numbers in the first matrix is an instruction of what number to use in the color map matrix.

RGB image

This is another format for color images. It represents an image with three matrices of sizes matching the image format. Each matrix corresponds to one of the colors red, green or blue and gives an instruction of how much of each of these colors a certain pixel should use.

Multiframe image

In some applications we want to study a sequence of images. This is very common in biological and medical imaging where you might study a sequence of slices of a cell. For these cases, the multiframe format is a convenient way of working with a sequence of images. In case you choose to work with biological imaging later on in this course, you may use this format.

How to convert between different formats

The following table shows how to convert between the different formats given above. All these commands require the Image processing tool box!

Image format conversion
(Within the parenthesis you type the name of the image you wish to convert.)

Operation:                                                                                                                                 Matlab command:
Convert between intensity/indexed/RGB format to binary format.                             dither()
Convert between intensity format to indexed format.                                                     gray2ind()
Convert between indexed format to intensity format.                                                     ind2gray()
Convert between indexed format to RGB format.                                                             ind2rgb()
Convert a regular matrix to intensity format by scaling.                                             mat2gray()
Convert between RGB format to intensity format.                                                             rgb2gray()
Convert between RGB format to indexed format.                                                             rgb2ind()

The command mat2gray is useful if you have a matrix representing an image but the values representing   the   gray   scale   range    between,    let's    say,    0    and    1000.    The command mat2gray automatically re scales all entries so that they fall within 0 and 255 (if you use the uint8 class) or 0 and 1 (if you use the double class).

How to convert between double and uint8
When you store an image,  you should store it as a uint8 image since this requires far less memory than double. When you are processing an image (that is performing mathematical operations on an image) you should convert it into a double. Converting back and forth between these classes is easy.

I=im2double(I);                                                         converts an image named I from uint8 to double.
I=im2uint8(I);                                                         converts an image named I from double to uint

How to read files

When you encounter an image you want to work with, it is usually in form of a file (for example, if you down load an image from the web, it is usually stored as a JPEG-file). Once we are done with processing an image, we may want to write it back to a JPEG-file so that we can, for example,    post    the    processed    image     on     the     web.     This     is     done     using the imread and imwrite commands. These commands require the Image processing tool box!
Reading and writing image files

Operation:                                                                                                                                                                                   Matlabcommand:
Read an image.
(Within the parenthesis you type the name of the image file you wish to read. Put                                                 imread()
the file name within single quotes ' '.)
Write an image to a file.                                                                                                                       
(As the first argument within the parenthesis you type the name of the image you have worked with.         imwrite( , )
As a second argument within the parenthesis you type the name of the file and format that you want to write the image to. Put the file name within single quotes ' '.)

Loading and saving variables in Matlab

This section explains how to load and save variables in Matlab. Once you have read a file, you probably convert it into an intensity image (a matrix) and work with this matrix. Once you are done you may want to save the matrix representing the image in order to continue to work with this matrix at another time. This  is  easily  done  using  the  commands save and load.  Note that save and load are commonly used Matlab commands, and works independently of what tool boxes that are installed.

Loading and saving variables

Operation:                         Matlab command:
Save the variable X          save X
Load the variable X         load X

How to display an image in Matlab

Here are a couple of basic Matlab commands (do not require any tool box) for displaying an image.

Displaying an image given on matrix form

Operation:                                                                                                     Matlab command:
Display an image represented as the matrix X.                                 imagesc(X)
Adjust the brightness. s is a parameter such that
-1<s<0 gives a darker image, 0<s<1 gives a brighter image.        brighten(s)
Change the colors to gray.                                                                          colormap(gray)

Sometimes your image may not be displayed in gray scale even though you might have converted it into a gray scale image. You can then use the command colormap(gray) to "force" Matlab to use a gray scale when displaying an image.

If you are using Matlab with an Image processing tool box installed, It is recommended you to use the command imshow to display an image.

Displaying an image given on matrix form (with image processing tool box)

Operation:                                                                                     Matlab command:
Display an image represented as the matrix X.                     imshow(X)
Zoom in (using the left and right mouse button).                 zoom on
Turn off the zoom function.                                                     zoom off




MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, Fortran and Python.
Although MATLAB is intended primarily for numerical computing, an optional toolbox uses the MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional package, Simulink, adds graphical multi-domain simulation and Model-Based Design for dynamic and embedded systems.
In 2004, MATLAB had around one million users across industry and academia.[3] MATLAB users come from various backgrounds of engineering, science, and economics. MATLAB is widely used in academic and research institutions as well as industrial enterprises.