Image Processing Using Matlab - IIT
Image Processing Using Matlab - IIT
1. Introduction
MATLAB (MATrix LABoratory) integrates computation, visualization, and programming in
an easy-to-use environment. MATLAB provides comprehensive collections of MATLAB
functions (M-files) called toolboxes, each of them are useful for particular class of problems.
Areas in which toolboxes are available include signal processing, control systems, neural
networks, fuzzy logic, wavelets, simulation, image processing and many others.
Image processing tool box has extensive functions for many operations for image restoration,
enhancement and information extraction. Some of the basic features of the image processing
tool box are explained in this lecture. The applications are demonstrated using IRS LISS-III
data covering a portion of the Uttara Kannada district in Karnataka.
Throughout this lecture the MATLAB command is given in pink color and the output is
presented in blue.
2. Images in MATLAB
MATLAB stores most images as two-dimensional arrays, in which each element of the
matrix corresponds to a single pixel in the displayed image.
For example, an image composed of 200 rows and 300 columns of different colored dots
would be stored in MATLAB as a 200-by-300 matrix. Some images, such as RGB, require a
three-dimensional array, where the first plane in the third dimension represents the red pixel
intensities, the second plane represents the green pixel intensities, and the third plane
represents the blue pixel intensities.
This convention makes working with images in MATLAB similar to working with any other
type of matrix data, and renders the full power of MATLAB available for image processing
applications. For example, a single pixel can be selected from an image matrix using normal
matrix subscripting.
I(2,15)
This command returns the value of the pixel at row 2, column 15 of the image.
M6L2
HDF
PCX
(Paintbrush)
M6L2
(ii)
(iii)
(iv)
M6L2
The command whos is used to see how the variable I (which is the image here) is stored in
memory.
whos
MATLAB responds to the command with the following output.
Name
I
Size
342x342
Bytes
Class
116964
uint8
M6L2
bitmap = imread('image4.BMP','bmp');
imwrite(bitmap,'image4.png','png');
4. Image Arithmetic
Standard arithmetic operations, such as addition, subtraction, multiplication, and division,
when implemented on images are generally called Image Arithmetic. Image arithmetic has
many uses in image processing both as a preliminary step and in more complex operations. It
can be used to enhance or suppress the information, to detect the differences between two or
more images of the same scene etc.
4.1 Adding Images
To add two images or add a constant value to an image, use the imadd function. imadd adds
the value of each pixel in one of the input images with the corresponding pixel in the other
input image and returns the sum in the corresponding pixel of the output image.
For example, the following commands use the image addition to superimpose one image on
top of another. The images must be of the same size and class.
I = imread('image3.JPG');
J = imread('image4.JPG');
K = imadd(I,J); imshow(K)
In this example, image3.JPG and image4.JPG are IRS LISS-III Band-3 (Red) and Band-4
(Near Infrared) images, respectively. Added image is shown in Fig. 3.
M6L2
One can also use addition to brighten an image by adding a constant value to each pixel.
For example, the following code brightens image4.JPG by adding a value 50to all the pixel
values.
I = imread('image4.JPG');
J = imadd(I,50);
M6L2
M6L2
Fig.5.
Image with color bar
In Fig. 5 are IRS LISS-III Band-5 image is displayed. The image is displayed in gray scale. A
colorbar for the image is shown alongside the image indicating the values corresponding to
different gray levels.
5.2 Image Resizing
An image can be resized using the imresize function. The function takes two primary
arguments viz., (i) The image to be resized and (ii) The magnification factor as given below.
F = imread ('image5.JPG');
J = imresize(F,0.5);
M6L2
The first function reads the IRS LISS-III Band-5 image and saves it as F. The second
function is used to resize the image 0.5 times and to save the output as J.
Another way of using the imresize function is by specifying the actual size of the output
image, instead of the resize factor. The command below creates an output image of size 100by-150.
Y = imresize (F, [100 150] );
5.3 Image Rotation
To rotate an image, the imrotate function can be used. The function requires two primary
arguments as input. These are (i) The image to be rotated and (ii) The rotation angle. The
rotation angle should be specified in degrees. For a positive value, imrotate rotates the image
counterclockwise; and for a negative value, imrotate rotates the image clockwise. The
imrotate option in matlab allows the choice of interpolation method to be as either nearest,
bilinear or bicubic. They are popular interpolation techniques in which bicubic
interpolation can produce pixel values outside the original image.
For example, the following set of commands may be used to read the IRS LISS-III Band-5
image and to rotate the same through 35 degrees counterclockwise. The rotated image is
stored as J and is displayed at the end as shown in Fig. 6.
F = imread('image5.JPG');
J = imrotate (F,35,'bilinear');
figure, imshow(J)
Fig. 6.
Image rotated by 35 degrees
D Nagesh Kumar, IISc, Bangalore
M6L2
6. Image Analysis
A range of standard image processing operations for image analysis are also supported by the
MATLAB image processing toolbox. Two categories of operations available for image
analysis and image enhancement are mentioned here:
Extracting pixel values and statistics
Analyzing images to extract information about their essential structure e.g contours,
and edges
6.1 Extracting pixel values
The toolbox includes two functions that provide information about the pixel values or the
color data values of an image viz., pixval and impixel.
The function pixval interactively displays the data values for pixels as the cursor is moved
over the image. It can also display the Euclidean distance between two pixels.
The second function, impixel, on the other hand, returns the data values for a selected pixel or
set of pixels. The coordinates of the pixel is used as the input argument. The coordinates can
be specified manually.
10
M6L2
If the coordinates are not specified manually, they are selected using the curser as it moves
over the image. Below are the set of commands used to extract the pixel values.
imshow image4.JPG;
vals = impixel;
6.2 Extracting Summary Statistics
Standard statistics of an image such as mean, standard deviation, correlation coefficient etc.
can also be computed using the functions available in the MATLAB image processing
toolbox.
For example, the functions mean2 and std2 compute the mean and standard deviation of the
elements of the image, which is stored as a matrix. The function corr2 computes the
correlation coefficient between two matrices of the same size.
6.3 Image Contours
Similar to the contour function in MATLAB, the toolbox function imcontour can be used to
display the contour plot of the data in an image. Contours connect pixels of equal pixel
values. The imcontour function automatically sets up the axes so their orientation and aspect
ratio match the image.
For example, the following set of commands is used to read an image and to display the
image information in the form of contours.
I = imread('image5.JPG');
figure, imcontour(I)
This reads the IRS LISS-III Band-5 image (which is shown in Fig. 5) and generates the
contours within the image. The contour image displayed is shown in Fig. 7.
11
M6L2
Fig. 7.
Contour plot of an
Image
Places where the first derivative of the intensity is larger in magnitude than some
threshold
12
M6L2
For example, the following commands are used to detect the edges in the image5.JPG using
Cannys methods. The edges are displayed as shown in Fig. 8
F = imread('image5.JPG');
BW1 = edge(F,'canny');
figure, imshow(BW1)
Fig.8.
Edge detection Image
Canys method is the most powerful edge-detection method, as it uses two different
thresholds to detect strong and weak edges separately. Further the weak edges connected to
strong edges are displayed in the output. This method is therefore less likely than the others
to be "fooled" by noise, and more likely to detect true weak edges.
13
M6L2
7. Image Enhancement
Image enhancement techniques are used to improve an image or to enhance the information
contained in the image. For example, enhancement of the signal-to-noise ratio, enhancement
of certain features by modifying the colors or intensities so than they can be easily identified
or differentiated from others.
7.1 Intensity Adjustment
Intensity adjustment is a technique for mapping an image's intensity values to a new range.
For example, as seen from Fig. 2, contrast of the original display of image4.JPG is poor.
Although the pixels can be displayed in the intensity range of 0-255 (in the 8-bit system),
only a narrow range is used for the display.
To see the distribution of intensities in image4.JPG in its current state, a histogram can be
created by calling the imhist function. (Precede the call to imhist with the figure command so
that the histogram does not overwrite the display of the image in the current figure window.)
figure, imhist (I)
Fig.9. Histogram of
raw image
14
M6L2
From the histogram it can be seen that most of the values are concentrated in the region 1080. There are very few values above 80. On the other hand the display levels are equally
distributed for the entire range, which results in poor contrast in the image display. Contrast
in the image can be improved if the data values are remapped to fill the entire intensity range
[0,255].
This kind of adjustment can be achieved with the imadjust function. The function takes 3
arguments as input viz., the image to be adjusted, the input intensity range and the
corresponding output intensity range.
The general syntax of imadjust is
J = imadjust (I, [low_in high_in], [low_out high_out] )
Where, low_in and high_in are the intensities in the input image, which are mapped to
low_out, and high_out in the output image.
For example, the code below performs the adjustment described above.
I = imread('image4.JPG');
J = imadjust (I, [0.0 0.3], [0 1]);
The first vector passed to imadjust, [0.0 0.3], specifies the low and high intensity values of
the input image I. The second vector, [0 1], specifies the scale over which you want to map
them in the output image J. Thus, the example maps the intensity value 0.0 in the input image
to 0 in the output image, and 0.3 to 1.
Note that one must specify the intensities as values between 0 and 1 regardless of the class of
I. If I uses unsigned 8-bit data format (uint8), the values supplied are multiplied by 255 to
determine the actual values to use.
7.2 Histogram Equalization
Another approach for image enhancement is through histogram equalization. In histogram
equalization, for each input intensity level, corresponding output intensity levels are assigned
based on the frequency. Higher frequency ranges in the input images generally carry more
information compared to the lower frequency ranges. Hence, by assigning more display
levels to the higher frequency ranges, information enhancement is better achieved through
histogram equalization compared to the simple linear intensity enhancement.
15
M6L2
Histogram equalization is done in MATLAB using the command histeq. The command
spreads the intensity values of I (original image) over the full range, thereby improving the
contrast. Store the modified image in the variable I2.
I2 = histeq (I);
New equalized image, I2, is displayed in a new figure window using the command imshow.
figure, imshow(I2)
Fig. 10 shows the display of the imaged enhanced through histogram equalization.
As a matter of fact MATLAB image processing tool box has many more capabilities and only
a small portion of them is explained in this lecture.
16
M6L2
Bibliography
17
M6L2