CHP - 15 - Image Processing Using Matlab Min
CHP - 15 - Image Processing Using Matlab Min
CHP - 15 - Image Processing Using Matlab Min
15
IMAGE PROCESSING USING MATLAB
Unit Structure :
15.0 Objectives
15.1 Introduction
15.2 MATLAB’ s Development Environment
15.3 Image Representation
15.3.1 Image Format
15.3.2 Image Loading and Displaying and Saving
15.3.3Image Information
15.4 Quantization
15.4.1 Grey Level Range
15.4.2 Number of Pixels
15.5 Point Processing
15.5.1Value Manipulation
15.5.2 Pixel Scaling
15.5.3Histogram
15.5.4 Histogram Equalization
15.5.5 Thresholding
15.6 Region Processing
15.6.1High Pass - Edge Detection
15.6.1.1 Prewitt
15.6.1.2 Sobel
15.6.1.3 Canny
15.6.1.4 MATLAB Code (Edge Detection)
15.6.2 Low Pass – Smoothing
15.6.2.1Averaging
15.6.2.2 Weighted Averaging
15.6.2.3 Gaussian Smoothing
15.6.2.4 Rank-Order Filters
15.6.3 Morphology
15. 6.3.1 Dilation and Erosion
15.6.3.2 Opening and Closing
15.6.3.3 Skeleton
15.7 Image Processing
15.7.1Fourier Transform
15.7.2 High-, Low- and Band-Pass filters
15.7.3 MATLAB Programming
www.rocktheit.com www.facebook.com/rocktheit
2 Image Processing Using Matlab
15.8 Summary
15.8 Unit End Exercise
15.9 Further Reading
15.0 OBJECTIVES
MATLAB is one of a platform being used for laboratory exercises in the Image Processing.
This chapter describes the MATLAB development environment you will be using, you are
expected to have read it and be familiar with it before attempting the Laboratory and
Coursework Assignments.
Pixels are explained in more detail.
Matrix operations are also discussed.
The chapter summarizes how the image processing operations discussed in this book may be
achieved in MATLAB.
It summarizes the MATLAB programming environment.
Further help is available online, by either clicking on the “Help” menu item, or typing help
browser at the command prompt.
15.1 INTRODUCTION
MATLAB is a data analysis and visualisation tool designed to make matrix manipulation as
simple as possible.
In addition, it has powerful graphics capabilities and its own programming language.
The basic MATLAB distribution can be expanded by adding a range of toolboxes, the one
relevant to Image Processing course is the image-processing toolbox (IPT).
MATLAB’s basic data structure is the matrix.
In MATLAB a single variable is a 11 matrix, a string is a 1 x n matrix of chars.
An image is a n x m matrix of pixels.
A matrix is a rectangular array of objects of the same type.
The matrix will have r rows and c columns, a matrix element is referenced as
M r ,c ,M r ,c M rc etc.
MATLAB is started from within the Windows environment by clicking the icon that should be
on the desktop. (MATLAB is also available for Linux and MacOS)
www.rocktheit.com www.facebook.com/rocktheit
3 Image Processing Using Matlab
MATLAB’s IDE has five components: the Command Window, the Workspace Browser, the
Current Directory Window, the Command History Window and zero or more Figure Windows
that are active only to display graphical objects.
The Command window is where commands and expressions are typed, and results are
presented as appropriate.
The workspace is the set of variables that have been created during a session. They are
displayed in the Workspace Browser. Additional information about a variable is available
there, some variables can also be edited.
The current directory window displays the contents of the current working directory and the
paths of previous working directories. The working directory may be altered.
The command history window gives a historical view of the current and previous sessions.
Commands appearing here can be re-executed.
MATLAB provides an editor for writing scripts. It is invoked by typing edit in the command
window. Scripts are stored with the extension .m and are therefore also known as m-files
Figure windows are generated by MATLAB to display graphical objects.
Figure 15.1
Help on any MATLB command can be found in the Help Browser which is entered via the
menu bar or by typing help browser in the command window.
www.rocktheit.com www.facebook.com/rocktheit
4 Image Processing Using Matlab
15.3 IMAGE REPRESENTATION
15.3.1Image Format
An image is a rectangular array of values (pixels).
Each pixel represents the measurement of some property of a scene measured over a finite
area.
The property could be many things, but we usually measure either the average brightness
(one value) or the brightness’s of the image filtered through red, green and blue filters (three
values).
The values are normally represented by an eight bit integer, giving a range of 256 levels of
brightness.
We talk about the resolution of an image: this is defined by the number of pixels and number
of brightness values.
A raw image will take up a lot of storage space.
Methods have been defined to compress the image by coding redundant data in a more
efficient fashion, or by discarding the perceptually less significant information.
MATLAB supports reading all of the common image formats. Image coding is not
addressed in this course unit.
An image is loaded into working memory using the command f imread (‘imagefile
name’);
The semicolon at the end of the command suppresses MATLAB output.
Without it, MATLAB will execute the command and echo the results to the screen.
We assign the image to the array f.
If no path is specified, MATLAB will look for the image file in the current directory.
The image can be displayed using imshow f ,G
f is the image to be displayed, G defines the range of intensity levels used to display it. If it is
omitted, the default value 256 is used.
If the syntax [low, high] is used instead of G, values less than low are displayed as black, and
ones greater than high are displayed as white.
Finally, if low and high are left out, i.e. use [ ], low is set to the minimum value in the image
and high to the maximum one, which is useful for automatically fixing the range of the image
if it is very small or vary large.
Images are usually displayed in a figure window.
www.rocktheit.com www.facebook.com/rocktheit
5 Image Processing Using Matlab
If a second image is displayed it will overwrite the first, unless the figure function is
used: figure,imshow f will generate a new figure window and display the image in it.
Note that multiple functions may be called in a single line, provided they are separated by
commas.
An image array may be written to file using: imwrite arrayname,' filename '
The format of the file can be inferred from the file extension, or can be specified by a third
argument.
Certain file formats have additional arguments.
15.3.3Image Information
Information about an image file may be found by imfinfo filename
15.4 QUANTIZATION
Images are normally captured with pixels in each channel being represented by eight bit
integers.
But there is no reason why pixels should be so limited, indeed, there are devices and
applications that deliver and require higher resolution data.
MATLAB supports the following data types.
www.rocktheit.com www.facebook.com/rocktheit
6 Image Processing Using Matlab
Single Single precision floating 1038 ,10
38
point number
Double Double precision floating 10308 ,10
308
point number
Char Character 2 bytes per element
Logical Values are 0 or 1 1 byte per element
An image is usually interpreted as being one of: intensity, binary, indexed or RGB.
An intensity image’s values represent brightness’s.
A binary image’s pixels have just two possible values (0 or 1).
Indexed image’s pixel values are treated as the index of a look-up table from which the “true”
value is read.
RGB images have three channels, representing intensities in ranges of wavelengths
corresponding to red, green and blue illumination (other wavelength ranges and more of
them are possible).
MATLAB provides functions for changing images from one type to another.
The syntax is Bdata _ class _ name( A) where data_class_name is one of the data types
in the above table, e.g. Buint 8( A) will convert image A (of some type) into image B of
unsigned 8-bit integers, with possible loss of resolution (values less than zero are fixed at
zero, values greater than 255 are truncated to 255.)
Functions are provided for converting between image types, in which case the data is scaled
to fit the new data type’s range. These are defined to be:
15.4.2Number of Pixels
MATLAB gives several methods of accessing the elements of an array, i.e. the pixels of an
image.
www.rocktheit.com www.facebook.com/rocktheit
7 Image Processing Using Matlab
An element can be accessed directly: typing the array name at the prompt will return all the
array elements
Typing the array name followed by element indices in round brackets, will return that value.
Ranges of array elements can be accessed using colons. A( first : last )
Will return the first to last elements inclusive of the one dimensional array A.
Note that the indices start at one. A( first: step: last )
Will return every step elements starting from first and finishing when last is reached or
exceeded.
A section of an image could be abstracted using fc f (top: bottom,left: right );
Or the image could be subsampled using fs f (1:2:end ,1:
2:end );
Point processing operations manipulate individual pixel values, without regard to any
neighbouringvalues.
Two types of transforms can be identified, manipulating the two properties of a pixel: its
value and position.
The fundamental value of a pixel is its brightness (in a monochrome image) or colour (in a
multichannel image).
15.5.2Pixel Scaling
15.5.3Histogram
www.rocktheit.com www.facebook.com/rocktheit
8 Image Processing Using Matlab
The histogram of an image measures the number of pixels with a given grey value.
The histogram of an image with L distinct intensity levels in the range [0, G] is defined as the
function
h rk nk
rk is the kthintensity level in the image, and nk will be the number of pixels with grey value rk .
G will be 255 for a uint8 image, For the integer valued images, G = L-1.
We often work with normalised histograms. A normalised histogram is obtained by dividing
each element h rk of by the total number of pixels in the image (equal to the sum of
histogram elements). Such a histogram is called the probability density function (pdf) and
reflects the probability of a given intensity level occurring.
p rk nk / n
MATLAB functions for computing and displaying the histogram and normalized histogram
are: himhist ( A,b);
>> p = imhist (a, b)/ numel (A);
b is the number of bits in the histogram. If it is omitted, 256 is assumed. The function numel(A)
returns the number of elements in the argument, in this case the number of pixels in A.
15.5.4Histogram Equalization
The aim of histogram equalization is to transform the grey scale such that the pdf of the output
image is uniform.
The transformation is achieved using:>> h = histeq(A, nlev);
Where nlev is the number of levels in the output image, h. Its default value is 64.
15.5.5Thresholding
This is a simple method of differentiating between an object and the background, which
works provided they are of different intensities.
A threshold value is defined.
Intensities greater than this are set to one value, intensities less than to another (1 or max,
and 0 or min are often used).
www.rocktheit.com www.facebook.com/rocktheit
9 Image Processing Using Matlab
1. Choose a threshold arbitrarily.
2. Threshold the image using it.
3. Compute the mean grey value of the pixels with intensities above and below the threshold,
and then compute the average of these two values.
4. Use the new value to rethreshold the image.
5. Repeat steps 3 and 4 until the threshold changes by an insignificant amount.
This is the method provided by the MATLAB toolbox:
>> T = graythresh(A);
T is a normalised value between 0.0 and 1.0.
The conversion function im2bw will then return the thresholded image.
Region processing operations compute the value of an output pixel using a set of the
pixel’s neighbours.
Various sizes and shapes of neighbourhoods are used.
Two objectives can be achieved by manipulating pixels in a region: smoothing the data and
sharpening it.
Both can use a technique called convolution.
MATLAB provides a simple syntax for filtering:
>>dst = imfilter(src, mask, filtering_mode, boundary_options, size_options);
Where dst and src are the output and input images respectively. mask is the filtering mask aka
kernel, template etc. filtering_mode specifies whether convolution (‘conv’) or correlation
(‘corr’) is to be used. boundary_options specifies how the data near the image border is to
be treated
size_options specifies how the destination image’s size is affected by the padding: ‘full’
implies the output will be the same size as the expanded image; ‘same’ implies the image
will be the size of the input image.
www.rocktheit.com www.facebook.com/rocktheit
10 Image Processing Using Matlab
The problem of edge detection is to locate the edge in this data (if it is possible, and if it
makes sense).
If an edge is a discontinuity in intensity, there must be a region around it where the intensity
changes by large amounts over small distances, i.e. the gradient is high.
15.6.1.1Prewitt
1 0 1 1 1 1
1 0 1and 0 0 0
1 0 1 1 1 1
15.6.1.2 Sobel
1 0 1 1 2 1
2 0 2and 0 0 0
1 0 1 1 2 1
This operator provides greater resilience to noise and is the best estimator of edge
orientation and strength of all the “small” kernels.
15.6.1.3 Canny
Canny took an information theoretic approach to edge detection, stating that an edge
detector should
1. Detect an edge
2. Should give a response in the correct location
3. Have a single response to an edge
www.rocktheit.com www.facebook.com/rocktheit
11 Image Processing Using Matlab
The aim of smoothing algorithms is to reduce the small scale, small amplitude fluctuations
in the data, usually associated with noise.
15.6.2.1 Averaging
The simplest approach to reducing noise is to replace each pixel’s value with the
average computed over some neighbourhood around it.
Compute the smoothed value and compare it with the value that is to be replaced.
Only if the two are reasonably similar is the smoothing is performed.
If there is a significant difference we assume that the neighbourhood spans a significant
edge in the image: the central pixel is on one side of the edge, there are sufficient pixels in
the neighbourhood from the other side of the edge to influence the average.
15.6.2.2Weighted Averaging
15.6.2.3Gaussian Smoothing
Gaussian smoothing is a special case of weighted smoothing, where the coefficients of the
smoothing kernel are derived from a Gaussian distribution. The amount of smoothing can be
controlled by varying the values of the two standard deviations (the distribution has a ó
value for the x and y orientations).
15.6.2.4Rank-Order Filters
Consider the example of a profile through a real edge (a step edge that is blurred and noise
corrupted).
The aim of smoothing is to reduce the noise fluctuations but retain the underlying structure.
Simple smoothing will reduce the noise but also blur the structure.
www.rocktheit.com www.facebook.com/rocktheit
12 Image Processing Using Matlab
Rank order filters can reduce noise and retain structure.
MATLAB provides tools for performing rank-order filtering (in the past it was known as order-
statistic filtering):
>>dst = ordfilt2(src, order, domain);
The filter gives the order-th element in the neighbourhood specified by domain. So the median
filter could be realised by (with m and n suitably intitialised):
>>dst = ordfilt2(src, m*n/2, ones(m, n)); Or by a call to the specific median filter: >>dst =
medfilt2(src, [m, n], padopt);
The optional argument padopt specifies how the border pixels are treated: ‘zeros’ (the
default) pads the border with zeroes, ‘symmetric’ pads the border with a mirror image of the
border pixels, and ‘index’ pads the border with 1s if the image is of class double, or 0s
otherwise.
15.6.3Morphology
Morphological transforms are usually applied to thresholded data, but can equally well be
defined for greyscale and colour images.
There are two fundamental transforms: erosion and dilation that can be combined in two
ways to give two derived operations: opening and closing. Further combinations are defined
to achieve other effects.
Morphological operations firstly require a structuring element.
This is simply a kernel that defines a shape, commonly a circle, square or cross, others are
equally possible and may be more useful in specific circumstances.
These are most simply defined for binary images (pixels have values of 1 or 0 only). A
structuring element is said to fit at a location in a binary image if all the image pixels that
overlap the structuring element have value 1. A structuring element is said to hit at an image
location if any of the pixels overlapping the structuring element are 1.
The effect of erosion is to shrink objects: pixels are removed from the boundary of the object to
a depth approximating half of the structuring element’s width. Objects of this characteristic
size are removed completely.
www.rocktheit.com www.facebook.com/rocktheit
13 Image Processing Using Matlab
Its effect is to increase the size of an object by adding pixels to a depth of about half the
structuring element’s width.
Gaps of this size in the object are filled.
The definitions can be modified for greyscale images.
Rather than setting the output to zero or one, the minimum or maximum of the set of values
selected by the structuring element are selected. So greyscale erosion is defined as
Structuring elements differ according to whether the image to be manipulated is binary (a flat
se) or greyscale (a non-flat se). The basic syntax for creating a structuring element is:>> se
= strel(shape, parameters);
Shape Parameters
distance from origin to extreme points of
diamond R
the diamond
disk R radius (plus other parameters)
length, length of element and angle wrt horizontal
line
orientation axis
length from origin to a vertical side, must
octagon R
be a multiple of 3
pair offset is a vector specifying an offset
periodicline P, V a se of 2P + 1 elements, each separated by
the vector V. Element 1 is at the origin,
element 2 at -1*V, etc.
rectangle MN a two element vector of non-negative
values: the numbers of row and columns in
these
square W the dimension of the square
www.rocktheit.com www.facebook.com/rocktheit
14 Image Processing Using Matlab
arbitrary NHOOD creates an arbitrary shaped se, NHOOD is
a matrix of 0s and 1s specifying the shape
In MATLAB, erosion and dilation of both binary and greyscale images are achieved
by:>>dst = imerode(src, se); >>dst = imdilate(src, se);
Opening is defined to be erosion applied a number of times, followed by dilation applied the
same number of times with the same structuring element.
Noise structures are removed and the features shrunk by erosion, the features are then
restored by dilation.
Closing is defined as dilation followed by erosion.
If we apply both opening and closing operations to a greyscale image, we achieve a degree
of smoothing.
Opening and closing are invoked by:
>>dst = imopen(src, se);
>>dst = imclose(src, se);
15.6.3.3 Skeleton
The skeleton of a binary object is a one-pixel thick line that represents the object’s shape.
The skeleton retains shape and topology but loses thickness information.
Obvious uses are in applications where these factors are important, such as character or
fingerprint recognition.
MATLAB provides the function skel.
Image processing operators compute the value of a single pixel in the output using all of the
input values.
15.7.1Fourier Transform
The Fourier transform states that any periodic function can be synthesised as the sum of
sines and/or cosines of different amplitudes and frequencies: the set of amplitudes is known
as a Fourier series.
www.rocktheit.com www.facebook.com/rocktheit
15 Image Processing Using Matlab
The amplitudes of the sines and cosines are computed using a Fourier transform.
The original data can be recovered exactly using an inverse Fourier transform.
So it is possible to transform the data to the “Fourier domain”, process it and inverse
transform back to the original domain.
If we compute the Fourier transform of an image:
>>dst = fft2(src);
The magnitude of the transform is returned by
>>dst = abs(src);
and scale the output linearly to make it more visible
>>imshow(src, [ ]);
We will observe the zero frequency component in the four corners of the image .
The transform can be simplified by moving the origin to the centre:
>>dst = fftshift(src);
Frequencies increase away from the centre.
The reverse of these transforms is as follows.
ifftshift(src) rearranges the quadrants of the data,
ifft2(src) performs the inverse Fourier transform.
Where D(u,v) computes the distance of the point (u,v) from the origin.
A band-pass filter is a combination of high- and low-pass filters, with the high-pass filter’s cutoff
frequency less being than the low-pass filter’s one.
www.rocktheit.com www.facebook.com/rocktheit
16 Image Processing Using Matlab
The H1 line
Help text
The function body
Comments
Conditional execution
ifexpression
statement(s)
end
OR
ifexpression 1
statement1elseifexpression2
statement2
else
statement3
end
For loop
forindex = start:increment:endstatements
www.rocktheit.com www.facebook.com/rocktheit
17 Image Processing Using Matlab
end
While loop
whileexpression statements
end
break Terminate loop execution
continuePasses control to the next iteration of a loop
Switch: executes one of several alternative statements
switchswitch_expressioncase case_expression 1 statement(s)
casecase_expression2 statement
otherwise
statement
end
return Causes execution to return to the calling function
try ... catchchanges flow of control if an error is encountered
C = mpower(x,A) is C(I,J) =
xA(I,J) C = mpower(A,B) is an
.’ Vector, matrix transpose(A) error
A.’ as standard
transpose
‘ Vector, matrix ctranspose(A) A’ as standard
complex conjugate
+ unary plus uplus(A) +A
- Unary minus uminus(A) -A
: Colon Discussed in section 4.2
www.rocktheit.com www.facebook.com/rocktheit
18 Image Processing Using Matlab
Function Description
imadd Adds two images, or an image and constant
imsubtract Subtracts two images, or a constant from an image
immultiply Multiplies an image by a constant, or two images pixel by
imdivide pixel
Divides an image by a constant, or two images pixel by pixel
imabsdiff Computes absolute difference between two images
imcomplem Computes the negative
ent
imlincomb Computes a linear combination, A*a + B*b + C*c + ..., A is an
image, a a scalar
15.8 SUMMARY
An image is loaded into working memory using the command f imread (‘imagefile
name’);
The image can be displayed using imshow f ,G
If a second image is displayed it will overwrite the first, unless the figure function is
used: figure,imshow f will generate a new figure window and display the image in it.
An image array may be written to file using: imwrite arrayname,' filename '
MATLAB provides functions for changing images from one type to another.
The syntax is Bdata _ class _ name( A) , e.g. Buint 8( A)
Ranges of array elements can be accessed using colons. A( first : last )
A section of an image could be abstracted using fc f (top: bottom,left: right );
Scaling of pixel values is achieved by multiplying by a constant. MATLAB provides a single
function that achieves several
effects Rimadjust A,low _ in,high _ in ,low _ out ,high _ out ,gamma ;
MATLAB functions for computing and displaying the histogram and normalized histogram
are: himhist ( A,b);
Thresholding, >> T = graythresh(A);
MATLAB provides a simple syntax for filtering:
>>dst = imfilter(src, mask, filtering_mode, boundary_options, size_options);
www.rocktheit.com www.facebook.com/rocktheit
19 Image Processing Using Matlab
MATLAB ’s Image Processing Toolbox provides a generic edge detection function
www.rocktheit.com www.facebook.com/rocktheit
20 Image Processing Using Matlab
3 A filter function from one of the following filters along with a set of values for the
parameter list is provided. For this filter function derive the an kxk filter mask for one of the
values of k, k = 3,5,9.
The filter mask is to be applied to the given input image and the enhanced image is to be
displayed.
(i) Gaussian Low Pass Filter (ii) Butterworth Low Pass Filter
(iii) Ideal High Pass Filter (iv) Gaussian High Pass Filter
4 Wavelet Analysis :
A one dimensional function is input. This can be extracted from scan line
information of an image. A wavelet basis for one dimensional function should be encoded. The
function is displayed at different resolution levels, j = 4, 8, 16, 32
www.rocktheit.com www.facebook.com/rocktheit