i
An Introduction to Digital Image
Processing with Matlab
Notes for SCM2511 Image
Processing 1
Alasdair McAndrew
School of Computer Science and Mathematics
Victoria University of Technology
ii
CONTENTS
Contents
1 Introduction
Images and pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1
1.2 What is image processing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Images and digital images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3
Some applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4
1.5
Aspects of image processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An image processing task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6
Types of digital images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7
1.8
Image File Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Image Acquisition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.9
1.10
Image perception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Basic use of Matlab
2.1
2.2
2.3
2.4
2.5
2.6
Exercises
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Basic use of Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variables and the workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dealing with matrices
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Help in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Images and Matlab
3.1
3.2
3.3
3.4
Exercises
Greyscale images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
RGB Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Indexed colour images
Data types and conversions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4 Image Display
4.1
4.2
4.3
4.4
Exercises
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The imshow function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bit planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Spatial Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5 Point Processing
5.1
5.2
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Arithmetic operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
1
4
6
7
7
8
11
12
12
15
15
16
17
19
28
30
32
33
33
35
35
38
39
41
41
41
44
45
47
51
51
52
CONTENTS
5.3
5.4
5.5
Exercises
Histograms
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Thresholding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Applications of thresholding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6 Spatial Filtering
6.1
6.2
6.3
6.4
6.5
6.6
Exercises
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filtering in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Frequencies; low and high pass lters . . . . . . . . . . . . . . . . . . . . . . . . .
Gaussian lters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Non-linear lters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7 Noise
7.1
7.2
7.3
7.4
Exercises
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Types of noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cleaning salt and pepper noise
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cleaning Gaussian noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8 Edges
8.1
8.2
8.3
8.4
8.5
Exercises
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Dierences and edges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Second dierences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Edge enhancement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Final Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9 The Fourier Transform
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
Exercises
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The one-dimensional discrete Fourier transform . . . . . . . . . . . . . . . . . . .
Properties of the one-dimensional DFT . . . . . . . . . . . . . . . . . . . . . . . .
The two-dimensional DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fourier transforms in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fourier transforms of images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filtering in the frequency domain . . . . . . . . . . . . . . . . . . . . . . . . . . .
Removal of periodic noise
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Inverse ltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10 The Hough and Distance Transforms
10.1 The Hough transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10.2
Implementing the Hough transform in Matlab . . . . . . . . . . . . . . . . . . .
10.3 The distance transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11 Morphology
iii
56
67
70
71
75
75
79
80
84
87
89
91
95
95
95
98
102
106
111
111
111
118
122
128
128
131
131
131
135
137
142
144
148
159
161
167
169
169
174
180
191
195
iv
CONTENTS
11.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.2 Basic ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.3 Dilation and erosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.4 Opening and closing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.5 The hit-or-miss transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some morphological algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.6
Exercises
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12 Colour processing
12.1 What is colour? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.2 Colour models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.3 Colour images in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.4 Pseudocolouring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12.5 Processing of colour images
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercises
13 Image coding and compression
13.1
Exercises
Lossless compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bibliography
Index
195
195
196
203
211
213
220
223
223
227
234
236
239
245
247
247
252
255
257
Chapter 1
Introduction
1.1 Images and pictures
As we mentioned in the preface, human beings are predominantly visual creatures: we rely heavily
on our vision to make sense of the world around us. We not only look at things to identify and
classify them, but we can scan for dierences, and obtain an overall rough feeling for a scene with
a quick glance.
Humans have evolved very precise visual skills: we can identify a face in an instant; we can
dierentiate colours; we can process a large amount of visual information very quickly.
However, the world is in constant motion: stare at something for long enough and it will change
in some way. Even a large solid structure, like a building or a mountain, will change its appearance
depending on the time of day (day or night); amount of sunlight (clear or cloudy), or various shadows
falling upon it.
We are concerned with single images: snapshots, if you like, of a visual scene. Although image
processing can deal with changing scenes, we shall not discuss it in any detail in this text.
For our purposes, an image is a single picture which represents something. It may be a picture
of a person, of people or animals, or of an outdoor scene, or a microphotograph of an electronic
component, or the result of medical imaging. Even if the picture is not immediately recognizable,
it will not be just a random blur.
1.2 What is image processing?
Image processing involves changing the nature of an image in order to either
1. improve its pictorial information for human interpretation,
2. render it more suitable for autonomous machine perception.
We shall be concerned with digital image processing, which involves using a computer to change the
nature of a digital image (see below). It is necessary to realize that these two aspects represent two
separate but equally important aspects of image processing. A procedure which satises condition
(1)a procedure which makes an image look bettermay be the very worst procedure for satis-
fying condition (2). Humans like their images to be sharp, clear and detailed; machines prefer their
images to be simple and uncluttered.
Examples of (1) may include:
1
2
CHAPTER 1.
INTRODUCTION
Enhancing the edges of an image to make it appear sharper; an example is shown in gure 1.1.
Note how the second image appears cleaner; it is a more pleasant image. Sharpening edges
is a vital component of printing: in order for an image to appear at its best on the printed
page; some sharpening is usually performed.
(a) The original image
(b) Result after sharperning
Figure 1.1: Image sharperning
Removing noise from an image; noise being random errors in the image. An example is given
in gure 1.2. Noise is a very common problem in data transmission: all sorts of electronic
components may aect data passing through them, and the results may be undesirable. As
we shall see in chapter 7 noise may take many dierent forms;each type of noise requiring a
dierent method of removal.
Removing motion blur from an image. An example is given in gure 1.3. Note that in the
deblurred image (b) it is easy to read the numberplate, and to see the spokes on the wheels
of the car, as well as other details not at all clear in the original image (a). Motion blur
may occur when the shutter speed of the camera is too long for the speed of the object. In
photographs of fast moving objects: athletes, vehicles for example, the problem of blur may
be considerable.
Examples of (2) may include:
Obtaining the edges of an image. This may be necessary for the measurement of objects in
an image; an example is shown in gures 1.4. Once we have the edges we can measure their
spread, and the area contained within them. We can also use edge detection algorithms as a
rst step in edge enhancement, as we saw above.
1.2. WHAT IS IMAGE PROCESSING?
3
(a) The original image
(b) After removing noise
Figure 1.2: Removing noise from an image
(a) The original image
(b) After removing the blur
Figure 1.3: Image deblurring
4
CHAPTER 1.
INTRODUCTION
From the edge result, we see that it may be necessary to enhance the original image slightly,
to make the edges clearer.
(a) The original image
(b) Its edge image
Figure 1.4: Finding edges in an image
Removing detail from an image. For measurement or counting purposes, we may not be
interested in all the detail in an image. For example, a machine inspected items on an assembly
line, the only matters of interest may be shape, size or colour. For such cases, we might want
to simplify the image. Figure 1.5 shows an example: in image (a) is a picture of an African
bualo, and image (b) shows a blurred version in which extraneous detail (like the logs of
wood in the background) have been removed. Notice that in image (b) all the ne detail is
gone; what remains is the coarse structure of the image. We could for example, measure ther
size and shape of the animal without being distracted by unnecessary detail.
1.3 Images and digital images
Suppose we take an image, a photo, say. For the moment, lets make things easy and suppose the
photo is black and white (that is, lots of shades of grey), so no colour. We may consider this image
as being a two dimensional function, where the function values give the brightness of the image at
any given point, as shown in gure 1.6. We may assume that in such an image brightness values
can be any real numbers in the range
depend on the image, but they can take all real values between their minima and maxima.
(black) to
A digital image diers from a photo in that the
(white). The ranges of and will clearly
, , and
values are all discrete. Usually
they take on only integer values, so the image shown in gure 1.6 will have and
ranging from 1
to 256 each, and the brightness values also ranging from 0 (black) to 255 (white). A digital image
can be considered as a large array of discrete dots, each of which has a brightness associated with
it. These dots are called picture elements, or more simply pixels. The pixels surrounding a given
pixel constitute its neighbourhood. A neighbourhood can be characterized by its shape in the same
way as a matrix: we can speak of a neighbourhood, or of a neighbourhood. Except in
very special circumstances, neighbourhoods have odd numbers of rows and columns; this ensures
that the current pixel is in the centre of the neighbourhood. An example of a neighbourhood is