Digital Image Processing - Lectures
Digital Image Processing - Lectures
Jorma Kekalainen
Contents
Jorma Kekalainen
Jorma Kekalainen
Jorma Kekalainen
Image sharpening
Original image
Jorma Kekalainen
Enhanced original
Processed images
Jorma Kekalainen
Image sharpening
Jorma Kekalainen
10
Thumb print
Jorma Kekalainen
11
Jorma Kekalainen
Enhanced image
12
Jorma Kekalainen
Restored image
13
Jorma Kekalainen
14
Jorma Kekalainen
15
16
Jorma Kekalainen
17
Jorma Kekalainen
18
Jorma Kekalainen
19
Ultrasound
Transmitted and reflected ultrasound
Jorma Kekalainen
20
Image
Suppose we take an image, e.g. a photo.
Suppose the photo is black and white (that is, lots of
shades of gray), so no color.
We may consider this image as being a two
dimensional function f(x,y), where the function values
give the brightness of the image at any given point
(x,y).
In other words, there is a function f that depends on
the spatial (room) coordinates x and y.
We may assume that in such an image brightness
values can be any real numbers in the range 0 (black)
to 1 (white).
Jorma Kekalainen
21
Digital image
A digital image differs from a photo in that the x, y and f(x,y)
values are all discrete.
Usually they take on only integer values, so e.g. x and y
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 picture elements called pixels.
Surroundings of a given pixel form a neighborhood.
A neighborhood can be characterized by its shape in the same
way as a matrix.
E.g. we can speak of a 5*5 neighborhood, or of a 7*9 neighborhood.
Note: Except in very special circumstances, neighborhoods have odd numbers of rows and
Image Processing
22
columns, because this ensures thatDigital
the current
pixel is in the centre of the neighborhood.
Jorma Kekalainen
Zoom
Jorma Kekalainen
Original
image: 500x340
Digital Image Processing
pixels
23
Jorma Kekalainen
24
Pixel: [ p, I(p)]
c
Value: I(p)=I(r,c)
Location: p=(r,c)
Pixel Location: p = (r , c)
Pixel Value: I(p) = I(r , c)
Jorma Kekalainen
25
Pixel: [ p, I(p)]
c
Value: I(p)=I(r,c)
p=(r,c)
=(row#, col#)
Jorma Kekalainen
Location: p=(r,c)
red
I(p ) = green
blue
26
Example
Pixels and a neighborhood
Current pixel
3*5 neighborhood
Note: If a neighborhood has an even number of rows or columns (or both), it may
Jorma Kekalainen
Processing
27
be necessary
to specify which pixel inDigital
theImage
neighborhood
is the current pixel.
Jorma Kekalainen
28
Some applications
Image processing has an enormous range of applications in almost
every area of science and technology e.g.,
Industry
Automatic inspection of items on a production line
Agriculture
Satellite/aerial views of land, for example to determine how much
land is being used for different purposes, or to investigate the
suitability of different regions for different crops, or inspection of fruit
and vegetables - distinguishing good and fresh produce from old.
Medicine
Inspection and interpretation of images obtained from X-rays, MRI
(magnetic resonance imaging) or CT scans.
Law enforcement:
Fingerprint analysis,
Sharpening or de-blurring of speed-camera images,
All kinds of surveillance camera applications
Jorma Kekalainen
29
30
31
Jorma Kekalainen
32
33
Jorma Kekalainen
34
Digital image
a grid of squares,
each of which
contains a single
color
each square is
called a pixel (for
picture element)
35
Binary image
Each pixel is just black or white.
Since there are only two possible values for
each pixel, we only need one bit per pixel.
Such images can therefore be very efficient in
terms of storage.
Images for which a binary representation may
be suitable include text (printed or
handwriting), fingerprints, blueprints,
architectural plans etc.
Jorma Kekalainen
36
Jorma Kekalainen
Grayscale
Each pixel is a shade of gray, normally from 0 (black) to
255 (white).
This range means that each pixel can be represented by
eight bits i.e. exactly one byte.
This is a very natural range for image file handling.
Other grayscale ranges are used, but generally they are
a power of 2.
Such images arise in medicine (X-rays), images of
printed works, and indeed 256 different gray levels is
sufficient for the recognition of most natural objects.
Jorma Kekalainen
38
C(150:170,210:235)
Jorma Kekalainen
39
Color images
Are constructed from
three intensity maps.
Each intensity map is
projected through a color
filter (e.g., red, green, or
blue, or cyan, magenta, or
yellow) to create a
monochrome image.
The intensity maps are
overlaid to create a color
image.
Each pixel in a color image
is a three element vector.
Jorma Kekalainen
40
Jorma Kekalainen
41
Color images
Formation of a vector from corresponding pixel
values in three RGB component images
Jorma Kekalainen
42
43
Red
Jorma Kekalainen
Green
Blue
44
Jorma Kekalainen
45
Indexed image
Most color images only have a small subset of the
more than sixteen million possible colors.
For convenience of storage and file handling, the image
has an associated color map, or color palette, which is
simply a list of all the colors used in that image.
Each pixel has a value which does not give its color (as
for an RGB image), but an index to the color in the
map.
It is convenient if an image has 256 colors or less, for
then the index values will only require one byte each to
store.
Jorma Kekalainen
46
Indices
Color map
In this image the indices, rather then being the gray values
of the pixels, are simply indices into the color map.
Without the color map, the image would be very dark and
colorless.
Jorma Kekalainen
47
Jorma Kekalainen
0.1176
0.2627
0.5451
0.2588
0.6941
0.8431
0.1412
0.2314
0.6392
0.4000
0.1490
0.8196
Example
A small index sample from the previous image
>> ind(117:123,347:352)
ans =
0.1451
2 1 2 1 1 4
0.7451
1 4 4 1 1 1
0.7412
0.2392
2 1 4 4 1 4
0.8706
5 5 2 2 1 1
0.8706
5 5 5 5 5 1
5 5 5 5 5 5
5 5 5 5 5 5
Jorma Kekalainen
Color map
0.1176
0.2627
0.5451
0.2588
0.6941
0.8431
0.1412
0.2314
0.6392
0.4000
0.1490
0.8196
49
50
52
Image perception
Much of image processing is concerned with
making an image appear better to human eyes.
We should therefore be aware of the limitations
of the human visual system.
Image perception consists of two basic steps:
capturing the image with the eye,
recognizing and interpreting the image with the visual
cortex in the brain.
53
Lens
Retina is
the sensor
Focal length
upside down
54
Color vision
The human eye has cones which are sensitive to
different wavelength bands
Jorma Kekalainen
55
Absorption of light by
the cones in the human eye
Jorma Kekalainen
56
57
Actual
intensity:
Perceived
intensity:
Jorma Kekalainen
59
Intensity resolution
We can only resolve 26=64 or at most 27=128 intensity levels
on an ordinary computer screen.
Based on hardware considerations, grayscale images are
usually stored with 8 bits per pixels, i.e. 28=256 intensity
levels.
Some images are stored with more than 8 bits per pixels.
E.g., CT images are stored with 12 bits per pixels, i.e. 212=4096
intensity levels.
During image processing, pixels can preferably be stored with
more than 8 bits or floating point numbers.
Color images are usually stored with 3x8 bits per pixels, 28 red
intensity levels, 28 green intensity levels and 28 blue intensity
levels giving 224 > 16 million different colors.
Jorma Kekalainen
60
Spatial resolution
Spatial resolution is a measure
of the smallest discernible
detail in the image.
Two common measures:
lp/mm (line pair/mm),
dpi (dot/inch)
5 discernible
line pairs (lp)
per mm, 5 lp/mm
Jorma Kekalainen
Newspaper: 75 dpi
Magazine: 133 dpi
Glossy brochures: 175 dpi
Gonzalez & Woods: 2400
dpi
6 dots
per inch, 6 dpi
A newspaper
image 61
Continuous
image
Line A-B
from
image
After
sampling
Jorma Kekalainen
After
quantization
Digital Image Processing
62
Typically camera
Jorma Kekalainen
63
For a color
image, there
are 3 different
types of sensor
elements:
red, green and
blue
Sensor element
Jorma Kekalainen
64
Linear transformation
A D/A-converter
converts a digital
value to an
analog value, an
Jorma Kekalainen
electrical voltage
To D/A-converter and
further to the screen
Digital Image Processing
65
To D/A-converter and
further to the red
Jorma Kekalainen
channel of the screen.
To D/A-converter and
further to the green
Digital Image Processing
channel
of the screen.
Over 16
million
colors
To D/A-converter and
further to the blue
66
channel of the screen.
Jorma Kekalainen
26
21
22
17
12
13
19
23
27
10
14
18
24
25
20
15
16
11
67
Jorma Kekalainen
68
Jorma Kekalainen
69
EM radiation
Electromagnetic radiation is energy which
propagate through space as electromagnetic
waves
The waves consist of transversal electrical and
magnetic fields that alternate with a temporal
frequency (Hertz) and spatial wavelength
(meter)
Jorma Kekalainen
71
Jorma Kekalainen
72
Jorma Kekalainen
73
Jorma Kekalainen
74
Spectrum
In practice, light consists of
photons with a range of energies, or
waves with a range of frequencies
75
Spectrum
Jorma Kekalainen
76
Color spectrum
In 1666, Newton discovered that sunlight (white light) passing
through a glass prism split up into a color spectrum of wave
lengths in the interval 400-700nm.
Jorma Kekalainen
77
Color wavelength
Jorma Kekalainen
78
Classification of EM spectrum
Jorma Kekalainen
79
Polarization
The electromagnetic field has a direction
Perpendicular to the direction of motion
80
Polarization
Plane polarization
The electric field varies only in a single plane
Jorma Kekalainen
81
Polarization
Circular/elliptical polarization
The electric field vector rotates
Can be constructed as the sum of two plane polarized
waves with 90o phase shift
82
Coherence
The phase of the light waves can either be
random: incoherent light (natural light)
in a systematic relation: coherent light
83
Radiation energy
Light radiation has energy
Each photon has a particular energy related to its
frequency (E = h )
The number of photons of a particular frequency
gives the amount of energy for this frequency
Described by the spectrum
Unit: Joule (J=Ws =Watt second)
Is usually not measured directly.
Jorma Kekalainen
84
Radiation power
The power of the radiation, i.e., the energy
per unit time, is the radiant flux
Since the energy depends on the frequency, so
does the radiant flux
Unit: Watt (W=J/s= Joule per second)
Is usually not measured directly.
Radiant intensity
For point sources, or distant sources of small
extent, the flux density can also be measured per
unit solid angle
87
Basic principle
Preservation of energy A constant light
source must produce the same amount of
energy through a solid angle regardless of
distance to the source
The radiant intensity is constant
The radiant flux density decreases with the square
of the distance to the source
Jorma Kekalainen
88
Radiometric chain
Jorma Kekalainen
89
Jorma Kekalainen
90
91
Basic principle
Based on preservation of energy:
E0 = E1 + E2 + E3
Jorma Kekalainen
92
Refraction
The light that is transmitted into the new
medium is refracted due to the change in light
speed
Snells law of refraction:
93
Absorption
Absorption implies attenuation of transmitted
or reflected light
Materials get their colors as a result of
different amount of absorption for different
wavelengths
E.g., A red object attenuates wavelengths in the
red band less than in other bands.
Jorma Kekalainen
94
Absorption
The absorption of light in matter depends on the
length that the light travels through the material
95
Absorption spectrum
The spectrum of the reflected/transmitted
light is given by
s1 = incident spectrum
s2 = reflected/transmitted spectrum
a = absorption spectrum ( 0 a() 1)
Jorma Kekalainen
96
Reflection
Highly dependent on the surface type
97
Emission
Almost independent of its interaction with
incident light:
Any object, even one that is not considered a light
source, emits electromagnetic radiation
Jorma Kekalainen
98
Scattering
All mediums (except vacuum) scatter light
E.g., air, water, glass etc.
Jorma Kekalainen
99
Scattering
Scattering means that the
light ray does not travel
along a straight line
through the medium
There is a probability that a
certain photon exits the
medium in another
direction than it entered.
Examples:
The sky is blue because of
scattering of the sun light
A strong laser beam
becomes visible in air
Jorma Kekalainen
100
Camera obscura
Since ancient times it has been known that a
brightly illuminated scene can be projected to
an image
In a dark room (Latin: camera obscura)
Through a small hole (aperture)
The image becomes rotated 180o
Jorma Kekalainen
104
Plenoptic function
At a point x = (x1,x2,x3) in space we can
measure how much light energy that travels in
the direction n = (n1,n2,n3), ||n|| = 1
The plenoptic function is the corresponding
radiance intensity function p(x,n)
A camera is a device that samples the
plenoptic function
Different types of cameras sample it in
different ways
Jorma Kekalainen
105
Jorma Kekalainen
106
107
Thin lenses
The simplest model of a lens
Focuses all points in an object plane onto the
image plane
Jorma Kekalainen
108
Object plane
The object plane consist of all points that appear
sharp when projected through the lens onto the
image plane.
The object plane is an ideal model of where the
sharp points are located
In practice the object plane may be non-planar: e.g.
described by the surface of a sphere
The shape of the object plane depends on the quality
of the lens (system)
109
Focal length
The thin lens is characterized by a single parameter:
the focal length fL
Jorma Kekalainen
110
Diffraction pattern
Due to the wave nature of light, even when
various lens effects are eliminated, light from
a single 3D point cannot be focused to an
arbitrarily small point if it has passed an
aperture.
For coherent light:
Huygens's principle: treat the incoming light as a
set of point light sources
Gives diffraction pattern at the image plane.
Jorma Kekalainen
111
Jorma Kekalainen
112
113
Superposition
The principle of superposition means that the
resulting wave-function at the image plane is a
sum/integral of the contributions from the
different light sources:
Jorma Kekalainen
114
Jorma Kekalainen
115
Airy disk
The smallest resolvable distance in the image
plane, x, is given by
116
Jorma Kekalainen
117
118
Lens distortion
A lens or a lens system can never map straight
lines in the 3D scene exactly to straight lines in
the image plane
Depending on the lens type, a square pattern
will typically appear like a barrel or a
pincushion
Jorma Kekalainen
119
4
Cos
law
Note: The flux density decreases with the square of the distance to the light source:
cos2 . The effective area of the detector relative to the aperture varies as cos . The
Jorma Kekalainen
Digital Image Processing
120
effective
area of the aperture relative
to the detector varies as cos
Chromatic aberration
The refraction index of matter (lenses) is
wavelength dependent
E.g., a prism can decompose the light into its
spectrum
A ray of white light is decomposed into rays of different
colors that intersect the image plane at different points
Jorma Kekalainen
121
Image formation
Jorma Kekalainen
122
Image formation
Jorma Kekalainen
123
Image formation
projection
through lens
image of object
Jorma Kekalainen
124
Capturing image
For natural images we need a light source (:
wavelength of the source) .
E(x; y; z; ): incident light on a point (x; y; z world
coordinates of the point)
125
Image formation
Camera(c(x; y; z; )) =
c=reflected light
Jorma Kekalainen
126
Jorma Kekalainen
127
Projection
There are two types of projections (P) of interest to us:
1. Perspective projection
Objects closer to the capture device appear bigger.
Most image formation situations can be considered to be under
this category, including images taken by camera and the human
eye.
2. Ortographic projection
This is unnatural.
Objects appear the same size regardless of their distance to the
capture device.
128
Perspective projection
Perspective projection: 1 = 2, l1 < l2 2 < 1.
Jorma Kekalainen
129
Jorma Kekalainen
130
131
Jorma Kekalainen
132
Image capturing
projection onto
discrete sensor
array
Jorma Kekalainen
digital camera
Digital Image Processing
133
Image capturing
sensors register
average color
Jorma Kekalainen
sampled image
134
Image capturing
continuous colors
discrete locations
Jorma Kekalainen
135
Sampled
Sampled and
quantized
Jorma Kekalainen
136
Quantization
Jorma Kekalainen
137
History of photography
Jorma Kekalainen
138
History of photography
1969: Boyle and Smith invent the first CCD chip for image
capture (based on the bubble memory)
1973: Fairchild Semiconductor markets the first CCD chip
(100 100 pixels)
1975: Bayer at Kodak: first single chip color CCD camera
1981: Sony markets the Mavica, the 1st consumer digital
camera. Stores images on a floppy disc
1986: Kodak presents the first megapixel CCD camera
2006: Dalsa Corporation presents a 111 Mpixel CCD
camera
2009: Kodak announces that it will discontinue production
of Kodachrome film
Jorma Kekalainen
139
Main effects
Main effects how the image is measured to
produce a digital image
The image is spatially sampled and truncated
Photons are converted to electric charge/voltage
The charges are converted to voltage
The voltage is quantized
Jorma Kekalainen
141
Photo-sensing chain
Jorma Kekalainen
142
143
144
Losses
When a photon enters the semiconductor
material, it may not interact as intended
1+2: reflection before
entering the active material
3+4: absorption before
entering the active material
5: absorption too deep in the material
6: the photon doesnt interact with the
material and exits at the back
Jorma Kekalainen
145
Quantum efficiency
All these effects are wavelength dependent
The mean number of electron/hole pairs
created per photon is the quantum efficiency
Jorma Kekalainen
146
Light electricity
Pure semiconductors can produce an electric
current Iphoto through the material if
It is embedded in an electric field
The material absorbs a photon with an energy
E=h which is larger than Qg, the gap between the
materials valence and conduction bands
Required:
Jorma Kekalainen
147
Intrinsic absorption
This is called intrinsic absorption
No doping of the semiconductor is needed
Near IR
Visible light
UV
X-ray
Jorma Kekalainen
148
Photovoltaic detectors
Absorption of light can also be based on the
photovoltaic effect
When photons of sufficiently high energy are
absorbed by a material, electrons are released and
produce a voltage
149
Thermal excitation
Because of heat in the material electrons are
always excited (moved from the valence band
to the conduction band) due to thermal
energy in the material.
This induces an electric current Ithermo
Jorma Kekalainen
150
Thermal noise
Ithermo is not a constant current, it is rather a
noise current of a mean given by the
expression
151
152
Photodiode
Electrons move from the
n+ region to fill holes in
the p-region
Jorma Kekalainen
153
Photodiode
Apply a bias voltage of
the same polarity as the
internal field.
The space-charge region
Increases.
Since the space-charge
region is an insulator, no
current runs through the
Junction.
154
Photodiode
Remove the voltage
the charge remains.
Jorma Kekalainen
155
Photodiode
The light creates a free
electron/hole pair.
Photodiode
The voltage difference
generated by the photons
occurs even if the diode
had not have been
precharged.
Caused by the photovoltaic
effect.
The diode can in principle
be used as a solar cell.
The pre-charging makes
the photovoltaic effect
stronger since it increases
the space-charge region.
Jorma Kekalainen
157
158
MOS capacitor
The oxide layer is a
perfect insulator,
no current passes
through this layer.
Metal
Oxide
Semiconductor
Jorma Kekalainen
159
MOS capacitor
Apply a voltage across
the capacitor.
Holes in the region under
the oxide will move into
the substrate and create
a space-charge region.
An electric field is
created across the oxide
barrier and through the
space-charge region.
Jorma Kekalainen
160
MOS capacitor
An absorbed photon
creates an electron/hole
pair
The hole is swept into the
substrate.
The electron is drawn by
the electric field toward the
oxide barrier.
Due to the oxide
insulation, the electrons
accumulate below the
oxide barrier, no current
flows through the capacitor.
The amount of
accumulated electrons is
proportional to the number
of absorbed photons.
Jorma Kekalainen
161
162
Blooming
Both the photodiode and the MOS capacitor
collect electric charge in a small region
corresponding to the conductor region
When this region becomes saturated, the
charge spills over to neighboring elements
This is called blooming
Barriers between the detectors can reduce
this effect, but not eliminate it entirely
Jorma Kekalainen
163
Fill factor
In practice, the light sensitive area of an image
sensor cannot fill the entire detector area.
Electronic components and wiring reduce the
light sensitive area
The fill factor is the percentage of the total
area which is light sensitive
Light sensitive area
Total pixel area
Jorma Kekalainen
164
Micro-lenses
To overcome low fill factors, an array of microlenses in front of the sensor array can be used
165
166
Transport problem
Light has caused a change in electric voltage or charge in a light
detector element (photodiode or MOS capacitor), and this change
needs to be measured to produce an image
Traditionally not measured per detector element
Would require many components per detector
Would give too small fill factor for 2D arrays
Note: The fill factor is the percentage of the total area which is light sensitive.
167
Jorma Kekalainen
168
Limiting factors:
there is a maximal readout frequency from the entire array
this limits the readout speed from the individual pixel
169
Covered with an
opaque metal shield
Jorma Kekalainen
170
Disadvantages
It takes some time to shift the entire image from A to
B, during this time area A is still sensitive to light
after-exposure
Mechanical shutters can be used to remove afterexposure
Jorma Kekalainen
171
172
4T APS
Add a fourth transistor to each pixel (4T)
This transistor acts as a memory during readout
All photo-charge is moved globally to the memory
transistor after exposure
The other three transistors operate as a standard 3T
APS
One for recharging the diode
One for transforming charge to voltage
One for connecting the voltage to the readout row
173
Noise sources
Reset noise
The measured voltage depends on the fix bias
voltage over the photo diode or MOS capacitor
This voltage has always some amount of variation =
noise
174
Noise sources
The space-charge region is not a perfect
isolator there is a small leakage current
Called dark current since it discharge the
capacitor even when no photons are absorbed
Thermal noise
Can be reduced by cooling
Shot noise
Even if a constant number of photons hit the
photo detector, the absorption process is
probabilistic:
Each time we observe/measure the
voltage/charge difference at the detector, there
will a small variation in the result
This variation is the larger the shorter the
exposure time is, and vice versa
This noise has approximately a Poisson
distribution
Jorma Kekalainen
176
V
SNR = 20 log
V
It means that darker images have a lower SNR than brighter
images (assuming constant average noise)
The dynamic range is the SNR of the largest detectable signal
Vmax
Vmax
DR = 20 log
Jorma Kekalainen
Jorma Kekalainen
178
Digitalization
The analogue voltage signal is normally transformed to a digital
representation by means of an analog-to-digital converter (ADC)
Two common principles:
Flash ADC (up to 8 bits)
Successive approximation (>8 bits)
DR
b=
20 log(2 )
gives a quantization noise of the same magnitude as the image
noise
Often, we want a few more bits than this to accurately represent
the image signal
Jorma Kekalainen
179
Flash ADC
Also called the parallel A/D converter
It is formed of a series of
comparators, each one comparing
the input signal to a unique
reference voltage.
The comparator outputs connect to
the inputs of a priority encoder
circuit, which then produces a
binary output.
Vref is a stable reference voltage.
As the analog input voltage exceeds
the reference voltage at each
comparator, the comparator
outputs will sequentially saturate
to a high state.
The priority encoder generates a
binary number based on the
highest-order active input, ignoring
all other active inputs.
Jorma Kekalainen
180
Jorma Kekalainen
181
Jorma Kekalainen
182
183
Image acquisition
Single imaging sensor
Jorma Kekalainen
184
Image acquisition
Combining a single sensor with motion to
generate a 2-D image.
Jorma Kekalainen
185
Color vision
The human eye has cones which are sensitive to
different wavelength bands
Jorma Kekalainen
186
187
Jorma Kekalainen
188
189
Jorma Kekalainen
190
191
Jorma Kekalainen
Extra green
192
Color post-processing
We can see the image detected by the sensor as a
mono-chrome signal (the raw image)
An RGB signal (3 components per pixel) is then
produced by interpolation from the raw image,
using different and space varying filters for each
of the three components (demosaicking)
Note: two types of filtering
An optical filter on the light before the sensor
An electronic filter on the image signal to produce
RGB signal
Jorma Kekalainen
193
Example
195
Color processing
The perception of color is complex
Humans tend to perceive color independent of
illumination
A color camera makes a measurement of physical
quantities: very dependent on illumination
White balancing
Transforms the color measurement to make what we
perceive as white to give equal RGB-values
Automatic or manual
196
Video camera
Basic idea: take one image after another in sequence
(temporal sampling)
Legacy television standards (PAL, NTCS,) require
interlaced video
Take one half-image with all odd rows and then another
half-image with all even rows, odd, even, etc.
Odd and even rows are exposed at different times
Motivation: better bandwidth usage in broadcasted TV
197
Progressive scan
E.g., one full image
at 25 Hz
Jorma Kekalainen
198
Jorma Kekalainen
199
Jorma Kekalainen
200
Rapid development
The technology related to image sensors is in
rapid development
The components are constantly becoming smaller
(Moores law)
New solutions to various problems appear at high
pace
More and more functionality is being integrated with
the image sensor
Image sensors are being integrated with other
functionalities (all kinds of supervision, control, and
surveillance anywhere and everywhere)
Jorma Kekalainen
201
Introduction
MATLAB is a data analysis and visualization tool which
has been designed with powerful support for matrices
and matrix operations.
Also Matlab has its own powerful programming
language and excellent graphics capabilities.
One of the reasons that Matlab has become such an
important tool is through the use of sets of Matlab
programs designed to support a particular task.
These sets of programs are called toolboxes, and here
the particular toolbox of interest to us is the image
processing toolbox.
Jorma Kekalainen
204
Jorma Kekalainen
205
Jorma Kekalainen
206
Command window
When we start up Matlab, we have a blank
window called the Command window in
which we enter commands.
Command Window is shown in the following
figure.
The prompt consists of two right arrows:
>>
Jorma Kekalainen
207
Jorma Kekalainen
208
Desktop panels
The desktop includes these panels:
Current Folder Access your files.
Command Window Enter commands at the
command line, indicated by the prompt (>>).
Workspace Explore data that you create or
import from files.
Command History View or rerun
commands that you entered at the command
line.
Jorma Kekalainen
209
Current Folder
Jorma Kekalainen
210
Command Window
Jorma Kekalainen
211
Workspace
Jorma Kekalainen
212
Command History
Jorma Kekalainen
213
Entering Commands
We first note that Matlab is command line driven; all
commands are entered by typing them after the
prompt symbol.
Let's start with a mathematical classic: first pressing
1+2
>> 1+2
and then press your Enter key.
This sends the command to the Matlab kernel.
What you should now see is
ans =
3
Jorma Kekalainen
214
Matlab as a calculator
Matlab can be used as a calculator; it understands the standard
arithmetic operations of addition, subtraction, multiplication,
division and exponentiation.
Try these:
>> 4*5
>> 6-3
>> 13/7
>> 2^5
Note that for the output of 13/7 the result was only given to a few
decimal places ( 1.8571)
In fact Matlab does all its calculations internally to double precision.
However, the default display format is to use only few decimal
places.
We can change this by using the format function.
Jorma Kekalainen
215
Display format
For example:
>> format long
>> 13/7
ans=
1.857142857142857
Entering the command format by itself returns to the default
format.
>> format
>> 13/7
ans =
1.8571
Jorma Kekalainen
216
Elementary mathematical
functions
Matlab has all the elementary mathematical functions built in:
>> sqrt(2)
ans =
1.4142
The trigonometric functions all
>> sin(pi/8)
ans =
take radian arguments; and pi is a
0.3827
built-in constant.
>> log(10)
ans =
The functions
2.3026
log and log10 are the natural
>> log10(2)
logarithm and logarithms to base
ans =
10.
0.3010
Jorma Kekalainen
217
Variables
When using any sort of computer system, we need to
store things with appropriate names.
In the context of Matlab, we use variables to store
values.
Here are some examples:
>> a=5^(7/2)
a=
279.5085
>> b=sin(pi/9)-cos(pi/9)
b=
-0.5977
Jorma Kekalainen
218
Note
Note that although a and b are displayed
using the short format, Matlab in fact stores
their full values.
We can see this with:
>> format long;a,format
a=
2.795084971874737e+02
Jorma Kekalainen
219
Example
We can now use these new variables in
further calculations:
>> log(a^2)/log(5)
ans =
7
>> atan(1/b)
ans =
-1.0321
Jorma Kekalainen
220
221
Jorma Kekalainen
222
223
Example
Simplest way to achieve
some printing on the screen
is to leave out the semicolon used to end rows.
Function disp works in the
same manner with the
exception that it does not
print the variable names
Jorma Kekalainen
>> A=rand(3);
>> A
A=
0.4218 0.9595
0.9157 0.6557
0.7922 0.0357
>> disp(A)
0.4218 0.9595
0.9157 0.6557
0.7922 0.0357
0.8491
0.9340
0.6787
0.8491
0.9340
0.6787
224
Workspace
The workspace contains variables that you create within or
import into MATLAB from data files or other programs.
Workspace lists all your currently defined variables, their
numeric data types, and their sizes in bytes.
The same information can be obtained using the whos
function:
>>whos
Name Size
a
1x1
ans 1x1
b
1x1
c
1x1
d
1x1
e Kekalainen
1x1
Jorma
Bytes
8
8
8
8
8
8
Class Attributes
double
double
double
double
double
double
Digital Image
Processing
225
who
A listing of the variable names only is obtained
using who:
>> who
Your variables are:
a ans b
c
d
e
The numeric data type double is Matlab's
standard for numbers; such numbers are
stored as doubleprecision 8-byte values.
Note also that ans is variable: it is automatically created by Matlab to store the result
Jorma Kekalainen
Digital Image Processing
226
of the
last calculation.
Complex numbers
Complex numbers have both real and imaginary
parts, where the imaginary unit is the square
root of 1.
sqrt(-1)
To represent the imaginary part of complex
numbers, use either i or j.
c = [3+4i, 4+3j, -i, 10j]
Jorma Kekalainen
227
Example
>> clear
>> sqrt(-1)
ans =
0.0000 + 1.0000i
>> c = [3+4i, 4+3j, -i, 10j]
c=
3.0000 + 4.0000i 4.0000 + 3.0000i 0.0000 - 1.0000i 0.0000 +10.0000i
Jorma Kekalainen
228
Example
>> whos
Name Size
ans
c
Jorma Kekalainen
1x1
1x4
Bytes Class
Attributes
16 double complex
64 double complex
229
Example
These statements create variables A and B in the
workspace.
clear
A = magic(4);
B = rand(3,5,2);
You can view the contents of the workspace using
whos.
>>whos
Name
Size
Bytes Class Attributes
A
4x4
128 double
B
3x5x2
240 double
Jorma Kekalainen
230
Example
>> B = rand(3,5,2)
>> A = magic(4)
B(:,:,1) =
A=
16 2 3
5 11 10
9 7 6
4 14 15
13
8
12
1
B(:,:,2) =
0.4898 0.7094 0.6797 0.1190 0.3404
0.4456 0.7547 0.6551 0.4984 0.5853
0.6463 0.2760 0.1626 0.9597 0.2238
Jorma Kekalainen
231
Workspace pane
The variables also appear in the Workspace
pane on the desktop.
Jorma Kekalainen
232
Jorma Kekalainen
233
235
Matrix
Matrix is an array of numbers consisting of horizontal rows
and vertical columns
Individual numbers in matrix are called elements: for example
element aij refers to the matrix element in row i and column j.
Matrices consisting of only one row or column are called row
or column vectors accordingly.
Jorma Kekalainen
236
Array creation
To create an array with four elements in a
single row, separate the elements with either
a comma (,) or a space.
A = [1 2 3 4] or A = [1,2,3,4]
returns
A=
1234
This type of array is a row vector.
Jorma Kekalainen
237
Creating vectors
1st way:
a = 3 5 7 -2 8 13
a = [3 5 7 -2 8 13]
2nd way: vector = first value : step : last value
a = 0:6
a =0 1 2 3 4 5
b = 1:3:14
b =1 4 7 10 13
c = 5:-1:0
c =5 4 3 2 1 0
3rd way:
vector = linspace(minimum value, maximum value,
number of elements)
a = linspace(1, 3, 6) a =1.0000 1.4000 1.8000 2.2000 2.6000
b=linspace(1, 3, 5)
3.0000
238
Array creation
To create a matrix that has multiple rows,
separate the rows with semicolons.
>>A= [1 2 3; 4 5 6; 7 8 10]
A=
1
2
3
4
5
6
7
8
10
Jorma Kekalainen
239
Array creation
Another way to create a matrix is to use a function, such as
ones, zeros, or rand.
For example, create a 5-by-1 column vector of zeros, 1-by-5
row vector of ones and a 5-by-5 identity matrix with ones on
the main diagonal and elsewhere zeros
>>x = zeros(5,1), y=ones(1,5), z=eye(5)
x=
z=
0
1 0 0 0 0
0
0 1 0 0 0
0
0 0 1 0 0
0
0 0 0 1 0
0
0 0 0 0 1
y=
1
Jorma Kekalainen
1
Digital Image Processing
240
Array indexing
Every variable in Matlab is an array that can hold many
numbers.
When you want to access selected elements of an
array, use indexing.
For example, consider the 4-by-4 magic square M:
>>M= magic(4)
M=
16
2
3
13
5
11
10
8
9
7
6
12
4
14
15
1
Jorma Kekalainen
241
Array indexing
There are two ways to refer to a particular
element in an array.
The most common way is to specify row and
column subscripts, such as
>>M(4,2)
ans =
M=
14
16 2 3
13
5 11 10 8
9 7 6 12
4 14 15
1
Jorma Kekalainen
242
Linear indexing
Less common, but sometimes useful, is to use
a single subscript that traverses down each
column in order:
M=
>>M(8)
16 2 3 13
5 11 10 8
ans =
9 7 6 12
4 14 15 1
14
Using a single subscript to refer to a particular
element in an array is called linear indexing.
Jorma Kekalainen
243
Note
If you try to refer to elements outside an array on the right side of an
assignment statement, MATLAB throws an error.
>>test = M(4,5)
Attempted to access M(4,5); index out of bounds because size(M)=[4,4].
However, on the left side of an assignment statement, you can specify
elements outside the current dimensions.
The size of the array increases to accommodate the newcomers
>>M(4,5) = 17
M=
16
2
3
13
0
5
11
10
8
0
9
7
6
12
0
4
14
15
1
17
Jorma Kekalainen
244
1 17
245
>>M(3,:)
16 2 3 13 0
5 11 10 8 0
9 7 6 12 0
4 14 15 1 17
ans =
9
Jorma Kekalainen
12
0
246
247
Entering a matrix
Matlab has a large number of commands for generating and
manipulating matrices.
Since a grayscale image is an matrix, we can use some of
those commands to investigate aspects of the image.
We can enter a small matrix by listing its elements row by row,
using spaces or commas as delimiters for the elements in each
row, and using semicolons to separate the rows.
Thus the matrix
can be entered as
>> a=[4 -2 -4 7;1 5 -3 2;6 -8 -5 -6;-7 3 0 1]
Jorma Kekalainen
248
Matrix elements
Matrix elements can be obtained by using the
standard row, column indexing scheme.
So for our image matrix a above, the command
>> a(2,3)
ans =
-3
returns the element of the matrix in row 2 and
column 3.
Jorma Kekalainen
249
Matrix elements
Matlab also allows matrix elements to be obtained using a
single number; this number being the position when the
matrix is written out as a single column.
Thus in a (4*4) matrix as above, the order of elements is
250
251
Example
So for example:
>> 2:3:16
generates the list
ans =
2 5 8 11 14
Applied to our matrix a, for example:
>> a(3,1:3)
ans =
6 -8 -5
lists all values in row 3 which are between columns 1 and 3
inclusive.
Jorma Kekalainen
252
Example
Similarly
>> a(2:4,3)
ans =
-3
-5
0
lists all the values in column 3 which are between rows 2 to 4
inclusive.
We can choose a block of values such as
>> a(2:3,3:4)
ans =
-3 2
-5 -6
which lists the 2 by 2 block of values which lie between rows 2 to 3
and columns 3 to 4.
Jorma Kekalainen
253
Example
The colon operator by itself lists all the elements along that
particular row or column.
So, for example, all of row 3 can be obtained with:
>> a(3,:)
ans =
6 -8 -5 -6
and all of column 2 with
>> a(:,2)
ans =
-2
5
-8
3
>> a(:)
ans =
4
1
6
-7
-2
5
-8
3
-4
-3
-5
0
7
2
-6
1
Note: Finally, the colon on its own lists all the matrix elements as a single column: a(:)
Jorma Kekalainen
Digital Image Processing
254
shows all the 16 elements of a.
Matrix operations
All the standard operations are supported.
We can add, subtract, multiply and invert matrices, and take matrix
powers.
For example, with the matrix a from above, and a new matrix b
defined by
>> b=[2 4 -7 -4;5 6 3 -2;1 -8 -5 -3;0 -6 7 -1]
we can have, for example:
>> 2*a-3*b
ans =
2 -16 13 26
-13 -8 -15 10
9 8 5 -3
-14 24 -21 1
Jorma Kekalainen
255
Matrix product
The product of matrixes A and B is defined
only if the following holds for matrix
dimensions (dim)
dim A = n x p, dim B = p x k, dim C = n x k
Jorma Kekalainen
>> A=[1:3;4:6];
>> B=[1
2;-1 -1;-2 0];
Digital Image Processing
>> C=A*B
256
Example
>> A=[1:3;4:6]
A=
1 2 3
4 5 6
>> B=[1 2;-1 -1;-2 0]
B=
1 2
-1 -1
-2 0
>> C=A*B
C=
-7 0
-13 3
Jorma Kekalainen
257
Example
As an example of matrix powers:
>> a^3*b^4
ans =
Jorma Kekalainen
258
Transpose
A transpose is obtained by using the
apostrophe:
>> a'
ans =
4 1 6 -7
-2 5 -8 3
-4 -3 -5 0
7 2 -6 1
Jorma Kekalainen
259
>> fliplr(a)
ans =
Jorma Kekalainen
260
Jorma Kekalainen
90
261
262
3
6
10
Jorma Kekalainen
1
4
7
2 3
5 6
263
8 10
264
Note
Notice that p is not a matrix of integer values.
Matlab stores numbers as floating-point values, and arithmetic operations
are sensitive to small differences between the actual value and its
floating-point representation.
We can display more decimal digits using the format command:
>>format long
>>p = a*inv(a)
p=
1.000000000000000
0
-0.000000000000000
0
1.000000000000000
0
0
0
0.999999999999998
Reset the display to the shorter format using
format short
format affects only the display of numbers, not the way Matlab computes
or saves them.
Jorma Kekalainen
265
Note
All these commands work equally well on
vectors.
In fact, Matlab makes no distinction between
matrices and vectors; a vector merely being a
matrix with number of rows or columns equal
to 1.
Jorma Kekalainen
266
Dot operators
A very distinctive class of operators in Matlab are those
which use dots; these operate in an element-wise
fashion element-wise operations.
For example, the command
a*b
performs the usual matrix multiplication of a and b.
But the corresponding dot operator:
a.*b
produces the matrix whose elements are the products
of the corresponding elements of a and b.
Jorma Kekalainen
267
Dot product
That is, if
a.*b
then the element of dot product is c(i,j)=
a(i,j)*b(i,j)
b=[2 4 -7 -4;5 6 3 -2;1 -8 -5 -3;0 -6 7 -1]
b=
>> a.*b
2 4 -7 -4
5 6 3 -2
ans =
1 -8 -5 -3
0 -6
Jorma Kekalainen
Digital Image Processing
Note:
We have also dot division,
and dot powers.
7 -1
268
Example
The command a.^2 produces a matrix each
element of which is a square of the
corresponding elements of a:
>> a.^2
ans =
Jorma Kekalainen
269
Matrix of reciprocals
Similarly, we can produce a matrix of
reciprocals by writing 1./a:
>> 1./a
ans =
Note: The value Inf is Matlab's version of infinity; Here it is returned for
Kekalainen
Digital Image Processing
270
the Jorma
operation
1/0.
Jorma Kekalainen
1
4
7
2 3
5 6
271
8 10
Concatenation
Concatenation is the process of joining arrays to
make larger ones.
The pair of square brackets [] is the
concatenation operator.
Horizontal concatenation
C = [A,A]
C=
1
2
3
1
2
3
4
5
6
4
5
6
A=
7
8
10 7
8
10
Jorma Kekalainen
1
4
7
2 3
5 6
272
8 10
Concatenation
Concatenating arrays next to one another using commas is called
horizontal concatenation.
Each array must have the same number of rows.
Similarly, when the arrays have the same number of columns, you
can concatenate vertically using semicolons.
C= [A; A]
Vertical concatenation
C=
1
2
3
4
5
6
7
8
10
1
2
3
4
5
6
7
8
10
Jorma Kekalainen
273
Constructing matrices
We have seen that we can construct matrices by listing all their
elements.
However, this can be tedious if the matrix is large.
Two special matrices are the matrix consisting of all zeros, and the
matrix consisting of all ones.
These are generated by the zeros and ones functions respectively.
Each function can be used in several different ways:
zeros(n) if n is a number, will produce a zeros matrix of size (n*n)
zeros(m,n) if m and n are numbers, will produce a zeros matrix of size (m*n)
zeros(m,n,p,...) where m, n, p and so on are numbers, will produce an
m*n*p* multidimensional array of zeros
zeros(a) where a is a matrix, will produce a matrix of zeros of the same size
as a.
Jorma Kekalainen
274
Example
Jorma Kekalainen
>> zeros(3)
ans =
0 0 0
0 0 0
0 0 0
>> zeros(3,4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
>> zeros(1,2)
ans =
0Digital Image
0 Processing
275
eye
Command eye creates an identity matrix
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
>> eye(3,4)
ans =
1 0 0
0 1 0
0 0 1
Jorma Kekalainen
0
0
0
276
277
278
Example
Suppose we wish to create a matrix every
element of which is a function of one of its
indices.
For example, the (5*5) matrix A for which
Aij=i+j-1.
In most programming languages, such a task
would be performed using nested loops.
We can use nested loops in Matlab, but it is
easier here to use dot operators.
Jorma Kekalainen
279
Example
We can first construct two matrices: one containing all the
row indices, and one containing all the column indices:
>> rows=(1:5)'*ones(1,5)
rows =
>> cols=ones(5,1)*(1:5)
11111
cols =
22222
12345
33333
12345
12345
44444
12345
55555
12345
Jorma Kekalainen
280
Example
Now we can construct our matrix using rows and cols:
>> [cols,rows]=meshgrid(1:5,1:5)
>> A=rows+cols-1
cols =
A=
1 2 3 4 5
1 2 3 4 5
12345
1 2 3 4 5
23456
1 2 3 4 5
1 2 3 4 5
34567
rows =
1 1 1 1 1
45678
2 2 2 2 2
56789
3 3 3 3 3
4
5
4
5
4
5
4
5
4
5
Note: The construction of rows and cols can be done automatically with the meshgrid
function:
[cols,rows]=meshgrid(1:5,1:5)
Jorma Kekalainen
Image Processing
281
will produce
the two index matrices Digital
above.
282
Note
Many functions in Matlab, when applied to a
matrix, work by applying the function to each
element in turn.
Such functions are the trigonometric and
exponential functions, and logarithms.
The use of functions in this way means that in
Matlab many iterations and repetitions can be
done with vectorization rather than by using
loops.
Jorma Kekalainen
283
Vectorization
Vectorization, in Matlab, refers to an operation
carried out over an entire matrix or vector.
In most programming languages, applying an
operation to elements of a list or array will
require the use of a loop, or a sequence of nested
loops.
Vectorization in Matlab allows us to do without
loops in almost all instances, and is a very
efficient replacement for them.
Jorma Kekalainen
284
Example
Suppose we wish to calculate the cosine values of all the
integer radians one to ten million.
We can do this with a for-loop:
for i=1:10^7,cos(i);end
We can measure the time of the operation with Matlab's tic,
toc timer: tic starts a stop watch timer, and toc stops it and
prints out the elapsed time in seconds.
Thus, on this computer:
>> tic,for i=1:10^7,cos(i);end,toc
Elapsed time is 5.836959 seconds.
Jorma Kekalainen
285
Example
We can perform the same calculation with:
>> i=1:10^7;cos(i);
and print out the elapsed time with:
>> tic,i=1:10^7;cos(i);toc
Elapsed time is 1.311342 seconds.
Note that the second command applies the
cosine function to all the elements of the vector
1:10^7, whereas with the for loop, cosine is only
applied to each element of the loop in turn.
Jorma Kekalainen
286
Example
As another example, we can easily generate
the first 10 square numbers with:
>> [1:10].^2
ans =
1 4 9 16 25 36 49 64 81 100
What happens here is that [1:10] generates a
vector consisting of the numbers 1 to 10; and
the dot operator .^2 squares each element in
turn.
Jorma Kekalainen
287
Example
Vectorization can also be used with logical operators;
we can obtain all positive elements of the matrix a
above with:
>> a>0
ans =
1 0 0 1
1 1 0 1
1 0 0 0
0 1 0 1
The result consists of 1's only in the places where the
elements are positive.
Note: Matlab is designed to perform vectorized commands very quickly, and whenever
Jorma Kekalainen
Processing
288
possible
such a command should be Digital
usedImage
instead
of a for loop.
Basic commands
Jorma Kekalainen
290
291
Line plots
The idea is straightforward: we create two
vectors x and y of the same size, then the
command
plot(x,y)
will draw two-dimensional line plot y against
x.
If y has been created from x by using a
vectorized function f(x) the plot will show the
graph of y=f(x)
Jorma Kekalainen
292
Example
x=[0:0.1:2*pi];
plot(x,sin(x))
and the result is shown
beside.
Jorma Kekalainen
A simple plot
293
Example
The plot function can be used
to produce many different
plots.
We can, e.g., plot two
functions simultaneously with
different colors or plot
symbols.
For example:
plot(x,sin(x),'o',x,cos(x),'*')
produces the graph shown
beside.
Jorma Kekalainen
294
Example
Plot the value of the cosine function from 0 to
2:
x = 0:pi/100:2*pi;
y = cos(x);
plot(x,y)
Jorma Kekalainen
295
Example
We can label the axes and add a title.
title('Plot of the Cosine Function')
xlabel('x')
ylabel('cos(x)')
Jorma Kekalainen
296
Example
By adding a third
input argument to
the plot function,
you can plot the
same variables using
a red dashed line.
plot(x,y,'r--')
Jorma Kekalainen
297
Line specification
The 'r--' string is a line specification.
Each specification can include characters for the line
color, style, and marker.
A marker is a symbol that appears at each plotted
data point, such as a +, o, or *.
For example, 'g:*' requests a dotted green line with *
markers.
plot(x,y,'g:*' )
Jorma Kekalainen
298
Note
Notice that the titles and labels that you
defined for the first plot are no longer in the
current figure window.
By default, Matlab clears the figure each time
you call a plotting function, resetting the axes
and other elements to prepare the new plot.
Jorma Kekalainen
299
Example
To add plots to an existing figure, use hold.
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
hold on
y2 = cos(x);
plot(x,y2,'r:')
legend('sin','cos')
Until you use hold off or close the window, all plots appear
in the current figure window.
Jorma Kekalainen
300
Note
It is possible to combine Greek alphabet to figures as well as sub-and
superscripts.
t = 0:0.1:500; y = sin(100.*t*10^-3).*exp(-8.*t*10^-3);
figure,plot(t, y);
title('{\itAe}^{-\alpha\itt}sin\beta{\itt} \alpha<<\beta');
xlabel('Time \musec.');
ylabel('Amplitude');
Jorma Kekalainen
301
Note
Explanation for the title:
302
Example
Basic command: plot
Jorma Kekalainen
303
Example
t = (0:0.1:10)';
y = sin(2*pi*0.2*t);
figure
plot(t,y,'r+')
hold on
line([0 10],[0 0])
xlabel('Time (s)')
ylabel('\it{sin(2\pift)}')
title('Graph')
grid on
Jorma Kekalainen
304
Subplots
We can display multiple plots in different subregions of the
same window using the subplot function.
For example, create three plots in a 3-by-1 grid within a figure
window. The first two inputs to the subplot function indicate the number
of plots in each row and column. The third input specifies which
plot is active.
t=-3*pi:0.01:3*pi;
yb=cos(t)-(1/3)*cos(3*t)+(1/5)*cos(5*t);
yc=cos(t)-0.5*(1/3)*cos(3*t);
yd=cos(t)-2*(1/3)*cos(3*t);
subplot(311),plot(t,yb),grid
title('yb=cos(t)-(1/3)*cos(3*t)+(1/5)*cos(5*t)')
subplot(312),plot(t,yc),grid
title('yc=cos(t)-0.5*(1/3)*cos(3*t)')
subplot(313),plot(t,yd),grid
Jorma Kekalainen
Digital Image Processing
title('yd=cos(t)-2*(1/3)*cos(3*t)')
305
3-D plots
Three-dimensional plots typically display a
surface defined by a function in two variables,
z = f (x,y).
To evaluate z, first create a set of (x,y) points
over the domain of the function using
meshgrid.
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
Jorma Kekalainen
306
3-D plots
Then, create a surface plot.
surf(X,Y,Z)
Jorma Kekalainen
307
Example
Generate a cylinder defined
by the profile function
2+sin(t).
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(2+cos(t));
surf(X,Y,Z)
axis square
Jorma Kekalainen
308
Subplots
We can display multiple plots in different subregions of the
same window using the subplot function.
For example, create four plots in a 2-by-2 grid within a
figure window.
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X); title('X');
subplot(2,2,2); mesh(Y); title('Y');
subplot(2,2,3); mesh(Z); title('Z');
subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');
309
Example: Subplots
Jorma Kekalainen
310
Character strings
A character string is a sequence of any number of
characters enclosed in single quotes.
You can assign a string to a variable.
>> myText = 'Hi, neighbor';
If the text includes a single quote, use two single
quotes within the definition.
>> otherText = 'You''re right!'
otherText =
You're right!
Jorma Kekalainen
312
Class char
myText and otherText are arrays, like all Matlab variables.
Their class or data type is char, which is short for character.
>> whos myText
Name
Size
Bytes Class Attributes
myText
1x12
24 char
1x13
26 char
Digital Image Processing
313
Concatenating strings
You can concatenate strings with square brackets, just as you
concatenate numeric arrays.
>> longText = [myText,' - ',otherText]
longText =
Hi, neighbor - You're right!
>> whos
Name
Size
longText
myText
otherText
Jorma Kekalainen
1x28
1x12
1x13
314
Jorma Kekalainen
315
whos
>> whos
Name
c
f
longText
myText
otherText
tempText
Jorma Kekalainen
Size
1x1
1x1
1x28
1x12
1x13
1x38
Bytes Class
8
8
56
24
26
76
Attributes
double
double
char
char
char
char
316
Interactivity: input
User can be asked for input,
The question to be asked from the user is given as a
parameter to the input-function
The input function returns the answer of the user.
age = input('How old are you?\n');
note the line feed (\n) in the question to obtain question and
answer on different rows
>> age = input('How old are you?\n');
How old are you?
20
Jorma Kekalainen
317
Interactivity: input
Use parameter 's' to express that you want the return value to
be of type character string.
type = input('Low or high pass filter?\n', 's');
>> type = input('low or high pass filter?\n', 's');
low or high pass filter?
low
>> type
type =
low
>> type=input('Give the filter type: Low pass = ''low'' and High pass = ''high''\n', 's');
Give the filter type: Low pass = 'low' and High pass = 'high'
high
>> type
type =
Jorma
Digital Image Processing
318
highKekalainen
Introduction
Matlab was first used by entering simple commands
from the command line
This is not practical when implementing complex things
requiring tens of commands or multiple repetitions of
the same task.
Reasons for this impracticality are:
errors are easily made
all the commands have to be entered once again when
detecting an error in the beginning of the command series
entering commands by hand is very slow
there will be no documentation of the entered commands
320
Script
The simplest type of Matlab program is called
a script.
A script is a file with a .m extension that
contains multiple sequential lines of Matlab
commands and function calls.
We can run a script by typing its name at the
command line (without .m extension).
Jorma Kekalainen
321
Example
To create a script, use the edit command, e.g.,
edit plotrand
This opens a blank file named plotrand.m.
Enter some code that plots a vector of random data:
n = 50;
r = rand(n,1);
plot(r)
Next, add code that draws a horizontal line on the plot at the mean:
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')
Note: Whenever we write code, it is a good practice to add comments that describe
the code. Comments allow others to understand our code, and can refresh our
Jorma Kekalainen
Digital Image Processing
memory
when we return to it later. Add
comments using the percent (%) symbol.322
Editor Window
>> edit plotrand1
Jorma Kekalainen
323
Creating a script
>> edit plotrand1
Jorma Kekalainen
324
Example: Plotrand1
% Generate random data from a uniform distribution
% and calculate the mean. Plot the data and the mean.
n = 50; % 50 data points
r = rand(n,1);
plot(r)
% Draw a line from (0,m) to (n,m)
m = mean(r);
hold on
plot([0,n],[m,m])
hold off; % What happens if we start this row with %?
title('Mean of Random Uniform Data')
Jorma Kekalainen
325
Jorma Kekalainen
326
Example: Plotrand1
% Generate random data from a uniform distribution
% and calculate the mean. Plot the data and the mean.
n = 50; % 50 data points
r = rand(n,1);
plot(r)
% Draw a line from (0,m) to (n,m)
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')
Jorma Kekalainen
327
Branching
Its often useful to make a choice between one
or multiple alternatives
if condition
% do something
else
% do something else
if (x > y)
end
bigger = x;
else
bigger = y;
Jorma Kekalainen
end
328
Branching
switch x
case 1
% code1
case 2
% code2
case {3, 4}
% code34
otherwise
% code
(optional
end
Jorma Kekalainen
x=1
x=2
x=3, x=4
otherwise
329
>> 3 < 4
ans =
1
Jorma Kekalainen
>> 3 > 4
ans =
0
Digital Image Processing
330
Example
331
>> A = 1; B = 0;
TRUE = 1; FALSE = 0;
Jorma Kekalainen
>> A && A
ans =
1
>> A && B
ans =
0
>> TRUE & TRUE
ans =
1
>> TRUE & FALSE
ans =
Digital
0 Image Processing
>> A || B
ans =
1
>> B || B
ans =
0
>> TRUE | FALSE
ans =
1
>> TRUE|TRUE
ans =
1
332
for-loop
for variable = initialValue:step:finalValue
% do something
end
for index = -5:2:0
index
end
Jorma Kekalainen
index =
-5
index =
-3
index =
-1
333
Examples
FOR I = 1:N,
FOR J = 1:N,
A(I,J) = 1/(I+J-1);
END
END
FOR S = 1.0: -0.1: 0.0, END
steps S with increments of -0.1
Note: The BREAK statement can be used to terminate the loop prematurely.
Jorma Kekalainen
Image Processing
See also
IF, WHILE, SWITCH, BREAK,Digital
END.
334
Example
Its possible to go through every matrix
element by using two internal for-loops
[M, N] = size(X);
total = 0;
for column = 1:N
for row = 1:M
total = total + X(row, column);
end
end
Jorma Kekalainen
335
while-loop
while condition
% do something
end
element = 1;
while (element <= 10)
x(element) = element;
element = element + 1;
end
>> x
x=
1
Jorma Kekalainen
9 10
336
Jorma Kekalainen
337
Example
How much is x?
>> a = 0;
while (2)
a = a + 1;
end
>> a
a=
304383259
Jorma Kekalainen
338
Jorma Kekalainen
339
Example
index = 1;
while (index < 6)
if (index == 4)
break
end
index
index = index + 1;
end
index =
1
index =
2
index =
Jorma Kekalainen
3
index = 1;
while (index < 6)
if (index == 4)
index = index + 1;
continue
end
index
index = index + 1;
end
index =
1
index =
2
index =
3
index =
5
340
Good practise
Program code should be easily read and
understood
easier to debug the errors
easier to remember what the code was intended for
after weeks or months
other people can more easily utilize the code in their
programs
341
342
Jorma Kekalainen
343
344
Jorma Kekalainen
345
Iteration #4
sampleMean =
Iteration #5
sampleMean =
overallMean =
0.4884
Jorma Kekalainen
346
347
Jorma Kekalainen
348
Editor
Type edit or edit file_name to open Matlab
editor.
It is possible to run command string files from
the command line by typing the files name.
Command string files can also be run by
pressing the run button while the editor
window is open.
Jorma Kekalainen
349
350
Jorma Kekalainen
351
Jorma Kekalainen
352
353
Script locations
MATLAB looks for scripts and other files in certain
places.
To run a script, the file must be in the current
folder or in a folder on the search path.
By default, the MATLAB folder that the MATLAB
Installer creates is on the search path.
If you want to store and run programs in another
folder, add it to the search path.
Select the folder in the Current Folder browser,
right-click, and then select Add to Path.
Jorma Kekalainen
354
Built-in functions
Matlab provides an enormous number of built-in functions
as well as the ability to define our own functions.
Functions are equivalent to subroutines in other
programming languages.
Matlab provides a large number of built-in functions that
perform computational tasks.
Fortunately the names of the built-in functions are very
similar to those commonly used in mathematics.
The general syntax is
output_value = function_name(input_value)
Jorma Kekalainen
356
Description
square root
Example
sqrt(2)
exp
exponential
exp(1)
Log
Log2
log10
logarithm base10
Trigonometric
MATLAB
function
sin
functions
Description
log10(1e4)
sine
Example
sin(pi/6)
cos
cosine
cos(pi/3)
tan
tangent
tan(pi/4)
asin
asin(1/2)
acos
acos(1/2)
atan
inverse
tangent (arctan)
Digital Image Processing
atan(1)
Jorma Kekalainen
357
Note
The arguments of sin, cos, tan are always in
radians (not degrees).
The results of the inverse trigonometric
functions will also be in radians.
Jorma Kekalainen
358
To call a function
Suppose that our workspace includes
variables A and B, such as
A = [1 3 5];
B = [10 6 4];
To call a function, enclose its input arguments
in parentheses:
max(A);
Jorma Kekalainen
359
To call a function
If there are multiple input arguments, separate them with commas:
max(A,B);
Return output from a function by assigning it to a variable:
maxA = max(A);
When there are multiple output arguments, enclose them in square
brackets:
[maxA,location] = max(A);
Enclose any character string inputs in single quotes:
disp('hello world');
To call a function that does not require any inputs and does not
return any outputs, type only the function name:
clc
The clc function clears the Command Window.
Jorma Kekalainen
360
Example
>> A=magic(5)
A=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> [maxA,location] = max(A)
maxA =
23 24 25 21 22
location =
2 1 5 4 3
Jorma Kekalainen
361
362
Syntax:
function [y1,...,yN] = myfun(x1,...,xM)
Description:
function [y1,...,yN] = myfun(x1,...,xM)
declares a function named myfun that accepts
inputs x1,...,xM and returns outputs y1,...,yN.
This declaration statement must be the first
executable line of the function.
Jorma Kekalainen
363
Jorma Kekalainen
364
Function m-files
When dealing with functions which will be
used in a variety of different programs, or
have multiple input and output arguments, or
require more complicated coding, then a
function m-file is required.
This is a m-file (a plain text file with a .m
extension) with a special header and then
Matlab commands to perform the required
calculations.
Jorma Kekalainen
365
366
and called by
[x, y, z] = process(A, B)
367
Write a function
Open a file in a text editor.
Within the file, declare the function and add program
statements:
function f = fact(n)
f = prod(1:n);
368
Write a function
If our function returns more than one output, enclose the output
names in square brackets, such as
function [one,two,three] = myfunction(x)
Jorma Kekalainen
369
370
Jorma Kekalainen
371
Parameters
Function is usually given one or more parameters
which it needs to execute its task
The following function avg_example(x) counts
the average value of the numbers given in
parameter vector
function [xmean]=avg_example(x)
% Getting the size of the vector
N = length(x);
% Counting the mean
xmean = 1 / N * sum(x);
Jorma
Kekalainen name = name of the
Digitalm-file.
Image Processing
Note:
Function
372
Example
Jorma Kekalainen
373
>> numbers=(1:100);
>> avg_example(numbers)
ans =
50.5000
Jorma Kekalainen
374
Return values
Return value in a function is declared as
follows:
function xmean = avg_example(x)
Variable xmean gets the value it has when the
function execution ends.
Returning multiple values is done in the same
manner:
function [xmean, N] = avg(x)
Jorma Kekalainen
375
376
Example
Jorma Kekalainen
377
help command
Documentation (HTML-help)
more in-depth instructions, more examples, hyperlinks to related
commands
379
Help
Jorma Kekalainen
380
Help
Matlab comes with a vast amount of online help and
information.
So much, in fact, that it's quite easy to use Matlab
without a manual.
To obtain information on a particular command, we
can use help.
For example:
>> help for
FOR Repeat statements a specific number of times.
The general form of a FOR statement is:
FOR variable = expr, statement, ..., statement END
Jorma Kekalainen
381
382
383
Example
Suppose we want to find out if Matlab supports the
exponential function
>> lookfor exponential
EXP Exponential.
EXPINT Exponential integral function.
EXPM Matrix exponential.
Note: Matlab convention is to use uppercase names for functions in help texts, even
Jorma Kekalainen
Image Processing
384
though
the function itself is called inDigital
lowercase.
Helping documentation
All MATLAB functions have supporting
documentation that includes examples and
describes the function inputs, outputs, and
calling syntax.
There are several ways to access this
information from the command line:
Open the function documentation in a separate
window using the doc command.
doc mean
Jorma Kekalainen
385
Helping documentation
Display function hints (the syntax portion of the
function documentation) in the Command Window by
pausing after you type the open parentheses for the
function input arguments.
mean(
View an abbreviated text version of the function
documentation in the Command Window using the
help command.
help mean
Access the complete product documentation by
clicking the help icon .
Jorma Kekalainen
386
Exercise
Test and study all the previously presented
Matlab commands.
Jorma Kekalainen
387
Matlab
Matlab is a data analysis and visualization tool which
has been designed with powerful support for matrices
and matrix operations.
Matlab has excellent graphics capabilities, and its own
powerful programming language.
One of the reasons that Matlab has become such an
important tool is through the use of sets of Matlab
programs designed to support a particular task.
These sets of programs are called toolboxes, and the
particular toolbox of interest to us is the image
processing toolbox.
Jorma Kekalainen
390
Images in Matlab
The basic data structure in Matlab is the array, an
ordered set of real or complex elements.
This object is naturally suited to the
representation of images, real-valued ordered
sets of color or intensity data.
Matlab stores most images as two-dimensional
arrays (i.e., matrices), in which each element of
the matrix corresponds to a single pixel in the
displayed image.
Pixel or picture element usually denotes a single
dot on a computer display.
Jorma Kekalainen
391
Example
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 true color images, require a threedimensional 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
makes the full power of Matlab available for image
processing applications.
Jorma Kekalainen
392
Example
Create the image of a ramp (256*256):
Jorma Kekalainen
393
Example
for i = 1 : 256
for j = 1 : 256
A(i, j) =j -1;
end
end
image(A)
colormap(gray(256));
axis image;
Jorma Kekalainen
394
Example
The image of a circle
(256*256) of radius 80
pixels centered at (128;
128):
Jorma Kekalainen
for i = 1 : 256
for j = 1 : 256
dist = ((i-128)^2 + (j-128)^2)^(.5);
if (dist < 80)
B(i,j) = 255;
else
B(i,j) = 0;
end
end
end
image(B);
colormap(gray(256));
axis image;
395
Example
The image of a graded
circle (256*256):
C(i,j) = A(i,j)*B(i,j)/255
Jorma Kekalainen
for i = 1 : 256
for j = 1 : 256
C(i,j) = A(i,j)*B(i,j)/255;
end
end
image(C);
colormap(gray(256));
axis image;
396
Example
Create the image of a ramp (256*256):
Jorma Kekalainen
397
Example
>> A=ones(256,1)*(0:255);
D=uint8(A);
figure,imshow(A)
figure,imshow(D)
whos
Name
Size
Bytes Class
A
D
Jorma Kekalainen
256x256
256x256
Attributes
524288 double
65536 uint8
398
399
Matrix
Matlab's standard data type is the matrix.
All data are considered to be matrices of some
sort.
Images, of course, are matrices whose elements
are the gray values (or possibly the RGB values) of
its pixels.
Single values are considered by Matlab to be
(1*1) matrices, while a string is merely a (1*n)
matrix of characters; n being the string's length.
Jorma Kekalainen
400
Grayscale images
Matrices can be handled very efficiently in
Matlab.
Images may be considered as matrices whose
elements are the pixel values of the image.
Suppose you will have a Matlab command
window open, and in it the Matlab prompt >>
ready to receive commands.
Type in the command
>> c=imread('coins.png');
Jorma Kekalainen
401
c=imread('coins.png');
This takes the gray values of all the pixels in
the grayscale image coins.png and puts them
all into a matrix c.
This matrix c is now a Matlab variable, and so
we can perform various matrix operations on
it.
In general the imread function reads the pixel
values from an image file, and returns a matrix
of all the pixel values.
Jorma Kekalainen
402
c=imread('coins.png');
A few notes about this command:
It ends in a semicolon; this has the effect of not
displaying the results of the command to the screen.
As the result of this particular command is a matrix of
size 246*300 = 73800 elements, so we don't want all
its values displayed.
The name coins.png is given in single quote marks.
Without them, Matlab would assume that coins.png
was the name of a variable, rather than the name of a
file.
Jorma Kekalainen
403
figure,imshow(c)
Now we can display this matrix as a grayscale image typing in
the command
>> figure, imshow(c)
This is really two commands on the one line.
Matlab allows many commands to be entered on the same
line; using commas to separate the different commands.
The two commands we are using here are:
figure, which creates a figure on the screen.
A figure is a window in which a graphics object can be placed.
Objects may include images, or various types of graphs.
imshow(c), which displays the matrix c as an image.
This is a display of the gray values of the pixels in the image.
Jorma Kekalainen
404
figure,imshow ('coins.png')
If there are no figures open,
then an imshow command,
or any other command
which generates a graphics
object, will open a new
figure for displaying the
object.
However, it is good practice
to use the figure command
whenever you wish to
create a new figure.
We could display this image
directly, without saving its
gray values to a matrix, with
the command
>>imshow('coins.png')
Jorma Kekalainen
406
Jorma Kekalainen
407
Jorma Kekalainen
408
Jorma Kekalainen
409
Command sequence
F=imread('flowers.tif');
class(F)
size(F)
figure
image(F)
title('flowers.tif')
xlabel('Introduction to Image Processing')
truesize
Jorma Kekalainen
410
411
Jorma Kekalainen
412
413
414
Example
>> a(100,200,2)
>> a(100,200,:)
ans =
25
ans(:,:,1) =
75
>> a(100,200,1:3)
ans(:,:,1) =
75
ans(:,:,2) =
25
ans(:,:,2) =
25
ans(:,:,3) =
ans(:,:,3) =
30
Jorma Kekalainen
30
415
impixel
A useful function for obtaining RGB values is impixel; the
command
>> impixel(a,200,100)
ans =
75 25 30
returns the red, green, and blue values of the pixel at
column 200, row 100.
This command also applies to grayscale images:
>> impixel(c,100,200)
ans =
175 175 175
will return three values, but since c is a single twodimensional matrix, all three values will be the same.
Jorma Kekalainen
416
Note
The order of indexing is opposite to the row,
column order for matrix indexing.
Jorma Kekalainen
>> a(100,200,:)
ans(:,:,1) =
75
ans(:,:,2) =
25
ans(:,:,3) =
30
>> impixel(a,200,100)
ans =
75 25 30
>> impixel(a,100,200)
ans =
161 161 155
417
418
Jorma Kekalainen
419
Gray
B
Jorma Kekalainen
420
Colormaps
Sometimes, the pixels are quantified to the interval [0,255].
These values are transformed through a colormap in the
computer to
Grayscale values, i.e. 0 black and 255 white or
Arbitrary colors (pseudocolors)
421
To D/A-converter and
further to the red
Jorma Kekalainen
channel of the screen.
To D/A-converter and
further
to the green
Digital Image Processing
channel of the screen.
Over 16
million
colors
To D/A-converter and
further to the blue422
channel of the screen.
Jorma Kekalainen
423
Jorma Kekalainen
424
Jorma Kekalainen
425
Jorma Kekalainen
426
427
Example
Jorma Kekalainen
428
Indexed images
An indexed image consists of a data matrix, X, and a colormap matrix,
map.
map is an m-by-3 array of class double containing floating-point values in
the range [0, 1].
Each row of map specifies the red, green, and blue components of a single
color.
An indexed image uses "direct mapping" of pixel values to colormap
values.
The color of each image pixel is determined by using the corresponding
value of X as an index into map.
Values of X therefore must be integers.
The value 1 points to the first row in map, the value 2 points to the second
row, and so on.
Display an indexed image with the statements
image(X); colormap(map)
Note: A colormap is often stored with an indexed image and is automatically loaded with
the image when you use the imread function. However, you are not limited to using the
Jorma Kekalainen
429
default
colormapuse any colormap Digital
thatImage
youProcessing
choose.
430
Intensity images
An intensity image is a data matrix, I, whose values
represent intensities within some range.
An intensity image is represented as a single matrix,
with each element of the matrix corresponding to one
image pixel.
The matrix can be of class double, uint8, or uint16.
While intensity images are rarely saved with a
colormap, a colormap is still used to display them.
In essence, intensity images are treated as indexed
images.
Jorma Kekalainen
431
Intensity images
To display an intensity image, we can
use the imagesc ("image scale")
function, which enables us to set the
range of intensity values.
imagesc scales the image data to use
the full colormap. Use the two-input
form of imagesc to display an intensity
image.
E.g.:
imagesc(I,clims); colormap(gray);
The second input argument
to imagesc specifies the desired
intensity range. The imagesc function
displays I by mapping the first value in
the range to the first colormap entry,
and the second value to the last
colormap entry. Values in between are
linearly distributed throughout the
remaining colormap colors
Jorma Kekalainen
432
Note
imagesc(C) displays C as an image.
Each element of C corresponds to a rectangular area in the
image.
The values of the elements of C are indices into the current
colormap that determine the color of each patch.
imagesc(...,clims) normalizes the values in C to the range
specified by clims and displays C as an image.
clims is a two-element vector that limits the range of data values in C.
Jorma Kekalainen
433
Example
We can expand midrange color
resolution by mapping low
values to the first color and
high values to the last color in
the colormap by specifying
color value limits (clims).
If the size of the current
colormap is 81-by-3, the
statements
clims = [ 10 60 ]
imagesc(C,clims)
map the data values in C to
the colormap as shown beside.
Jorma Kekalainen
434
Example
Jorma Kekalainen
435
Jorma Kekalainen
436
437
Note
To display a matrix A with an arbitrary range of values as an intensity
image, use the single-argument form of imagesc.
With one input argument, imagesc maps the minimum value of the data
matrix to the first colormap entry, and maps the maximum value to the
last colormap entry.
For example, these two lines are equivalent:
imagesc(A); colormap(gray)
imagesc(A,[min(A(:)) max(A(:))]); colormap(gray)
Jorma Kekalainen
438
Example
Jorma Kekalainen
439
Example
Jorma Kekalainen
440
imfinfo('clown.bmp')
imfinfo('clown.bmp')
ans =
Filename: 'C:\Users\ktkekjo\Documents\MATLAB\clown.bmp'
FileSize: 65078
Format: 'bmp'
Width: 320
Height: 200
BitDepth: 8
ColorType: 'indexed'
FormatSignature: 'BM'
NumColormapEntries: 256
Jorma Kekalainen
441
442
Example
>> imshow('ancienttrees.tif')
Jorma Kekalainen
>> AT=imread('ancienttrees.tif');
>> figure,imshow(AT)
443
Example
>> whos('AT')
Name
Size
AT
258x350
Jorma Kekalainen
444
Example
load trees
>> T=ind2gray(X,map);
>> imshow(X,map)
>> figure,imshow(T)
Jorma Kekalainen
445
Note
load trees
T=ind2gray(X,map);
imshow(X,map)
figure,imshow(T)
figure,imagesc(X)
figure,imagesc(X),colormap(map)
Jorma Kekalainen
446
Imfinfo
>> imwrite(X,map,'ancienttrees.tif')
>> imshow('ancienttrees.tif')
>> imfinfo('ancienttrees.tif')
ans =
Filename:
'C:\Users\ktkekjo\Documents\MATLAB\ancienttrees
.tif'
FileModDate: '23-kes-2015 16:40:36'
FileSize: 75764
Format: 'tif'
FormatVersion: []
Width: 350
Height: 258
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
Jorma Kekalainen
447
Note
AT=imread('ancienttrees.tif');
imshow(AT)
Jorma Kekalainen
[AT,cmap]=imread('ancienttrees.tif');
figure,image(AT)
colormap(cmap)
448
449
Jorma Kekalainen
Size
258x350
256x3
Bytes
90300
6144
Class Attributes
uint8
double
450
Jorma Kekalainen
>> round(255*emap(199:203,:))
237 123 176
63 60 99
239 95 149
218 208 207
107 80 88
451
0.9020
0.9294
0.2471
0.9373
0.8549
0.4196
0.8902
0.3098
0.6902
0.3882
0.5843
0.8118
0.3451
0.4549
Jorma Kekalainen
red
0.2235
0.4824
0.2353
0.3725
0.8157
0.3137
0.3137
256
ind(100,100,:)+1=201
emap(198:204,:)
Colormap
Number+1 at
pixel location is
an index into a
colormap.
green
blue
Intensity values
are between 0
and 1.
452
Example
Jorma Kekalainen
24-bit truecolor
8-bit colormapped
453
actual values in
map(112:116,:)
Index+1
0.8392
0.6471
0.8078
0.7098
0.9686
0.7098
0.8078
0.8078
0.8392
0.7098
0.8706
0.9373
0.6118
0.8706
0.8706
255*map(112:116,:)
214
165
206
181
247
Jorma Kekalainen
181
206
206
214
181
222
239
156
222
222
454
The m x n x 3 matrix of
intensity values is
reshaped into a m x n x
3 image of type double.
The values are scaled
to lie between 0 and
255 then converted to
Jorma Kekalainen
type uint8.
455
181
206
206
214
181
222
239
156
222
222
Jorma Kekalainen
456
Jorma Kekalainen
457
Jorma Kekalainen
458
Jorma Kekalainen
459
Jorma Kekalainen
460
Jorma Kekalainen
461
Jorma Kekalainen
462
Example
Jorma Kekalainen
463
464
Example: Scramble/unscramble
an image
D = imread('Dee.jpg');
r = randperm(333);
The image can be unscrambled
c = randperm(500);
using the row and column
Scr = D(r,c,:);
permutation vectors, r and c.
figure
image(Scr)
truesize
title('Scrambled Image')
xlabel('What is it?')
pause
Unscr(r,c,:)=Scr;
figure
image(Unscr)
truesize
title('Unscrambled Image')
Jorma Kekalainen
465
Example
he= imread('he.tif');
r = randperm(300);
c = randperm(480);
Scrambled = he(r,c,:);
figure
image(Scrambled)
truesize
title('Scrambled Image')
xlabel('What is this?')
pause
Unscrambled(r,c,:)=Scrambled;
figure
image(Unscrambled)
truesize
title('Unscrambled Image')
466
Scrambled/unscrambled
D = imread('Dee.jpg');
rng(2,'twister');
s=rng;
r = randperm(333);
c = randperm(500);
Scr = D(r,c,:);
figure
image(Scr)
truesize
title('Scrambled Image')
xlabel('What is it?')
imwrite(Scr,'Scrambled_Image.bmp',
'bmp');
Scrambled=imread('Scrambled_I
mage.bmp');
rng(2,'twister');
s=rng;
figure, imshow(Scrambled)
title('Scrambled image')
pause
r = randperm(333);
c = randperm(500);
Unscrambled(r,c,:)=Scrambled;
figure
image(Unscrambled)
truesize
title('Unscrambled Image')
Note: rng controls random number generation. Generator = 'twister' referred to the Mersenne
Jorma Kekalainen
Digital Image Processing
467
Twister
generator which is now Matlab startup
generator.
Example
Jorma Kekalainen
468
Note: rng
Save the current generator settings in s:
s = rng;
Call rand to generate a vector of random values:
First, initialize the random number generator to make the results in this
example repeatable.
rng(1,'twister');
Save the generator settings in a structure, s.
s = rng;
x = rand(1,10)
Restore the original generator settings by calling rng.
Generate a new set of random values and verify that x and y are equal:
rng(s);
y = rand(1,10)
Jorma Kekalainen
469
Jorma Kekalainen
470
imfinfo('ancienttrees.tif')
>> imfinfo('ancienttrees.tif')
ans =
Filename: [1x50 char]
FileModDate: '23-kes-2015 16:40:36'
FileSize: 75764
Format: 'tif'
FormatVersion: []
Width: 350
Height: 258
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
Jorma Kekalainen
471
472
>> imfinfo('flowers.tif')
>> imfinfo('flowers.tif')
ans =
Filename: [1x45 char]
FileModDate: '22-kes-2015 23:32:10'
FileSize: 547618
Format: 'tif'
FormatVersion: []
Width: 500
Height: 362
BitDepth: 24
ColorType: 'truecolor'
Jorma Kekalainen
473
>> imfinfo('text.png')
Now we shall test this function on a binary image:
>> imfinfo('text.png')
ans =
Filename: [1x62 char]
FileModDate: '13-Oct-2002 08:48:20'
FileSize: 1322
Format: 'png'
FormatVersion: []
Width: 256
Height: 256
BitDepth: 1
ColorType: 'grayscale
Jorma Kekalainen
474
Note
Matlab does not distinguish between grayscale and binary
images: a binary image is just a special case of a grayscale
image which has only two intensities.
>> whos
Name
Size
Te
256x256
Jorma Kekalainen
475
Jorma
Kekalainen
Digital
Processing
Note:
There
are other data types; see
theImage
help
for datatypes.
476
Example
>> a=23;
>> b=uint8(a);
>> b
b=
23
>> whos a b
Name Size Bytes
Class
a
1x1 8
double array
b
1x1 1
uint8 array
Note: Even though the variables a and b have the same numeric value, they are of
different data types. An important consideration is that arithmetic operations are not
Jorma Kekalainen
Digital Image Processing
477
permitted
with the data types int8, int16,
uint8 and uint16.
Example
>> whos a b
Name Size
a
b
1x1
1x1
Bytes Class
Attributes
8 double
1 uint8
>> a/2
ans =
11.5000
>> b/2
ans =
12
Jorma Kekalainen
478
Note
A grayscale image may consist of pixels whose values
are of data type uint8.
These images are thus reasonably efficient in terms
of storage space, since each pixel requires only one
byte.
Arithmetic operations are not permitted on this data
type; a uint8 image should be converted to double
before arithmetic is attempted.
However, most Image Processing Toolbox functions
accept uint8 and uint16 input.
Jorma Kekalainen
479
480
Use
Format
rgb2gray
RGB to grayscale
y=rgb2gray(x);
rgb2ind
RGB to indexed
[ind,cmap]=rgb2ind(x);
ind2rgb
Indexed to RGB
y=ind2rgb(ind,cmap);
gray2ind
Grayscale to indexed
[ind,cmap]=gray2ind(x);
ind2gray
Indexed to grayscale
y=ind2gray(ind,cmap);
Jorma Kekalainen
481
rgb2gray
% RGB to grayscale y=rgb2gray(x)
I=imread('flowers.tif'); figure, imshow(I)
RGBsize=size(I), title('flowers.tif'), xlabel('Original image')
J=rgb2gray(I);
RGBsize =
figure, imshow(J), Graysize=size(J), title('rgb2gray')
362 500
xlabel('RGB to gray')
Graysize =
362 500
Jorma Kekalainen
482
Note: 4 color
rgb2ind
Jorma Kekalainen
0.1412
0.2314
0.6392
0.4000
0.1490
0.8196
483
Example
Read and display a truecolor uint8 JPEG image of a nebula.
RGB = imread('ngc6543a.jpg');
figure('Name','RGB Image')
imagesc(RGB)
title('ngc6543a.jpg')
axis tight
zoom(5)
Jorma Kekalainen
484
Example
Convert RGB to an indexed image with 32 colors
[IND,map] = rgb2ind(RGB,32);
figure('Name','Indexed image')
imagesc(IND)
title('Indexed image with 32 Colors')
colormap(map)
axis tight
zoom(5)
Note: [X,map] = rgb2ind(RGB,n) converts the RGB image to an indexed image X using
minimum variance quantization. map contains at most n colors. n must be less than or
65,536.
The input image can be of class uint8, uint16, single, or double. If the length of map is
less than or equal to 256, the output image is of class uint8. Otherwise, the output image
is of class uint16.
Jorma Kekalainen
Digital Image Processing
485
The value 0 in the output array X corresponds to the first color in the colormap.
imfinfo('ngc6543a.jpg')
>> imfinfo('ngc6543a.jpg')
ans =
Filename: 'C:\Program
Files\MATLAB\R2013a\toolbox\matlab\demos\ngc6543a.jpg'
FileModDate: '01-Oct-1996 16:19:44'
FileSize: 27387
Format: 'jpg'
FormatVersion: ''
Width: 600
Height: 650
BitDepth: 24
ColorType: 'truecolor'
Jorma Kekalainen
486
ind2rgb
% Indexed to RGB y=ind2rgb(x,map)
y=ind2rgb(ind,emap);
figure,imshow(y)
title('ind2rgb')
xlabel('Indexed to RGB using 6 colors')
emap, indsize=size(ind), RGBsize=size(y)
indsize =
362 500
RGBsize =
362 500
Jorma Kekalainen
487
gray2ind
% Grayscale to indexed [y,map]=gray2ind(x,map)
[ind,map]=gray2ind(J);
figure, imshow(ind,map)
indsize=size(ind), mapsize=size(map)
title('gray2ind'), xlabel('Gray to indexed')
indsize =
362 500
mapsize =
64 3
Jorma Kekalainen
488
ind2gray
% Indexed to grayscale y=ind2gray(x,map)
y=ind2gray(ind,map);
figure, imshow(y), title('ind2gray')
indsize=size(ind), mapsize=size(map),Graysize=size(y),
xlabel('Indexed to gray')
indsize =
362 500
mapsize =
64 3
Graysize =
362 500
Jorma Kekalainen
489
Gray to RGB
z(:,:,1)=J;
z(:,:,2)=J;
z(:,:,3)=J;
figure, imshow(z)
title('Gray to RGB')
xlabel('Gray to RGB')
RGBsize=size(z)
Graysize=size(J)
RGBsize =
362 500
Graysize =
362 500
Note: There is not such a gray2rgb function, which create a color image. This
transform is done by simply replicating the gray values of each pixel: grays in an RGB
Jorma Kekalainen
Digital Image Processing
490
image
are obtained by equality of the
red, green and blue values.
Description:
[Y,newmap] = cmunique(X,map) returns the indexed image Y and associated
colormap, newmap, that produce the same image as (X,map) but with the
smallest possible colormap. The cmunique function removes duplicate rows
from the colormap and adjusts the indices in the image matrix accordingly.
[Y,newmap] = cmunique(RGB) converts the truecolor image RGB to the
indexed image Y and its associated colormap, newmap. The return value
newmap is the smallest possible colormap for the image, containing one entry
for each unique color in RGB.
[Y,newmap] = cmunique(I) converts the grayscale image I to an indexed image
Y and its associated colormap, newmap. The return value, newmap, is the
smallest possible colormap for the image, containing one entry for each
unique intensity level in I.
Jorma Kekalainen
491
Example
Eliminate duplicate entries in colormap
Use the magic function to define X as a 4-by-4 array that uses every value
in the range between 1 and 16.
X = magic(4);
Use the gray function to create an eight-entry colormap.
Then, concatenate the two eight-entry colormaps to create a colormap
with 16 entries, map.
In map, entries 9 through 16 are duplicates of entries 1 through 8.
map = [gray(8); gray(8)];
size(map)
ans = 16 3
Use cmunique to eliminate duplicate entries in the colormap.
[Y, newmap] = cmunique(X, map);
size(newmap)
ans = 8 3
cmunique adjusts the values in the original image X so that Y and newmap
produce the same image as X and map.
Jorma Kekalainen
492
Example
figure
image(X)
colormap(map)
title('X and map')
figure
image(Y)
colormap(newmap)
title('Y and newmap')
The input image can be of class uint8, uint16, or double. The class of the output image Y is uint8
if the length of newmap is less than or equal to 256. If the length of newmap is greater than
Kekalainen
Digital Image Processing
493
256, YJorma
is of
class double.
Example
X = magic(4);
map = [gray(8); gray(8)];
size(map)
[Y, newmap] = cmunique(X, map);
size(newmap)
figure
image(X)
colormap(map)
title('X and map')
figure
image(Y)
colormap(newmap)
title('Y and newmap')
Jorma Kekalainen
494
Exercise 1
Test and study all the previously presented
Matlab command sequences.
Jorma Kekalainen
495
Exercise 2
Type in the command
>> help imdemos
This will give you a list of, amongst other
things, all the sample PNG images which come
with the Image Processing Toolbox.
(a) Make a list of these sample images, and
(b) for each image determine its type (binary,
grayscale, true color or indexed color), and its
size (in pixels)
Jorma Kekalainen
496
Exercise 3
Pick a grayscale image, e.g., cameraman.tif.
Using the imwrite function,write it to files of
type JPEG, PNG and BMP.
What are the sizes of those files?
Jorma Kekalainen
497
Exercise 4
Jorma Kekalainen
498
Introduction
Here, we look at image quality, and how that
may be affected by various image attributes.
Quality is, of course, a highly subjective
matter.
However, for human vision in general, images
are preferred to be sharp and detailed.
This is a consequence of two properties of an
image: spatial resolution, and quantization.
Jorma Kekalainen
501
502
503
Example
c=imread('cameraman.tif');figure,imshow(c)
cd=double(c);figure,imshow(cd)
Jorma Kekalainen
Example
As we can see, figure (b) doesn't look like the original
picture at all!
This is because for a matrix of type double, the imshow
function expects the values to be between 0 and 1, where 0
is displayed as black, and 1 is displayed as white.
A value 0<v<1 is displayed as grayscale [255v].
Conversely, values greater than 1 will be displayed as 1
(white) and values less than 0 will be displayed as zero
(black).
In the cameraman image, every pixel has value greater
than or equal to 1 (in fact the minimum value is 7), so that
every pixel will be displayed as white.
Jorma Kekalainen
505
Example
To display the matrix cd, we need to scale it to
the range 0 - 1.
This is easily done simply by dividing all values
by 255:
imshow(cd/255)
and the result will be the cameraman image
as shown in the previous figure (a).
Jorma Kekalainen
506
Scaling
We can vary the display by changing the scaling of the matrix.
Results of the commands:
imshow(cd/512)
imshow(cd/128)
Jorma Kekalainen
507
Results of scaling
Dividing by 512 darkens the image, as all
matrix values are now between 0 and 0.5, so
that the brightest pixel in the image is a midgray.
Dividing by 128 means that the range is 0-2,
and all pixels in the range 1-2 will be displayed
as white.
Thus the image has an over-exposed, washedout appearance.
Jorma Kekalainen
508
Comment
The display of the result of a command whose
output is a matrix of type double can be
greatly affected by a judicious choice of a
scaling factor.
Jorma Kekalainen
509
im2double
We can convert the original image to double more
properly using the function im2double.
This applies correct scaling so that the output values
are between 0 and 1.
So the commands
cd=im2double(c);
imshow(cd)
will produce a correct image.
Jorma Kekalainen
510
Jorma Kekalainen
511
512
Image types
>> whos
Name
c
c2
c3
cd
cd1
cd512
Jorma Kekalainen
Size
256x256
256x256
256x256
256x256
256x256
256x256
Bytes
65536
65536
65536
524288
524288
524288
Digital Image Processing
Class
Attributes
uint8
uint8
uint8
double
double
double
513
Example
load('mandrill.mat');
m=im2uint8(ind2gray(X,map));
figure,imshow(m)
title('imshow(m)')
figure,imshow(X,map)
title('imshow(X,map)')
Jorma Kekalainen
514
You can also pass imshow the name of a file containing an image.
imshow('moon.tif');
Note that when you use this syntax, imread does not store the image data
in the Matlab workspace. If you want to bring the image into the
workspace, you must use the getimage function.
This example assigns the image data from moon.tif to the variable moon,
if the figure window in which it is displayed is currently active
moon = getimage;
For more information about using imshow to display the various image
types supported by the toolbox, see Display Different Image Types.
Jorma Kekalainen
515
Images displayed
Jorma Kekalainen
516
Initial magnification
Jorma Kekalainen
518
The Image Processing Toolbox Preferences dialog box contains display preferences
for imtool and imshow. We can set all preferences at the command line with the
iptsetpref function.
Jorma Kekalainen
519
520
Jorma Kekalainen
521
F=imread('peppers.png');
imshow(F(:,:,1))
figure, imshow(F(:,:,2))
figure, imshow(F(:,:,3))
Jorma Kekalainen
522
Jorma Kekalainen
523
Note: When we use subplot to display multiple color images in one figure window,
the images must share the colormap of the last image displayed. In some cases the
display results can be unacceptable. As an alternative, we can use the subimage
Jorma Kekalainen
Digital Image Processing
524
function,
or we can map all images to
the same colormap as we load them.
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), subimage(X1,map1)
subplot(1,2,2), subimage(X2,map2)
Jorma Kekalainen
525
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), subimage(X1,map1)
subplot(1,2,2), subimage(X2,map2)
[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)
Jorma Kekalainen
526
Jorma Kekalainen
527
528
Binary images
Recall that a binary image will have only two
values: 0 and 1.
Matlab does not have a binary data type as such,
but it does have a logical flag, where uint8 values
as 0 and 1 can be interpreted as logical data.
The logical flag will be set by the use of relational
operations such as ==, < or > or any other
operations which provide a yes/no answer.
Jorma Kekalainen
529
Example
For example, suppose we take the cameraman matrix and
create a new matrix with the relational operator >
>> c1=c>128;
If we now check all of our variables with whos, the output will
include the line:
c1
256x256
Jorma Kekalainen
65536 logical
530
Bit planes
Grayscale images can be transformed into a sequence of
binary images by breaking them up into their bit-planes.
If we consider the gray value of each pixel of an 8-bit image
as an 8-bit binary word, then the 0th bit plane consists of
the last bit of each gray value.
Since this bit has the least effect in terms of the magnitude
of the value, it is called the least significant bit, and the
plane consisting of those bits the least significant bit plane.
Similarly the 7th bit plane consists of the first bit in each
value.
This bit has the greatest effect in terms of the magnitude of
the value, so it is called the most significant bit, and the
plane consisting of those bits the most significant bit plane.
Jorma Kekalainen
531
Example
If we take a grayscale image, we start by making it a matrix of type double;
this means we can perform arithmetic on the values. E.g.,
>> c=imread('cameraman.tif');
>> cd=double(c);
We now isolate the bit planes by simply dividing the matrix cd by
successive powers of 2, taking the remainder, and seeing if the final bit is 0
or 1.
We can do this with the mod function.
c0=mod(cd,2);
c1=mod(floor(cd/2),2);
c2=mod(floor(cd/4),2);
c3=mod(floor(cd/8),2);
c4=mod(floor(cd/16),2);
c5=mod(floor(cd/32),2);
c6=mod(floor(cd/64),2);
c7=mod(floor(cd/128),2);
A corresponding example is seen in the following figures.
Jorma Kekalainen
532
c=imread('Hedyg-scale.tif');
cd=double(c);
c0=mod(cd,2);
imshow(c0)
Jorma Kekalainen
533
Example: The
st
1
bit plane
Jorma Kekalainen
534
Example: The
nd
2
bit plane
Jorma Kekalainen
535
Example: The
rd
3
bit plane
Jorma Kekalainen
536
Example: The
th
4
bit plane
Jorma Kekalainen
537
Example: The
th
5
bit plane
Jorma Kekalainen
538
Example: The
th
6
bit plane
Jorma Kekalainen
539
Jorma Kekalainen
540
cc=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;
figure, imshow(uint8(cc))
Original grayscale image
Jorma Kekalainen
541
Original image
Jorma Kekalainen
542
Hedy Lamarr aka Hedwig Kiesler Hollywood star and inventor (Patent US 2292387 A)
Note
The least significant bit plane, c0, is practically a random array.
The most significant bit plane, c7, is actually a threshold of the
image at level 127:
ct=c>127;
all(c7(:)==ct(:))
ans =
1
We can recover and display the original image with
cc=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;
imshow(uint8(cc))
Jorma Kekalainen
543
whos
>> whos
Name
c
c0
c1
c2
c3
c4
c5
c6
c7
c8
cc
cd
Size
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
467x557
Jorma Kekalainen
Bytes
Class
260119
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
2080952
Attributes
uint8
double
double
double
double
double
double
double
double
double
double
double
544
c=imread('Hedyg-scale.tif');
cd=double(c);
>> c0=mod(cd,2);
c1=mod(floor(cd/2),2);
c2=mod(floor(cd/4),2);
c3=mod(floor(cd/8),2);
c4=mod(floor(cd/16),2);
c5=mod(floor(cd/32),2);
c6=mod(floor(cd/64),2);
c7=mod(floor(cd/128),2);
Jorma Kekalainen
imshow(c0)
figure, imshow(c1)
figure, imshow(c2)
figure, imshow(c3)
figure, imshow(c4)
figure, imshow(c5)
figure, imshow(c6)
figure, imshow(c7)
cc=2*(2*(2*(2*(2*(2*(2*c7
+c6)+c5)+c4)+c3)+c2)+c1)+c
0;
figure,imshow(uint8(cc))
545
Exercise
Transform the image cameraman.tif into a
sequence of binary images by breaking them
up into their bit-planes.
Jorma Kekalainen
546
Solution
c=imread('cameraman.tif');
cd=double(c);
%We now isolate the bit planes by simply dividing the matrix cd by successive powers of 2, taking
%the remainder, and seeing if the final bit is 0 or 1. We can do this with the mod function.
c0=mod(cd,2);
c1=mod(floor(cd/2),2);
c2=mod(floor(cd/4),2);
c3=mod(floor(cd/8),2);
c4=mod(floor(cd/16),2);
c5=mod(floor(cd/32),2);
c6=mod(floor(cd/64),2);
c7=mod(floor(cd/128),2);
>> imshow(c0)
>> figure, imshow(c1)
>> figure, imshow(c2)
>> figure, imshow(c3)
>> figure, imshow(c4)
>> figure, imshow(c5)
>> figure, imshow(c6)
>> figure, imshow(c7)
>>
Jorma Kekalainen
547
Jorma Kekalainen
548
Jorma Kekalainen
549
Note
We can recover and display the original image
with
cc=2*(2*(2*(2*(2*(2*(2*c7+c6)+c5)+c4)+c3)+c2)+c1)+c0;
imshow(uint8(cc))
Jorma Kekalainen
550
mod
Remainder after division (modulo operation)
Z = mod(X,Y)
Z = mod(X,Y) returns the remainder after
division of X by Y, where X is the dividend and
Y is the divisor.
This function is called the modulo operation
and is computed using Z = X - Y.*floor(X./Y).
The mod function follows the convention that
mod(X,0) returns X.
Note:Jorma
rem(X,Y)
and mod(X,Y) are equal if Digital
X and
Y have the same sign, but differ by Y if X and
Y
Kekalainen
Image Processing
551
have different signs. Notice that rem retains the sign of X, while mod retains the sign of Y.
Example
Compute 22 modulo 5.
Z = mod(X/Y)=mod(22,5)
Z =2
Check
Z = X - Y.*floor(X./Y)=22-5*floor(22/5)
Z = 22-5*4=2
Jorma Kekalainen
552
Example
Z = X - Y.*floor(X./Y)
>> X=22;
>> Y=5;
>> X./Y
ans =
4.4000
>> floor(4.4)
ans =
4
>> Z=X-Y.*4
Z=
2
>> Z = X - Y.*floor(X./Y)
Z=
2
>> Z=mod(X,Y)
Z=
2
Jorma Kekalainen
553
Exercise
Jorma Kekalainen
554
Solution
>> A=floor(255*rand(5))
A=
207 24 40 36 167
230 71 247 107 9
32 139 244 233 216
232 244 123 202 238
161 246 204 244 173
>> A0=rem(A,2)
A0 =
1 0 0 0 1
0 1 1 1 1
0 1 0 1 0
0 0 1 0 0
1 0 0 0 1
>> A1=rem(floor(A/2),2)
A1 =
1 0 0 0 1
1 1 1 1 0
0 1 0 0 0
0 0 1 1 1
0 1 0 0 0
Jorma Kekalainen
>> A2=rem(floor(A/4),2)
A2 =
1 0 0 1 1
1 1 1 0 0
0 0 1 0 0
0 1 0 0 1
0 1 1 1 1
>> A3=rem(floor(A/8),2)
A3 =
1 1 1 0 0
0 0 0 1 1
0 1 0 1 1
1 0 1 1 1
0 0 1 0 1
>> A4=rem(floor(A/16),2)
A4 =
0 1 0 0 0
0 0 1 0 0
0 0 1 0 1
0 1 1 0 0
0 1 0 1 0
555
Solution
>> A5=rem(floor(A/32),2)
A5 =
0 0 1 1 1
1 0 1 1 0
1 0 1 1 0
1 1 1 0 1
1 1 0 1 1
>> A6=rem(floor(A/64),2)
A6 =
1 0 0 0 0
1 1 1 1 0
0 0 1 1 1
1 1 1 1 1
0 1 1 1 0
>> A7=rem(floor(A/128),2)
A7 =
1 0 0 0 1
1 0 1 0 0
0 1 1 1 1
1 1 0 1 1
1 1 1 1 1
Jorma Kekalainen
>>
AA=2*(2*(2*(2*(2*(2*(2*A7+A6)+A5)+A4)+A3)+A2)+A1)+A0
AA =
207 24 40 36 167
230 71 247 107 9
32 139 244 233 216
232 244 123 202 238
161 246 204 244 173
>> Ath=A>127
Ath =
1 0 0 0 1
1 0 1 0 0
0 1 1 1 1
1 1 0 1 1
1 1 1 1 1
>> A=floor(255*rand(5))
A=
207 24 40 36 167
230 71 247 107 9
32 139 244 233 216
232 244 123 202 238
161 246 204 244 173
556
Spatial resolution
Spatial resolution is the density of pixels over
the image: the greater the spatial resolution,
the more pixels are used to display the image.
We can experiment with spatial resolution
with Matlab's imresize function.
Jorma Kekalainen
558
imresize function
Suppose we have an 256*256 8-bit grayscale image saved to
the matrix x.
Then the command
imresize(x,1/2, 'nearest');
will halve the size of the image by taking out every other row
and every other column, thus leaving only those matrix
elements whose row and column indices are even:
imresize(x,1/2, 'nearest')
Jorma Kekalainen
559
imresize function
If we apply imresize to The effective resolution of this
the result with the
new image is only 128*128.
parameter 2 rather than We can do all this at the same
1/2, all the pixels are
time:
repeated to produce an imresize(imresize(x,1/2,
'nearest'),2,'nearest');
image with about the
same size as the
original, but with half
the resolution in each
direction:
Jorma Kekalainen
560
28
198
99
61
102
24
Jorma Kekalainen
33 90 186 48 20
240 209 165 175 236
243 3 114 46 197
146 10 139 93 124
15 43 75 159 111
59 165 189 198 113
Digital Image Processing
561
240
240
146
146
59
59
Jorma Kekalainen
562
3
3
3
3
165
165
165
165
Jorma Kekalainen
3 3 3
3 3 3
3 3 3
3 3 3
165 165
165 165
165 165
165 165
197
197
197
197
165
165
165
165
197
197
197
197
113
113
113
113
197
197
197
197
113
113
113
113
197
197
197
197
113
113
113
113
113
113
113
113
563
159
159
159
159
159
159
159
159
Jorma Kekalainen
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
159
564
>>
Aold1=imresize_old(imresize_old(
A,1/4),4)
Aold =
Aold1 =
240
240
146
146
59
59
Jorma Kekalainen
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
565
Note
About the same effect we can achieve
applying the command
imresize_old( imresize_old(x,1/2),2);
Jorma Kekalainen
566
Effective resolution
By changing the parameters of imresize, we
can change the effective resolution of the
image to smaller amounts:
Jorma Kekalainen
567
Example
To see the effects of these commands,
suppose we apply them to the image
Deepiga.png:
x=imread('Deepiga.png');
The effects of decreasing blockiness or
pixelization become quite pronounced as the
resolution increases.
Jorma Kekalainen
568
Example
x=imread('Deepiga.png');
x6=imresize(imresize(x,1/64, 'nearest'),64,'nearest');
figure,imshow(x6)
x5=imresize(imresize(x,1/32, 'nearest'),32,'nearest');
figure,imshow(x5)
x4=imresize(imresize(x,1/16, 'nearest'),16,'nearest');
figure,imshow(x4)
x3=imresize(imresize(x,1/8 ,'nearest'),8,'nearest');
figure,imshow(x3)
x2=imresize(imresize(x,1/4, 'nearest'),4,'nearest');
figure,imshow(x2)
x1=imresize(imresize(x,1/2 ,'nearest'),2,'nearest');
figure,imshow(x1)
figure,imshow(x)
Jorma Kekalainen
569
Jorma Kekalainen
570
Jorma Kekalainen
571
Jorma Kekalainen
572
Jorma Kekalainen
573
Jorma Kekalainen
574
Jorma Kekalainen
575
Jorma Kekalainen
576
579
Transforms
We require a knowledge of all the gray levels in the entire
image to transform the image.
I.e., the entire image is processed as a single large block.
Jorma Kekalainen
580
581
Point operations
Although point operations are the simplest,
they contain some of the most powerful and
widely used of all image processing
operations.
They are especially useful in image preprocessing, where an image is required to be
modified before the main job is attempted.
Jorma Kekalainen
582
Arithmetic operations
These operations act by applying a simple
function y=f(x) to each gray value in the image.
Thus f(x) is a function which maps the range 0
255 onto itself.
Simple functions include adding or subtract a
constant value to each pixel: y=xC or multiplying
each pixel by a constant: y=Cx.
In each case we may have to fix the output
slightly in order to ensure that the results are
integers in the range 0 255.
Jorma Kekalainen
583
Arithmetic operations
We can do this by first rounding the result (if
necessary) to obtain an integer, and then clipping
the values by setting:
255, if y > 255
y=
0, if y < 0
584
Note: When we add 128, all gray values of 127 or greater will be mapped to 255.
And when we subtract 128, all gray values of 128 or less will be mapped to 0.
By looking at these graphs, we see that in general adding a constant will lighten an
Jorma Kekalainen
Digital Image Processing
585
image, and subtracting a constant will darken it.
Testing example
We can test the previous
conclusions on the cameraman
image cameraman.tif.
We start by reading the image
in:
>> c=imread(cameraman.tif');
>> whos c
Name Size Bytes Class
c 256x256 65536 uint8 array
Jorma Kekalainen
586
Testing example
We can do this straightly c1=c+128 or we can first turn
c into a matrix of type double, add the 128, and then
turn back to uint8 for display:
>> c1=uint8(double(c)+128);
A more elegant way, is to use the Matlab function
imadd which is designed precisely to do this:
>> c1=imadd(c,128);
Subtraction is similar; we can transform out matrix in
and out of double, or use the imsubtract function:
>> c2=imsubtract(c,128);
Jorma Kekalainen
587
Jorma Kekalainen
588
Lightening or darkening of an
image by multiplication
We can also perform lightening or darkening of an
image by multiplication.
To implement these functions, we use the immultiply
function.
All these resulting images can be viewed with imshow.
Compare the results of darkening c2 and c3.
Note that c3, although darker than the original, is still
quite clear, whereas a lot of information has been lost
by the subtraction process, as can be seen in image c2.
This is because in image c2 all pixels with gray values
128 or less have become zero.
Jorma Kekalainen
589
c3: y=x/2
Jorma Kekalainen
590
Jorma Kekalainen
c4: y=2x
591
Comparing results
A similar loss of information has occurred in
the images c1 and c4.
Note that the light colored buildings in the
background; in both c1 and c4 have been
disappeared.
However, the buildings are quite visible in
image c5.
Jorma Kekalainen
592
c5: y=x/2+128
Jorma Kekalainen
593
Image complementation
The complement of a grayscale image is its
photographic negative.
If an image matrix m is of type double and so its
gray values are in the range 0.0 1.0, we can
obtain its negative with the command
>> 1-m
If the image is binary, we can use
>> ~m
If the image is of type uint8, the best approach is
the imcomplement function.
Jorma Kekalainen
594
Binary conversion
BI=im2bw(Deepiga,0.5);% Binary conversion with threshold
>> imshow(BI)
>> figure, imshow(~BI)
Jorma Kekalainen
595
Note: im2bw
BW = im2bw(I, level)
BW = im2bw(X, map, level)
BW = im2bw(RGB, level)
Description
BW = im2bw(I, level) converts the grayscale
image I to a binary image.
The output image BW replaces all pixels in the
input image with luminance greater than level
Jorma Kekalainen
596
Image complementation
Next figure shows the complement function y=255-x, and the
result of the commands
>> cc=imcomplement(c);
>> imshow(cc)
Jorma Kekalainen
597
Part complementation
Interesting special effects can be obtained by
complementing only part of the image.
E.g., by taking the complement of pixels of gray
value 128 or less, and leaving other pixels
untouched.
Or we could take the complement of pixels which
are 128 or greater, and leave other pixels
untouched.
Figure shows these functions.
The effect of these functions is called solarization.
Jorma Kekalainen
598
Solarization
599
Histograms
Given a grayscale image, its histogram consists of
the histogram of its gray levels; that is, a graph
indicating the number of times each gray level
occurs in the image.
We can deduce a great deal about the
appearance of an image from its histogram:
In a dark image, the gray levels (and hence the
histogram) would be clustered at the lower end.
In a uniformly bright image, the gray levels would be
clustered at the upper end.
In a well contrasted image, the gray levels would be
well spread out over much of the range.
Jorma Kekalainen
600
Jorma Kekalainen
601
imhist
We can view the histogram of an image in
Matlab by using the imhist function:
>> p=imread('pout.tif');
>> imshow(p),figure,imhist(p),axis tight
(the axis tight command ensures the axes of
the histogram are automatically scaled to fit
all the values in).
Jorma Kekalainen
602
Jorma Kekalainen
603
604
Jorma Kekalainen
605
Histogram stretching
We can stretch the gray
levels in the centre of the
range out by applying the
piecewise linear function
shown at the right.
This function has the effect
of stretching the gray levels
5-9 to gray levels 2-14
according to the equation:
606
Stretching function
Gray levels outside this
range are either left alone
as in this case (or
transformed according to
the linear functions at the
ends of the graph).
Jorma Kekalainen
607
Corresponding stretched
histogram
A stretched histogram indicates an image
with greater contrast than the original.
Jorma Kekalainen
A histogram of a poorly
contrasted image
608
Note: Pixel values less than a are all converted to c and pixel values greater than b are
Jorma Kekalainen
Digital Image Processing
609
all converted
to d.
Example
P=imread('pout.tif');
figure, imshow(P)
title('Image before histogram streching')
figure, imhist(P)
title('Image histogram before histogram streching')
Padj=imadjust(P,[75/255,150/255],[0,1]);
figure, imshow(Padj)
title('Image after histogram streching')
figure, imhist(Padj)
title('Image after histogram streching')
figure,plot(P,Padj,'.'),axis tight
title('Stretching function imadjust(P,[75/255,150/255],[0,1])')
Jorma Kekalainen
611
Example
Jorma Kekalainen
612
Jorma Kekalainen
613
Gamma value
The imadjust function has one other optional
parameter: the gamma value, which describes
the shape of the function between the
coordinates (a,c) and (b,d).
If gamma is equal to 1, which is the default, then
a linear mapping is used, as shown in the
previous case.
However, values less than one produce a function
which is concave downward, and values greater
than one produce a function which is concave
upward, as shown in the following figure.
Jorma Kekalainen
614
concave upward
concave downward
Jorma Kekalainen
615
Example
The function used is a slight variation on the standard
line between two points:
616
Example
The tire image and after adjustment with the
gamma value =0.5.
Jorma Kekalainen
617
Stretching function
We may view the imadjust stretching function
with the plot function.
For example,
>> plot(t,th,'.'),axis tight
produces the plot shown in the following figure.
Since t and th are matrices which contain the
original values and the values after the imadjust
function, the plot function simply plots them,
using dots () to do it.
Jorma Kekalainen
618
Stretching function
Stretching function used in the previous tire image.
=0.5 (<1 concave downward)
Jorma Kekalainen
619
620
classChanged = 0;
if ~isa(im, 'double'),
classChanged = 1;
im = im2double(im);
end
if length(a) ~= length (b)
error('Vectors A and B must be of equal size');
end
N=length(a);
out=zeros(size(im));
for i=1:N-1
pix=find(im>=a(i) & im<a(i+1));
out(pix)=(im(pix)-a(i))*(b(i+1)-b(i))/(a(i+1)-a(i))+b(i);
end
pix=find(im==a(N));
out(pix)=b(N);
if classChanged==1
out = uint8(255*out);
end
621
Example: histpwl
As an example of the use of this function:
>> th=histpwl(t,[0 .25 .5 .75 1],[0 .75 .25 .5 1]);
>> imshow(th)
>> figure,plot(t,th,'.'),axis tight
produces the figures shown below.
Jorma Kekalainen
622
Histogram equalization
The trouble with any of the above methods of
histogram stretching is that they require user
input.
Sometimes a better approach is provided by
histogram equalization, which is an entirely
automatic procedure.
Suppose our image has L different gray levels
0, 1, 2, , L-1 and that gray level i occurs ni
times in the image.
Jorma Kekalainen
623
Histogram equalization
Suppose also that the total number of pixels in
the image is n so that n0+n1+n2+ +nL-1=n.
To transform the gray levels to obtain a better
contrasted image, we change gray level i to
624
Example
Suppose a 4-bit grayscale image has the histogram
shown in the following figure associated with a table
of the numbers (n=360)
625
Example
Histogram indicating poor contrast
Jorma Kekalainen
626
Example
To equalize this
histogram, we
form running
totals of the ni ,
and multiply each
by 15/360=1/24.
Jorma Kekalainen
627
Example
We now have the following transformation of gray
values, obtained by reading off the first and last
columns in the previous table:
628
Example
The histogram before
equalization
Jorma Kekalainen
629
histeq
To apply histogram equalization in Matlab, use
the histeq function; for example:
>> p=imread('pout.tif');
>> ph=histeq(p);
>> imshow(ph),figure,imhist(ph),axis tight
applies histogram equalization to the pout image,
and produces the resulting histogram.
Notice the far greater spread of the histogram
which corresponds to the greater increase of
contrast in the image.
Jorma Kekalainen
630
Jorma Kekalainen
631
Jorma Kekalainen
Since the
index matrix
contains only
low values it
will appear
dark when
displayed.
Digital Image Processing
632
Jorma Kekalainen
633
Jorma Kekalainen
634
Example
H=imread('he.tif');
figure,imshow(H),figure,imhist(H(:,:,1)),axis tight
[he,emap]=gray2ind(H);
figure, imshow(he)
title('Index values only')
figure,imhist(he(:,:,1)),axis tight
title('Index matrix consisting low values only')
hee=histeq(he(:,:,1));
figure, imshow(hee),
title('After equalization')
figure,imhist(hee),axis tight
title('After equalization')
Jorma Kekalainen
635
Example
H=imread('he.tif');
figure,imshow(H),figure,imhist(H(:,:,1)),axis
tight
Jorma Kekalainen
636
Example
[he,emap]=gray2ind(H);
figure, imshow(he)
title('Index values only')
figure,imhist(he(:,:,1)),axis tight
title('Index matrix consisting low values only')
Jorma Kekalainen
637
After equalization
hee=histeq(he(:,:,1));
figure, imshow(hee),
title('After equalization')
figure,imhist(hee),axis tight
title('After equalization')
Jorma Kekalainen
638
Example
Jorma Kekalainen
639
Comment
Consider the histogram
beside.
To apply histogram
stretching, we would
need to stretch out the
values between gray
levels 9 and 13.
Thus, we would need to
apply a piecewise
function similar to that
shown beside.
Jorma Kekalainen
640
Comment
Let's consider the
Cumulative histogram
cumulative histogram,
which is shown beside.
The dashed line is simply
joining the top of the
histogram bars.
However, it can be
interpreted as an
appropriate histogram
stretching function.
To do this, we need to
scale the y values so that
they are between 0 and
But this is precisely the method
15, rather than 0 and 360. described in the histogram
Jorma Kekalainen
equalization section.
641
643
FileSize: 16388
Format: 'png'
FormatVersion: []
Width: 357
Height: 221
BitDepth: 8
ColorType: 'grayscale'
Colormap: []
Histogram: []
Jorma Kekalainen
645
Jorma Kekalainen
647
Jorma Kekalainen
Size
221x357
649
Jorma Kekalainen
651
Jorma Kekalainen
652
Jorma Kekalainen
653
Jorma Kekalainen
654
>> subplot(121),imshow(I)
>> title('Grayscale image ex4-fig1.png')
>> subplot(122),imshow(I2)
>> title('Equalized image')
>> truesize
Jorma Kekalainen
655
Note
The Image Toolbox includes several other functions
that perform contrast adjustment, including the
imadjust and adapthisteq functions.
See Adjusting Pixel Intensity Values for more information.
656
657
658
imfinfo('ex4-fig12.tif') and
imfinfo('ex4-fig12.png')
>> imfinfo('ex4-fig12.tif')
ans =
Filename: [1x47 char]
FileModDate: '07-elo-2015 23:23:34'
FileSize: 46868
Format: 'tif'
FormatVersion: []
Width: 357
Height: 221
BitDepth: 8
ColorType: 'grayscale'
Jorma Kekalainen
659
Exercise
The following data is picked from a small (8*8)
image of type uint8 which has gray values in the
range 0 to 19. Compute the gray level histogram
of the image and then equalize this histogram.
Compare histograms and images before and after
equalization.
data=[12 6 5 13 14 14 16 15 11 10 8 5 8 11 14 14
9 8 3 4 7 12 18 19 10 7 4 2 10 12 13 17 16 9 13 13
16 19 19 17 12 10 14 15 18 18 16 14 11 8 10 12
14 13 14 15 8 6 3 7 9 11 12 12]
Jorma Kekalainen
660
Solution
data=[12 6 5 13 14 14 16 15 11 10 8 5 8 11 14 14 9 8 3 4 7 12 18 19 10 7 4 2 10 12
13 17 16 9 13 13 16 19 19 17 12 10 14 15 18 18 16 14 11 8 10 12 14 13 14 15 8 6 3
7 9 11 12 12]
imdata =
12 11 9 10 16 12 11 8
imdata=reshape(data,8,8)
6 10 8 7 9 10 8 6
imdata1=imdata'
5 8 3 4 13 14 10 3
IM=uint8(imdata1)
13 5 4 2 13 15 12 7
figure, imhist(IM)
14 8 7 10 16 18 14 9
title('Histogram of the image')
14 11 12 12 19 18 13 11
eqIM=histeq(IM)
16 14 18 13 19 16 14 12
figure, imhist(eqIM)
15 14 19 17 17 14 15 12
title('Histogram of the equalized image')
imdata1 =
figure, imshow(IM)
12 6 5 13 14 14 16 15
title('The small image before equalization')
11 10 8 5 8 11 14 14
figure, imshow(eqIM)
9 8 3 4 7 12 18 19
title('The small image after equalization')
10 7 4 2 10 12 13 17
16 9 13 13 16 19 19 17
12 10 14 15 18 18 16 14
11 8 10 12 14 13 14 661
15
Jorma Kekalainen
Digital Image Processing
8 6 3 7 9 11 12 12
Note
nbins=8;
figure,hist(data,nbins)
title('Histogram plot of the original data')
Jorma Kekalainen
662
Solution
IM =
12 6 5 13 14 14 16 15
11 10 8 5 8 11 14 14
9 8 3 4 7 12 18 19
10 7 4 2 10 12 13 17
16 9 13 13 16 19 19 17
12 10 14 15 18 18 16 14
11 8 10 12 14 13 14 15
8 6 3 7 9 11 12 12
eqIM =
130 28 20 154 178 178 215 202
105 89 57 20 57 105 178 178
73 57 4 12 40 130 239 251
89 40 12 0 89 130 154 227
215 73 154 154 215 251 251 227
130 89 178 202 239 239 215 178
105 57 89 130 178 154 178 202
28 4 40 73 105 130 130
Jorma57
Kekalainen
Digital Image Processing
663
Solution
The images before and after equalization
Jorma Kekalainen
664
Exercise
Create a dark image with
c=imread('cameraman.tif');
[x,map]=gray2ind(c);
Jorma Kekalainen
665
Single thresholding
A grayscale image is turned into a binary (black and
white) image by first choosing a gray level T in the
original image, and then turning every pixel black or
white according to whether its gray value is greater than
or less than T
white if its gray level is >T
A pixel becomes
black if its gray level is T
Thresholding is a vital part of image segmentation,
where we wish to isolate objects from the background.
It is also an important component of robot vision.
Jorma Kekalainen
667
Thresholding in Matlab
Thresholding can be done simply in Matlab.
Suppose we have an image, stored as the
variable X.
Then the command
X>T
will perform the thresholding.
We can view the result with imshow.
Jorma Kekalainen
668
Example
The commands
>> r=imread('rice.tif');
>> imshow(r),figure,imshow(r>120)
will produce the images shown below.
Jorma Kekalainen
669
Note
To see how this thresholding works, recall that in
Matlab, an operation on a single number, when applied
to a matrix, is interpreted as being applied
simultaneously to all elements of the matrix; this is
vectorization.
The command X>T will thus return 1 (for true) for all
those pixels for which the gray values are greater than
T, and 0 (for false) for all those pixels for which the gray
values are less than or equal to T.
We thus end up with a matrix of 0's and 1's, which can
be viewed as a binary image.
Jorma Kekalainen
670
Example
Consider the following 8*8 image
3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
Threshold it at
(a) level 100
(b) level 150
Jorma Kekalainen
671
Example
>> data=[3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
]
data =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176
117
174
124
170
138
143
156
137
Jorma Kekalainen
148
110
113
106
113
183
119
114
145
185
193
158
108
137
188
135
178
155
136
130
127
162
179
123
132
118
146
178
144
105
100
134
174
165
108
170
139
169
151
183
IM=uint8(data)
thIM1=IM>100
thIM2=IM>150
figure, imshow(IM)
title('The small image before thresholding')
figure, imshow(thIM1)
title('The small image after thresholding>100')
figure, imshow(thIM2)
title('The small image after thresholding>150')
672
Example
IM =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176
117
174
124
170
138
143
156
137
148
110
113
106
113
183
119
114
Jorma Kekalainen
145
185
193
158
108
137
188
135
178
155
136
130
127
162
179
123
132
118
146
178
144
105
100
134
174
165
108
170
139
169
151
183
thIM1 =
0 1
0 1
0 0
0 1
0 1
0 1
0 1
0 1
thIM2 =
0 0
0 1
0 0
0 1
0 1
0 1
0 0
0 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
0
1
0
1
0
0
1
0
0
0
0
0
0
1
0
0
0
1
1
1
0
0
1
0
1
1
0
0
0
1
1
0
0
0
0
1
0
0
0
0
1
1
0
1
0
1
1
1
673
Note
>> whos
Name
Size
8x8
8x8
8x8
8x8
IM
data
thIM1
thIM2
Jorma Kekalainen
Bytes Class
64
512
64
64
Digital Image Processing
Attributes
uint8
double
logical
logical
674
Example
The rice image shown previously has light grains on a dark
background; an image with dark objects over a light
background may be treated the same:
>> b=imread('bacteria.tif');
>> imshow(b),figure,imshow(b>100)
will produce the images shown below
Jorma Kekalainen
675
im2bw function
Besides the above method, Matlab has the im2bw
function, which thresholds an image of any data
type, using the general syntax
im2bw(image,level)
where level is a value between 0 and 1 (inclusive),
indicating the fraction of gray values to be turned
white.
This command will work on grayscale,
colored and indexed images of data
type
Jormauint8,
Kekalainen uint16 or double.
Digital Image Processing
676
im2bw function
For example, the thresholded rice and bacteria images above
could be obtained using
>> im2bw(r,0.47);
>> im2bw(b,0.39);
The im2bw function automatically scales the value level to a
gray value appropriate to the image type, and then performs a
thresholding by our first method.
Jorma Kekalainen
677
Exercise
Consider the following 8*8 image
3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
Using im2bw function threshold it at
(a) level 100
(b) level 150
Jorma Kekalainen
678
Solution
>> data=[3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
]
data =
3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
Jorma Kekalainen
IM=uint8(data)
thIM1=im2bw(IM,100/255)
thIM2= im2bw(IM,150/255)
figure, imshow(IM)
title('The small image before thresholding')
figure, imshow(thIM1)
title('The small image after thresholding>100')
figure, imshow(thIM2)
title('The small image after thresholding>150')
679
Solution
data =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176
117
174
124
170
138
143
156
137
148
110
113
106
113
183
119
114
Jorma Kekalainen
145
185
193
158
108
137
188
135
178
155
136
130
127
162
179
123
132
118
146
178
144
105
100
134
174
165
108
170
139
169
151
183
thIM1 =
0 1
0 1
0 0
0 1
0 1
0 1
0 1
0 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
thIM2 =
0 0 0 0 0 1
0 1 1 0 1 1
0 0 0 0 1 0
0 1 1 0 1 0
0 1 0 0 0 0
0 1 0 1 0 1
0 0 1 0 1 1
0 Digital
1 Image
0 0Processing
0 0
0
0
0
1
0
0
0
0
1
1
0
1
0
1
1
1
680
Example
Besides isolating objects from the background,
thresholding provides a very simple way of
showing hidden aspects of an image.
For example, the image paper.tif appears all
white, as nearly all the gray values are very
high.
However, thresholding at a high level
produces an image of far greater interest.
Jorma Kekalainen
681
Example
We can use the commands
>> p=imread('paper1.tif');
>> imshow(p),figure,imshow(p>241)
to provide the images shown below
Jorma Kekalainen
682
Double thresholding
Here we choose two values T1 and T2 and apply a thresholding
operation as
white if its gray level is between T1 and T2
A pixel becomes
black if its gray level is otherwise
We can implement this by a simple variation on the above
method:
X>T1 & X<T2
Since the ampersand acts as a logical and, the result will only
produce a one where both inequalities are satisfied.
Jorma Kekalainen
683
Example
Consider the following 8*8 image
3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
9 196 138 113 108 127 144 139
6 188 143 183 137 162 105 169
9 122 156 119 188 179 100 151
8 176 137 114 135 123 134 183
Double threshold it at
(a) level 100
(b) level 150
684
Example
>> data=[3 148 117 148 145 178 132 174
2 176 174 110 185 155 118 165
0 100 124 113 193 136 146 108
0 155 170 106 158 130 178 170
IM=uint8(data)
9 196 138 113 108 127 144 139
thIM=IM>100 & IM<150
6 188 143 183 137 162 105 169
figure, imshow(IM)
9 122 156 119 188 179 100 151
title('Image before thresholding')
8 176 137 114 135 123 134 183
figure, imshow(thIM)
]
data =
3 148 117
2 176 174
0 100 124
0 155 170
9 196 138
6 188 143
9 122 156
Jorma Kekalainen
8 176 137
148
110
113
106
113
183
119
114
145
185
193
158
108
137
188
135
178
155
136
130
127
162
179
123
132
118
146
178
144
105
100
134
174
165
108
170
139
169
151
183 Digital Image Processing
685
Example
IM =
3 148
2 176
0 100
0 155
9 196
6 188
9 122
8 176
117
174
124
170
138
143
156
137
148
110
113
106
113
183
119
114
Jorma Kekalainen
145
185
193
158
108
137
188
135
178
155
136
130
127
162
179
123
132
118
146
178
144
105
100
134
174
165
108
170
139
169
151
183
thIM =
0 1
0 0
0 0
0 0
0 0
0 0
0 1
0 0
1
0
1
0
1
1
0
1
1
1
1
1
1
0
1
1
1
0
0
0
1
1
0
1
0
0
1
1
1
0
0
1
1
1
1
0
1
1
0
1
0
0
1
0
1
0
0
0
686
Example
Consider the following sequence of commands, which start by
producing an 8-bit gray version of the indexed image spine.tif:
Double thresholding brings out subtle
[x,map]=imread('spine.tif');
features of the spine which single
s=uint8(ind2gray(x,map));
thresholding would be unable to do.
figure,imshow(s),figure,imshow(s>115 & s<125)
Jorma Kekalainen
687
Note
We can obtain similar results using im2bw:
imshow(im2bw(x,map,0.45)&~im2bw(x,map,0.5))
Jorma Kekalainen
688
Note: ~
~ means logical not.
~A performs a logical not of input array A, and
returns an array containing elements set to
either logical 1 (TRUE) or logical 0 (FALSE).
An element of the output array is set to 1 if A
contains a zero value element at that same
array location.
Otherwise, that element is set to 0.
Jorma Kekalainen
689
Applications of thresholding
Thresholding can be useful in the following situations:
(a) When we want to remove unnecessary detail from an
image and to concentrate on essentials.
(b) To bring out hidden detail.
(c) When we want to remove a varying background from text
or a drawing.
Examples of (a) were given in the rice and bacteria images: by
removing all gray level information, the rice and bacteria were
reduced to binary blobs.
But this information may be all we need to investigate sizes,
shapes, or numbers of blobs.
Jorma Kekalainen
690
Example
R=rand(256)*128+127;
T=imread('text.png');figure,imshow(T)
TT=uint8(R.*double(not(T)));
figure,imshow(TT)
691
Example
The third command row
TT=uint8(R.*double(not(T)));
does several things at once: not(T) reverses the text image so
as to have black text on a white background; double changes
the numeric type so that the matrix can be used with
arithmetic operations; finally the result is multiplied into the
random matrix, and the whole thing converted to uint8 for
display.
Jorma Kekalainen
692
Example
If we threshold this image and display the result with
>> imshow(TT>100)
the background has been completely removed.
Jorma Kekalainen
693
Example
R=rand(107,122)*128+127;
L=imread('logo.tif');
figure, imshow(L)
TL=uint8(R.*double(not(L)));
figure, imshow(TL)
figure, imshow(TL>100)
Jorma Kekalainen
694
Exercise
Superimpose the image text.png onto the
image cameraman.tif:
t=imread('text.png');
c=imread('cameraman.tif');
m=uint8(double(c).*double(~t));
Can you threshold this new image m to isolate
the text?
Jorma Kekalainen
695
Solution
Jorma Kekalainen
696
Introduction
We have seen previously that an image can be
modified by applying a particular function to
each pixel value.
Spatial filtering may be considered as an
extension of these point operations, where we
apply a function to a neighborhood of each
pixel.
Jorma Kekalainen
699
Jorma Kekalainen
700
Filter
The combination of mask and function is called a
filter.
If the function by which the new gray value is
calculated is a linear function of all the gray
values in the mask, then the filter is called a
linear filter.
We can implement a linear filter by multiplying
all elements in the mask by corresponding
elements in the neighborhood, and adding up all
these products.
Jorma Kekalainen
701
Spatial filter
Suppose that the 3*5 mask
values are given by:
Jorma Kekalainen
702
Spatial filtering
Spatial filtering requires three steps:
1. position the mask over the current pixel,
2. form all products of filter elements with the
corresponding elements of the neighborhood,
3. add up all the products.
This process must be repeated for every pixel
in the image.
Jorma Kekalainen
703
Jorma Kekalainen
704
Example
An example: One important linear filter is to use a (3*3) mask
and take the average of all nine values within the mask.
This value becomes the gray value of the corresponding pixel
in the new image.
We may describe this operation as follows:
Example
To apply this to an image, consider the 5*5 image obtained by:
x=uint8(10*magic(5))
x=
170
240
10
80
150
230
50
70
140
160
40
60
130
200
220
100
120
190
210
30
110
180
250
20
90
We may regard this array as being made of nine overlapping 3*3
neighborhoods.
The output of our working will thus consist only of nine values.
Jorma Kekalainen
Digital Image Processing
Note:
We shall see later how to obtain
25 values in the output.
706
Example
Consider the top left 3*3 neighborhood of our image x:
170 240 10
80
150
230 50
70
140 160
40
60
130 200 220
100 120 190 210 30
110 180 250 20
90
Now we take the average of all these values:
mean2(x(1:3,1:3))
ans =
111.1111
which we can round to 111.
Jorma Kekalainen
Digital Image Processing
707
Note:
mean2(A) computes the
average or mean of matrix elements.
Example
Now we can move to the second neighborhood:
170 240 10
80
150
230 50
70
140 160
40
60
130 200 220
100 120 190 210 30
110 180 250 20
90
and take its average:
mean2(x(1:3,2:4))
ans =
108.8889
We can round this either down to 108, or to the nearest
integer 109.
Jorma Kekalainen
Digital Image Processing
708
Example
If we continue in this manner, we will build up
the following output:
111.1111
108.8889 128.8889
110.0000
130.0000 150.0000
131.1111
151.1111 148.8889
This array is the result of filtering x with the
3*3 averaging filter.
Jorma Kekalainen
709
Matrix notation
It is convenient to describe a linear filter simply in terms of
the coefficients of all the gray values of pixels within the
mask.
This can be written as a matrix.
The averaging filter above, for example, could have its output
written as
Jorma Kekalainen
710
Example
The filter
Jorma Kekalainen
711
713
Filtering in Matlab
The filter2 function does the job of linear filtering for
us.
Its use is
filter2(filter,image,shape)
and the result is a matrix of data type double.
The parameter shape is optional, it describes the
method for dealing with the edges:
filter2(filter,image,'same') is the default; it produces a
matrix of equal size to the original image matrix.
It uses zero padding.
Jorma Kekalainen
715
Example: filter2(a,x,'same')
a=ones(3,3)/9
a=
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
>> filter2(a,x,'same')
ans =
76.6667 85.5556 65.5556
87.7778 111.1111 108.8889
66.6667 110.0000 130.0000
67.7778 131.1111 151.1111
56.6667 105.5556 107.7778
Jorma Kekalainen
67.7778
128.8889
150.0000
148.8889
87.7778
58.8889
105.5556
106.6667
85.5556
38.8889
716
Example: filter2(a,x,'same')
x=uint8(10*magic(5))
x=
170
230
40
100
110
a=ones(3,3)/9
240 10 80 150
50 70 140 160
60 130 200 220
120 190 210 30
180 250 20 90
a=
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
filter2(a,x,'same')
ans =
76.6667
87.7778
66.6667
67.7778
56.6667
85.5556
111.1111
110.0000
131.1111
105.5556
Jorma Kekalainen
uint8(filter2(a,x,'same'))
65.5556 67.7778 58.8889
108.8889 128.8889 105.5556
130.0000 150.0000 106.6667
151.1111 148.8889 85.5556
107.7778 87.7778 38.8889
Digital Image Processing
ans =
77 86
88 111
67 110
68 131
57 106
66 68 59
109 129 106
130 150 107
151 149 86
108 88 39
717
filter2(filter,image,'valid')
filter2(filter,image,'valid') applies the mask
only to inside pixels.
We can see that the result will always be
smaller than the original:
filter2(a,x,'valid')
uint8( filter2(a,x,'valid'))
ans =
111.1111 108.8889 128.8889
110.0000 130.0000 150.0000
131.1111 151.1111 148.8889
ans =
111 109 129
110 130 150
131 151 149
Jorma Kekalainen
718
Note
We can obtain the previous result of 'same' by padding with
zeros and using 'valid':
x2=zeros(7,7); x2(2:6,2:6)=x
x2 =
filter2(a,x2,'valid')
Jorma Kekalainen
719
filter2(filter,image,'full')
filter2(filter,image,'full') returns a
result larger than the original; it
does this by padding with zero, and
applying the filter at all places on
and around the image where the
mask intersects the image matrix.
filter2(a,x,'full')
ans =
18.8889
44.4444
48.8889
41.1111
27.7778
23.3333
12.2222
45.5556
76.6667
87.7778
66.6667
67.7778
56.6667
32.2222
46.6667
85.5556
111.1111
110.0000
131.1111
105.5556
60.0000
uint8(filter2(a,x,'full'))
ans =
19 46
44 77
49 88
41 67
28 68
23 57
12 32
47
86
111
110
131
106
60
37 27 26 17
66 68 59 34
109 129 106 59
130 150 107 46
151 149 86 38
108 88 39 13
50 40 12 10
filter2(a,x,'same')
filter2(a,x,'valid')
Note: The shape parameter, being optional, can be omitted; in which case the default
Kekalainen
Digital Image Processing
720
valueJorma
is 'same'.
Compare
filter2(a,x,'same')
ans =
76.6667
87.7778
66.6667
67.7778
56.6667
85.5556
111.1111
110.0000
131.1111
105.5556
filter2(a,x,'valid')
ans =
111.1111 108.8889 128.8889
110.0000 130.0000 150.0000
131.1111 151.1111 148.8889
conv2(double(x),a,'same')
ans =
76.6667 85.5556
87.7778 111.1111
66.6667 110.0000
67.7778 131.1111
Jorma Kekalainen
56.6667 105.5556
conv2(double(x),a,'valid')
ans =
111.1111 108.8889 128.8889
110.0000 130.0000 150.0000
721
131.1111 151.1111 148.8889
Compare
conv2(double(x),a,'full')
ans =
18.8889
44.4444
48.8889
41.1111
27.7778
23.3333
12.2222
45.5556
76.6667
87.7778
66.6667
67.7778
56.6667
32.2222
46.6667
85.5556
111.1111
110.0000
131.1111
105.5556
60.0000
or conv2(double(x),a)
36.6667 26.6667 25.5556 16.6667
65.5556 67.7778 58.8889 34.4444
108.8889 128.8889 105.5556 58.8889
130.0000 150.0000 106.6667 45.5556
151.1111 148.8889 85.5556 37.7778
107.7778 87.7778 38.8889 13.3333
50.0000 40.0000 12.2222 10.0000
filter2(a,x,'full')
ans =
18.8889 45.5556
44.4444 76.6667
48.8889 87.7778
41.1111 66.6667
27.7778 67.7778
23.3333 56.6667
Jorma Kekalainen
12.2222 32.2222
46.6667
85.5556
111.1111
110.0000
131.1111
105.5556
60.0000
filter2(a,x,'same')
filter2(a,x,'valid')
722
Note
C = conv2(A,B) computes the two-dimensional convolution of
matrices A and B.
If one of these matrices describes a two-dimensional finite impulse
response (FIR) filter, the other matrix is filtered in two dimensions.
The size of C is determined as follows: if [ma,na] = size(A), [mb,nb]
= size(B), and [mc,nc] = size(C), then mc = max([ma+mb-1,ma,mb])
and nc = max([na+nb-1,na,nb]).
C = conv2(...,shape) returns a subsection of the two-dimensional
convolution, as specified by the shape parameter:
'full' Returns the full two-dimensional convolution (default).
'same' Returns the central part of the convolution of the same size
as A.
'valid' Returns only those parts of the convolution that are
computed without the zero-padded edges. Using this option, size(C)
= max([ma-max(0,mb-1),na-max (0,nb-1)],0).
Jorma Kekalainen
Image Processing
Note:
All numeric inputs to conv2Digital
must
be of type double or single.
723
Two-dimensional convolution
If a and b are functions of two discrete variables, n1 and n2,
then the formula for the two-dimensional convolution of a
and b is
724
fspecial function
We can create our filters by hand, or by using the fspecial
function; this has many options which makes for easy creation
of many different filters.
We shall use the average option, which produces averaging
filters of given size; thus fspecial('average',[5,7]) will return
an averaging filter of size 5*7 or more simply
fspecial('average',11) will return an averaging filter of size
11*11.
If we leave out the final number or vector, the 3*3 averaging
filter is returned.
Jorma Kekalainen
725
Example
For example, suppose we apply the 3*3 averaging filter to an
image as follows:
c=imread('cameraman.tif');
f1=fspecial('average');
cf1=filter2(f1,c);
We now have a matrix of data type double.
To display this, we can do any of the following:
transform it to a matrix of type uint8, for use with imshow,
divide its values by 255 to obtain a matrix with values in
the 0.1 1.0 range, for use with imshow,
use mat2gray to scale the result for display (later more on
this function).
Jorma Kekalainen
726
Example
Using the second method i.e. functions
figure,imshow(c),figure,imshow(cf1/255)
will produce the images shown below.
c=imread('cameraman.tif');
f1=fspecial('average');
or
cf1=filter2(f1,c);
figure,imshow(c),figure,imshow(cf1/255)
imshow(uint8(cf1))
Original image
Jorma Kekalainen
727
Example
f1=fspecial('average',[5,5]);
cf1=filter2(f1,c);
figure,imshow(cf1/255)
f1=fspecial('average',[7,7]);
cf1=filter2(f1,c);
figure,imshow(cf1/255)
Jorma Kekalainen
728
Example
f1=fspecial('average',[9,9]);
cf1=filter2(f1,c);
figure,imshow(cf1/255)
f1=fspecial('average',[13,13]);
cf1=filter2(f1,c);
figure,imshow(cf1/255)
Jorma Kekalainen
729
Example
f1=fspecial('average',[21,21]);
cf1=filter2(f1,c);
figure,imshow(cf1/255)
f1=fspecial('average',[29,29]);
cf1=filter2(f1,c);
figure,imshow(cf1/255)
The averaging filter blurs the image; the
edges in particular are less distinct than in
the original.
The image can be further blurred by using
an averaging filter of larger size.
Jorma Kekalainen
730
Jorma Kekalainen
731
Frequency concept
Fundamentally, the frequencies of an image are the
amount by which gray values change with distance.
High frequency components are characterized by large
changes in gray values over small distances;
Example of high frequency components are edges and
noise.
Low frequency components, on the other hand, are
parts of the image characterized by little change in the
gray values.
These may include backgrounds and skin textures.
Jorma Kekalainen
732
The filter
Jorma Kekalainen
733
734
735
0.0448
0.0468
0.0564
0.0468
0.0448
0.0468
0.3167
0.7146
0.3167
0.0468
0.0564
0.7146
-4.9048
0.7146
0.0564
0.0468
0.3167
0.7146
0.3167
0.0468
0.0448
0.0468
0.0564
0.0468
0.0448
cf1=filter2(f1,c);
figure,imshow(cf1/100)
Jorma Kekalainen
736
Example
Lets consider block of similar values pixels,
Jorma
Kekalainen
Digital Image
Processing
737
Note:
How
to deal with negative outside
of the
displayable range we shall see next.
Exercise
Verify the calculation in the previous example:
apply the high pass filter
Jorma Kekalainen
738
Solution
filter2(filter,image,'valid') applies the mask
only to inside pixels.
>> A=[150 152 148 149;147 152 151 150;152 148 149 151;151 149 150 148]
A=
150 152 148 149
147 152 151 150
152 148 149 151
151 149 150 148
>> f2=[1 -2 1;-2 4 -2;1 -2 1]
f2 =
1 -2 1
-2 4 -2
1 -2 1
>> cf2=filter2(f2,A, 'valid')
cf2 =
11 6
Jorma Kekalainen
Digital Image Processing
-13 -5
739
740
Jorma Kekalainen
741
Clip values
This will produce an image with all pixel values in the required
range, but is not suitable if there are many gray values outside
the 0 - 255 range; in particular, if the gray values are equally
spread over a larger range.
In such a case this operation will tend to destroy the results of
the filter.
Jorma Kekalainen
742
Scaling transformation
Suppose the minimum gray value produced by the filter is gL
and the maximum value is gH.
Now we can transform all values in the range gL gH to the
range 0 - 255 by the linear transformation
Since the gradient of the line is
255/(gH g L),the equation of the
line is
743
Example
Let's apply the high pass filter
to the cameraman image
c=imread('cameraman.tif');
f2=[1 -2 1;-2 4 -2;1 -2 1];
cf2=filter2(f2,c);
Now the maximum and minimum values of the matrix cf2 are
well over 255 (593) and well below 0 (-541).
So we use the mat2gray function and get the result
figure,imshow(mat2gray(cf2));
Jorma Kekalainen
744
Example
f2=[1 -2 1;-2 4 -2;1 -2 1]
cf2=filter2(f2,c);
f2 =
1 -2 1
-2 4 -2
1 -2 1
cf2=filter2(f2,c);
figure,imshow(mat2gray(cf2));
Jorma Kekalainen
745
Example
We can generally obtain a
better result by dividing
the result of the filtering
by a constant before
displaying it:
figure,imshow(cf2/60)
Note: High pass filters are often used for edge detection. This can be
Kekalainen
Image Processing
746
seenJorma
quite
clearly in the imageDigital
above.
Note
Applying the previous linear transformation by hand, we
can use:
maxcf2=max(cf2(:));
mincf2=min(cf2(:));
cf2g=(cf2-mincf2)/(maxcf2-mincf2);
figure,imshow(cf2g)
747
748
Example
The array below represents a small grayscale image. Compute
the images that result when the image is convolved with the
-1 -1 0
mask
-1 0 1
0 1 1
20 20 20 10 10 10 10 10 10
20 20 20 20 20 20 20 20 10
20 20 20 10 10 10 10 20 10
20 20 10 10 10 10 10 20 10
20 10 10 10 10 10 10 20 10
10 10 10 10 20 10 10 20 10
10 10 10 10 10 10 10 10 10
20 10 20 20 10 10 10 20 20
20 10 10 20 10 10 20 10 20
Jorma Kekalainen
749
Example
>> data=[20 20 20 10 10 10 10 10 10
>> f2=[-1 -1 0;-1 0 1;0 1 1]
20 20 20 20 20 20 20 20 10
f2 =
20 20 20 10 10 10 10 20 10
-1 -1 0
20 20 10 10 10 10 10 20 10
-1 0 1
20 10 10 10 10 10 10 20 10
0 1 1
10 10 10 10 20 10 10 20 10
10 10 10 10 10 10 10 10 10
20 10 20 20 10 10 10 20 20
dataf2v=filter2(f2,data,'valid')
20 10 10 20 10 10 20 10 20]
dataf2s=filter2(f2,data,'same')
data =
dataf2f=filter2(f2,data,'full')
20 20 20 10 10 10 10 10 10
20 20 20 20 20 20 20 20 10
20 20 20 10 10 10 10 20 10
20 20 10 10 10 10 10 20 10
20 10 10 10 10 10 10 20 10
10 10 10 10 20 10 10 20 10
10 10 10 10 10 10 10 10 10
20 10 20 20 10 10 10 20 20
Jorma Kekalainen
750
20 10 10 20 10 10 20 Digital
10 Image
20 Processing Note: filter2(filter,image,shape)
Example
data =
20
20
20
20
20
10
10
20
20
20
20
20
20
10
10
10
10
10
20
20
20
10
10
10
10
20
10
10
20
10
10
10
10
10
20
20
10
20
10
10
10
20
10
10
10
10
20
10
10
10
10
10
10
10
10
20
10
10
10
10
10
10
20
10
20
20
20
20
20
10
20
10
10
10
10
10
10
10
10
20
20
>> dataf2v=filter2(f2,data,'valid')
dataf2v =
0 -10 -10 0 0 10 0
-10 -30 -30 -20 -20 0 -10
-30 -30 -10 0 0 20 0
-30 -10 10 10 0 20 0
-10 0 10 0 -10 10 -10
10 20 10 -10 -10 10 10
0 20 0 -10 10 20 20
>> dataf2f=filter2(f2,data,'full')
dataf2f =
20 40 40 30 20 20 20 20 20 10 0
>> dataf2s=filter2(f2,data,'same')
40 60 40 30 30 40 40 40 30 0 -10
dataf2s =
40 40 0 -10 -10 0 0 10 0 -30 -20
60 40 30 30 40 40 40 30 0
40 40 -10 -30 -30 -20 -20 0 -10 -40 -20
40 0 -10 -10 0 0 10 0 -30
40 30 -30 -30 -10 0 0 20 0 -40 -20
40 -10 -30 -30 -20 -20 0 -10 -40
30 10 -30 -10 10 10 0 20 0 -40 -20
30 -30 -30 -10 0 0 20 0 -40
20 10 -10 0 10 0 -10 10 -10 -40 -20
10 -30 -10 10 10 0 20 0 -40
30 30 10 20 10 -10 -10 10 10 -20 -20
10 -10 0 10 0 -10 10 -10 -40
40 30 0 20 0 -10 10 20 20 -20 -30
30 10 20 10 -10 -10 10 10 -20
20 -10 -40 -20 -40 -40 -10 -20 -30 -50
-40
30
20 0 -10 10 20 20 -20 Digital Image Processing
Jorma0Kekalainen
751
0 -20 -30 -20 -30 -30 -20 -30 -30 -30 -20
-10 -40 -20 -40 -40 -10 -20 -30 -50
Example
>> whos
Name
Size
data
dataf2f
dataf2s
dataf2v
f2
9x9
11x11
9x9
7x7
3x3
Bytes Class
648
968
648
392
72
Attributes
double
double
double
double
double
figure,imshow(mat2gray(data))
title('imshow(mat2gray(data)')
figure,imshow(mat2gray(dataf2v))
title('imshow(mat2gray(dataf2v)')
figure,imshow(mat2gray(dataf2s))
title('imshow(mat2gray(dataf2s)')
figure,imshow(mat2gray(dataf2f))
title('imshow(mat2gray(dataf2f)')
Jorma Kekalainen
Example
Jorma Kekalainen
753
Note
Matlab also has an imfilter function, which has the syntax
imfilter(x,f);
where x is an image matrix (of any type), and f is a filter.
It differs from filter2 in the different parameters it takes (read its help file),
and in that the output is always of the same class as the original image.
B = imfilter(A,h) filters the multidimensional array A with the
multidimensional filter h.
The array A can be logical or a nonsparse numeric array of any class and
dimension.
The result B has the same size and class as A.
imfilter computes each element of the output, B, using doubleprecision
floating point.
If A is an integer or logical array, imfilter truncates output elements that
exceed the range of the given type, and rounds fractional values.
Jorma Kekalainen
754
Exercise
The array below represents a small grayscale image. Compute
the images that result when the image is convolved with each
of the masks (a) to (f) shown
(a)
0 -1 -1
1 0 -1
110
(b)
-1 -1 -1
222
-1 -1 -1
(c)
-1 2 -1
-1 2 -1
-1 2 -1
(d)
-1 -1 -1
-1 8 -1
Jorma Kekalainen
-1 -1 -1
(e)
111
111
111
(f)
-1 0 1
-1 0 1
-1 0 1
(g)
0 -1 0
-1 4 -1
0 -1 0
20 20 20 10 10 10 10 10 10
20 20 20 20 20 20 20 20 10
20 20 20 10 10 10 10 20 10
20 20 10 10 10 10 10 20 10
20 10 10 10 10 10 10 20 10
10 10 10 10 20 10 10 20 10
10 10 10 10 10 10 10 10 10
20 10 20 20 10 10 10 20 20
20 10 10 20 10 10 20 10 20
755
Exercise
Obtain a grayscale image of a mandrill with
the following commands:
>> load('mandrill.mat');
>> m=im2uint8(ind2gray(X,map));
Apply all the filters (a) - (g) listed in the
previous exercise to this image.
Can you now see what each filter does?
Jorma Kekalainen
756
Exercise
Apply larger and larger averaging filters to the
image of the mandrill.
What is the smallest sized filter for which the
whiskers cannot be seen?
Jorma Kekalainen
757
Gaussian filters
The fspecial function can produce many
different filters for use with the filter2
function.
Some of these are Gaussian filters.
Gaussian filters are an important class of lowpass filters, all based on the Gaussian
probability distribution function.
Jorma Kekalainen
759
Jorma Kekalainen
760
761
Jorma Kekalainen
=9
762
Example
Gaussian filters have a blurring effect which looks very similar
to that produced by neighborhood averaging.
Let's experiment with the cameraman image, and some
different gaussian filters.
g1=fspecial('gaussian',[5,5]);
g2=fspecial('gaussian',[5,5],2);
g3=fspecial('gaussian',[11,11],1);
g4=fspecial('gaussian',[11,11],5);
The final parameter is the standard deviation; if not given the
default is 0.5.
The second parameter (which is also optional), gives the size
of the filter; the default is 3*3.
Note: If the filter is square, as in all the above examples, we can just give a single
Jorma Kekalainen
Digital Image Processing
763
number
in each case.
Example
g1=fspecial('gaussian',[5,5]);
g2=fspecial('gaussian',[5,5],2);
Now we can apply the filter to
the cameraman image matrix c
and view the result.
figure,imshow(filter2(g1,c)/256)
figure,imshow(filter2(g2,c)/256)
Jorma Kekalainen
764
Example
g3=fspecial('gaussian',[11,11],1);
g4=fspecial('gaussian',[11,11],5);
Now we can apply the filter to
the cameraman image matrix c
and view the result.
figure,imshow(filter2(g3,c)/256)
figure,imshow(filter2(g4,c)/256)
Jorma Kekalainen
765
Jorma Kekalainen
=0.5
=1
=2
=5
766
Note
We see that to obtain a spread out blurring effect, we need a
large standard deviation.
In fact, if we let the standard deviation grow large without
bound, we obtain the averaging filters as limiting values as we
see below when =100.
fspecial('gaussian',3,100)
ans =
0.1111
0.1111
0.1111
0.1111
0.1111
0.1111
0.1111
0.1111
0.1111
and we have 3*3 averaging filter.
Jorma Kekalainen
767
Example
c=imread('cameraman.tif');
figure,imshow(c),title('Original image')
g5=fspecial('gaussian',3,100);
figure,imshow(filter2(g5,c)/256)
title('Filtered by (''gaussian'',3,100)
Jorma Kekalainen
768
Nonlinear filters
Linear filters can be applied very quickly and
efficiently by Matlab.
However, non-linear filters can also be used.
The function to use is nlfilter, which applies a
filter to an image according to a pre-defined
function.
If the function is not already defined, we have
to create an m-file which defines it.
Jorma Kekalainen
769
Jorma Kekalainen
Digital Image Processing
Note:
nlfilter can take a long time
to process large images.
770
Example
c=imread('cameraman.tif');
figure,imshow(c),title('Original image')
cmax=nlfilter(c,[3,3],'max(x(:))');
figure,imshow(cmax)
title('Filtered by maximum filter')
Jorma Kekalainen
771
Example
We can measure the time of the operation with Matlab's tic,
toc timer: tic starts a stop watch timer, and toc stops it and
prints out the elapsed time in seconds.
Thus, on this computer applying neighborhood operation
below took time over 40 seconds.
>> c=imread('cameraman.tif');
%figure,imshow(c),title('Original image')
tic
cmax=nlfilter(c,[3,3],'max(x(:))');
toc
%figure,imshow(cmax)
%title('Filtered by maximum filter')
Elapsed time is 42.275047 seconds.
Jorma Kekalainen
772
Example
c=imread('cameraman.tif');
figure,imshow(c),title('Original image')
cmin=nlfilter(c,[3,3],'min(x(:))');
figure,imshow(cmin)
title('Filtered by minimum filter')
Jorma Kekalainen
773
Comparison of results
Filtered by maximum
filter
Jorma Kekalainen
Filtered by minimum
filter
774
Exercise
Display the difference between the previous
cmax and cmin images.
You can do this with
>> imshow(imsubtract(cmax,cmin))
What are you seeing here?
Display the difference between the cmax and
c images and also the difference between the
c and cmin images
Jorma Kekalainen
775
Solution
Jorma Kekalainen
776
Solution
Jorma Kekalainen
777
Comment
Note that in each case the image has lost
some sharpness, and has been brightened by
the maximum filter, and darkened by the
minimum filter.
The nlfilter function is very slow; in general
there is little call for non-linear filters except
for a few which are defined by their own
commands.
We shall investigate these in later chapters.
Note: Both these filters can in fact be more efficiently implemented by using the
Jorma Kekalainen
Image Processing
778
Matlab
function ordfilt2 (see its helpDigital
page).
Median filter
Recall that the median of a set is the middle value when they
are sorted.
In Matlab, median filter is implemented by the medfilt2
function:
cmed=medfilt2(c);
performs median filtering of the matrix c using the default 3by-3 neighborhood.
As with most functions, medfilt2 takes an optional parameter;
in this case a 2 element vector giving the size of the mask to
be used
cmed=medfilt2(c,[m,n]);
Jorma Kekalainen
779
Example
c=imread('cameraman.tif');
figure,imshow(c),title('Original image')
cmed=medfilt2(c);
figure,imshow(cmed)
title('Filtered by median filter')
Jorma Kekalainen
780
Rank-order filters
Median filtering is a special case of a more general
process called rank-order filtering.
Rather than take the median of a set, we order the set
and take the n-th value, for some predetermined value
of n.
Matlab implements rank-order filtering with the
ordfilt2 function.
In fact the procedure for medfilt2 is really just a
wrapper for a procedure which calls ordfilt2.
There is only one reason for using rank-order filtering
instead of median filtering, and that is that it allows us
to choose the median of non-rectangular masks.
Jorma Kekalainen
781
Rank-order filters
For example, if we decided to use as a mask a (3*3)
cross shape:
782
Example
c=imread('cameraman.tif');
figure,imshow(c),title('Original image')
ccross=ordfilt2(c,3,[0 1 0;1 1 1;0 1 0]);
figure,imshow(ccross)
title('Filtered by cross-shaped median filter')
Jorma Kekalainen
783
Rank-order filters
In general, the second argument of ordfilt2
gives the value of the ordered set to take, and
the third element gives the domain; the nonzero values of which specify the mask, e.g.,
ccross=ordfilt2(c,3,[0 1 0;1 1 1;0 1 0]);
If we wish to use a cross with size and width 5
(so containing nine elements), we can use:
>> ordfilt2(c,5,[0 0 1 0 0;0 0 1 0 0;1 1 1 1 1;0 0 1 0 0;0 0 1 0 0]);
Jorma Kekalainen
784
Definition of noise
We may define noise to be any degradation in the image
signal, caused by external disturbance.
If an image is being sent electronically from one place to
another, via satellite or wireless transmission, or through
cable, we may expect errors to occur in the image signal.
These errors will appear on the image output in different ways
depending on the type of disturbance in the signal.
Usually we know what type of errors to expect, and hence the
type of noise on the image; hence we can choose the most
appropriate method for reducing the effects.
Cleaning an image corrupted by noise is thus an important
area of image restoration.
Jorma Kekalainen
787
Types of noise
Now we will investigate some of the standard
noise forms, and the different methods of
eliminating or reducing their effects on the
image.
Impulse noise (also called shot noise, binary noise
or salt and pepper noise)
Gaussian noise
Speckle noise
Periodic noise
Note: Other theoretical noise models are Poisson, Rayleigh, Gamma and Exponential
Jorma Kekalainen
Digital Image Processing
788
noises.
They can be used for approximating
skewed histograms.
Impulse noise
This degradation can be caused by sharp, sudden
disturbances in the image signal; its appearance is
randomly scattered white or black (or both) pixels
over the image.
To demonstrate its appearance, we will use the
familiar gray-scale image:
c=imread('cameraman.tif');
figure, imshow('cameraman.tif');
title('Clean image')
Jorma Kekalainen
789
Imnoise function
To add noise, we use the Matlab function imnoise, which
takes a number of different parameters.
To add salt and pepper noise:
>> c_sp=imnoise(c,'salt & pepper');
The amount of noise added is 10% (= default value) to add
more or less noise we include an optional parameter, being a
value between 0 and 1 indicating the fraction of pixels to be
corrupted.
E.g.,
>> imnoise(c,'salt & pepper',0.2);
would produce an image with 20% of its pixels corrupted by
salt and pepper noise.
Jorma Kekalainen
790
Impulse noise
c=imread('cameraman.tif');
c_sp=imnoise(c,'salt & pepper');
figure, imshow(c_sp)
title('Image with 10% of pixels corrupted by binary noise')
c_sp=imnoise(c,'salt & pepper',0.2);
figure, imshow(c_sp)
title('Image with 20% of pixels corrupted by binary noise')
Jorma Kekalainen
791
Gaussian noise
Gaussian noise is an idealized form of white
noise, which is caused by random fluctuations
in the signal
Electronic circuit noise and sensor noise
792
Gaussian noise
If the image is represented as I and the Gaussian
noise by N then we can model a noisy image by
simply adding the two:
I + N.
Here we may assume that I is a matrix whose
elements are the pixel values of our image, and N
is a matrix whose elements are normally
distributed.
It can be shown that this is an appropriate model
for noisy image.
Jorma Kekalainen
793
imnoise(c,'gaussian');
The effect can again be demonstrated by the
imnoise function:
>> c_ga=imnoise(c,'gaussian');
As with salt and pepper noise, the gaussian
parameter also can take optional values,
giving the mean (m) and variance (v) of the
noise.
The default values are m=0.0 and v=0.01.
Jorma Kekalainen
794
Example
c=imread('cameraman.tif');
c_ga=imnoise(c,'gaussian');
figure, imshow(c_ga)
title('Image with additive Gaussian noise with m=0 and
v=0.01')
Jorma Kekalainen
795
Speckle noise
Whereas Gaussian noise can be modeled by random values added to an
image; speckle noise (or speckle) can be modeled by random values
multiplied by pixel values, hence it is also called multiplicative noise.
Speckle noise is a major problem in some radar applications.
As before, imnoise can do speckle:
>> c_spk=imnoise(c,'speckle');
In Matlab, speckle noise is implemented as
I+N*I= I(1+N)
where I is the image matrix, and N consists of normally distributed
values with mean 0.
An optional parameter gives the variance of N ; its default value is 0.04.
Jorma Kekalainen
796
Example
c=imread('cameraman.tif');
c_spk=imnoise(c,'speckle');
figure, imshow(c_spk)
title('Image with speckle noise with m=0 and v=0.04')
Jorma Kekalainen
797
Speckle noise
Note: Although Gaussian noise and speckle noise appear superficially similar, they are
formed by two totally different methods, and, thus require different approaches for
Kekalainen
Digital Image Processing
798
theirJorma
removal.
Periodic noise
If the image signal is subject to a periodic, rather than a
random disturbance, we might obtain an image
corrupted by periodic noise.
Periodic noise may occur if the imaging equipment (the
acquisition or networking hardware) is subject to
electronic disturbance of a repeating nature, such
noise may be caused by an electric motor.
The effect of this kind noise is bars over the image.
The function imnoise does not have a periodic option,
but we can create our own periodic noise by adding a
periodic matrix (using a trigonometric function) to our
image.
Jorma Kekalainen
799
Periodic noise
Jorma Kekalainen
800
Periodic noise
We can create periodic noise by overlaying an image with a
trigonometric function:
c=imread('cameraman.tif');
s=size(c);
[x,y]=meshgrid(1:s(1),1:s(2));
pn=sin(x+y)+1;
c_pn=(im2double(c)+pn/2)/2;
figure,imshow(c_pn)
title('Image with periodic noise pn=sin(x+y)+1')
The first lines simply create a suitable sine matrix function, and
adjusts its output to be in the range 0 - 2.
The c_pn line first adjusts the cameraman image to be double class;
adds the scaled sine function to it, and divides by 2 to produce a
matrix of type double with all elements in the range 0.0 1.0.
This can be viewed directly with imshow.
Jorma Kekalainen
801
Example
c=imread('cameraman.tif');
s=size(c);
[x,y]=meshgrid(1:s(1),1:s(2));
pn=sin(x+y)+1;
c_pn=(im2double(c)+pn/2)/2;
figure,imshow(c_pn)
title('Image with periodic noise pn=sin(x+y)+1')
Jorma Kekalainen
802
Example
c=imread('cameraman.tif');
s=size(c);
[x,y]=meshgrid(1:s(1),1:s(2));
pn=sin(x/2+y/3)+1;
c_pn=(im2double(c)+pn/2)/2;
figure,imshow(c_pn)
title('Image with periodic noise pn=sin(x/2+y/3)+1')
803
Example
[x,y]=meshgrid(1:256,1:256);
s=1+sin(x+y/1.5);
cp=(double(c)/128+s)/4;
figure,imshow(cp)
where c is the cameraman image.
The second line simply creates a sine function, and adjusts its output to be
in the range 0 - 2.
The next line first adjusts the cameraman image to be in the same range;
adds the sine function to it, and divides by 4 to produce a matrix of type
double with all elements in the range 0.0 1.0.
This can be viewed directly with imshow
Jorma Kekalainen
804
Note
Salt and pepper noise, Gaussian noise and
speckle noise can all be cleaned by using
spatial filtering techniques.
Periodic noise, however, requires image
transforms for best results.
Jorma Kekalainen
805
Example
c=imread('cameraman.tif');
c_sp=imnoise(c,'salt & pepper');
figure, imshow(c_sp)
title('Image with 10% of pixels corrupted by binary noise')
a3=fspecial('average');
c_sp_a3=filter2(a3,c_sp);
figure, imshow(uint8(c_sp_a3))
title('Image with 10% of pixels corrupted by binary noise filtered
by 3*3 averaging filter')
a5=fspecial('average',[5,5]);
c_sp_a5=filter2(a5,c_sp);
figure, imshow(uint8(c_sp_a5))
title('Image with 10% of pixels corrupted by binary noise filtered
by 5*5 averaging filter')
Jorma Kekalainen
807
808
Jorma Kekalainen
809
Example
a7=fspecial('average',[7,7]);
c_sp_a7=filter2(a7,c_sp);
figure, imshow(uint8(c_sp_a7))
title('Image with 10% of pixels corrupted by binary noise
filtered by 7*7 averaging filter')
a11=fspecial('average',[11,11]);
c_sp_a11=filter2(a11,c_sp);
figure, imshow(uint8(c_sp_a11))
title('Image with 10% of pixels corrupted by binary noise
filtered by 11*11 averaging filter')
Jorma Kekalainen
810
Median filtering
Median filtering seems almost tailor-made for
removal of salt and pepper noise.
Recall that the median of a set is the middle
value when they are sorted.
If there are an even number of values, the
median is the mean of the middle two.
A median filter is an example of a non-linear
spatial filter.
Jorma Kekalainen
811
Example
Using a 3*3 mask, the output value is the median of
the values in the mask.
812
Median filtering
In Matlab, median filtering is
implemented by the medfilt2 function:
>> c_sp_m3=medfilt2(c_sp);
The result is a vast improvement on using
averaging filters.
Jorma Kekalainen
813
Example
% Cleaning binary (salt & pepper noise) noise with median filter
C=imread('cameraman.tif');
figure,imshow(C)
title('Clean image')
Csp=imnoise(C,'salt & pepper');%The amount of noise added to 10%
figure, imshow(Csp)
title('Image with 10% of its pixels corrupted by binary noise')
Cspm3=medfilt2(Csp);% class: uint8
figure, imshow(Cspm3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 10% of its pixels corrupted by binary noise')
Jorma Kekalainen
814
Jorma Kekalainen
815
Example
% Cleaning binary (salt & pepper noise) noise with median filter
% 20% of pixel corrupted by noise
Csp2=imnoise(C,'salt & pepper',0.2);% 20% of pixel corrupted by
noise
figure, imshow(Csp2)
title('Image with 20% of its pixels corrupted by binary noise')
Cspm3=medfilt2(Csp);% class: uint8
figure, imshow(Cspm3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
Jorma Kekalainen
816
Jorma Kekalainen
817
Example
Jorma Kekalainen
818
Image averaging
It may sometimes happen that instead of just one
image corrupted with Gaussian noise, we have
many different copies of it.
An example is satellite imaging; if a satellite
passes over the same spot many times, we will
obtain many different images of the same place.
Another example is in microscopy: we might take
many different images of the same object.
In such a case a very simple approach to cleaning
Gaussian noise is to simply take the average - the
mean - of all the images.
Jorma Kekalainen
820
Image averaging
To see why this works, suppose we have n copies of our
image, each with noise; then the ith noisy image will be:
M+Ni
where M is the matrix of original values, and Ni is a matrix of
normally distributed random values with mean 0.
We can find the mean Mmean = M
M av
1 n
1 n
1 n
= (M + N i ) = M + N i
n i =1
n i =1
n i =1
1 n
= M + Ni
n i =1
Image Processing
M av n
MDigital
mean = M
Jorma Kekalainen
821
Image averaging
Since Ni is normally distributed with mean 0, it
can be readily shown that the average of all
the Ni s will be close to zero; actually the
greater the number of Ni s the closer to zero
the average is .
Thus Mav Mmean = M and the approximation
is closer for larger number of images.
Jorma Kekalainen
822
Example
We can demonstrate this with the familiar cameraman image.
We first need to create different versions with Gaussian noise,
and then take the average of them.
We shall create 10 versions.
One way is to create an empty three-dimensional array of
depth 10, and fill each level with a noisy image:
>> s=size(c);
>> c_ga10=zeros(s(1),s(2),10);
>> for i=1:10 c_ga10(:,:,i)=imnoise(c,'gaussian'); end
Jorma Kekalainen
823
Example
Jorma Kekalainen
824
Comment
Note here that the gaussian option of imnoise
calls the random number generator randn,
which creates normally distributed random
numbers.
Each time randn is called, it creates a different
sequence of numbers.
So we may be sure that all levels in our threedimensional array do indeed contain different
images.
Jorma Kekalainen
825
Image averaging
Now we can take the average:
c_ga10_av=mean(c_ga10,3);
The optional parameter 3 here indicates that we
are taking the mean along the third dimension of
our array.
The averaged image is not quite clear, but we
obtained a vast improvement on the noisy
image.
An even better result is obtained by taking the
average of 100 images; this can be done by
replacing 10 with 100 in the commands above.
Jorma Kekalainen
826
100 images
Jorma Kekalainen
Image Processing
Note:
This method only worksDigital
if the
Gaussian noise has zero mean.827
Example
Jorma Kekalainen
828
Average filtering
If the Gaussian noise has mean 0, then we would
expect that an average filter would average the
noise to 0.
The larger the size of the filter mask, the closer to
zero.
Unfortunately, averaging tends to blur an image,
as we have seen previously.
However, if we are prepared to trade off blurring
for noise reduction, then we can reduce noise
significantly by this method.
Jorma Kekalainen
829
Example
Suppose we take the (3*3) and (5*5)
averaging filters, and apply them to the noisy
image c_ga.
>> a3=fspecial('average');
>> a5=fspecial('average',[5,5]);
>> cg3=filter2(a3,c_ga);
>> cg5=filter2(a5,c_ga);
Jorma Kekalainen
830
Jorma Kekalainen
(3*3) averaging
(5*5) averaging
831
(m
i, j
ri , j )
i, j
832
Wiener filters
If we can minimize this value, we may be sure
that our procedure has done as good a job as
possible.
Filters which operate on this principle of least
squares are called Wiener filters.
They come in many guises; we shall look at
the particular filter which is designed to
reduce Gaussian noise.
Jorma Kekalainen
833
Jorma Kekalainen
Image Processing
Note:
MMSE(minimum mean Digital
square
error) filtering
834
835
836
837
Example
Suppose we take the noisy image with additive Gaussian noise
with m=0,v=0.01 shown below, and attempt to clean this
image with Wiener filtering.
We will use the wiener2 function, which can take an optional
parameter indicating the size of the mask to be used.
The default size is (3*3).
We shall create four images:
>> c1=wiener2(c_ga);
>> c2=wiener2(c_ga,[5,5]);
>> c3=wiener2(c_ga,[7,7]);
>> c4=wiener2(c_ga,[9,9]);
Jorma Kekalainen
838
(5*5) filtering
(9*9) filtering
(7*7) filtering
Jorma Kekalainen
839
Example
%Wiener filtering
C=imread('cameraman.tif');
figure,imshow(C)
title('Clean image')
Cga=imnoise(C,'gaussian'); %Default
%mean=0.0 and variance=0.01
figure, imshow(Cga)
title('Image with additive Gaussian noise
with m=0,v=0.01')
%
Cgaw3=wiener2(Cga);
Cgaw5=wiener2(Cga,[5,5]);
Cgaw7=wiener2(Cga,[7,7]);
Cgaw9=wiener2(Cga,[9,9]);
Jorma Kekalainen
figure, imshow(Cgaw3)
title('Image filtered by 3*3 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
figure, imshow(Cgaw5)
title('Image filtered by 5*5 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
figure, imshow(Cgaw7)
title('Image filtered by 7*7 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
figure, imshow(Cgaw9)
title('Image filtered by 9*9 Wiener filter')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')
840
Comment
Being a low pass filter, Wiener filtering tends to blur edges
and high frequency components of the image.
But it does a far better job than using a low pass blurring filter.
We can achieve very good results for noise where the
variance is not as high as that in our current image (variance
0.01).
Jorma Kekalainen
841
Example
% Using smaller gaussian noise
(m=0,v=0.005)
Cga2=imnoise(C,'gaussian',0,0.005);
figure, imshow(Cga2)
title('Image with additive Gaussian
noise with m=0,v=0.005')
Cga2w=wiener2(Cga2,[7,7]);
figure,imshow(Cga2w)
title('Image filtered by 7*7 Wiener
filter')
xlabel('Image with additive Gaussian
noise with m=0,v=0.005')
Jorma Kekalainen
842
Jorma Kekalainen
843
Size
C
256x256
Cga
256x256
Cga10
256x256x10
Cga100
256x256x100
Cga100av
256x256
Cga10av
256x256
Cga2
256x256
Cga2w
256x256
Cga3
256x256
Cga5
256x256
Cgaw3
256x256
Cgaw5
256x256
Cgaw7
256x256
Cgaw9
256x256
Csp
256x256
Csp2
256x256
Jorma Kekalainen
Csp2m5
256x256
65536 uint8
Bytes Class Attributes Cspm3
256x256
65536 uint8
Cspm3twice 256x256
65536 uint8
65536 uint8
a11
11x11
968 double
65536 uint8
a3
3x3
72 double
5242880 double
a5
5x5
200 double
52428800 double
a7
7x7
392 double
524288 double
ans
1x2
16 double
524288 double
c
256x256
65536 uint8
65536 uint8
c_pn
256x256
524288 double
65536 uint8
c_sp
256x256
65536 uint8
524288 double
c_sp_a11
256x256
524288 double
524288 double
c_sp_a3
256x256
524288 double
65536 uint8
c_sp_a5
256x256
524288 double
65536 uint8
c_sp_a7
256x256
524288 double
65536 uint8
cp
256x256
524288 double
65536 uint8
i
1x1
8 double
65536 uint8
pn
256x256
524288 double
65536 uint8
s
1x2
16 double
x
256x256
524288 double
y
256x256
524288 double
844
Exercise
The arrays below represent small grayscale
images. Compute the 4*4 image that would
result in each case if the middle 16 pixels were
transformed using a 3*3 median filter:
845
Exercise
Produce a gray subimage of the color image flowers.tif by
>> f=imread('flowers.tif');
>> fg=rgb2gray(f);
>> f=im2uint8(f(30:285,60:315));
(a) Add 10% salt & pepper noise to the image. Attempt to
remove the noise with (3*3) average filtering and (3*3)
median filtering.
Which method gives the best results?
(b) Repeat the above question but with 20% noise and (3*3)
median filtering only.
(c) Compare the previous result with a double (3*3) median
filtering and (5*5) median filter.
Jorma Kekalainen
846
Solution
>> f=imread('flowers.tif');
fg=rgb2gray(f);
f=im2uint8(f(30:285,60:315));
figure,imshow(f)
title('f=im2uint8(f(30:285,60:315))')
Jorma Kekalainen
847
Solution (a)
>> a3=fspecial('average');% class double
fspa3=filter2(a3,fsp);% class: double
figure, imshow(uint8(fspa3);% or (fspa3/255)
title('Image filtered by 3*3 averaging filter')
xlabel('Image with 10% of its pixels corrupted
by binary noise')
Jorma Kekalainen
848
(b)
Jorma Kekalainen
849
Jorma Kekalainen
fsp20m5=medfilt2(fsp20,[5,5]);
figure, imshow(fsp20m5)
title('Image filtered by 5*5 median filter')
xlabel('Image with 20% of its pixels corrupted
by binary noise')
850
Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all components back into a
single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
Jorma Kekalainen
Digital Image Processing
851
xlabel('Image with 20% of its pixels
corrupted by binary noise')
Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
a3=fspecial('average');
fsp20a3r=filter2(a3,fsp20(:,:,1));%Red component
fsp20a3g=filter2(a3,fsp20(:,:,2));
fsp20a3b=filter2(a3,fsp20(:,:,3));
fsp20a3=cat(3,fsp20a3r, fsp20a3g, fsp20a3b);% all components back into a
single 3D array for use with imshow
Jorma Kekalainen
852
Note
figure,imshow(uint8(fsp20a3))
title('Image filtered by 3*3 average filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
%
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all
components back into a single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
Jorma Kekalainen
853
Note
Jorma Kekalainen
854
Note
Jorma Kekalainen
855
Exercise
Add Gaussian noise to the grayscale flowers image
with the following parameters:
(a) mean 0, variance 0.01 (default)
(b) mean 0, variance 0.02
(c) mean 0, variance 0.05
(d) mean 0, variance 0.1
In each case, attempt to remove the noise with
average filtering and with Wiener filtering.
Can you produce satisfactory results with the last
two noisy images?
Jorma Kekalainen
856
Introduction
Edges contain the most useful information in an image.
In an image, an edge is a curve that follows a path of rapid
change in image intensity.
Edges are often associated with the boundaries of objects in a
scene.
We may use edges
to measure the size of objects in an image;
to isolate particular objects from their background;
to recognize or classify objects.
Jorma Kekalainen
859
Edge-finding algorithms
There are a large number of edge-finding
algorithms, and we shall look at some of the most
straightforward of them.
Here, we shall show how to create edge images
using basic filtering methods, and discuss the
Matlab edge function.
The general Matlab command for finding edges is
edge(image,'method',parameters. . . )
where the available parameters depend on the
method used.
Jorma Kekalainen
860
Definition of edge
An edge may be loosely defined as a line of pixels showing an
observable difference.
For example, consider the two blocks of pixels
861
Edges
Suppose we follow the gray values of pixels in a line
crossing an edge, as illustrated below.
862
A ramp edge
A line
Jorma Kekalainen
A roof
863
Differences
If we now plot the differences between each gray
value and its predecessor from the ramp edge, we
would obtain a graph
Differences of the edge function
864
Jorma Kekalainen
865
Different differences
We can define the difference in three separate
ways:
the forward difference:
the backward difference:
the central difference:
Jorma Kekalainen
866
Generalized differences
An image is a function of two variables, so we
must generalize those definitions to include
both the x and y values
Jorma Kekalainen
867
868
Jorma Kekalainen
869
Prewitt filter Px
Both of the previous filters can be applied at once
convolving them.
As a result of convolution we get a combined filter:
>> Px=conv2(A,B,'same')
Px =
-1 0 1
-1 0 1
870
-1 0 1
Jorma Kekalainen
871
0 -1 0
0 0 0
0 1 0
Digital Image Processing
872
0
0
0
0
1
0
0
1
0
873
Prewitt filter Py
Both of the previous filters can be applied at
once convolving them.
As a result of convolution we get a combined
filter:
A =
0 -1
0 0
0 1
B' =
0
1
0
0
0
0
0 0
1 1
Jorma Kekalainen
0 0
>> Py=conv2(A',B','same')
Py =
-1 -1 -1
0 0 0
874
1 1 1
Note
>> A=[0 0 0;-1 0 1;0 0 0]
A=
0 0 0
-1 0 1
0 0 0
>> B=[0 1 0;0 1 0;0 1 0]
B=
0 1 0
0 1 0
0 0
0 1 0
1 1
>> Px=conv2(A,B,'same')
0 0
Px =
-1 0 1
0 0
-1 0 1
1 1
-1 0 1
0 0
>> Py=conv2(A',B','same')
Py =
-1 -1 -1
0 0 0
Digital Image Processing 1
1 1
0
0
0
0
0
0
Jorma Kekalainen
>> B'
ans =
0
1
0
0
1
0
875
Prewitt filters
Filter for finding vertical edges
876
Note
If we change increasing directions in the function array just
the opposite we correspondingly get matrices flilr(Px) and
flipud(Py)
1
1
1
Py =
-1 -1 -1
0 0 0
1 1 1
>> fliplr(Px)
ans =
1 0 -1
1 0 -1
1 0 -1
>>flipud(Py)
ans =
1 1 1
0 0 0
-1 -1 -1
Px =
-1
-1
-1
0
0
0
Flipud flip matrix up to down and Fliplr flip matrix left to right.
Jorma Kekalainen
877
Note
h = fspecial('prewitt') returns the 3-by-3 filter
h=[ 1 1 1; 0 0 0; -1 -1 -1 ]
that emphasizes horizontal edges by approximating a
>> fspecial('prewitt')
vertical gradient.
ans =
1 1 1
0 0 0
-1 -1 -1
878
Jorma Kekalainen
879
Example
Let us take the image of
the integrated circuit ,
which can be read into
Matlab with
>> ic=imread('ic.tif');
Applying each of Px and
Py individually provides
the results shown in the
following slice
Jorma Kekalainen
An original image
880
Example
After filtering with the Prewitt filters
Figure (a)
Jorma Kekalainen
Figure (b)
881
Example
Figure (a) was produced with the following Matlab
commands:
px=[-1 0 1;-1 0 1;-1 0 1];
icx=filter2(px,ic);
figure,imshow(icx/255)
and figure (b) with
py=px';
icy=filter2(py,ic);
figure,imshow(icy/255)
Note that the filter Px highlights vertical edges, and Py
horizontal edges.
Jorma Kekalainen
882
Example
%Edge detection
ic=imread('ic.tif');
figure,imshow(ic)
title('Original image ic.tif')
%
px=[-1 0 1;-1 0 1;-1 0 1];
icx=filter2(px,ic);
figure,imshow(icx/255)
title('Vertical edges in ic.tif')
%
py=px';
icy=filter2(py,ic);
figure,imshow(icy/255)
title('Horizontal edges in ic.tif')
%
% All the edges with:
edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255)
title('All edges in the gray-scale image ic.tif')
Jorma Kekalainen
883
Example
>> %
level=graythresh(ic)
BW=im2bw(ic,level);
figure,imshow(BW)
title('Binary image thresholded from ic.tif using graythresh leveling')
xlabel(['Threshold level= ',num2str(level)])
level =
0.6784
Jorma Kekalainen
884
Example
>> edge_th=im2bw(edge_p/255,graythresh(edge_p));
figure,imshow(edge_th)
title('Binary image thresholded after Prewitt process using graythresh
leveling')
xlabel(['Threshold level=',num2str(graythresh(edge_p))])
%
Jorma Kekalainen
885
Example
We can create a figure containing all the edges with:
edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255)
This is a gray-scale image; a binary image containing
edges only can be produced by thresholding.
edge_t=im2bw(edge_p/255,0.3);
>> whos('edge_p','edge_t')
Name
Size
Bytes Class Attributes
edge_p 256x256
524288 double
Jorma Kekalainen
edge_t 256x256
65536 logicalDigital Image Processing
886
Example
Jorma Kekalainen
887
Note
We can obtain edges by the Prewitt filters directly by
using the command
>> edge_p=edge(ic,'prewitt');
and the edge function takes care of all the filtering, and
of choosing a suitable threshold level.
The result is shown in the following figure.
Note that the previous figure and the next figure seem
different to each other.
This is because the edge function does some extra
processing besides taking the square root of the sum of
the squares of the filters.
Jorma Kekalainen
Image Processing
Note:
See the help text of theDigital
edge
function for more information.888
Example
%We can obtain edges by the Prewitt filters directly by using the command
edge_p=edge(ic,'prewitt');
figure, imshow(edge_p)
title('Edges of ic.tif directly by using the Prewitt filters')
xlabel('Using command edge(ic,''prewitt'');')
Jorma Kekalainen
889
Sobel filters
The Sobel filters are similar to the Prewitt filters, in that they
apply a smoothing filter in the opposite direction to the
central difference filter.
Now the smoothing takes the form which gives slightly more
prominence to the central pixel
0
1
0
Jorma Kekalainen
0
2
0
0
1
0
0
0
0
1
2
1
0
0
0
890
Sobel filter Sx
As we previously notice in Prewitts case this filter
1
2
1
0
0
0
891
Sobel filter Sx
Both of the previous filters can be applied at once
convolving them.
As a result of convolution we get a combined filter:
Sx =
>> Sx=conv2(A,B','same')
Sx =
-1 0 1
-2 0 2
892
-1 0 1
Sobel filter Sy
As we previously notice in Prewitts case this filter
0 -1
0 0
0 1
0
0
0
0
2
0
0
1
0
Sobel filter Sy
Both of the previous filters can be applied at once
convolving them.
As a result of convolution we get a combined filter:
Sy =
Sy will find horizontal edges in an image.
>> Sx=conv2(A,B','same')
Sx =
-1 0 1
-2 0 2
Jorma Kekalainen
-1 0 1
Sy=Sx'=(conv2(A,B','same'))'
Sy =
-1 -2 -1
0 0 0
894
1 2 1
Sobel filters
Filter for finding vertical edges
Sx =
and its companion for finding horizontal edges
Sy =
are the Sobel filters for edge detection.
Jorma Kekalainen
895
Note
If we change increasing directions in the function array just
the opposite we correspondingly get matrices flilr(Sx) and
flipud(Sy)
Sx =
-1
-2
-1
1
2
1
Sy =
-1 -2 -1
0 0 0
1 2 1
>> fliplr(Sx)
ans =
1 0 -1
2 0 -2
1 0 -1
>> flipud(Sy)
ans =
1 2 1
0 0 0
-1 -2 -1
0
0
0
Flipud flip matrix up to down and Fliplr flip matrix left to right.
Jorma Kekalainen
896
fspecial('sobel')
h = fspecial('sobel') returns a 3-by-3 filter
h =[ 1 2 1; 0 0 0; -1 -2 -1 ]
that emphasizes horizontal edges using the
smoothing effect by approximating a vertical
gradient.
If you need to emphasize vertical edges,
transpose the filter h'.
Jorma Kekalainen
h=
1 2 1
0 0 0
-1 -2 -1
h=h' =
1 0 -1
2 0 -2
897
1 0 -1
Example
edge_r=edge(ic,'roberts');
figure,imshow(edge_r)
edge_s=edge(ic,'sobel');
figure,imshow(edge_s)
Of the three filters, the Sobel filters are probably the best; they provide good edges,
and they
perform reasonably well in Digital
the presence
of noise.
Jorma Kekalainen
Image Processing
898
Example
Jorma Kekalainen
899
Example
Jorma Kekalainen
900
Difference of differences
Another class of edge-detection method is
obtained by considering the difference of
differences.
These are called second differences.
E.g.,
2
x = x x f ( x, y )
y2 = y y f (x, y )
Jorma Kekalainen
902
Note
We can define the difference in three separate
ways:
the forward difference:
the backward difference:
the central difference:
Jorma Kekalainen
903
Note
An image is a function of two variables, so we
must generalize those definitions to include
both the x and y values
Jorma Kekalainen
904
Example
x2 = x x f (x, y ) = x [ f (x + 1, y ) f (x, y )]
= [ f ( x + 1, y ) f ( x, y )] [ f ( x, y ) f ( x 1, y )]
= f ( x + 1, y ) 2 f ( x, y ) + f ( x 1, y )
Jorma Kekalainen
905
x2 = x x f ( x, y ) = x [ f ( x + 1, y ) f (x, y )]
= [ f ( x + 1, y ) f ( x, y )] [ f ( x, y ) f ( x 1, y )]
= f ( x + 1, y ) 2 f ( x, y ) + f ( x 1, y )
The corresponding filter for second differences x2 in the x
direction can be implemented by the filter:
Jorma Kekalainen
906
= [ f ( x, y + 1) f ( x, y )] [ f ( x, y ) f ( x, y 1)]
= f ( x, y + 1) 2 f ( x, y ) + f ( x, y 1)
The corresponding filter for second differences y2 in the y
direction can be implemented by the filter
Jorma Kekalainen
907
Laplacian filter
The corresponding filters for second differences x2 in the x
direction and for second differences y2 in the y direction are
Jorma Kekalainen
908
Example
Take the pixel values describing a ramp edge
Jorma Kekalainen
909
Laplacian
We see that the Laplacian (after taking an
absolute value, or squaring) gives double
edges.
It is also extremely sensitive to noise.
However, the Laplacian does have the
advantage of detecting edges in all directions
equally well.
Jorma Kekalainen
910
Example
Suppose we enter the Matlab commands:
l=fspecial('laplacian',0);% Basic laplacian mask
ic_l=filter2(l,ic);
figure,imshow(mat2gray(ic_l))
The result of which is shown in the following
figure.
Jorma Kekalainen
911
Example
%
ic=imread('ic.tif');
figure,imshow(ic)
title('Original image ic.tif')
%
L=fspecial('laplacian',0);
ic_L=filter2(L,ic);
figure,imshow((ic_L))
title('Filtering by discrete Laplacian')
xlabel('fspecial(''laplacian'',0)')
%
Jorma Kekalainen
912
Jorma Kekalainen
913
914
fspecial('laplacian', alpha)
fspecial('laplacian', alpha) returns a 3-by-3
filter approximating the shape of the twodimensional Laplacian operator.
The parameter alpha controls the shape of the
Laplacian and must be in the range 0.0 to 1.0.
The default value for alpha is 0.2.
Jorma Kekalainen
915
fspecial('laplacian', alpha)
>> fspecial('laplacian',1.0)
ans =
0.5000
0 0.5000
0 -2.0000
0
0.5000
0 0.5000
>> fspecial('laplacian',0.9)
ans =
>> fspecial('laplacian',0.8)
ans =
>> fspecial('laplacian',0.7)
ans =
>> fspecial('laplacian',0.6)
ans =
>> fspecial('laplacian',0.5)
ans =
>> fspecial('laplacian',0.4)
ans =
0.2857 0.4286 0.2857
0.4286 -2.8571 0.4286
0.2857 0.4286 0.2857
>> fspecial('laplacian',0.3)
ans =
0.2308 0.5385 0.2308
0.5385 -3.0769 0.5385
0.2308 0.5385 0.2308
>> fspecial('laplacian',0.2)
ans =
0.1667 0.6667 0.1667
0.6667 -3.3333 0.6667
0.1667 0.6667 0.1667
>> fspecial('laplacian',0.1)
ans =
0.0909 0.8182 0.0909
0.8182 -3.6364 0.8182
0.0909 0.8182 0.0909
>> fspecial('laplacian',0.0)
ans =
0 1 0
1 -4 1
0 1 0
default value
alpha=0.2
916
Zero crossings
A more appropriate use for the Laplacian is to find the
position of edges by locating zero crossings.
If we look at figure below, we see that the position of the
edge is given by the place where the value of the filter takes
on a zero value.
In general, these are places where the result of the filter
changes sign.
Jorma Kekalainen
917
Jorma Kekalainen
Filtered image
Zero crossings are inside red rectangles
918
Example
Zero crossings are inside red circles
Jorma Kekalainen
919
Example
To give an indication of the
way zero-crossings work,
look at the edge plots and
their second differences.
In each case the zerocrossing is circled.
The important point is to
note that across any edge
there can be only one zerocrossing.
Thus an image formed from
zero-crossings has the
potential to be very handy.
Jorma Kekalainen
920
50
50
50
50
50
50
50
50
50
50
50
50
200
200
200
200
50
50
50
50
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
50 50 50 50 50 50 50
-50 0 150 150 150 150 150 150 0 -50
50 50 50 50 50 50 50
200 200 200 200 200 50 50 -50 150 -300 -150 -150 -150 -150 -300 150 -50
200 200 200 200 200 50 50 -50 150 -150 0 0 0 0 -150 150 -50
200 200 200 200 200 50 50 -50 150 -150 0 0 0 0 -150 150 -50
200 200 200 200 200 50 50 -50 150 -300 -150 0 0 0 -150 150 -50
-50 0 150 300 -150 0 0 -150 150 -50
50 200 200 200 200 50 50
-50 0 0 150 -300 -150 -150 -300 150 -50
50 200 200 200 200 50 50
-50 0 0 0 150 150 150 150 0 -50
50 50 50 50 50 50 50
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
50 50 50 50 50 50 50
Jorma Kekalainen
921
922
Marr-Hildreth method
As we saw previously far too many gray level changes have
been interpreted as edges by that method.
To eliminate this problem, we may first smooth the image
with a Gaussian filter.
This leads to the following sequence of steps for edge
detection called Marr-Hildreth method:
1. smooth the image with a Gaussian filter,
2. convolve the result with a laplacian,
3. find the zero crossings.
Jorma Kekalainen
923
Marr-Hildreth method
This method was designed to provide an edge detection method to
be as close as possible to biological vision.
The first two steps (smooting and convolving) can be combined
into one, to produce a Laplacian of Gaussian (LoG) or 'log' filter.
These filters can be created with the fspecial function.
If no extra parameters are provided to the zerocross edge option,
then the filter is chosen to be the LoG filter found by
fspecial('log',13,2)
This means that the following command:
edge(ic,'log');
produces exactly the same result as the commands:
log=fspecial('log',13,2);
edge(ic,'zerocross',log);
Jorma Kekalainen
924
925
Marr-Hildreth method
In fact the LoG and
zerocross options
implement the same edge
finding method; the
difference being that the
zerocross option allows
us to specify our own
filter.
The result after applying
an LoG filter and finding
its zero crossings is given
in figure beside
Jorma Kekalainen
926
Marr-Hildreth method
log=fspecial('log',13,2);
edge(ic,'zerocross',log);
Jorma Kekalainen
edge(ic,'log');
927
Edge detection
In an image, an edge is a curve that follows a path of rapid
change in image intensity.
Edges are often associated with the boundaries of objects in a
scene.
Edge detection is used to identify the edges in an image.
To find edges, we can use the edge function.
This function looks for places in the image where the intensity
changes rapidly, using one of these two criteria:
Places where the first derivative of the intensity is larger in magnitude
than some threshold
Places where the second derivative of the intensity has a zero crossing
Jorma Kekalainen
928
Note
There are a a vast number of different edge detection algorithms
and methods.
As to which method is the best; like so many image processing
tasks, this is highly subjective.
The choice of an suitable edge detector will depend very much on
the nature of the image, the amount (and type) of noise, and the
use for which the edges are to be put.
The edge function provides a number of derivative estimators.
The most powerful edge-detection method that Matlabs edge
function provides is the Canny method.
The Canny method differs from the other edge-detection methods
in that it uses two different thresholds (to detect strong and weak
edges), and includes the weak edges in the output only if they are
connected to strong edges.
This method is therefore less likely than the others to be fooled by
noise, and more likely to detect true weak edges.
Jorma Kekalainen
929
Example
I = imread('coins.png');
figure,imshow(I)
>> BW1 = edge(I,'sobel');
BW2 = edge(I,'canny');
figure;
imshowpair(BW1,BW2,'montage')
title('Sobel Filter
Canny Filter');
Jorma Kekalainen
930
Exercise
Construct the next matrix image using matrix operations
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
200
200
200
200
50
50
50
50
50 50 50 50 50 50 50
50 50 50 50 50 50 50
200 200 200 200 200 50 50
200 200 200 200 200 50 50
200 200 200 200 200 50 50
200 200 200 200 200 50 50
50 200 200 200 200 50 50
50 200 200 200 200 50 50
50 50 50 50 50 50 50
50 50 50 50 50 50 50
931
Solution
>> B=50*ones(10)
B=
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50 50 50 50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
>> B(3:8,3:8)=200*ones(6)
B=
50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 200 200 200 200 200 200
50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50
>> B(7:8,3:4)=50*ones(2)
B=
50 50 50 50 50 50 50 50 50 50
50 50 50 50 50 50 50 50 50 50
50 50 200 200 200 200 200 200 50 50
50 50 200 200 200 200 200 200 50 50
50 50 200 200 200 200 200 200 50 50
50 50 200 200 200 200 200 200 50 50
50 50 50 50 200 200 200 200 50 50
50 50 50 50 200 200 200 200 50 50
50 50 50 50 50 50 50 50 50 50
Jorma Kekalainen
Digital Image Processing
50 50 50 50 50 50 50 50 50 50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
B=100*ones(10)
B(3:8,3:8)=200*ones(6)
B(7:8,3:4)=100*ones(2)
l=fspecial('laplacian',0);
B_L=filter2(l,B)
932
Solution
l=fspecial('laplacian',0);
B_L=filter2(l,B)
B_L =
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
-50 0 150 150 150 150 150 150 0 -50
-50 150 -300 -150 -150 -150 -150 -300 150 -50
-50 150 -150 0 0 0 0 -150 150 -50
-50 150 -150 0 0 0 0 -150 150 -50
-50 150 -300 -150 0 0 0 -150 150 -50
-50 0 150 300 -150 0 0 -150 150 -50
-50 0 0 150 -300 -150 -150 -300 150 -50
-50 0 0 0 150 150 150 150 0 -50
-100 -50 -50 -50 -50 -50 -50 -50 -50 -100
Jorma Kekalainen
933
Edge enhancement
Previously we have seen how to isolate edges
in an image.
A related operation is to make edges in an
image slightly sharper, which generally results
in an image more pleasing to the human eye.
The operation is called edge enhancement, or
unsharp masking.
This last term comes from the printing
industry.
Jorma Kekalainen
935
Idea of unsharp
The idea of unsharp masking is to subtract a scaled unsharp
version of the image from the original.
In practice, we can achieve this affect by subtracting a scaled
blurred image from the original.
The schema for unsharp masking
Jorma Kekalainen
936
Example
Suppose we have an image x of type uint8, then we can apply
unsharp masking by the following sequence of commands:
f=fspecial('average');
xf=filter2(f,x);
xu=double(x)-xf/1.5;% scale k=1/1.5<1
imshow(xu/70)
The last command scales the result so that imshow displays
an appropriate image; the value may need to be adjusted
according to the input image.
Jorma Kekalainen
937
Example
%Unsharp masking
%Suppose we have an image C of type uint8. Then we can apply
%unsharp masking by the following sequence of commands:
%
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image')
f=fspecial('average');
Cf=filter2(f,C);
Cu=double(C)-Cf/1.5;
figure, imshow(Cu/70)
title('Unsharp masking with 3*3 averaging filter')
Jorma Kekalainen
938
Example
Suppose that C is the image shown in figure(a), then the result
of unsharp masking is given in figure (b).
The result appears to be a better image than the original; the
edges are sharper and more clearly defined.
(a) Original image
Jorma Kekalainen
939
Unsharp masking
To see why this unsharp
masking works, we may
consider the function of
gray values as we travel
across an edge, as shown
in graph (a).
As a scaled (k<1) blur is
subtracted from the
original, the result is that
the edge is enhanced, as
shown in graph (c).
Jorma Kekalainen
940
941
Jorma Kekalainen
942
Jorma Kekalainen
943
Exercise
Verify with Matlab the previous equality
Jorma Kekalainen
944
Solution
Verify with Matlab the previous equality
>> B=-3*1/9*ones(3)
B=
-0.3333 -0.3333 -0.3333
-0.3333 -0.3333 -0.3333
>> C=1/3*[-1,-1,-1;-1,11,-1;-1,-1,-1]
-0.3333 -0.3333 -0.3333
C=
>> A+B
-0.3333 -0.3333 -0.3333
ans =
-0.3333 3.6667 -0.3333
-0.3333 -0.3333 -0.3333
-0.3333 -0.3333 -0.3333
-0.3333 3.6667 -0.3333
Jorma Kekalainen
Digital Image Processing
-0.3333 -0.3333 -0.3333
945
Example
Figure below was created using the Matlab commands
p=imread('cameraman.tif');
u=fspecial('unsharp',0.5);
pu=filter2(u,p);
imshow(p),figure,imshow(pu/255)
Original
Jorma Kekalainen
Jorma Kekalainen
947
Jorma Kekalainen
948
Example
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image')
f=[-1 -1 -1;-1 11 -1;-1 -1 -1]/9;
Cf=filter2(f,C);
figure, imshow(Cf/90)
title('Filtering very similar to the unsharp filter above, except
for a scaling factor')
xlabel('Coefficient 1/90 was obtained by trial and error')
Jorma Kekalainen
949
Example
Jorma Kekalainen
950
Note
We can also write the high boost formula above as
Jorma Kekalainen
951
If we take A=5/6
Jorma Kekalainen
952
953
hb1=3*id-2*f
hb2=1.25*id-0.25*f
hb1 =
-0.2222 -0.2222 -0.2222
-0.2222 2.7778 -0.2222
-0.2222 -0.2222 -0.2222
hb2 =
-0.0278 -0.0278 -0.0278
-0.0278 1.2222 -0.0278
-0.0278 -0.0278 -0.0278
954
Example
If each of the filters hb1 and hb2 are applied to an image with filter2, the
result will have enhanced edges.
The images were obtained with
x1=filter2(hb1,x);
x2=filter2(hb1,x);
imshow(x2/255)
imshow(x1/255)
(a) High boost filtering with hb1
Jorma Kekalainen
hb1 appears
to produce the
best result;
hb2 produces
an image not
very much
sharper than
955
the original.
Introduction
The Fourier Transform is of essential importance to all
signal processing, and of course, also to image processing.
It allows us to perform tasks which would be impossible to
perform any other way and its efficiency allows us to
perform other tasks more quickly.
The Fourier Transform provides, among other things, a
powerful alternative to linear spatial filtering; it is more
efficient to use the Fourier transform than a spatial filter for
a large filter.
The Fourier Transform also allows us to isolate and process
particular image frequencies, and so perform low-pass and
high-pass filtering with a great degree of precision.
Jorma Kekalainen
958
Jorma Kekalainen
959
Jorma Kekalainen
960
Jorma Kekalainen
961
Note
When we deal with a discrete function, as we
shall do for images, the situation changes
slightly.
Since we only have to obtain a finite number
of values, we only need a finite number of
functions to do it.
Jorma Kekalainen
962
Example
Consider the discrete
sequence
963
Jorma Kekalainen
964
The formula for the inverse DFT is very similar to the forward
transform:
Jorma Kekalainen
965
Jorma Kekalainen
966
Note
The formulas given in equations
Jorma Kekalainen
967
Note
Perhaps, more familiar forms
2
DFT{x( n)} X( k ) x( n) exp j kn , k=0,1, ,N-1
N
n=0
N 1
1 N 1
2
x( n) = IDFT{X( k )} X( k ) exp j kn , n=0,1, ,N-1
N k =0
N
x(n) X(k)
Jorma Kekalainen
968
Example
Express DFT of the sequence
1 , n = 0,1,2,6,7
x( n) =
3 n 5
0 ,
Jorma Kekalainen
969
Example
Lets substitute the given sequence x
1 , n = 0,1,2,6,7
x( n) =
3 n 5
0 ,
N
n=0
N 1
, k = 0,1,L , N 1
3
7
X( k ) = 1 + exp j k + exp j k + exp j k + exp j k
4
2
2
4
3
7
= 1 + exp j k + exp j k + exp j k + exp j k
4 2
2
4
= 1 + exp j k + exp j k + exp j k + exp j k
4 2
2
4
Jorma Kekalainen
= 1 + 2 cos k + 2 cos k , k = 0,1,L ,7
4
2Image
Processing
Digital
970
Example
Matlab calculations
k=0:7;
X(k+1)=1+2*cos(pi*k/4)+2*cos(pi*k/2)
X=
Columns 1 through 8
5.0000 2.4142 -1.0000 -0.4142 1.0000 -0.4142 -1.0000 2.4142
or
X(k+1)=1+exp(-i*pi*k/4)+exp(-i*pi*k/2)+exp(-i*3*pi*k/2)+exp(-i*7*pi*k/4)
X=
Columns 1 through 4
5.0000
Columns 5 through 8
Jorma Kekalainen
1.0000
971
Jorma Kekalainen
972
Example
>> a=[1,2,3,4,5,6]
a=
1 2 3 4 5 6
>> fft(a')
ans =
21.0000 + 0.0000i
-3.0000 + 5.1962i
-3.0000 + 1.7321i
-3.0000 + 0.0000i
-3.0000 - 1.7321i
-3.0000 - 5.1962i
Note: To apply a DFT to a single vector in Matlab, we should use a
Jorma Kekalainen
Digital Image Processing
column
vector.
973
Exercise
Give the same input vector x' to function muunnos1 and
Matlab fft-function.
Comparing outputs show that muunnos1 makes DFT.
Hint: Give the vector so that its length is a power of 2,
e.g., x =[1 1 1 0 0 0 1 1] or x=rand(1,128)
Jorma Kekalainen
974
function [X]=muunnos1(x)
function [X]=muunnos1(x)
N=length(x);
P=log10(N)/log10(2);
J=1;
for I=1:N-1;
if I<J;
A=x(J);x(J)=x(I);x(I)=A;end;
K=N/2;
while K<J; J=JK;K=K/2;end;
J=J+K;
end;
Jorma Kekalainen
for L=1:P;
B=2^L;
B1=B/2;
V=1;
W=cos(pi/B1)-i*sin(pi/B1);
for J=1:B1;
for I=J:B:N;
C=I+B1;
A=x(C)*V;
x(C)=x(I)-A;
x(I)=x(I)+A;
end;
V=V*W;
end;
end;
975
Solution
>> a=rand(1,256)-0.5+j*(rand(1,256)-0.5);% Inputs
>>X1=muunnos1(a);
>>X2=fft(a);
% Comparing outputs we verify that the function muunnos1 performs DFT.
>>max(abs(X2-X1))
ans=
4.3375e-14
>> figure
plot(real(X2-X1))
title('real(fft-muunnos1)')
grid
figure
plot(imag(X2-X1))
title('imag(fft-muunnos1)')
grid
Jorma Kekalainen
%
976
Re{fft}-Re{muunnos1} and
Im {fft}-Im{muunnos1}
Jorma Kekalainen
977
Exercise
Give the same input vector
xx=[1,1,1,0,0,0,1,1]
to function muunnos2 and Matlab ifftfunction.
Show results are equivalent.
Jorma Kekalainen
978
function [x]=muunnos2(x)
function [x]=muunnos2(x)
% IFFT algorithm
x=x';
% Complex conjugate
N=length(x);
P=log10(N)/log10(2);
J=1;
for I=1:N-1;
if I<J; A=x(J);x(J)=x(I);x(I)=A;end;
K=N/2;
while K<J; J=J-K;K=K/2;end;
J=J+K;
end;
%
Jorma Kekalainen
for L=1:P;
B=2^L;
B1=B/2;
V=1;
W=cos(pi/B1)-i*sin(pi/B1);
for J=1:B1;
for I=J:B:N;
C=I+B1;
A=x(C)*V;
x(C)=x(I)-A;
x(I)=x(I)+A;
end;
V=V*W;
end;
end;
x=x'/N; % IFFT
979
Solution
>> xx=[1,1,1,0,0,0,1,1]
xx =
1 1 1 0 0 0 1 1
>> ifft(xx)
ans =
Columns 1 through 7
0.6250 0.3018 -0.1250 -0.0518 0.1250 -0.0518 -0.1250
Column 8
0.3018
>> muunnos2(xx)
ans =
Columns 1 through 3
0.6250 + 0.0000i 0.3018 - 0.0000i -0.1250 + 0.0000i
Columns 4 through 6
-0.0518 + 0.0000i 0.1250 + 0.0000i -0.0518 + 0.0000i
Columns 7 through 8
-0.1250 + 0.0000i 0.3018 - 0.0000i
Jorma Kekalainen
980
Solution
>> ifft(xx')
ans =
0.6250
0.3018
-0.1250
-0.0518
0.1250
-0.0518
-0.1250
0.3018
Jorma Kekalainen
>> muunnos2(xx')
ans =
0.6250 + 0.0000i
0.3018 - 0.0000i
-0.1250 + 0.0000i
-0.0518 + 0.0000i
0.1250 + 0.0000i
-0.0518 + 0.0000i
-0.1250 + 0.0000i
0.3018 - 0.0000
Digital Image Processing
981
Exercise
Combine the functions of the previous
exercises so that one and the same function
performs FFT and IFFT, for example, with
commands like styles muunnos3('fft',x) or
muunnos3('ifft',x)
Jorma Kekalainen
982
function [x]=muunnos3(type,x)
for L=1:P;
B=2^L;
B1=B/2;
V=1;
W=cos(pi/B1)-i*sin(pi/B1);
for J=1:B1;
for I=J:B:N;
C=I+B1;
A=x(C)*V;
x(C)=x(I)-A;
x(I)=x(I)+A;
end;
V=V*W;
end;
end;
if strcmp(type,'ifft');x=x'/N;end% IFFT
function [x]=muunnos3(type,x)
% FFT and IFFT algorithm
% type='fft' or 'ifft'
if strcmp(type,'ifft');x=x';end% Conjugate
N=length(x);
P=log10(N)/log10(2);
J=1;
for I=1:N-1;
if I<J; A=x(J);x(J)=x(I);x(I)=A;end;
K=N/2;
while K<J; J=J-K;K=K/2;end;
J=J+K;
end;
Jorma
Digital Image Processing
% Kekalainen
983
1]
>> muunnos3('fft',x')
ans =
5.0000 + 0.0000i
2.4142 + 0.0000i
-1.0000 + 0.0000i
-0.4142 - 0.0000i
1.0000 + 0.0000i
-0.4142 - 0.0000i
-1.0000 + 0.0000i
2.4142 + 0.0000i
Digital Image Processing
984
Jorma Kekalainen
>> ifft(XX)
ans =
1.0000
1.0000
1.0000
0
0.0000
0
1.0000
1.0000
>> muunnos3('ifft',XX)
ans =
1.0000 + 0.0000i
1.0000 - 0.0000i
1.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
1.0000 + 0.0000i
1.0000 - 0.0000i
985
Note
>> a=rand(1,8)-0.5+j*(rand(1,8)-0.5)
a=
Columns 1 through 3
0.3147 + 0.4575i 0.4058 + 0.4649i -0.3730 - 0.3424i
Columns 4 through 6
0.4134 + 0.4706i 0.1324 + 0.4572i -0.4025 - 0.0146i
Columns 7 through 8
-0.2215 + 0.3003i 0.0469 - 0.3581i
>> a'
ans =
0.3147 - 0.4575i
0.4058 - 0.4649i
-0.3730 + 0.3424i
0.4134 - 0.4706i
0.1324 - 0.4572i
-0.4025 + 0.0146i
-0.2215 - 0.3003i
0.0469 + 0.3581i
Jorma Kekalainen
Digital Image Processing
986
987
Jorma Kekalainen
>> muunnos3('fft',a')
ans =
0.3162 - 1.4353i
0.2124 - 0.4326i
0.7038 - 0.4999i
-1.6977 - 1.2294i
-0.6110 - 0.3098i
1.4377 + 0.7349i
1.3794 - 1.4137i
0.7771 + 0.9257i
988
989
Jorma Kekalainen
>> muunnos3('ifft',a')
ans =
0.0395 - 0.1794i
0.0971 + 0.1157i
0.1724 - 0.1767i
0.1797 + 0.0919i
-0.0764 - 0.0387i
-0.2122 - 0.1537i
0.0880 - 0.0625i
0.0265 - 0.0541i
990
Verification of muunnos3
a=rand(1,256)-0.5+j*(rand(1,256)-0.5);
X1=[];X2=[];
X1=[];X2=[];
X1=muunnos3('fft',a);
X2=fft(a);
max(abs(X2-X1))
ans =
3.2610e-14
figure
plot(real(X2-X1))
title('real(fft-muunnos3)')
grid
figure
plot(imag(X2-X1))
title('imag(fft-muunnos3)')
grid
Jorma Kekalainen
x1=[];
x2=[];
x1=muunnos3('ifft',a);
x2=ifft(a);
max(abs(x2-x1))
ans =
1.1857e-16
figure
plot(real(x2-x1))
title('real(ifft-muunnos3)')
grid
figure
plot(imag(x2-x1))
title('imag(ifft-muunnos3)')
grid
Digital Image Processing
991
Verification of muunnos3
a=rand(1,256)-0.5+j*(rand(1,256)-0.5);
Jorma Kekalainen
992
Comparison of results
>> x'
ans =
1
1
1
0
0
0
1
1
x = Jorma
1 Kekalainen
1 1
>> muunnos1(x')
ans =
5.0000 + 0.0000i
2.4142 + 0.0000i
-1.0000 + 0.0000i
-0.4142 - 0.0000i
1.0000 + 0.0000i
-0.4142 - 0.0000i
-1.0000 + 0.0000i
2.4142 + 0.0000i
>> fft(x')
ans =
5.0000
2.4142
-1.0000
-0.4142
1.0000
-0.4142
-1.0000
2.4142
0
>> dftv(x')
ans =
5.0000 + 0.0000i
2.4142 + 0.0000i
-1.0000 - 0.0000i
-0.4142 + 0.0000i
1.0000 + 0.0000i
-0.4142 - 0.0000i
-1.0000 - 0.0000i
2.4142 + 0.0000i
993
Comparison of results
>> a
a=
2
3
4
5
6
7
8
1
>> fft(a)
ans =
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
Jorma Kekalainen
>> muunnos1(a)
ans =
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
>> dftv(a)
ans =
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
994
Simple DF transformer
Jorma Kekalainen
995
996
Example
Let it be vectors
f=[1,2,3,4,5,6,7,8]
g=[2,4,6,8,10,12,14,16]
and scalars p=3, q=4
First lets calculate h=pf+qg and H=fft(h')
then PF=fft(pf') and QG=fft(qg') and H1=PF+QG
Finally, we compare H to H1
Jorma Kekalainen
997
11 22 33 44 55 66 77 88
H=
1.0e+02 *
3.9600 + 0.0000i
-0.4400 + 1.0623i
-0.4400 + 0.4400i
-0.4400 + 0.1823i
-0.4400 + 0.0000i
-0.4400 - 0.1823i
-0.4400 - 0.4400i
-0.4400 - 1.0623i
Jorma Kekalainen
998
QG =
1.0e+02 *
2.8800 + 0.0000i
-0.3200 + 0.7725i
-0.3200 + 0.3200i
-0.3200 + 0.1325i
-0.3200 + 0.0000i
-0.3200 - 0.1325i
-0.3200 - 0.3200i
-0.3200 - 0.7725i
Digital Image Processing
999
H1=PF+QG vs. H
>> H-H1
H1 =
H=
ans =
1.0e+02 *
1.0e+02 *
3.9600 + 0.0000i
3.9600 + 0.0000i 1.0e-13 *
0.0000 + 0.0000i
-0.4400 + 1.0623i -0.4400 + 1.0623i
0.0000 + 0.1421i
-0.4400 + 0.4400i -0.4400 + 0.4400i 0.0000 + 0.0000i
-0.4400 + 0.1823i -0.4400 + 0.1823i 0.0000 + 0.0000i
-0.4400 + 0.0000i -0.4400 + 0.0000i 0.0000 + 0.0000i
-0.4400 - 0.1823i -0.4400 - 0.1823i 0.0000 + 0.0000i
-0.4400 - 0.4400i -0.4400 - 0.4400i 0.0000 + 0.0000i
-0.4400 - 1.0623i -0.4400 - 1.0623i 0.0000 - 0.1421i
Jorma Kekalainen
1000
>> 3*fft(f')+4*fft(g')
ans =
1.0e+02 *
3.9600 + 0.0000i
-0.4400 + 1.0623i
-0.4400 + 0.4400i
-0.4400 + 0.1823i
-0.4400 + 0.0000i
-0.4400 - 0.1823i
-0.4400 - 0.4400i
-0.4400 - 1.0623i
Digital Image Processing
1001
1002
Example
>> x = [2 3 4 5 6 7 8 1]
x=
2 3 4 5 6 7
>> X=fft(x')
X=
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
>> x1=(-1).^[0:7].*x
x1 =
2 -3 4 -5 6 -7
>> X1=fft(x1')
X1 =
4.0000 + 0.0000i
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
36.0000 + 0.0000i
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
8 -1
Jorma Kekalainen
1003
Note:
The first four elements of X areDigital
theImage
lastProcessing
four elements of X1, and vice versa.
Example
>> x = [2 3 4 5 6 7 8 1];
>> X=fft(x')
36.0000
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
4.0000
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
>> x1=(-1).^[0:7].*x
x1 =
2 -3 4 -5 6 -7 8 -1
>> X1=fft(x1')
X1 =
4.0000
1.6569 + 4.0000i
-4.0000 + 4.0000i
-9.6569 - 4.0000i
36.0000
-9.6569 + 4.0000i
-4.0000 - 4.0000i
1.6569 - 4.0000i
Jorma Kekalainen
1004
Note:
The first four elements of X areDigital
theImage
lastProcessing
four elements of X1, and vice versa.
Conjugate symmetry
If x is real, and of length N , then its DFT X satisfies the
condition that
is the complex conjugate of XN-k , for all k=1,2, , N-1.
So in our example of length 8, we have
In this case we also have X4 = conj(X4), which means X4 must
X0=4.0000
be real.
X1=1.6569 + 4.0000i
In fact if N is even, then XN/2 will be real,
X2=-4.0000 + 4.0000i
X3=-9.6569 - 4.0000i
as we can see above.
X =36.0000
4
Jorma Kekalainen
X5=-9.6569 + 4.0000i
X6=-4.0000 - 4.0000i
1005
X7=1.6569 - 4.0000i
1006
1007
Jorma Kekalainen
1008
Exercise
How length the vector (2n) must be if the
increase in speed with FFT compared to the
direct method is at least 1000 times?
Jorma Kekalainen
1009
1010
Corrugation functions
A one-dimensional function can be written as
a sum of sines and cosines.
Given that an image may be considered as a
two-dimensional function f(x,y), it seems
reasonable to assume that f can be expressed
as sums of corrugated (wave) functions
which have the general form
z=asin(bx +cy).
Jorma Kekalainen
1011
Corrugation functions
A sample such function is shown below.
Actually, this is exactly what the two-dimensional Fourier
transform does: it rewrites the original matrix in terms of
sums of corrugations.
More precisely: the magnitude of the Fourier transform is a
matrix (the same size as the starting matrix) whose elements
are fij .
fij give the height of
the corrugation for
which the horizontal
and vertical
distances between
consecutive wave
bottoms are 1/i and
1/j. Jorma Kekalainen
1012
Jorma Kekalainen
1013
Note: For notational convenience we assume that the x indices are from 0 to (M-1)
Jorma Kekalainen
Digital Image Processing
1014
and the y indices are from 0 to (N-1),
Note
The formulas given in equations
1016
Jorma Kekalainen
1017
Separability
Notice that the Fourier transform filter elements can be
expressed as products:
1018
Separability
This means that we can break down our long formulas above
to simpler formulas that work on single rows or columns e.g.:
1019
DFT of
each row
Digital Image Processing
DFT of
each
column
1020
Linearity
An important property of the DFT is its linearity; the DFT of a
sum is equal to the sum of the individual DFT's, and the same
goes for scalar multiplication:
d=f+n
where f is the original image; n is the noise and d is the
degraded image.
Jorma Kekalainen
1021
Linearity
Since
we may be able to remove or reduce n by modifying
the transform.
Some noise appears on the DFT in a way which
makes it particularly easy to remove.
Jorma Kekalainen
1022
Convolution theorem
This result provides one of the most powerful
advantages of using the DFT.
Suppose we wish to convolve an image M with a spatial
filter S.
Our method has been place S over each pixel of M in
turn, calculate the product of all corresponding gray
values of M and elements of S , and add the results.
The result is called the digital convolution of M and S,
and is denoted MS.
This method of convolution can be very slow, especially
if S is large.
Jorma Kekalainen
1023
Application of convolution
theorem
The convolution theorem states that the result
MS can be obtained by the following sequence
of steps:
1. Pad S with zeroes so that is the same size as M;
denote this padded result by S.
2. Form the DFTs of both M and S, to obtain
F(M) and F(S).
3. Form the element - by - element product of
these two transforms:
F(M) F(S).
Jorma Kekalainen
1024
Application of convolution
theorem
4. Take the inverse transform of the result:
F -1(F(M) F(S)).
The convolution theorem states:
M S = F -1(F ( M) F (S))
or equivalently that
F(M S) = F(M) F(S).
Although this might seem like an unnecessarily
clumsy and roundabout way of computing a
convolution, it can have enormous speed
advantages if S is large.
Jorma Kekalainen
1025
Example
Suppose we wish to convolve a 512 x 512 image with a 32 x 32 filter.
To do this directly would require 322 = 1024 multiplications for each pixel,
of which there are 512 x 512 =262144.
Thus there will be a total of 1024 x 262144 = 268, 435, 456 multiplications
needed.
Now look at applying the DFT (using an FFT algorithm).
Each row requires 4608 multiplications by the previous table.
There are 512 rows, so a total of 4608 x 512 = 2359296 multiplications; the
same must be done again for the columns.
Thus to obtain the DFT of the image requires 4718592 multiplications.
We need the same amount to obtain the DFT of the filter, and for the
inverse DFT.
We also require 512 x 512 = 262144 multiplications to perform the
product of the two transforms.
Thus the total number of multiplications needed to perform convolution
using the DFT is 4718592 x 3 + 262144 = 14, 417, 920 which is an
enormous saving compared to the direct method.
Jorma Kekalainen
1026
DC coefficient
If we put u = v = 0 in the definition given in equation
we get
1027
Shifting a DFT
For purposes of display, it is convenient to have the DC
coefficient in the centre of the matrix.
This will happen if all elements f(x,y) in the matrix are
multiplied by (-1)x+y before the transform.
Figure below demonstrates how the matrix is shifted by this
method.
In each diagram the DC coefficient is the top left hand
element of submatrix A and is shown as a black square.
FFT
Jorma Kekalainen
After shifting
1028
Conjugate symmetry
An analysis of the Fourier transform definition
leads to a symmetry property; if we make the
substitutions u=-u and v=-v in the equation
we find that
For integers
p and q
Jorma Kekalainen
1029
Jorma Kekalainen
1030
Displaying transforms
Having obtained the Fourier transform F(u, v) of an
image f(x, y) we would like to see what it looks like.
As the elements F(u, v) are complex numbers, we cant
view them directly, but we can view their magnitude
|F(u, v)|.
Since these will be numbers of type double, generally
with large range, we have two approaches
1. find the maximum value m of |F(u, v)| (this will be the
DC coefficient), and use imshow to view |F(u, v)|/m,
2. use mat2gray to view |F(u, v)| directly.
Jorma Kekalainen
1031
1032
Jorma Kekalainen
1033
Example
Suppose we take a constant matrix f(x, y) = 1.
Going back to the idea of a sum of corrugations, we
see that in fact no corrugations are required to form
a constant.
Thus we would hope that the DFT consists of a DC
coefficient and zeroes everywhere else.
We will use the ones function, which produces an
(n x n) matrix consisting of 1s, where n is an input to
the function.
Jorma Kekalainen
1034
Example
>> a=ones(8);
>> fft2(a)
The result is indeed as we expected:
ans =
64 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Note that the DC coefficient is indeed the sum of all the matrix values.
Jorma Kekalainen
1035
Example
>> a=ones(8)
a=
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
>> fft2=fft2(a)
fft2 =
64 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Note:
Jorma Kekalainen
1036
Example
Now we'll take a matrix consisting of a single corrugation:
>> a = [100 200; 100 200];
>> a = repmat(a,4,4)
ans =
Jorma Kekalainen
1037
Example
>> af = fft2(a)
ans =
What we have here is really the sum of two matrices: a constant matrix
each element of which is 150, and a corrugation which alternates -50 to 50
from left to right.
The constant matrix alone would produce (as in example 1), a DC
coefficient alone of value 64*150=9600; the corrugation a single value.
By linearity, the DFT will consist of just the two values.
Jorma Kekalainen
1038
Example
Jorma Kekalainen
100
100
100
100
100
100
100
100
200
200
200
200
200
200
200
200
100
100
100
100
100
100
100
100
200
200
200
200
200
200
200
200
1039
Example
af =
9600
Jorma Kekalainen
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
-3200
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1040
Example
We will take here a single step edge:
>> a = [zeros(8,4) ones(8,4)]
a=
Jorma Kekalainen
1041
round(abs(af))
%
a=
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
ans =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 9 0 21 32 21 0 9
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Jorma Kekalainen
0 0 0 0 0 0 0 0
1042
Jorma Kekalainen
The mirroring of
values about the
DC coefficient is
a consequence
of the symmetry
of the DFT.1043
Example
Lets create a few simple images, and see what the Fourier
transform produces.
The first example is a single edge
>> a=[zeros(256,128) ones(256,128)];
This is displayed
Then we shall take its DFT,
and shift it:
>> af=fftshift(fft2(a));
Jorma Kekalainen
1044
Example
Now we'll view its spectrum; we have the choice of two
commands:
1. afl=log(1+abs(af));%af is shifted DFT
imshow(afl/afl(129,129))
This normalization works because after shifting, the DC
coefficient is at position x=129 and y=129.
We stretch the transform using log, and divide the result by
the middle value to obtain matrix of type double with values
in the range 0.0 1.0.
This can then be viewed directly with imshow.
2. imshow(mat2gray(log(1+abs(af))))
The mat2gray function automatically scales a matrix for
display as an image.
Jorma Kekalainen
1045
Jorma Kekalainen
1046
function fftshow(f,type)
% Function to display a Fourier transform
%fftshow(f,'log') and fftshow(f,'abs')
% Usage: FFTSHOW(F,TYPE)
%
% Displays the fft matrix f using imshow,
% where TYPE must be one of
% 'abs' or 'log'. If TYPE='abs', then then abs(f)
% is displayed; if
% TYPE='log' then log(1+abs(f)) is displayed.
% If TYPE is omitted, then
% 'log' is chosen as a default.
%
% Example:
% c=imread('image.tif');
% cf=fftshift(fft2(c));
% fftshow(cf,'abs')
%
Jorma Kekalainen
if nargin<2,
type='log';
end
if (type=='log')
fl = log(1+abs(f));
fm = max(fl(:));
imshow(im2uint8(fl/fm))
elseif (type=='abs')
fa=abs(f);
fm=max(fa(:));
imshow(fa/fm)
else
error('TYPE must be abs or log.');
end;
1047
fftshow(f,type) function
function [ ] = fftshow(f,type)
if nargin<2,
%Function to display a Fourier transform
type='log';
%fftshow(f,'log') and fftshow(f,'abs')
end
%
if (type=='log')
% Usage: FFTSHOW(F,TYPE)
fl = log(1+abs(f));
%
fm = max(fl(:));
% Displays the fft matrix f using imshow,
where %TYPE must be one of 'abs' or 'log'.
imshow(im2uint8(fl/fm))
%If TYPE='abs', then then abs(f) is displayed;
elseif (type=='abs')
%if TYPE='log' then log(1+abs(f)) is displayed.
fa=abs(f);
%If TYPE is omitted, then 'log' is chosen as a
fm=max(fa(:));
%default.
imshow(fa/fm)
%
else
% Example:
error('TYPE must be abs or log.');
% c=imread('image.tif');
% cf=fftshift(fft2(c));
end
% fftshow(cf,'abs')
Jorma
Digital Image Processing
1048
% Kekalainen
Using fftshow(f,type)
Then for example
fftshow(af,'log')
will show the logarithm of the absolute
values of the transform log(1+abs(f)),
and
fftshow(af,'abs')
will show the absolute values of the
transform abs(f) without any scaling.
An example command sequence is
shown on the right.
Jorma Kekalainen
% Single edge
a=[zeros(256,128) ones(256,128)];
figure,imshow(a)
title('Single edge')
af=fftshift(fft2(a));
figure,fftshow(af,'abs')
title('DFT of the single edge')
xlabel('fftshow(af,''abs'')')
figure,fftshow(af,'log')
title('DFT of the single edge')
xlabel('fftshow(af,''log'')')
1049
Jorma Kekalainen
1050
Note
DFT of the single edge with fftshow(af,'abs')
Note that the line is seen only in enlarged scale!
Jorma Kekalainen
1051
Example
Now we'll create a box, and
then its Fourier transform:
a=zeros(256,256);
A box and its DFT
a(78:178,78:178)=1;
imshow(a)
title('Box')
af=fftshift(fft2(a));
figure,fftshow(af,'abs')
title('DFT of the box')
xlabel('fftshow(af,''abs'')')
figure,fftshow(af,'log')
title('DFT of the box')
xlabel('fftshow(af,''log'')')
Jorma Kekalainen
1052
Example: Box
Jorma Kekalainen
1053
Example
A
rotated
box and
its DFT
Jorma Kekalainen
1054
Note: meshgrid
[X,Y] = meshgrid(xgv,ygv)
replicates the grid vectors xgv
and ygv to produce a full grid.
>> [x,y]=meshgrid(1:5,1:5)
x=
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
y=
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
Jorma Kekalainen
5 5 5 5 5
>> [x,y]=meshgrid(-4:3,-4:3)
x=
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
y=
-4 -4 -4 -4 -4 -4 -4
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
Digital Image
3 Processing
3 3 3 3 3 3
3
3
3
3
3
3
3
3
-4
-3
-2
-1
0
1
2
3
1055
Note
Jorma Kekalainen
1056
Example
We will create a small circle,
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<15);
and then transform and display it:
cf=fft2shift(fft2(z));
>> fftshow(cf,'log')
Jorma Kekalainen
1057
Note
>> x=1:5
x=
1 2 3
[X,Y] = meshgrid(xgv,ygv)
replicates the grid vectors xgv
and ygv to produce a full grid.
>> [x,y]=meshgrid(1:5,1:5)
x=
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
y=
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
Jorma Kekalainen
>> z=sqrt(x.^2+y.^2)
z=
1.4142 2.2361 3.1623
2.2361 2.8284 3.6056
3.1623 3.6056 4.2426
4.1231 4.4721 5.0000
5.0990 5.3852 5.8310
4.1231
4.4721
5.0000
5.6569
6.4031
5.0990
5.3852
5.8310
6.4031
7.0711
1058
Note
>> [x,y]=meshgrid(-4:3,-4:3)
x=
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
-4 -3 -2 -1 0 1 2
y=
-4 -4 -4 -4 -4 -4 -4
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
Jorma Kekalainen
3
3
3
3
3 >> z=sqrt(x.^2+y.^2)
3 z=
5.6569 5.0000 4.4721
3
5.0000 4.2426 3.6056
3
4.4721 3.6056 2.8284
-4 4.1231 3.1623 2.2361
-3 4.0000 3.0000 2.0000
-2 4.1231 3.1623 2.2361
-1 4.4721 3.6056 2.8284
5.0000 4.2426 3.6056
0
1
2
3
Digital Image Processing
4.1231
3.1623
2.2361
1.4142
1.0000
1.4142
2.2361
3.1623
1059
Jorma Kekalainen
1060
Comment
Note the ringing in the Fourier transform.
This is an effect associated with the sharp cutoff the
circle.
As we have seen from both the edge and box images in
the previous examples, an edge appears in the
transform as a line of values at right angles to the edge.
We may consider the values on the line as being the
coefficients of the appropriate corrugation functions
which sum to the edge.
With the circle, we have lines of values radiating out
from the circle; these values appear as circles in the
transform.
Jorma Kekalainen
1061
Example
A circle with a gentle cutoff, so
that its edge appears blurred, will
have a transform with no ringing.
Such a circle can be made with
the command (given z above):
b=1./(1+(z./15).^2);
This image appears as a blurred
circle, and its transform is very
similar.
Jorma Kekalainen
Example
Jorma Kekalainen
1063
1066
Ideal filters
As the ideal filters are radially symmetric about the
center of the transform, they can be simply
described in terms of their cross sections.
That is, we can describe the filter as a function of the
distance x from the center.
For an ideal low pass filter, this function can be
expressed as
1067
Low pass
Jorma Kekalainen
High pass
1068
1069
Then the inverse Fourier transform of the elementwise product of F and m is the result we require:
Jorma Kekalainen
1070
Note: The circle c displayed previously is just such a matrix with D=15.
Example
Let's see what happens if we apply this filter to an image. First
we obtain an image and its DFT.
>> cm=imread('cameraman.tif');
>> cf=fftshift(fft2(cm));
>> figure,fftshow(cf,'log')
The cameraman image and its DFT are shown below.
Jorma Kekalainen
1071
Jorma Kekalainen
1072
Example
Jorma Kekalainen
1073
1074
Jorma Kekalainen
1075
Comment
Note that even though cfli is obviously a matrix of
real numbers, we are still using fftshow to display
it.
This is because the ifft2 and fft2 functions being
numeric, will not produce mathematically perfect
results, but rather very close numeric
approximations.
So using fftshow with the 'abs' option rounds out
any errors obtained during the transform and its
inverse.
Jorma Kekalainen
1076
Example
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
xlabel('fftshow(Cf,''log'')')
%
%Lowpass filtering
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<15);
Cfl=Cf.*c;% Lowpass filtering(symbol l)
figure,fftshow(Cfl,'log')
title('DFT of the cameraman image multiplied by an ideal low-pass matrix c')
xlabel('fftshow(Cfl,''log'')')
%
%Now we can take the inverse transform and display the result:
Cfli=ifft2(Cfl);
figure,fftshow(Cfli,'abs')
title('IDFT [the cameraman image multiplied by an ideal low-pass matrix c]')
xlabel('fftshow(Cfli,''abs'')')
Jorma
Kekalainen
Digital Image Processing
1077
We would expect that the smaller the circle, the more blurred the image, and the
larger the circle; the less blurred.
%Effect of a very small circle (c<5)
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<5);
figure, imshow(c)
title('Very small circle (c<5)')
%
cf=fftshift(fft2(c));
figure, fftshow(cf,'log')
title('DFT of the very small circle(c<5)')
xlabel('fftshow(cf,''log'')')
%without function
%figure, imshow(mat2gray(log(1+abs(cf))))
%title('DFT of the very small circle (c<5)')
Jorma Kekalainen
1078
Jorma Kekalainen
1079
Jorma Kekalainen
1080
Jorma Kekalainen
1081
Jorma Kekalainen
1082
Note
We would expect that the smaller the circle, the more blurred
the image, and the larger the circle; the less blurred.
Figure below demonstrates this, using cutoffs of 5 and 30.
Notice that ringing is still present, and clearly visible in figure (b).
(a) Cutoff 5
Jorma Kekalainen
(b) Cutoff 30
1083
Jorma Kekalainen
1084
Comparison
Jorma Kekalainen
1085
Jorma Kekalainen
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
%Lowpass filtering
Cfl=Cf.*b;% Lowpass filtering(symbol l)
figure,fftshow(Cfl,'log')
title(['DFT of of the cameraman image multiplied
by a blurred low-pass matrix c<',num2str(r)])
%
%Now we can take the inverse transform and
display the result:
%
Cfli=ifft2(Cfl);
figure,fftshow(Cfli,'abs')
title(['IDFT the cameraman image multiplied by a
blurred low-pass matrix c<',num2str(r)])
1086
Jorma Kekalainen
1088
Jorma Kekalainen
1089
IDFT
The inverse DFT can be easily produced and displayed:
>> cfhi=ifft2(cfh);
The resulting image
>> figure,fftshow(cfhi,'abs')
and this is shown beside.
1090
Jorma Kekalainen
1091
Example
%High pass filtering
%First we create the filter circle:
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z>15);% High pass filtering
figure, imshow(c)
title('Ideal high pass filter (c>15)')
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
%and the multiply it by the DFT of the image:
Cfh=Cf.*c;
figure,fftshow(Cfh,'log')
title('Product of an ideal high pass filter (c>15) and DFT of the image')
%
% The inverse DFT can be easily produced and displayed:
Cfhi=ifft2(Cfh);
figure,fftshow(Cfhi,'abs')
Kekalainen
Image Processing
Jormatitle('IDFT
of the ideal high passDigital
filtered
(c>15) image')
1092
Jorma Kekalainen
1093
Examples
Cutoffs: c>15, c>30, c>60
Jorma Kekalainen
1094
c>50
c>100
c>120
Jorma Kekalainen
1095
Butterworth filtering
Ideal filtering simply cuts off the Fourier
transform at some distance from the center.
This is very easy to implement, as we have
seen, but has the disadvantage of introducing
unwanted effects: ringing, into the result.
One way of avoiding this is to use as a filter
matrix a circle with a less sharp cutoff.
A popular choice is to use Butterworth filters.
Jorma Kekalainen
1097
Butterworth filters
Butterworth filters are based on the following functions for
low pass filters and for high pass filters :
Jorma Kekalainen
1098
Jorma Kekalainen
1099
Example
It is easy to implement these filters in Matlab;
here are the commands to produce a
Butterworth low pass filter of size 256*256
with cutoff circle D=15 and order n=2:
[x,y]=meshgrid(-128:127,-128:127));
bl=1./(1+((x.^2+y.^2)/15^2).^2);
Jorma Kekalainen
1100
Jorma Kekalainen
1101
Example
Applying a Butterworth low
pass filter to the DFT of the
cameraman image we get
results shown beside and
the next slices.
Note that there is no sharp
cutoff as seen previously
with the ideal low pass
filter; also that the outer
parts of the transform are
not equal to zero, although
they are dimmed
considerably.
Jorma Kekalainen
1102
Note
Compare the transform after multiplying with a Butterworth filter with
the original transform
We see that the Butterworth filter causes an attenuation of values away
from the center, but they don't become suddenly zero, as with the ideal
low pass filter.
Jorma Kekalainen
1103
Example
Performing the inverse
transform and
displaying it as we have
done previously
produces the resulting
image beside.
This is a blurred image,
but now the ringing is
completely absent.
Jorma Kekalainen
Resulting image
1104
Compare
Jorma Kekalainen
1105
Exercise
Study effects of Butterworth low pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image
Jorma Kekalainen
1106
Solution
Jorma Kekalainen
1107
n=1,D=1,10,20,30
Jorma Kekalainen
1108
n=1,D=40,50,100
Jorma Kekalainen
1109
n=2,D=1,10,20,30
Jorma Kekalainen
1110
n=2,D=40,50,100
Jorma Kekalainen
1111
n=4,D=1,10,20,30
Jorma Kekalainen
1112
n=4,D=40,50,100
Jorma Kekalainen
1113
n=8,D=1,10,20,30
Jorma Kekalainen
1114
n=8,D=40,50,100
Jorma Kekalainen
1115
D=50,n=16,32,64,128
Jorma Kekalainen
1116
Exercise
Study effects of Butterworth high pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image
Jorma Kekalainen
1117
D=1
D=5
D=10
D=20,
D=50,
Jorma Kekalainen
D=100
1118
Jorma Kekalainen
1119
Exercise
(
)
=
f
x
2n
low
x
1+
1
f (x ) =
2n
low
D
1+
1. Show that
+
=1,when
2. Write a Matlab function
function out=lbutter(im,d,n)
to generate low pass Butterworth filters of general sizes.
3. Write a Matlab function
function out=hbutter(im,d,n)
to generate high pass Butterworth filters of general sizes.
4. Write a Matlab function
function out=butter(im,d,n,type)
to generate low or high pass Butterworth filters of
general sizes.
Jorma Kekalainen
1120
Solution
1.
Show that
(
)
f
x
=
2n
low
x
1+
1
f (x ) =
2n
low
D
1+
=1, kun
2n
f low ( x ) + f high (x ) =
1
x
1+
D
2n
1
D
1+
x
2n
Jorma Kekalainen
2n
2n
D
x
1+ +1+
x
D
=
x 2n D 2n
1 + 1 +
D x
2n
2n
2n
D
x
D
x
1+ +1+
2+ +
x
D
x
D =1
=
=
2n
2n
2n
2n
2n
2n
x
D
x
D
D
x
1+ + +
2+ +
x Image1
D4
x4
D
Digital
2
4
3
x
D 1121
Processing
4
=1
Jorma Kekalainen
1122
1123
Jorma Kekalainen
1124
Example
So to apply a Butterworth
low pass filter function
lbutter to the DFT of the
cameraman image:
bl=lbutter(c,15,1);
cfbl=cf.*bl;
figure,fftshow(cfbl,'log')
and then inverting and
displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
Jorma Kekalainen
1125
Example
So to apply a Butterworth low
pass filter function lbutter to the
DFT of the cameraman image:
bl=lbutter(C,15,1);
cfbl=cf.*bl;
figure,fftshow(cfbl,'log')
and then inverting and displaying
the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
Jorma Kekalainen
1126
Example
Jorma Kekalainen
1127
Example
% Applying Butterworth low pass filter
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
bl=lbutter(Cf,15,1);
Cfbl=Cf.*bl;
figure,fftshow(Cfbl,'log')
title('DFT of the cameraman image
multiplied by Bu-lpf (D=15,n=1)')
%inverting and displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
title('IDFT')
Jorma Kekalainen
1128
Example
%Experiments with Butterworth low or high pass filter
promt1='Give filter order n=';
n=input(promt1)
promt2='Give cutoff radius D=';
D=input(promt2)
promt3='Give Butterworth filter type (low or high) type=';
type=input(promt3,'s')
C=imread('cameraman.tif');
figure, imshow(C)
title('Original image - cameraman.tif')
Cf=fftshift(fft2(C));
figure,fftshow(Cf,'log')
title('DFT of the cameraman image')
bl=butter(Cf,D,n,type);% Call Bu function
Cfbl=Cf.*bl;
figure,fftshow(Cfbl,'log')
title('Applying butterworth filter to the DFT of the cameraman image')
%%and then inverting and displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
title(['Butterworth (D=',num2str(D),', n=',num2str(n),', type= ',type,' pass) filtered image'])
Jorma Kekalainen
1129
Example
Jorma Kekalainen
1130
Example
Jorma Kekalainen
1131
Gaussian filtering
We have discussed Gaussian filters earlier, and we saw that
they could be used for low pass filtering in the spatial
domain.
However, we can also use Gaussian filters in the frequency
domain.
As with ideal and Butterworth filters, the implementation is
very simple: create a Gaussian filter, multiply it by the
image transform, and invert the result.
Since Gaussian filters have the very nice mathematical
property that a Fourier transform of a Gaussian is a
Gaussian, we should get exactly the same results as when
using a linear Gaussian spatial filter.
Jorma Kekalainen
1133
Gaussian filter
Gaussian filters may be considered to be the most smooth of all the filters
we have discussed so far, with ideal filters the least smooth, and
Butterworth filters in the middle.
We can create Gaussian filters using the fspecial function, and apply them
to our transform.
C=imread('cameraman.tif');
Cf=fftshift(fft2(C));
h = fspecial('gaussian', hsize, sigma)
g1=mat2gray(fspecial('gaussian',256,10)); returns a rotationally symmetric
Gaussian lowpass filter of size hsize
Cg1=Cf.*g1;
with standard deviation sigma
fftshow(Cg1,'log')
(positive). hsize can be a vector
g2=mat2gray(fspecial('gaussian',256,30)); specifying the number of
rows and columns in h, or it can be a
Cg2=Cf.*g2;
scalar, in which case h is a square
figure,fftshow(Cg2,'log')
Jorma Kekalainen
matrix.
The default value for hsize is [3 3]; the
Digital Image Processing
1134
default value for sigma is 0.5.
Note
Note the use of the mat2gray function.
The fspecial function on its own produces a low pass Gaussian filter with a
very small maximum:
>> g=fspecial('gaussian',256,10);
format long, max(g(:))
ans =
0.001587575526792
>> g=fspecial('gaussian',256,30);
format long, max(g(:)), format
ans =
1.767967201419648e-04
The reason is that fspecial adjusts its output to keep the volume under the
Gaussian function always 1.
This means that a wider function, with a large standard deviation, will
have a low maximum.
So we need to scale the result so that the central value will be 1; and
mat2gray does that automatically.
Jorma Kekalainen
1135
1136
=30
Jorma Kekalainen
1137
1138
=30
Note: As with ideal and Butterworth filters, the wider the high pass
filter, the more of the transform we are reducing, and the less of the
Jorma Kekalainen
Digital Image Processing
1139
original
image will appear in the
result.
Jorma Kekalainen
1140
1142
1143
Jorma Kekalainen
This is because a
small period
corresponds to a
high frequency
(large change over
a small distance),
and is therefore
further away from
the center of the
1144
shifted transform.
1145
Jorma Kekalainen
1146
Jorma Kekalainen
After inversion
1147
Notch filtering
With a notch filter, we simply make the rows
and columns of the spikes zero:
cpf(156,:)=0;
cpf(102,:)=0;
cpf(:,170)=0;
cpf(:,88)=0;
Jorma Kekalainen
1148
Jorma Kekalainen
1149
Inverse filtering
We have seen that we can perform filtering in the Fourier
domain by multiplying the DFT of an image by the DFT of a
filter: this is a direct use of the convolution theorem.
We thus have
where X is the DFT of the image; F is the DFT of the filter, and
Y is the DFT of the result.
If we are given Y and F, then we should be able to recover the
DFT of the original image X simply by dividing by F:
Jorma Kekalainen
1151
Example
Suppose, for example we take the cameraman image
cameraman.tif, and blur it using a low-pass Butterworth filter:
C=imread('cameraman.tif');
Cf=fftshift(fft2(C));
b=lbutter(C,15,2);
Cb=Cf.*b;
Cba=abs(ifft2(Cb));
Cba=uint8(255*mat2gray(Cba));
imshow(Cba)
The result is shown beside.
Jorma Kekalainen
1152
Example
We can attempt to recover the
original image by dividing by the
filter:
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
imshow(mat2gray(C1a))
The result is shown beside and
there is no improvement!
Jorma Kekalainen
An attempt to recover
by inverse filtering
1153
Jorma Kekalainen
1154
Example
We can apply the first method by multiplying a Butterworth
low pass filter to the division:
Cbf=fftshift(fft2(Cba));
C1=(Cbf./b).*lbutter(C,40,10);
C1a=abs(ifft2(C1));
imshow(mat2gray(C1a))
Next figure shows the results obtained by using a different
cutoff radius of the Butterworth filter each time: (a) 30, (b) 40
(as in the Matlab commands just given), (c) 50, (d) 60, and
(e) 80.
Jorma Kekalainen
1155
n=10
D=30,40,50,60,70,80
Jorma Kekalainen
1156
Example
C=imread('cameraman.tif');
Cf=fftshift(fft2(C));
% Blurring the image
b=lbutter(C,15,2);
Cb=Cf.*b;
Cba=abs(ifft2(Cb));
Cba=uint8(255*mat2gray(Cba));
figure, imshow(Cba)
title('Image blurred by Bu-lpf (D=15, n=2)')
%
%We can attempt to recover the original image by dividing by the filter:
%
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
figure, imshow(mat2gray(C1a))
title('Recovery attempt of the original image at inverse filtering')
Jorma Kekalainen
1157
Example
We can at randomly test on the ability of Butterworth
low pass filter with different cutoff radius and order to
eliminate very low values.
promt1='Give filter order n=';
n=input(promt1)
promt2='Give cutoff radius D=';
D=input(promt2)
Cbf=fftshift(fft2(Cba));
C1=(Cbf./b).*lbutter(C,D,n);
C1a=abs(ifft2(C1));
figure, imshow(mat2gray(C1a))
title(['Inverse filtering using Bu-lpf (D=',num2str(D),',
n=',num2str(n),') to eliminate
zeros'])
Jorma Kekalainen
Digital Image Processing
1158
1159
Example
We can systematically search for suitable
threshold d with certain cutoff radius e.g.:
for d=0:0.001:0.01;
b=lbutter(C,15,2);b(find(b<d))=1;
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
figure, imshow(mat2gray(C1a))
title(['All small Bu-lpf outputs(<',num2str(d),')
are set equal to 1'])
end
Jorma Kekalainen
1160
d<0.005
d<0.01
1161
Motion deblurring
We can consider the removal of blur caused by motion to be a
special case of inverse filtering.
Suppose we take an image with text and blur it by a small
amount.
bc=imread('board.tif');
bg=im2uint8(rgb2gray(bc));
b=bg(100:355,30:285);
figure,imshow(b)
These commands simply take the color image of a circuit
board (the image board.tif), makes a grayscale version of data
type uint8, and picks out a square subimage.
JormaThe
result is shown in the
following figure.
Kekalainen
Digital Image Processing
1162
Originals
board.tif
Jorma Kekalainen
Grayscaled subimage
1163
1164
fspecial('motion',len,theta)
m= fspecial('motion',len,theta) returns a filter to
approximate, once convolved with an image, the
linear motion of a camera by len pixels, with an
angle of theta degrees in a counterclockwise
direction.
The filter becomes a vector for horizontal and
vertical motions.
The default len is 9 and the default theta is 0,
which corresponds to a horizontal motion of nine
pixels.
Jorma Kekalainen
1165
Note
fspecial(type) creates a two-dimensional filter of the specified
type. type is a string having one of these values.
Value
Description
average
disk
gaussian
laplacian
Averaging filter
Circular averaging filter (pillbox)
Gaussian lowpass filter
Approximates the two-dimensional Laplacian
operator
Laplacian of Gaussian filter
Approximates the linear motion of a camera
Prewitt horizontal edge-emphasizing filter
Sobel horizontal edge-emphasizing filter
log
Motion
Prewitt
Sobel
Jorma Kekalainen
1166
1167
Jorma Kekalainen
Straight division
1168
Jorma Kekalainen
1169
Jorma Kekalainen
Constrained division
1170
Compare
Original and
blurred
Jorma Kekalainen
1171
Example
Jorma Kekalainen
1172
What is a colormap?
A colormap is matrix of values between 0 and 1 that define the colors for
graphics objects such as surface and image.
Matlab draws the objects by mapping data values to colors in the
colormap.
Colormaps can be any length, but must be three columns wide.
Each row in the matrix defines one color using an RGB triplet.
An RGB triplet is a three-element row vector whose elements specify the
intensities of the red, green, and blue components of the color.
The intensities must be in the range [0,1].
A value of 0 indicates no color and a value of 1 indicates full intensity.
E.g., this is a colormap with five colors: black, red, green, blue, and white
mymap = [0 0 0; 1 0 0; 0 1 0; 0 0 1; 1 1 1];
Jorma Kekalainen
mymap =
0 0
1 0
0 1
0 0
1 1
0
0
0
1
1
1175
Colormap
Colormaps are associated with axes and
figures.
Use the colormap function to change the
colormap for a particular axes or figure.
E.g., create a surface plot and use mymap as
the colormap for the figure
surf(peaks)
colormap(mymap)
Jorma Kekalainen
1176
Example
Jorma Kekalainen
1177
1178
Example
load flujet
figure,image(X)
title('Using jet colormap')
figure,image(X)
colormap(lines)
title('Using lines colormap')
Jorma Kekalainen
1179
1180
Color study
Color study consists of
1. the physical properties of light which give rise
to color,
2. the nature of the human eye and the ways in
which it detects color,
3. the nature of the human vision center in the
brain, and the ways in which messages from the
eye are perceived as color.
Jorma Kekalainen
1181
The values for the wavelengths of blue, green and red were
set in 1931 by the CIE (Commission Internationale
d'Eclairage), an organization responsible for color standards.
Jorma Kekalainen
1182
Jorma Kekalainen
1183
Jorma Kekalainen
1184
Note: The mixing of pigments is subtractive because it depends on the lights reflected
by those pigments (i.e., on the wavelengths that are not absorbed or subtracted by
Jorma Kekalainen
Digital Image Processing
1185
them)
Absorption of light by
the cones in the human eye
Jorma Kekalainen
1186
Jorma Kekalainen
1187
1188
1189
Jorma Kekalainen
1190
XYZ RGB
To remove negative values from color information, the CIE
introduced the XYZ color model.
The values of X, Y and Z can be obtained from the
corresponding R, G and B values by a linear transformation:
Jorma Kekalainen
1191
Exercise
Show with the help of Mablab that the
previous coefficient matrices of the color
model pair XYZRGB are the inverse
matrices.
Jorma Kekalainen
1192
Solution
>> A=[0.431,0.342,0.178;0.222,0.707,0.071;0.020,0.130,0.939]
A=
0.4310 0.3420 0.1780
0.2220 0.7070 0.0710
0.0200 0.1300 0.9390
>> inv(A)
ans =
3.0596 -1.3927 -0.4747
-0.9676 1.8748 0.0417
0.0688 -0.2299 1.0693
>> invA=inv(A)
invA =
3.0596 -1.3927 -0.4747
-0.9676 1.8748 0.0417
0.0688 -0.2299 1.0693
>> B=inv(invA)
B=
0.4310 0.3420 0.1780
0.2220 0.7070 0.0710
0.0200 0.1300 0.9390
Jorma Kekalainen
1193
Jorma Kekalainen
1194
1195
Jorma Kekalainen
1196
Jorma Kekalainen
1197
Jorma Kekalainen
1198
Jorma Kekalainen
1199
Tristimulus values
In general, the values of X, Y and Z needed to form any
particular color are called the tristimulus values.
Values corresponding to particular colors can be obtained
from published tables.
In order to discuss color independent of brightness, the
tristimulus values can be normalized by dividing by X+Y+Z:
So x + y + z = 1.
Jorma Kekalainen
1200
Chromaticity coordinates
Thus a color can be specified by x and y alone,
called the chromaticity coordinates.
Given x, y, and Y, we can obtain the tristimulus
values X and Z:
Jorma Kekalainen
1201
Exercise
Show that given x, y, and Y, we can obtain the
tristimulus values X and Z:
Jorma Kekalainen
1202
Solution
X
(1)
x
=
X +Y + Z
(2)
y
=
X +Y + Z
z = X + Y + Z (3)
x + y + z = 1 (4 )
Dividing equation (1) by equation (2 )
X x
=
Y
y
X=
x
Y
y
Jorma Kekalainen
Z z
=
Y y
z
Z= Y
y
z =1 x y
1 x y
Y
y
1203
Exercise
Show that from equations
x+y+z=1
1204
Solution
> eq4:=x+y+z=1;
eq4 := x + y + z = 1
> eq5:=X=(x/y)*Y;
eq5 := X =
> eq6:=Z=(1-x-y)*Y/y;
eq6 := Z =
xY
y
( 1 x y) Y
y
> solve({eq4,eq5,eq6},[x,y,z]);
X
Y
Z
x =
,
y
=
,
z
=
X+Y+Z
X+Y+Z
X + Y + Z
>
Jorma Kekalainen
1205
Chromaticity diagram
We can plot a chromaticity diagram using the data of XYZ values
originated in Colour & Vision Research Laboratories database.
load OmaCIExyz64.mat; % CIE data
xyz=OmaCIExyz64(:,2:4);
xy=xyz./(sum(xyz')'*[1 1 1]);
x=xy(:,1)';
y=xy(:,2)';
figure,plot([x x(1)],[y y(1)]),axis square
title('CIE 1964 chromaticity coordinates from ''OmaCIExyz64.mat''')
xlabel('X coordinate'),ylabel('Y coordinate')
Note: Here the matrix xyz consists of the second, third and fourth columns of the data, and plot
is a function
which draws a polygon withDigital
vertexes
taken from the x and y vectors.
Jorma Kekalainen
Image Processing
1206
The extra x(1) and y(1) ensures that the polygon joins up.
Note: The values of x and y which lie within the horseshoe shape represent values
Jorma Kekalainen
Digital Image Processing
1207
which
correspond to physically realizable
colors.
Jorma Kekalainen
1208
Color models
A color model is a method for specifying colors in
some standard way.
It generally consists of a three dimensional
coordinate system and a subspace of that system
in which each color is represented by a single
point.
We shall investigate three models
RGB
HSV
YIQ
Jorma Kekalainen
1210
1211
Backsides
Jorma Kekalainen
R=0
G=0
B=0
1212
1213
RGB gamut
The set of colors that can be represented
using a particular color space is called its
gamut (spectrum, range of colors, color
space)
The colors corresponding to positive values
form the RGB gamut; in general a color gamut
consists of all the colors realizable with a
particular color model.
Note: In color reproduction, including computer graphics and photography,
the gamut, or color gamut, is a certain complete subset of colors. The most
common usage refers to the subset of colors which can be accurately
represented in a given circumstance, such as within a given color space or by a
Jorma Kekalainen
Digital Image Processing
1214
certain
output device.
RGB gamut
To define the gamut, we shall create a
100*100*3 array, and to each point (i,j) in the
array, associate an XYZ triple defined by
(i/100, j/100, 1- i/100 - j/100).
We can then compute the corresponding RGB
triple, and if any of the RGB values are
negative, make the output value white.
This is done with the few commands.
Jorma Kekalainen
1215
title('RGB gamut')
Jorma Kekalainen
1216
RGB gamut
Jorma Kekalainen
1217
Jorma Kekalainen
1218
Jorma Kekalainen
1219
A typical gamut
The grayed-out horseshoe
shape is the entire range of
possible chromaticities,
displayed in the CIE 1931
chromaticity
diagram format.
The colored triangle is the
gamut available to a typical
computer monitor; it does
not cover the entire space.
The corners of the triangle
are the primary colors for
this gamut; in the case of
a CRT, they depend on the
colors of the phosphors of
the monitor.
Jorma Kekalainen
1220
1221
1222
1223
Jorma Kekalainen
1224
For closer inspection of the HSV color space, the next block of code displays the
separate color planes (hue, saturation, and value) of an HSV image.
RGB=reshape(ones(64,1)*reshape(jet(64),1,192),[64,64,3]);
HSV=rgb2hsv(RGB);
H=HSV(:,:,1);
S=HSV(:,:,2);
V=HSV(:,:,3);
subplot(2,2,1), imshow(H)
title('Hue')
subplot(2,2,2), imshow(S)
title('Saturation')
subplot(2,2,3), imshow(V)
title('Value')
subplot(2,2,4), imshow(RGB)
title('RGB')
1226
reshape(A,siz)
B = reshape(A,siz) returns an n-dimensional array with
the same elements as A, but reshaped to siz, a vector
representing the dimensions of the reshaped array.
B = reshape(A,m,n,p,...) or B = reshape(A,[m n
p ...]) returns an n-dimensional array with the same
elements as A but reshaped to have the size m-by-nby-p-by-....
The product of the specified dimensions, m*n*p*...,
must be the same as numel(A).
n = numel(A) returns the number of elements, n, in
array A.
Jorma Kekalainen
1227
1228
Hue
Red
Yellow
1/6
Green
1/3
Cyan
1/2
Blue
2/3
Magenta
5/6
Jorma Kekalainen
1229
Jorma Kekalainen
1230
1231
Example
H=(4+(0.1-0.3)/0.4)/6= 0.5833
The result is
(R,G,B)=(0.1,0.3,0.5) (0.5833,0.8,0.5)=(H,S,V)
Jorma Kekalainen
1232
Example
(R,G,B)=(0.1,0.3,0.5) (0.5833,0.8,0.5)=(H,S,V)
Checking:
>> hsv=rgb2hsv([0.1,0.3,0.5])
hsv =
0.5833 0.8000 0.5000
Checking also other way: >> rgb=hsv2rgb(hsv)
rgb =
0.1000 0.3000 0.5000
Jorma Kekalainen
1233
Exercise
Convert (R,G,B)=(0.2,0.4,0.6) to the
corresponding HSV. Check the result with
conversion function rgb2hsv.
Jorma Kekalainen
1234
Solution
(R,G,B)=(0.2,0.4,0.6)
Now V=max(R,G,B)=0.6
=max(R,G,B)-min(R,G,B)=0.6-0.2=0.4,
S= /V=0.4/0.6= 2/30.6667
Since B=V then
H=(4+(0.2-0.4)/0.4)/6=7/12 0.5833
The result is
(R,G,B)=(0.2,0.4,0.6) (0.5833,0.8,0.5)=(H,S,V)
Jorma Kekalainen
1235
Solution
Checking:
>> hsv=rgb2hsv([0.1000 0.3000 0.5000])
hsv =
0.5833 0.8000 0.5000
Checking other way:
>> rgb=hsv2rgb([0.5833,0.8,0.5])
rgb =
0.1000 0.3001 0.5000
Note:
>> rgb=hsv2rgb([7/12,0.8,0.5])
rgb =
0.1000 0.3000 0.5000
Jorma Kekalainen
1236
YIQ
This color space was originally used for TV/video in America
and other countries where NTSC was the video standard.
In this scheme Y is the luminance (this corresponds roughly
with intensity), and I and Q carry the color information
(chrominance).
The conversion between RGB is straightforward:
and
Jorma Kekalainen
Digital Image Processing
The
two conversion matrices are of course
inverses of each other.
1237
Exercise
Using Matlab show that the two previous
coefficient matrices are inverses of each other.
Jorma Kekalainen
1238
Solution
>> %[YIQ]=[C]*[RGB]
C=[0.299,0.587,0.114;0.596,-0.274,-0.322;0.211,-0.523,0.312]
C=
0.2990 0.5870 0.1140
0.5960 -0.2740 -0.3220
0.2110 -0.5230 0.3120
>> invC=inv(C)
invC =
1.0000 0.9562 0.6214
A=
1.0000 -0.2727 -0.6468
0.2990 0.5870 0.1140
1.0000 -1.1037 1.7006
0.5960 -0.2740 -0.3220
>> inv(invC)
0.2110 -0.5230 0.3120
ans =
>> inv(A)
0.2990 0.5870 0.1140
0.5960 -0.2740 -0.3220
ans =
0.2110 -0.5230 0.3120
1.0000 0.9562 0.6214
>>
Jorma Kekalainen
1239
Note
Because
The human visual system assigns more
intensity to the green component of an image
than to the red and blue components
Jorma Kekalainen
1240
RGB YIQ
Since YIQ is a linear transformation of RGB
and thus the conversions are linear, and hence
easy to do, makes YIQ a good choice for color
image processing.
Conversion between RGB and YIQ are
implemented with the Matlab functions
rgb2ntsc and ntsc2rgb.
Note:
Jorma Kekalainen
>> rgb2ntsc([1,1,1])
ans =
1 0 0
>> rgb2ntsc([0.5,0.5,0.5])
ans =
1241
0.5000
0
0
Note
Jorma Kekalainen
>> rgb2ntsc([1,1,1])
ans =
1 0 0
>> rgb2ntsc([0.5,0.5,0.5])
ans =
0.5000
0
0
>> ntsc2rgb([0.5,0.5,0.5])
ans =
1.0000 0.0314 0.6197
>> ntsc2rgb([1,1,1])
ans =
1.0000 0.0314 0.6197
>> ntsc2rgb([1,0,0])
ans =
1 1 1
>> ntsc2rgb([0.5,0,0])
ans =
0.5000 0.5000
0.5000
Digital Image Processing
1242
rgb2ntsc
Convert RGB color values to NTSC color space
Syntax:
yiqmap = rgb2ntsc(rgbmap)
YIQ = rgb2ntsc(RGB)
Description:
yiqmap = rgb2ntsc(rgbmap) converts the m-by-3 RGB
values in rgbmap to NTSC color space.
yiqmap is an m-by-3 matrix that contains the NTSC
luminance (Y) and chrominance (I and Q) color
components as columns that are equivalent to the colors
in the RGB colormap.
YIQ = rgb2ntsc(RGB) converts the truecolor image RGB to
the equivalent NTSC image YIQ.
Jorma Kekalainen
1243
rgb2ntsc
Class Support
RGB can be of class uint8, uint16, int16, single, or double.
rgbmap can be double.
The output is double.
Note:
In the NTSC color space, the luminance is the grayscale
signal used to display pictures on monochrome (black and
white) televisions.
The other components carry the hue and saturation
information.
rgb2ntsc defines the NTSC components using
Jorma Kekalainen
1244
ntsc2rgb
Convert NTSC values to RGB color space
Syntax:
rgbmap = ntsc2rgb(yiqmap)
RGB = ntsc2rgb(YIQ)
Description:
rgbmap = ntsc2rgb(yiqmap) converts the m-by-3 NTSC
(television) color values in yiqmap to RGB color space.
If yiqmap is m-by-3 and contains the NTSC luminance (Y) and
chrominance (I and Q) color components as columns, then
rgbmap is an m-by-3 matrix that contains the red, green, and
blue values equivalent to those colors.
Both rgbmap and yiqmap contain intensities in the range 0 to
1.0.
The intensity 0 corresponds to the absence of the component,
while the intensity 1.0 corresponds to full saturation of the
component.
Jorma Kekalainen
Digital Image Processing
1245
ntsc2rgb
RGB = ntsc2rgb(YIQ) converts the NTSC image YIQ
to the equivalent truecolor image RGB.
ntsc2rgb computes the RGB values from the NTSC
components using
Class Support:
The input image or colormap must be of class double.
The output is of class double.
Jorma Kekalainen
1246
Examples
Jorma Kekalainen
1247
Example
Jorma Kekalainen
1248
1249
Jorma Kekalainen
1250
Jorma Kekalainen
1251
Jorma Kekalainen
1252
Red component
Jorma Kekalainen
Green component
Blue component
1253
RGB HSV
We can convert RGB to HSV and view the
components again:
fh=rgb2hsv(f);
figure, imshow(fh)
figure,imshow(fh(:,:,1))
figure,imshow(fh(:,:,2))
figure,imshow(fh(:,:,3))
Jorma Kekalainen
1254
RGB HSV
Hue
Saturation
Jorma Kekalainen
Value
1255
RGB YIQ
We can do precisely the same thing for the
YIQ color space:
fn=rgb2ntsc(f);
figure,imshow(fn)
figure,imshow(fn(:,:,1))
figure,imshow(fn(:,:,2))
figure,imshow(fn(:,:,3))
Jorma Kekalainen
1256
YIQ components
Jorma Kekalainen
1257
Note
The National Television Systems Committee (NTSC) has
defined a color space known as YIQ.
One of the main advantages of this format is that
grayscale information is separated from color data, so
the same signal can be used for both color and black
and white sets.
In the NTSC color space, image data consists of three
components: luminance (Y), hue (I), and saturation
(Q).
The first component, luminance, represents grayscale
information, while the last two components make
up chrominance (color information).
Jorma Kekalainen
1258
Note
The function rgb2ntsc converts colormaps of RGB images to the
NTSC color space and ntsc2rgb performs the reverse operation.
For example, these commands convert an RGB image to NTSC
format.
RGB = imread('peppers.png');
YIQ = rgb2ntsc(RGB);
Example
RGB = imread('peppers.png');
figure,imshow(RGB)
title('RGB')
YIQ = rgb2ntsc(RGB);
figure,imshow(YIQ)
title('YIQ')
I = YIQ(:,:,1);
figure,imshow(I)
title('Y component')
figure,imshow(rgb2gray(RGB))
title('rgb2gray(RGB)')
Jorma Kekalainen
1260
Example
Jorma Kekalainen
1261
1263
Pseudocoloring
This means assigning colors to a gray-scale
image in order to make certain aspects of the
image more open to visual interpretation, e.g.,
for medical images.
There are different methods of
pseudocoloring:
Intensity slicing
Gray - color transformations
Jorma Kekalainen
1265
Intensity slicing
In this method, we break up the image into various gray level
ranges.
We simply assign a different color to each range.
For example:
We can consider this as a mapping
Jorma Kekalainen
1266
1267
Example
The gray level x in the diagram is mapped onto
red, green and blue values of 0.375, 0.25, 0.75
Jorma Kekalainen
1268
Colormaps
In Matlab, a simple way to view an image with
added color is to use imshow with an extra
colormap parameter.
We can add a colormap with the colormap
function; there are several existing colormaps to
choose from.
The available colormaps (and also other
information concerning 3d graphics) are listed in
the help file for graph3d
>> help graph3d
Jorma Kekalainen
1269
Color maps
Jorma Kekalainen
1270
Jorma Kekalainen
1271
% Pseudocoloring
[x,map]=imread('spine.tif');
b=uint8(ind2gray(x,map));
size(b)
figure, imshow(b)
title('Original grayscale image')
figure,imshow(b,colormap(jet(256)))
title('Pseudocoloring with ''colormap(jet(256))''')
% Reducing the number of grayscales in the image to 16
b16=grayslice(b,16);
figure,imshow(b16,colormap(vga))
title('Pseudocoloring with reduced to 16 tones using vga colormap')
mycolormap=[0 0 1;1 0 1;0 1 0;1 0 0]; % Creating own colormap
% Scaling the image down so that there are only the
% four grayscales 0, 1, 2 and 3
b4=grayslice(b,4);
figure,imshow(b4,mycolormap)
title('Pseudocolored image using homemade map consisting only 4 tones')
Jorma Kekalainen
1272
Jorma Kekalainen
1273
Jorma Kekalainen
1274
1275
Jorma Kekalainen
1276
Example
[x,map]=imread('palikat.png');
figure,imshow(x,map)
title('Original image')
b=uint8(ind2gray(x,map));
figure,imshow(b)
title('Image converted grayscale')
figure, imshow(b,colormap(jet(256)))
title('Image colormapped ''colormap(jet(256))''')
b16=grayslice(b,16);
figure,imshow(b16,colormap(vga))
title('Image colormapped ''colormap(vga)''')
mycolormap=[0 0 1;1 0 1;0 1 0;1 0 0];
b4=grayslice(b,4);
figure,imshow(b4,mycolormap)
title('Image colormapped ''mycolormap''')
Jorma Kekalainen
1277
Jorma Kekalainen
1278
imfinfo('palikat.png')
>> imfinfo('palikat.png')
ans =
Filename: 'C:\Users\ktkekjo\Documents\MATLAB\palikat.png'
FileSize: 39932
Format: 'png'
FormatVersion: []
Width: 307
Height: 308
BitDepth: 8
ColorType: 'indexed'
Histogram: []
Jorma Kekalainen
1279
1281
Jorma Kekalainen
1282
Example
Filtering a three-dimensional image with a twodimensional filter is equivalent to filtering each plane of the
three-dimensional image individually with the same twodimensional filter.
%Read truecolor image.
rgb = imread('peppers.png');
figure,imshow(rgb);
%Create a filter.
h = ones(7,7)/49
%Filter the image using imfilter and display it.
rgb2 = imfilter(rgb,h);
figure, imshow(rgb2)
Jorma Kekalainen
1283
Example
h = ones(7,7)/49
h=
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
0.0204 0.0204
Original image
Jorma Kekalainen
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
h = ones(7,7)/49
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
0.0204
h = ones(30,30)/900
1284
Contrast enhancement
Contrast enhancement is best done by processing
the intensity component.
Suppose we start with the image forest.tif, which
is an indexed color image, and convert it to a
truecolour (RGB) image.
[x,map]=imread('forest.tif');
c=ind2rgb(x,map);
1285
RGB YIQ
Jorma Kekalainen
1286
Jorma Kekalainen
Jorma Kekalainen
1288
imfinfo('forest.tif')
>> imfinfo('forest.tif')
ans =
FileSize: 124888
Format: 'tif'
FormatVersion: []
Width: 447
Height: 301
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
SamplesPerPixel: 1
RowsPerStrip: 18
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Jorma Kekalainen Colormap: [256x3 double]
Digital Image Processing
1289
Example
But suppose we try to apply histogram equalization to each of
the RGB components:
cr=histeq(c(:,:,1));
cg=histeq(c(:,:,2));
cb=histeq(c(:,:,3));
Jorma Kekalainen
1291
Comparison
Jorma Kekalainen
1292
Jorma Kekalainen
1293
Example
Original RGB image
Jorma Kekalainen
1294
Spatial filtering
It depends on the filter as to which schema we use.
For a low pass filter, say a blurring filter, we can apply the
filter to each RGB component:
Low pass filtering
a15=fspecial('average',15);
cr=filter2(a15,c(:,:,1));
cg=filter2(a15,c(:,:,2));
cb=filter2(a15,c(:,:,3));
blur=cat(3,cr,cg,cb);
figure,imshow(blur)
The result is shown beside.
Jorma Kekalainen
1295
Example
Jorma Kekalainen
1296
cn=rgb2ntsc(c);
a=fspecial('unsharp');
cn(:,:,1)=filter2(a,cn(:,:,1));
cu=ntsc2rgb(cn);
imshow(cu)
1297
Jorma Kekalainen
1298
Jorma Kekalainen
1299
Example
Jorma Kekalainen
1300
Jorma Kekalainen
1301
1302
Noise reduction
Now we use the image pears.png:
p=imread('pears.png');
We can add noise, and look at the noisy image, and its RGB
components:
pn=imnoise(p,'salt & pepper');
%About 5% of pixel corrupted (default value)
figure,imshow(pn)
figure,imshow(pn(:,:,1))
figure,imshow(pn(:,:,2))
figure,imshow(pn(:,:,3))
Jorma Kekalainen
1303
Jorma Kekalainen
1304
Red
component
Green
component
Blue
component
Jorma Kekalainen
1305
1306
Denoising Y only
Now we apply the median filter to the intensity component
only and we remove the noise from luminance (Y) :
pnn=rgb2ntsc(pn);
pnn(:,:,1)=medfilt2(pnn(:,:,1));
pm2=ntsc2rgb(pnn);
figure,imshow(pm2)
1307
Jorma Kekalainen
1308
p=imread('pears.png');
figure, imshow(tw)
title('Original image')
% Now we add noise, and look at the noisy image, and its RGB components
pn=imnoise(tw,'salt & pepper');
figure,imshow(pn)
title('Noisy image')
figure,imshow(pn(:,:,1))
title('Red component')
figure,imshow(pn(:,:,2))
title('Green component')
figure,imshow(pn(:,:,3))
title('Blue component')
% Applying median filtering to each of the RGB components
prm=medfilt2(pn(:,:,1));
pgm=medfilt2(pn(:,:,2));
pbm=medfilt2(pn(:,:,3));
pm=cat(3,prm,pgm,pbm);
figure,imshow(pm)
title('Median filtered each of the RGB components')
% If we remove the noise from Y only
pnn=rgb2ntsc(pn);
pnn(:,:,1)=medfilt2(pnn(:,:,1));
pm2=ntsc2rgb(pnn);
figure,imshow(pm2)
title('Median filtered luminance (Y) component only')
Jorma Kekalainen
1309
Heavy noise
Jorma Kekalainen
1310
Jorma Kekalainen
1311
p=imread('pears.png');
figure, imshow(p)
title('Original image')
figure,imshow(tn)
figure,imshow(pn(:,:,1))
title('Red component')
figure,imshow(pn(:,:,2))
title('Green component')
figure,imshow(pn(:,:,3))
title('Blue component')
prm=medfilt2(pn(:,:,1));
pgm=medfilt2(pn(:,:,2));
pbm=medfilt2(pn(:,:,3));
pm=cat(3,prm,pgm,pbm);
figure,imshow(pm)
pnn=rgb2ntsc(pn);
pnn(:,:,1)=medfilt2(pnn(:,:,1));
pm2=ntsc2rgb(pnn);
Jormafigure,imshow(pm2)
Kekalainen
Digital Image Processing
1312
Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all components back into
a single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
Jorma Kekalainen
Digital Image Processing
1313
xlabel('Image with 20% of its pixels
corrupted by binary noise')
Note
f=imread('flowers.tif');
figure,imshow(f)
title('Original image ''flowers.tif''')
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of pixel corrupted by noise
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by binary noise')
a3=fspecial('average');
fsp20a3r=filter2(a3,fsp20(:,:,1));%Red component
fsp20a3g=filter2(a3,fsp20(:,:,2));
fsp20a3b=filter2(a3,fsp20(:,:,3));
fsp20a3=cat(3,fsp20a3r, fsp20a3g, fsp20a3b);% all components back into a
single 3D array for use with imshow
Jorma Kekalainen
1314
Note
figure,imshow(uint8(fsp20a3))
title('Image filtered by 3*3 average filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
%
fsp20m3r=medfilt2(fsp20(:,:,1));% Red component
fsp20m3g=medfilt2(fsp20(:,:,2));
fsp20m3b=medfilt2(fsp20(:,:,1));
fsp20m3=cat(3,fsp20m3r, fsp20m3g, fsp20m3b);% all
components back into a single 3D array for use with imshow
figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')
Jorma Kekalainen
1315
Note
Jorma Kekalainen
1316
Note
Jorma Kekalainen
1317
Edge detection
An edge image will be a binary image
containing the edges of the input.
We can go about obtaining an edge image in
two ways:
1. we can take the intensity component only, and
apply the edge function to it,
2. we can apply the edge function to each of the
RGB components, and join the results.
Jorma Kekalainen
1318
fg=rgb2gray(f);
fe1=edge(fg);
figure,imshow(fe1)
Jorma Kekalainen
1319
1320
Example
Jorma Kekalainen
1321
Comment
The edge image fe2 is a
much more complete
edge image than fe1.
Jorma Kekalainen
1322
Example
Jorma Kekalainen
1323
Jorma Kekalainen
1324
Exam Up to This
Effective Image File
Compression Methods