You are on page 1of 1325

Digital Image Processing

Jorma Kekalainen

Contents

Introduction to images and image processing


Electromagnetic radiation and image formation
Image acquisition and sensing
Use of Matlab
Images and Matlab
Image display
Point operations and histograms
Spatial filtering
Noises and noise cleaning
Differences and edges
Fourier transforms and frequency filtering
Color models and processing

Jorma Kekalainen

Digital Image Processing

Literature and sources


An Introduction to Digital Image Processing
with Matlab; Notes for Image Processing by
Alasdair McAndrew
Digital Image Processing by Gonzalez & Woods
Image processing course material in Internet
Matlab virtual manuals

Jorma Kekalainen

Digital Image Processing

Digital Image Processing


Introduction to Image
and Image Processing

Image and picture


For our purposes, an image is a single picture
which represents something.
It may be a picture of a person, of people or
animals, or of an outdoor scene, or a
microphotograph of an electronic component,
or the result of medical imaging.
Even if the picture is not immediately
recognizable, it will not be just a random blur.
Jorma Kekalainen

Digital Image Processing

What is image processing?


Image processing involves changing the nature of an image
in order to either
1) improve its pictorial information for human interpretation,
2) render it more suitable for autonomous machine perception.

These two aspects represent two separate but equally


important aspects of image processing.
A procedure which satisfies the first condition - a
procedure which makes an image look better - may be the
very worst procedure for satisfying the second condition.
Humans like their images to be sharp, clear and detailed;
machines prefer their images to be simple and uncluttered.

Jorma Kekalainen

Digital Image Processing

Improving pictorial information


for human interpretation
Enhancing the edges of an image to make it
appear sharper.
Sharpening edges is a vital component of printing.
In order for an image to appear at its best on the
printed page some sharpening is usually
performed.

Removing noise from an image.


Noise being random errors in the image.

Removing motion blur from an image.


Jorma Kekalainen

Digital Image Processing

Image sharpening
Original image

Jorma Kekalainen

Enhanced original

Digital Image Processing

Processed images

Jorma Kekalainen

Digital Image Processing

Image sharpening

Jorma Kekalainen

Digital Image Processing

10

Thumb print

Jorma Kekalainen

Digital Image Processing

11

Removing motion blur


Original, blurry image

Jorma Kekalainen

Enhanced image

Digital Image Processing

12

Removing motion blur from an


image
Blurred image

Jorma Kekalainen

Restored image

Digital Image Processing

13

Image corrupted by noise


Noise reduction with a
X-ray image of circuit
3 x3 median filter.
board corrupted by saltand-pepper noise.

Jorma Kekalainen

Digital Image Processing

14

Image scrambling: Same information?!

Jorma Kekalainen

Digital Image Processing

15

Rendering images more suitable for


autonomous machine perception
Examples may include:
Obtaining the edges of an image.
This may be necessary for the measurement of objects in an
image.
Once we have the edges we can measure their spread, and the
area contained within them.
We can also use edge detection algorithms as a first step in edge
enhancement.

Removing details from an image.


For measurement or counting purposes, we may not be
interested in all the detail in an image.
For example, a machine inspected items on an assembly line,
the only matters of interest may be shape, size or color.
For such cases, we might want to simplify the image.
Jorma Kekalainen

Digital Image Processing

16

Images and edges

Jorma Kekalainen

Digital Image Processing

17

Automated visual inspection of


manufactured goods etc.

Jorma Kekalainen

Digital Image Processing

18

Imaging the creation of images


Examples of different sources
Electromagnetic radiation
Transmitted gamma-rays
Transmitted and reconstructed gamma-rays, Single
Photon Emission Tomography (SPECT)
Emitted and reconstructed gamma-rays, Positron
Emission Tomography (PET)
Transmitted and reconstructed X-rays, Computed
Tomography (CT)

Jorma Kekalainen

Digital Image Processing

19

Imaging the creation of images


Examples of different sources
Electromagnetic radiation
Excitation rays from ultraviolet light, fluorescence
microscopy
Reflected rays from visible light
Reflected infrared rays
Emitted radio waves + magnetic fields and
reconstruction => magnetic resonance imaging (MRI)

Ultrasound
Transmitted and reflected ultrasound
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Example of a digital image


pixel =
picture element

Zoom

Jorma Kekalainen

Original
image: 500x340
Digital Image Processing
pixels

23

Digital image: Formal definition


A digital image, I, is a mapping from a 2D grid of uniformly
spaced discrete points, {p = (r,c)}, into a set of positive integer
values, {I( p)}, or a set of vector values, e.g., {[R G B]T( p)}.
At each column location in each row of I there is a value.
The pair ( p, I( p) ) is called a pixel (for picture element).
p = (r,c) is the pixel location indexed by row, r, and column, c.
I( p) = I(r,c) is the value of the pixel at location p.
If I( p) is a single number then I is monochrome.
If I( p) is a vector (ordered list of numbers) then I has multiple
bands (e.g., a color image).

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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.

Digital image processing


We shall be concerned with digital image
processing, which involves using a computer
to change the nature of a digital image.
DFT of the small circle

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

29

Aspects of image processing


It is convenient to subdivide different image processing
algorithms into broad subclasses.
There are different algorithms for different tasks and
problems, and often we would like to distinguish the nature
of the task at hand.
Image enhancement refers to processing an image so that the
result is more suitable for a particular application.
Image restoration may be considered as reversing the damage
done to an image by a known cause.
Image analysis or segmentation involves subdividing an image
into principal parts, or isolating certain aspects of an image.

These classes are not disjoint.


A given algorithm may be used for both image enhancement or
for image restoration.
Jorma Kekalainen

Digital Image Processing

30

Example: Image enhancement


Image enhancement refers to processing an image so that
the result is more suitable for a particular application:

sharpening or de-blurring an out of focus image,


highlighting edges,
improving image contrast, or brightening an image,
removing noise

Image enhancement techniques bring out the detail in an


image that is obscured or highlight certain features of
interest in an image.
Image enhancement techniques, such as contrast
adjustment and filtering, typically return a modified version
of the original image.
These techniques are frequently used as a preprocessing step to
improve the results of image analysis.
Jorma Kekalainen

Digital Image Processing

31

Example: Image restoration


Image restoration may be considered as
reversing the damage done to an image by a
known cause:
removing of blur caused by linear camera motion,
removal of optical distortions,
removing periodic interference.

Jorma Kekalainen

Digital Image Processing

32

Example: Image analysis or


segmentation
Image analysis is the process of extracting meaningful
information from images such as finding shapes, counting
objects, identifying colors, or measuring object properties.
Image segmentation is the process of partitioning an image
into parts or regions
finding lines, circles, or particular shapes in an image,
in an aerial photograph: identifying cars, trees, buildings, or
roads.

This division into parts is often based on the characteristics


of the pixels in the image.
E.g., one way to find regions in an image is to look for abrupt
discontinuities in pixel values, which typically indicate edges.
These edges can define regions.

Another method is to divide the image into regions based


on color values.
Jorma Kekalainen

Digital Image Processing

33

Types of digital images


We shall consider four basic types of images:
Binary
Grayscale
True color, or RGB
Indexed

Jorma Kekalainen

Digital Image Processing

34

Digital image
a grid of squares,
each of which
contains a single
color

each square is
called a pixel (for
picture element)

Color images have 3 values per pixel; monochrome


images have 1 value per pixel.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

36

Example: Binary image

Jorma Kekalainen

In this image, we have only the two


colors: black for the edges, and
white for the background or the
Digital Image Processing
37
other way round.

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

Digital Image Processing

38

Example: Grayscale image

C(150:170,210:235)

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

40

Primary and secondary colors of


light (Additive color mixing)
Here, secondary colors are mixtures of two primary colors.
yellow = red + green
cyan = green + blue
magenta = red + blue
CRT
LCD
plasma

Jorma Kekalainen

Digital Image Processing

41

Color images
Formation of a vector from corresponding pixel
values in three RGB component images

Jorma Kekalainen

Digital Image Processing

42

True color or RGB


Here each pixel has a particular color; that color being
described by the amount of red, green and blue in it.
If each of these components has a range 0 to 255, this
gives a total of 2563 =16777200 different possible colors in
the image.
This is enough colors for any image.
Since the total number of bits required for each pixel is 24
such images are also called 24-bit color images.
Such an image may be considered as consisting of a stack of
three matrices representing the red, green and blue values
for each pixel.
This means that for every pixel there correspond three
values.
Jorma Kekalainen

Digital Image Processing

43

Example: True color image

Red

Jorma Kekalainen

Green

Digital Image Processing

Blue

44

Example: Numerical values of


some discrete pixels

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

46

Example: Indexed image

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

Digital Image Processing

47

Example: Indexed image


In this image the indices are simply indices into the color map.
Color map
0.1451
0.7451
0.7412
0.2392
0.8706
0.8706

Jorma Kekalainen

Digital Image Processing

0.1176
0.2627
0.5451
0.2588
0.6941
0.8431

0.1412
0.2314
0.6392
0.4000
0.1490
0.8196

Without the color map,


this image would be
nearly black, because
here the maximum index
48
value is 5.

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

Digital Image Processing

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

Image file sizes


Image files tend to be large.
For example, suppose we consider a 512*512
binary image.
The number of bits used in this image
(assuming no compression, and neglecting any
header information) is
512*512*1 bit = 262 144 bits =32768 bytes
= 32.768 kB 0.033 MB
Note: Here we use the convention that a kilobyte is one thousand bytes, and a
Jorma Kekalainen
Digital Image Processing
megabyte
is one million bytes.

50

Image file sizes


A grayscale image of the same size requires:
512*512*1 byte = 262 144 bytes 262.14 kB
0.262MB
In color images each pixel is associated with
3 bytes of color information.
A 512*512 image thus requires
512*512*3 bytes = 786 432 bytes 786.43 kB
0.786 MB
Note: Many images are of course much larger than this. E.g. satellite images may be of
Jorma Kekalainen
Digital Image Processing
51
the order
of ten thousand pixels in each
direction.

Example: A picture is worth one


thousand words
Lets test the well-known proverb A picture is worth
one thousand words.
Assuming a word to contain 10 ASCII characters (on
average), and that each character requires 8 bits of
storage, then 1000 words contain
1000*10*8 = 80 000 bits of information
This is roughly equivalent to the information in a
283*283
binary image
100*100
grayscale image
58*58
RGB color image
Jorma Kekalainen

Digital Image Processing

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.

The combination and immense variability of


these steps influences the ways in we perceive
the world around us.
Jorma Kekalainen

Digital Image Processing

53

Eye works rather much like a


camera
Reflected light from
the object

Lens

Retina is
the sensor

Focal length

upside down

But the brain performs some more processing that


gives rise to visual phenomena
Jorma Kekalainen

Digital Image Processing

54

Color vision
The human eye has cones which are sensitive to
different wavelength bands

Jorma Kekalainen

Digital Image Processing

55

Absorption of light by
the cones in the human eye

Jorma Kekalainen

Digital Image Processing

56

Example: Visual phenomenon


Observed intensities vary as to the
background.
A single block of gray will appear darker if
placed on a white background than if it were
placed on a black background.
That is, we don't perceive gray scales as they
are, but rather as they differ from their
surroundings.
Jorma Kekalainen

Digital Image Processing

57

Example: Visual phenomenon


A gray square on different backgrounds

Notice how much darker the square appears when it is


surrounded by a light gray. However, the three central squares
Jormaexactly
Kekalainen
Digital Image Processing
58
have
the same intensity.

Example: Visual phenomenon


Gray scale
image:

Actual
intensity:

Perceived
intensity:
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

A newspaper
image 61

Image sampling and quantization

Continuous
image

Line A-B
from
image

After
sampling
Jorma Kekalainen

After
quantization
Digital Image Processing

62

Digital image acquisition

Typically camera
Jorma Kekalainen

Digital Image Processing

63

Digital image sensor


A continuous image projected
onto the sensor array

Result after sampling


and quantization

For a color
image, there
are 3 different
types of sensor
elements:
red, green and
blue

Sensor element

Jorma Kekalainen

Digital Image Processing

64

Grayscale color map (256 colors)


Pixel value f(x,y)

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

True color map

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.

Example: Different colormaps


>> x=magic(6)
x=
35 1 6
3 32 7
31 9 2
8 28 33
30 5 34
4 36 29
>> imagesc(x)

Jorma Kekalainen

26
21
22
17
12
13

19
23
27
10
14
18

Digital Image Processing

24
25
20
15
16
11

67

Electromagnetic spectrum and


colors

Jorma Kekalainen

Digital Image Processing

68

Electromagnetic spectrum and


colors
Note that the visible spectrum is a rather narrow
portion of the EM spectrum.

Jorma Kekalainen

Digital Image Processing

69

Digital Image Processing


Electromagnetic Radiation
and Image Formation

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

Digital Image Processing

71

Frequency and wavelength


The relation between frequency and
wavelength is
c=
c is the speed of light and depends on the
medium, c c0
c0 = speed of light in vacuum 3108 m/s

Jorma Kekalainen

Digital Image Processing

72

Particles and energy

Jorma Kekalainen

Digital Image Processing

73

Particles and energy

Jorma Kekalainen

Digital Image Processing

74

Spectrum
In practice, light consists of
photons with a range of energies, or
waves with a range of frequencies

This mix of frequencies/wavelengths/energies is


called the spectrum of the light.
The spectrum gives the total amount of energy
for each frequency/wavelength/energy.
Monochromatic light consists of only one
frequency/wavelength
Can be produced by special light sources, e.g., lasers
Jorma Kekalainen

Digital Image Processing

75

Spectrum

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

77

Color wavelength

Jorma Kekalainen

Digital Image Processing

78

Classification of EM spectrum

Jorma Kekalainen

Digital Image Processing

79

Polarization
The electromagnetic field has a direction
Perpendicular to the direction of motion

The polarization of the light is defined as the


direction of the electric field.
Natural light is a mix waves with polarization
in all possible directions: it is unpolarized
light.
Special light sources or filters can produce
polarized light of well-defined polarization.
Jorma Kekalainen

Digital Image Processing

80

Polarization
Plane polarization
The electric field varies only in a single plane

Jorma Kekalainen

Digital Image Processing

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

Conversely: plane polarized light can be


decomposed as a sum of two circular polarized
waves that rotate in opposite directions.
Jorma Kekalainen

Digital Image Processing

82

Coherence
The phase of the light waves can either be
random: incoherent light (natural light)
in a systematic relation: coherent light

Coherent light is related to monochromatic


light sources
Compare a red LED and a red laser
Both produce light within a narrow range
The LED light is incoherent
The laser light is coherent
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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.

Note: Radiant flux is one of radiometric quantities. Radiometry is a set of techniques


for measuring electromagnetic radiation, including visible light. Radiometric
Jorma Kekalainen
Image Processing
techniques
in optics characterize theDigital
distribution
of the radiation power in space.85

Radiant flux density


The radiant flux per unit area is the flux density
Since the flux depends on the frequency, so does the flux
density
Unit: Watt per square meter
As the energy propagates
through a area during a
Can be measured directly!
specific time interval

Irradiance is the radiant flux received by a surface per unit


area
Excitance or emittance (old term): flux density emitted from a
surface
Note: Irradiance is often called "intensity" in branches of physics other than
Jorma Kekalainen
Digital Image Processing
86
radiometry, but in radiometry this usage leads to confusion with radiant intensity.

Radiant intensity
For point sources, or distant sources of small
extent, the flux density can also be measured per
unit solid angle

The radiant intensity is the radiant flux per unit


solid angle
Unit: W/sr= Watt per steradian
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

88

Radiometric chain

Jorma Kekalainen

Digital Image Processing

89

Interaction between light and


matter
Most types of light-matter interactions can be
represented by
n = the materials refractive index
= the materials absorption coefficient
Both parameters depend on .
More complex interactions include
polarization effects or non-linear effects.

Jorma Kekalainen

Digital Image Processing

90

Light incident upon a surface


When light meets a surface
Some part of it is transmitted through the new
media
Possibly with another speed and direction

Some part of it is absorbed by the new media


Usually: the light energy is transformed to heat

Some part of it is reflected

All these effects are different for different


wavelengths!
Jorma Kekalainen

Digital Image Processing

91

Basic principle
Based on preservation of energy:
E0 = E1 + E2 + E3

Jorma Kekalainen

Digital Image Processing

92

Refraction
The light that is transmitted into the new
medium is refracted due to the change in light
speed
Snells law of refraction:

Snell's law states that the ratio of the sines of the


angles of incidence and refraction is equivalent to
the ratio of phase velocities in the two media, or
equivalent to the reciprocal of the ratio of the
Jorma Kekalainen
Digital Image Processing
indices 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

Digital Image Processing

94

Absorption
The absorption of light in matter depends on the
length that the light travels through the material

a = attenuation of the light (0 a 1)


= the materials absorption coefficient
x = length that the light travels in the material
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

96

Reflection
Highly dependent on the surface type

A real surface is often a mix between the two


cases
Jorma Kekalainen

Digital Image Processing

97

Emission
Almost independent of its interaction with
incident light:
Any object, even one that is not considered a light
source, emits electromagnetic radiation

Primarily in the IR-band, based on its


temperature.

Jorma Kekalainen

Digital Image Processing

98

Scattering
All mediums (except vacuum) scatter light
E.g., air, water, glass etc.

We can think of the medium as consisting of


small particles and with some probability they
reflect the light

In any possible direction


Different probability for different directions
Weak effect and roughly proportional to -4
In general, the probability depends also on the
distribution of particle sizes

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

100

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Image Formation

The history of image formation


The idea of a camera (= an imaging device) is
linked to how a human perceives the world
with her eyes.
But in the early days humans had only vague
or incorrect ideas about
What is light
How the eye maps objects in the 3D world to the
image that we perceive

Prior to the camera: the artist/painter

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

Digital Image Processing

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

Digital Image Processing

105

Pinhole camera model

Jorma Kekalainen

Digital Image Processing

106

Lenses vs. infinitesimal aperture


The pinhole camera model doesnt work in
practice since
If we make the aperture small, too little light
enters the camera
If we make the aperture larger, the image
becomes blurred

Solution: we replace the aperture with a lens


or a system of lenses
Jorma Kekalainen

Digital Image Processing

107

Thin lenses
The simplest model of a lens
Focuses all points in an object plane onto the
image plane

Jorma Kekalainen

Digital Image Processing

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)

For thin lenses the object plane can often be


approximated as a plane.
Jorma Kekalainen

Digital Image Processing

109

Focal length
The thin lens is characterized by a single parameter:
the focal length fL

To change a (distance to object plane), we need to


change b since fL is constant
a = for b = fL !

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

111

Diffraction limited systems

Jorma Kekalainen

Digital Image Processing

112

Diffraction limited systems


Each point along the aperture, at vertical position
x, acts as a wave source
In the image plane, at position x, each point
source contributes with a wave that has a phase
difference = 2 (x sin / ) relative the
position at the centre of the aperture
is the angle from point x to the aperture, and
assuming that x << x it follows that sin x / f
We get: 2 [xx /( f)]
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

114

Point spread function from a


single 3D point

Jorma Kekalainen

Digital Image Processing

115

Airy disk
The smallest resolvable distance in the image
plane, x, is given by

fL/D is the F-number of the lens or lens system


Jorma Kekalainen

Digital Image Processing

116

Point spread function (Airy disk)


Conclusions:
The image cannot have a better resolution than
x!
No need to measure the image with higher
resolution than x!
Image resolution is not defined by number of
pixels in the cameras with high pixel resolution
and high diffraction!

Jorma Kekalainen

Digital Image Processing

117

Point spread function


The point spread function or Airy disk is also
called blur disk or circle of confusion or
sometimes modulation transfer function (MTF)
In general the point spread function can be
related to several effects that make the image of
a point appear blurred
Diffraction
Lens imperfections
Imperfections in the position of the image plane

Often modeled as constant over the image


Can be variable for poor optical systems
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

119

4
Cos

law

In general, there is an attenuation of the


image towards the edges of the image,
approximately according to cos4
This effect can be compensated in a digital
camera

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

Digital Image Processing

121

Image formation

Jorma Kekalainen

Digital Image Processing

122

Image formation

Jorma Kekalainen

Digital Image Processing

123

Image formation

projection
through lens
image of object

Jorma Kekalainen

Digital Image Processing

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)

Each point in the scene has a reflectivity function.


r(x; y; z; ): reflectivity function

Light reflects from a point and the reflected light


is captured by an imaging device (= a camera).
c(x; y; z; ) = E(x; y; z; )r(x; y; z; ): reflected light.
Jorma Kekalainen

Digital Image Processing

125

Image formation

Camera(c(x; y; z; )) =
c=reflected light

Jorma Kekalainen

Digital Image Processing

126

Inside the Camera - Projection


Projection (P) from world coordinates (x; y; z)
to camera or image coordinates (x; y)
cp(x; y; ) = P(c(x; y; z; ))
Camera c(x; y; z; ) =

Jorma Kekalainen

Digital Image Processing

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.

Both types of projections can be represented via


mathematical formulas.
Ortographic projection is easier and is sometimes used as a
mathematical convenience
Jorma Kekalainen

Digital Image Processing

128

Perspective projection
Perspective projection: 1 = 2, l1 < l2 2 < 1.

Jorma Kekalainen

Digital Image Processing

129

Inside the camera - Sensitivity


Once we have the projection of the reflected light cp(x; y; )
the characteristics of the capture device take over.
V () is the sensitivity function of a capture device.
Each capture device has such a function which determines
how sensitive it is in capturing the range of wavelengths ()
present in cp(x; y; ).
The result is an image function which determines the
amount of reflected light that is captured at the camera
coordinates (x; y)
f(x; y) =cp(x; y; )V()d

Jorma Kekalainen

Digital Image Processing

130

Example: Sensitivity functions


Let us determine the image functions for the below sensitivity
functions V1() , V2() , V3() imaging the same scene:
1. This is the most realistic of the three. Sensitivity is
concentrated in a band around 0
f1(x; y) =cp(x; y; )V1()d
2. This is an unrealistic capture device which has sensitivity
only to a single wavelength 0 as determined by the delta
function. However there are devices that get close to such
selective behavior.
f2(x; y) =cp(x; y; )V2()d = cp(x; y; )(- 0)d
= cp(x; y; 0)
Jorma Kekalainen

Digital Image Processing

131

Example: Sensitivity functions


3. This is what happens if we take a picture without
taking the cap off the lens of our camera.
f3(x; y) =cp(x; y; )V3()d = cp(x; y; )0d=0

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

134

Image capturing

continuous colors
discrete locations

Jorma Kekalainen

discrete realvalued image


Digital Image Processing

135

Sampling and quantization

Sampled

Sampled and
quantized

Original real image


Quantized

Jorma Kekalainen

Digital Image Processing

136

Quantization

discrete color output

Continuous colors mapped to a finite, discrete


set of colors.

Jorma Kekalainen

continuous color input


Digital Image Processing

137

History of photography

1839: Daguerre develops the first practical method for photography


1839: Herschel invents glass negatives
1861: Maxwell demonstrates color photographs
1878: Muybridge demonstrates moving images
1887: Celluloid film is introduced
1888: Kodak markets its first easy-to-use camera
1891: Edison patents his kinetoscopic camera
1895: Lumire Bros. invent the cinmatographe
1925: Leica introduces the 35mm film format for still images
1936: Kodachrome color film
1948: Land invents the Polaroid camera
1957: First digitized image
1959: AGFA introduces the first automatic camera

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

139

Digital Image Processing


Image Acquisition
and Sensing

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

Digital Image Processing

141

Photo-sensing chain

Jorma Kekalainen

Digital Image Processing

142

Light interacts with matter


The main or wanted interaction in a
photodetector is absorption
a photon is converted to an electron/hole pair

Electrons bond to atoms with a certain energy Qq


Photon absorption occurs with a certain probability
when the photon energy
E = h Qg

When a photon is absorbed: the electron is


released from the atom and becomes free.
Leaves a hole, a missing electron, in the atom
The hole is free to move around.
Jorma Kekalainen

Digital Image Processing

143

Basic layers of an image sensor


transparent coating and
conductors, a few m
Space charge region, a few m.
Contains an electric field which
effectively removes electrons.
Is localized: only deals with
electrons in a specific area
Semiconductor bulk, this is
where the photon-matter
interaction is designed to
happen. 2-100 m
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

145

Quantum efficiency
All these effects are wavelength dependent
The mean number of electron/hole pairs
created per photon is the quantum efficiency

Quantum efficiency for a


particular photo device as
a function of wavelength
Typically: Effect 1+6 here
Typically: Effect 6 here

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

147

Intrinsic absorption
This is called intrinsic absorption
No doping of the semiconductor is needed

Can be made in large arrays


Can be silicon based
Have high quantum efficiency
Basic effect in CCD-arrays
Different types of materials can be used for < 15 m
(shorter than IR) which are sensitive to

Near IR
Visible light
UV
X-ray

Jorma Kekalainen

Digital Image Processing

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

This can be used in a photodiode to produce a


voltage
Solar cells
Image sensors in the range near IR - UV
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

150

Thermal noise
Ithermo is not a constant current, it is rather a
noise current of a mean given by the
expression

We have to treat it as a random signal added


onto the wanted signal Iphoto
Ithermo is a type of noise
Jorma Kekalainen

Digital Image Processing

151

Two main types of photo


detectors
Most image sensors are based on either of
two distinct types of photo detectors
The photodiode (the photovoltaic effect)
The MOS capacitance (intrinsic absorption)

Both can be manufactured using standard


semiconductor processes.
Both can be seen as a capacitor which is
discharged by means of Iphoto and Ithermo .
Jorma Kekalainen

Digital Image Processing

152

Photodiode
Electrons move from the
n+ region to fill holes in
the p-region

Holes from the p-region


move to the n+ region to
be filled with free electrons
The net result is that
A space-charge region
develops, depleted of free
electrons or holes.
The space-charge region is
an electric insulator.
An electric field is
established in the space
charge region (from n+ to p).

Jorma Kekalainen

Digital Image Processing

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.

The diode acts as an electric capacitor:


It becomes electrically charged.
Jorma Kekalainen

Digital Image Processing

154

Photodiode
Remove the voltage
the charge remains.

It acts as a charged electric capacitor

Jorma Kekalainen

Digital Image Processing

155

Photodiode
The light creates a free
electron/hole pair.

lightly p-doped = mainly intrinsic absorption


Jorma Kekalainen

Digital Image Processing

Due to the electric field:


The electron sweeps to the
n+ region and cancels a hole.
The hole sweeps to the pregion and cancels an
electron.
This builds up a negative
voltage across the junction.
The electric field reduces
linearly to the number of
absorbed photons.
This voltage difference can be
measured.
156

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

Digital Image Processing

157

Basic mode of photodiode


operation
1. Pre-charge to a specific voltage (few volts)
2. Let the photovoltaic effect discharge the diode
a specific time period (the exposure time)
3. The corresponding voltage difference is
proportional to the flux density incident to the
sensor area
4. Measure the voltage difference
5. Go to 1.
We need to measure voltage
Jorma Kekalainen

Digital Image Processing

158

MOS capacitor
The oxide layer is a
perfect insulator,
no current passes
through this layer.

Metal
Oxide

Semiconductor

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

161

Basic mode of MOS capacitor


operation
1. Apply a voltage across the capacitor
2. Photon absorption creates an electron deposit under
the oxide layer
3. Allow this deposit to accumulate over a certain time
period (the exposure time)
4. The corresponding electron charge is proportional to
the incident flux density of the sensor
5. Move the charge deposit to somewhere where it can
be measured (typically CCD transport)
6. Go to 1.
We need to measure electric charge
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

164

Micro-lenses
To overcome low fill factors, an array of microlenses in front of the sensor array can be used

At large incident angles, this spot may


miss the detector area
Jorma Kekalainen

Digital Image Processing

165

Pro and cons of micro-lenses


Micro-lenses enhance the fill factor
But
Due to the manufacturing process, the detector
area can often have an inhomogeneous sensitivity
When light is focused onto a smaller spot in the
sensor, the inhomogeneities become more
noticeable as measurement noise
At large incident angles, this spot may miss the
detector area
Jorma Kekalainen

Digital Image Processing

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

The transport problem:


The voltage/charge has to be transported out of the array and
measured outside
Often with a single measurement unit per sensor array

Two principles for solving the transport problem


The CCD array (MOS capacitor only)
Switches to a common signal/video line (photodiode or MOS
capacitor)
Regardless of whether the photo charge has been transported by a
CCD or a signal line, it needs to be converted to a voltage signal
Jorma Kekalainen

Digital Image Processing

Note: The fill factor is the percentage of the total area which is light sensitive.

167

CCD (Charge Coupled Device)


array
A chain of MOS capacitors where the voltages change in the
pattern shown below can move the charge
This transport can take place along an entire row/column of a
detector array
One pixel = 3 capacitors

Jorma Kekalainen

Digital Image Processing

168

Limiting factor of readout


A CCD array can have different ways of implementing
the readout of an entire image
Frame-transfer CCD
Interline-transfer CCD
Field-interline-transfer CCD

Limiting factors:
there is a maximal readout frequency from the entire array
this limits the readout speed from the individual pixel

the MOS-capacitors are sensitive to light exposure during


the transport

Charges should be moved to light insensitive areas as


quickly as possible
Jorma Kekalainen

Digital Image Processing

169

Example: Frame-transfer CCD


Active sensor area

Covered with an
opaque metal shield

Jorma Kekalainen

Digital Image Processing

170

Example: Frame-transfer CCD


Advantages
The whole of area A is light sensitive, fill factor can be
close to 1
Simple to manufacture

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

Digital Image Processing

171

CMOS camera (APS)


Developments from mid 1990s an onward have
led to an improved CMOS sensor called Active
Pixel Sensor (APS)
Basic idea:
Move the charge-to-voltage transistor in the amplifier
stage to the pixel (one per pixel):
Voltage readout instead of charge transport
The readout line becomes less sensitive to noise

With modern technology:


the extra transistors per pixel can be very small compared to
the rest of the pixel area devoted to light sensing
reasonable fill factor
Jorma Kekalainen

Digital Image Processing

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

Can implement a global shutter read-out


Jorma Kekalainen

Digital Image Processing

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

Flicker or 1/f noise


Inhomogeneities and impurities in the materials
produce low-frequency noise due to statistical
fluctuations in various parameters which control the
photon-to-voltage conversion

These two factors may vary both across the array


(spatially) and over time
Jorma Kekalainen

Digital Image Processing

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

Design noise effect: blooming, after-effects


Note: Both the photodiode and the MOS capacitor collect electric charge in a small
region. When this region becomes saturated, the charge spills over to neighboring
Jorma Kekalainen
175
elements.
This is called blooming Digital Image Processing

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

Digital Image Processing

176

SNR and dynamic range


V = The overall noise voltage measured at the output
V = the actual output voltage

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

Digital Image Processing

Typical values for CMOS


and CCD: DR 40-60 dB
177

Saturation and noise


Saturation is the highest
value beyond which all
intensity levels are clipped

Noise appears often as


a grainy texture

Jorma Kekalainen

Digital Image Processing

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)

Quantization noise is independent of method


If b bits are used to represent voltage up to Vmax:

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

Digital Image Processing

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

Digital Image Processing

3-bit flash ADC circuit

180

Successive approximation ADC

The values of the resulting digital output


Instead of counting up in binary
sequence, this successiveare determined successively, from MSB to
approximation register register
LSB
counts by trying all values of bits
starting with the MSB and finishing at
the LSB.
Throughout the count process, the
register monitors the comparators
output to see if the binary count is
less than or greater than the analog
signal input, adjusting the bit values
accordingly.
The advantage to this counting
strategy is much faster results: the
DAC output converges on the analog
signal input in much larger steps than
with the 0-to-full count sequence of a
regular counter.

Jorma Kekalainen

Digital Image Processing

181

Example: Image acquisition using


CCD camera
CCD camera is one of the means for getting a picture into a
computer.
CCD camera has, in place of the usual film, an array of
photosensors, whose output is proportional to the
intensity of light falling on them.
For a camera attached to a computer, information from the
photosensors is then output to a suitable storage medium.
Generally this is done on hardware, as being much faster
and more efficient than software, using a frame-grabbing
card.
This allows a large number of images to be captured in a
very short time.

Jorma Kekalainen

Digital Image Processing

182

Example: Image acquisition using


flat bed scanner
Flat bed scanner works on a principle similar to
the CCD camera.
Instead of the entire image being captured at
once on a large array, a single row of
photosensors is moved across the image,
capturing it row-by-row as it moves.
Since this is a much slower process than taking a
picture with a camera, it is quite reasonable to
allow all capture and storage to be processed by
suitable software.
Jorma Kekalainen

Digital Image Processing

183

Image acquisition
Single imaging sensor

Line imaging sensor

Array imaging sensor

Jorma Kekalainen

Digital Image Processing

184

Image acquisition
Combining a single sensor with motion to
generate a 2-D image.

Jorma Kekalainen

Digital Image Processing

185

Color vision
The human eye has cones which are sensitive to
different wavelength bands

Jorma Kekalainen

Digital Image Processing

186

Three color channels


Grassmans law:
It is (in principle) sufficient to measure the light
spectrum in three distinct wavelength bands to
represent any perceivable color

We have not a synthetic sensor with sensitivity


curves identical to the human eye at our disposal
The three color channels are called red, green,
blue, even though they dont correspond to the
eyes curves
Jorma Kekalainen

Digital Image Processing

187

3 chip color cameras


sensor array for red light

sensor array for


blue light

sensor array for green light

Jorma Kekalainen

Three identical standard chips


Two semi-transparent mirrors that
refract different wavelengths
Digital Image Processing

188

Three chip color cameras


Based on standard black-and-white sensor
chips (three identical sensor chips)
The three sensor arrays need to be aligned
with tolerances smaller than the inter-pixel
distance
Gives good performance
is expensive
used in professional cameras
Jorma Kekalainen

Digital Image Processing

189

One chip color cameras


To reduce cost:
use one sensor array
place a color filter on top of each detector
element
each detector area is now sensitive to only a
specific wavelength range
reduces the fill factor for each range
the colors are not measured at the same places

Jorma Kekalainen

Digital Image Processing

190

One chip color cameras


Standard RGB-filters
Each color channel is rather narrow
blocks more photons less effective

Cyan-Yellow-Magenta (white) filters


Each color channel is wider
blocks fewer photons more effective
Post-processing needed to convert to RGB

The eye is more sensitive to green light and less


to blue light
It makes sense to have more green detectors and
fewer blue detectors
Jorma Kekalainen

Digital Image Processing

191

Stripe filters: Examples


Darker area is a cell that represents one pixel

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

193

One chip color camera


Most consumer cameras output only the
interpolated image which is typically
compressed using JPEG
In more advanced cameras, the raw
uninterpolated image can be read out from
the camera and processed externally by the
user
JPEG ( Joint Photographic Experts Group) is a commonly used method of lossy
compression for digital images. The degree of compression can be adjusted,
Jorma Kekalainen
Digital Image Processing
194
allowing
a selectable tradeoff between
storage size and image quality.

Example

A photo with the compression rate


decreasing, and hence quality
increasing, from left to right.

Ville The Cat


Jorma Kekalainen

Digital Image Processing

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

The color information may also be converted to some


other color space than RGB (e.g. HIS or XYZ)
Jorma Kekalainen

Digital Image Processing

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

Today, progressive scan (or non-interlaced) video is


becoming more and more common
Used in many modern video standards
Jorma Kekalainen

Digital Image Processing

197

Interlaced vs. progressive scan


Interlaced scan
E.g., one half image
at 50 Hz
one full image at 25 Hz

Progressive scan
E.g., one full image
at 25 Hz
Jorma Kekalainen

Digital Image Processing

198

Interlaced vs. progressive scan


Sometimes interlaced video (top) is represented as a
sequence of complete images, but where the even and odd
lines are taken at different time points (bottom)
De-interlacing can be made by interpolation both spatially
and over time
loss of spatial resolution

Jorma Kekalainen

Digital Image Processing

199

Modern consumer cameras


The effects described here relate to any type of light
measuring digital camera
Modern cameras (e.g., in mobile phones), however,
include increasingly more and more sophisticated
processing of the image and control of the camera

Automatic exposure time control


Automatic focus
Red-eye removal
Color balancing
Motion compensation

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

201

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Basics of Matlab

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

Digital Image Processing

204

Functions and commands


Rather than give a description of all of Matlab's capabilities, we
shall introduce functions, commands and techniques as required.
A Matlab function is a keyword which accepts various parameters,
and produces some sort of output: for example a matrix, a string, a
graph or figure.
Examples of such functions are sin, imread.
There are many ready made functions in Matlab, but it sometimes
necessary to write our own.
A command is a particular use of a function.
Examples of commands might be
>>sin(pi/3)
>>c=imread('cameraman.tif');

Jorma Kekalainen

Digital Image Processing

205

Matlab's standard data type


Matlab's standard data type is the matrix -- all
data are considered to be matrices of some sort.
Images, of course, are matrices or stack of
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

Digital Image Processing

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

Digital Image Processing

207

Command window ready for action

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

209

Current Folder

Jorma Kekalainen

Digital Image Processing

210

Command Window

Jorma Kekalainen

Digital Image Processing

211

Workspace

Jorma Kekalainen

Digital Image Processing

212

Command History

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

220

Working with desktop


As we work in MATLAB, we write commands that
create variables and call functions.
For example, create a variable named a by typing
this statement at the command line:
>>a = 1
MATLAB adds variable a to the workspace and
displays the result in the Command Window.
a=
1
Jorma Kekalainen

Digital Image Processing

221

Working with desktop

Create a few more variables:


>>b = 2
b=
2
>>c = a + b
c=
3
>>d = cos(a)
d=
0.5403
When you do not specify an output variable, MATLAB uses the variable ans, short
for answer, to store the results of your calculation.
>>sin(a)
ans =
0.8415

Jorma Kekalainen

Digital Image Processing

222

Working with desktop


If you end a statement with a semicolon, MATLAB
performs the computation, but suppresses the
display of output in the Command Window.
e = a*b;
You can recall previous commands by pressing
the up- and down-arrow keys, and .
Press the arrow keys either at an empty
command line or after you type the first few
characters of a command.
For example, to recall the command b = 2, type b,
and then press the up-arrow key.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

228

Example
>> whos
Name Size
ans
c

Jorma Kekalainen

1x1
1x4

Bytes Class

Attributes

16 double complex
64 double complex

Digital Image Processing

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

Digital Image Processing

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

0.7060 0.0462 0.6948 0.0344 0.7655


0.0318 0.0971 0.3171 0.4387 0.7952
0.2769 0.8235 0.9502 0.3816 0.1869

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

Digital Image Processing

231

Workspace pane
The variables also appear in the Workspace
pane on the desktop.

Jorma Kekalainen

Digital Image Processing

232

Saving and restoring data


Workspace variables do not persist after you exit MATLAB.
Save your data for later use with the save command,
save myfile.mat
Saving preserves the workspace in your current working
folder in a compressed file with a .mat extension, called a
MAT-file.
To clear all the variables from the workspace, use the clear
command.
Restore data from a MAT-file into the workspace using
load.
load myfile.mat

Jorma Kekalainen

Digital Image Processing

233

Digital Image Processing


Arrays and matrices in
Matlab

Matrices and arrays


Matlab is an abbreviation for Matrix laboratory."
While other programming languages mostly work
with numbers one at a time, Matlab is designed
to operate primarily on whole matrices and
arrays.
All Matlab variables are multidimensional arrays,
no matter what type of data.
A matrix is a two dimensional array often used for
linear algebra.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

b =1.0000 1.5000 2.0000 2.5000 3.0000


Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

244

Referring to multiple elements


To refer to multiple elements of an array, use the
colon operator, which allows you to specify a
range of the form start:end.
For example, list the elements in the first three
rows in the second column of M:
>>M(1:3,2)
ans =
M=
2
11
16 2 3 13 0
5 11 10 8 0
7
9 7 6 12 0
4 14 15
Jorma Kekalainen

Digital Image Processing

1 17
245

Referring to multiple elements


The colon alone, without start or end values,
specifies all of the elements in that dimension.
For example, select all the columns in the third
row of M:
M=

>>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

Digital Image Processing

0
246

Referring to multiple elements


The colon operator also allows you to create an
equally spaced vector of values using the more
general form start:step:end.
>>B = 0:10:100
B=
0 10 20 30 40 50 60 70 80 90 100
If you omit the middle step, as in start:end, Matlab
uses the default step value of 1
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

So the element a(2,3) can also be obtained as a(10):


>> a(10)
ans =
-3
Jorma Kekalainen

Digital Image Processing

250

Colon operator (:)


To obtain a row of values, or a block of values, we
use Matlab's colon operator (:).
This generates a vector of values; the command
a:b
where a and b are integers, lists all integers from a to
b.
The more general version of this command:
a:i:b
lists all values from a by increment i up to b.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

257

Example
As an example of matrix powers:
>> a^3*b^4
ans =

Inversion is performed using the inv function:


>> inv(a)
ans =

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

259

Geometric operations on matrices


As well as these standard arithmetic operations, Matlab
supports some geometric operations on matrices; flipud and
fliplr flip a matrix up/down and left/right respectively, and
rot90 rotates a matrix by 90 degrees:
>> flipud(a)
ans =

>> fliplr(a)
ans =
Jorma Kekalainen

Digital Image Processing

260

Geometric operations on matrices


>> rot90(a)
ans =

Jorma Kekalainen

90

Digital Image Processing

261

Matrix and array operations


Matlab allows to process all of the values in a matrix using a
single arithmetic operator or function.
>>A + 10
A= [1 2 3; 4 5 6; 7 8 10]
ans =
A=
11
12
13
1
2
14
15
16
4
5
7
8
17
18
20
>>sin(A)
ans =
0.8415
0.9093
0.1411
-0.7568
-0.9589
-0.2794
0.6570
0.9894
-0.5440
Jorma Kekalainen

Digital Image Processing

262

3
6
10

Matrix and array operations


To transpose a matrix, use a single quote ('):
>>A'
ans =
1
4
7
2
5
8
3
6
10
A=

Jorma Kekalainen

Digital Image Processing

1
4
7

2 3
5 6
263
8 10

Matrix and array operations


We can perform standard matrix multiplication,
which computes the inner products between
rows and columns, using the * operator.
For example, confirm that a matrix times its
inverse returns the identity matrix:
>>p = A*inv(A)
p=
1.0000
0
-0.0000
0
1.0000
0
0
0
1.0000
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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.

Example: Element-wise operations


To perform element-wise multiplication rather than matrix multiplication,
use the .* operator:
A=[1:3;4:6;7 8 10]
>>p = A.*A
p=
A=
1
4
9
16
25
36
1 2 3
49
64
100
4 5 6
The matrix operators for multiplication, division, and power each have7 a 8 10
corresponding array operator that operates element-wise.
For example, raise each element of a to the third power:
>>A.^3
ans =
1
8
27
64
125
216
A=
343
512
1000

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

0
0
0
276

Matrices of random numbers


Matrices of random numbers can be produced using
the rand and randn functions.
They differ in that the numbers produced by rand are
taken from a uniform distribution on the interval [0,1],
and those produced by randn are take from a normal
distribution with mean zero and standard deviation
one.
For creating matrices the syntax of each is the same as
the first three options of zeros above.
The rand and randn functions on their own produce
single numbers taken from the appropriate
distribution.
Jorma Kekalainen

Digital Image Processing

277

Random integer matrices


We can construct random integer matrices by multiplying the
results of rand or randn by an integer and then using the floor
function to take the integer part of the result:
>> floor(10*rand(3))
The floor function will be automatically
applied to every element in the matrix.
ans =
8 4 6
8 8 8
5 8 6
>> floor(100*randn(3,5))
ans =
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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.

Size of the matrix


The size of our matrix a can be obtained by
using the size function:
size(a)
which returns the number of rows and
columns of a.
>>size(A)
ans =
5
5
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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.

Digital Image Processing


2-D and 3-D Plots in
Matlab

Basic commands

Jorma Kekalainen

Digital Image Processing

290

Title and labels


Figure header can be added by using
command title
Axis explanations can be added by typing
xlabel, ylabel and zlabel
Line comments are added with command
legend
Construction lines are shown by typing grid on
Scale of the axes can be altered using
command axis
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

292

Example
x=[0:0.1:2*pi];
plot(x,sin(x))
and the result is shown
beside.

Jorma Kekalainen

A simple plot

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

295

Example
We can label the axes and add a title.
title('Plot of the Cosine Function')
xlabel('x')
ylabel('cos(x)')

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

301

Note
Explanation for the title:

Subscripts are added by using underline character


( _ ): For example, A0 is given in form A_0
Jorma Kekalainen

Digital Image Processing

302

Example
Basic command: plot

First parameter: values in the x-axis


Second parameter: values in the y-axis
Additional parameters to customize the line type
and colors.
t = (0:0.1:10)';
y = sin(2*pi*0.2*t);
figure
plot(t,y,'r+')

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

306

3-D plots
Then, create a surface plot.
surf(X,Y,Z)

Jorma Kekalainen

Both the surf function and its


companion mesh display surfaces in
three dimensions.
surf displays both the connecting
lines and the faces of the surface
in color.
mesh produces wireframe surfaces
that color only the lines connecting
the defining points.

Digital Image Processing

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

Digital Image Processing

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');

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.
Jorma Kekalainen

Digital Image Processing

309

Example: Subplots

Jorma Kekalainen

Digital Image Processing

310

Digital Image Processing


Character Strings in
Matlab

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

Digital Image Processing

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

>> whos otherText


Name
Size
Bytes Class Attributes
otherText
Jorma Kekalainen

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

Bytes Class Attributes


56 char
24 char
26 char
Digital Image Processing

314

Converting numeric values to


strings
To convert numeric values to strings, use functions, such as
num2str or int2str.
>> f = 69;
>>c = (f-32)/1.8;
>>tempText = ['Temperature is ',num2str(c), ' degrees
Celsius']
tempText =
Temperature is 20.5556 degrees Celsius

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

>> age = input('How old are you?');


How old are you?20

Digital Image Processing

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

Digital Image Processing


Programming and Scripts
in Matlab

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

Programming solves this problem.


Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

323

Creating a script
>> edit plotrand1

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

325

Saving and running script


Save the file in the current folder using save
button in Editor panel.
To run the script, type its name at the
command line:
plotrand1
You can also run scripts from the Editor by
pressing the Run button, .

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

329

Relational operators in Matlab

>> 3 < 4
ans =
1

Jorma Kekalainen

>> 3 > 4
ans =
0
Digital Image Processing

330

Example

These operators are different:


== is equal to
= is signed a value
Character strings cannot be compared using operator
==.
Use strcmp-function instead
>> 'cats' == 'dogs'
ans =
0 0 0 1
Jorma Kekalainen

>> strcmp('cats', 'dogs')


ans =
0
>> strcmp('cats', 'cats')
ans =
Digital Image Processing
1

331

Logical operators in Matlab

>> 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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

9 10
336

for-loop vs. while-loop


for-loop is used when it is exactly known a priori how
many repetitions are to be made
e.g., going through every row and column in a matrix
number of repetitions does not necessarily have to be a
constant for instance, the size of the matrix to go through
can change from one execution to another

while-loop is used when the number of repetitions is


unknown in the beginning of the loop
e.g., the user feeds an unknown amount of numbers

Jorma Kekalainen

Digital Image Processing

337

Example
How much is x?
>> a = 0;
while (2)
a = a + 1;
end
>> a
a=
304383259

Process was stopped violently by control C

Jorma Kekalainen

Digital Image Processing

338

break & continue


break-command exits the loop and program
execution continues from the line after the
loop.
continue-command forces the loop to start
immediately from the beginning.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Code is easily read when:


variables have descriptive names
indentation and empty space is used properly
it is well commented
Jorma Kekalainen

Digital Image Processing

341

Command string files


Matlab command string file (script, macro) is a text file
consisting of consecutive Matlab-function calls
(commands).
These files are of type *.m and they are called Matlab mfiles.
Matlab executes the commands when the name of the
command string file is written to the command line.
These files utilize the existing variables in the workspace
and store new variables created during execution to the
workspace.
These new variables can be used from the command line
after the script execution has stopped.
Jorma Kekalainen

Digital Image Processing

342

Loops and conditional statements


Within a script, you can loop over sections of code and
conditionally execute sections using the keywords for, end, while,
if, and switch.
For example, create a script named calcmean1.m that uses a for
loop to calculate the mean of five random samples and the overall
mean.
nsamples = 5;
npoints = 50;
for k = 1:nsamples
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData);
end
overallMean = mean(sampleMean)

Jorma Kekalainen

Digital Image Processing

343

Loops and conditional statements


Now, modify the for-loop so that you can view the results at
each iteration.
Display text in the Command Window that includes the
current iteration number, and remove the semicolon from
the assignment to sampleMean.
for k = 1:nsamples
iterationString = ['Iteration #',int2str(k)];
disp(iterationString)
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData)
end
overallMean = mean(sampleMean)
Jorma Kekalainen

Digital Image Processing

344

Example: Running script


>> calcmean1
Iteration #1
sampleMean =
0.5250
Iteration #2
sampleMean =
0.5250 0.5641
Iteration #3
sampleMean =
0.5250 0.5641 0.4332

Jorma Kekalainen

Digital Image Processing

345

Example: Running script

Iteration #4

sampleMean =

0.5250 0.5641 0.4332 0.4950

Iteration #5

sampleMean =

0.5250 0.5641 0.4332 0.4950 0.4250

overallMean =
0.4884

Jorma Kekalainen

Digital Image Processing

346

Example: Running the script


When you run the previous calcmean1 script, it displays the
intermediate results, and then calculates the overall mean.
calcmean
Iteration #1
sampleMean =
0.3988
Iteration #2
sampleMean =
0.3988
0.4950
Iteration #3
sampleMean =
0.3988
0.4950
0.5365
Jorma Kekalainen

Digital Image Processing

347

Example: Running the script


Iteration #4
sampleMean =
0.3988 0.4950 0.5365 0.4870
Iteration #5
sampleMean =
0.3988 0.4950 0.5365 0.4870 0.5501
overallMean =
0.4935

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

349

Example: Editing script


In the editor, add conditional statements to the end of
calcmean1.m that display a different message
depending on the value of overallMean.
if overallMean < .49
disp('Mean is less than expected')
elseif overallMean > .51
disp('Mean is greater than expected')
else
disp('Mean is within the expected range')
end
Jorma Kekalainen

Digital Image Processing

350

Example: Running the edited


script
Run calcmean1 and verify that the correct
message displays for the calculated
overallMean.
For example:
overallMean =
0.5178
Mean is greater than expected

Jorma Kekalainen

Digital Image Processing

351

Example: Running the script


>> calcmean1
Iteration #1
sampleMean =
0.4886 0.5641 0.4332 0.4950 0.4250
Iteration #2
sampleMean =
0.4886 0.4174 0.4332 0.4950 0.4250
Iteration #3
sampleMean =
0.4886 0.4174 0.4447 0.4950 0.4250

Jorma Kekalainen

Digital Image Processing

352

Example: Running the script


Iteration #4
sampleMean =
0.4886 0.4174 0.4447 0.4977 0.4250
Iteration #5
sampleMean =
0.4886 0.4174 0.4447 0.4977 0.5112
overallMean =
0.4719
Mean is less than expected
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

354

Digital Image Processing


Functions in Matlab

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

Digital Image Processing

356

Some built-in functions


Exponential functions
MATLAB function
sqrt

Description
square root

Example
sqrt(2)

exp

exponential

exp(1)

Log
Log2

(natural) logarithm (base e) log( exp(2) )


log2(16)
logarithm base 2

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

inverse sine (arcsine)

asin(1/2)

acos

inverse cosine (arccos)

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

361

User defined functions


Functions are used to make the code more easily readable
and to implement new features in Matlab
Differences between functions and command string files:
function can be given parameters which affect the execution of the
function
function can have one or more return values or the result of the
function.
every function has its own workspace created when the function is
called and destroyed when the execution of the function ends
functions cannot directly utilize variables declared in Matlabs
workspace or internal variables in other functions
internal variables of the function are lost if they are not explicitly
returned or saved
Jorma Kekalainen

Digital Image Processing

362

Syntax and description of


function

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

Digital Image Processing

363

Naming and saving the function


Save the function code in a text file with a .m extension.
The name of the file should match the name of the first
function in the file.
Valid function names begin with an alphabetic character,
and can contain letters, numbers, or underscores.
We can declare multiple local functions within the same
file, or nest functions.
If any function in a file contains a nested function, all
functions in the file must use the end keyword to indicate
the end of the function.
Otherwise, the end keyword is optional.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

365

Function m-file header


The first non-comment line of a function m-file
must be
function [Out1, Out2, ...] = function_name(In1, In2, ...)
where In1, In2, ... are input arguments (typically at least
one, but often several), and Out1, Out2, ... are output
arguments (again there are typically one or more output
arguments).

If there is only one output argument, then the


square bracket are not required.
The Matlab keyword function must be present.
Jorma Kekalainen

Digital Image Processing

366

Input and output arguments


When calling a function, the order of the input and output
arguments determines which variables correspond.
For example if a function is defined by
function [Ret, Vol, Div] = process(Prices, Dates)

and called by
[x, y, z] = process(A, B)

then the variable A in the calling program gives the value of


the variable Prices in the function, and the variable B will
give the values for the variable Dates within the function.
The variables may be scalars, vectors, matrices, strings or
more complicated structures.
Similarly, the value of Vol calculated within the function will
give the value of the variable y in the calling program.
Jorma Kekalainen

Digital Image Processing

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);

Function fact accepts a single input argument n, and


returns the factorial of n in output argument f.
The definition statement is the first executable line of
any function.
Each function definition includes these elements.
Jorma
Kekalainen definitions are not valid
Digitalat
Image
Processing
Note:
Function
the
command line or within a script.

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)

The body of a function can include valid Matlab expressions, control


flow statements, comments, blank lines, and nested functions.
Any variables that we create within a function are stored within a
workspace specific to that function, which is separate from the base
workspace.
Functions end with either an end statement, the end of the file, or
the definition line for another function, whichever comes first.
The end statement is required only when a function in the file
contains a nested function (a function completely contained within
its parent).

Jorma Kekalainen

Digital Image Processing

369

Save the file and call the


function
Save the file (in this example, fact.m), either in the
current folder or in a folder on the Matlab search path.
Matlab looks for programs in these specific locations.
From the command line, call the new fact function to
calculate, say 10!, using the same syntax rules that
apply to calling functions installed with Matlab:
>>x = 10;
>>y = fact(x);

The variables that you pass to the function do not need


to have the same names as the arguments in the
function definition line.
Jorma Kekalainen

Digital Image Processing

370

Function in Editor Window

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

373

Function calling from command


line
When the function is saved as avg_example.m, it can
be called from the command line in the usual manner.
>> numbers=1:10;
>> avg_example(numbers)
ans =
5.5000

>> numbers=(1:100);
>> avg_example(numbers)
ans =
50.5000

Functions can be given multiple parameters by listing


them all in parentheses after the function name
function weightedmean= weightedavg(x, w)

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

375

Function with multiple outputs


Define a function in a file named statistics.m that
returns the mean and standard deviation of an
input vector.
function [m,s] = statistics(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
Call the function from the command line.
values =1:10;
[ave,stdev] = statistics(values)
Jorma Kekalainen

Digital Image Processing

376

Example

Jorma Kekalainen

Digital Image Processing

377

Digital Image Processing


Helping documentation

How to get help?


Command line help
every function has its own instructions and we can also write
instructions for your own functions! (the commented rows after
function declaration)
used when the name of the function is known but the details on how
to use it (parameters, return values) are not known or have been
forgotten

help command
Documentation (HTML-help)
more in-depth instructions, more examples, hyperlinks to related
commands

doc command or press the question mark in the upper part of


the Matlabs main window or press F1
Jorma Kekalainen

Digital Image Processing

379

Help

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

381

More help on help


If there is too much information, it may scroll past
too fast to see.
In such case we can turn on the Matlab pager
with the command
>> more on
For more help on help, try:
>> help help
Better formatted help can be obtained with the
doc function
>> doc help
Jorma Kekalainen

Digital Image Processing

382

More about the doc function


You can find more about the doc function with any of
>> doc doc
>> help doc
If you want to find help on a particular topic, but don't
know the function to use, the lookfor function is extremely
helpful.
The command
lookfor topic
lists all commands for which the first line of the help text
contains the string topic.
For example
>> lookfor exponential
Jorma Kekalainen

Digital Image Processing

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.

Now we know that the function is implemented using exp.


>> lookfor exp

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

Digital Image Processing

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

Digital Image Processing

386

Exercise
Test and study all the previously presented
Matlab commands.

Jorma Kekalainen

Digital Image Processing

387

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Images in Matlab

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

392

Example
Create the image of a ramp (256*256):

using for loops.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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;

Digital Image Processing

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;

Digital Image Processing

396

Example
Create the image of a ramp (256*256):

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

398

Matlab function and command


A Matlab function is a keyword which accepts
various parameters, and produces some sort of
output, e.g., a matrix, a string, a graph or figure.
A command is a particular use of a function.
Examples of commands might be
>> sin(pi/3)
>> [c,emap]=imread('kids.tif');
>> figure,imshow(c)
>> figure,imshow(c,emap)
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

Note: PNG Portable Network Graphics is a graphics file format.

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Note: Since coins.png is an 8-bit grayscale


image, the pixel values appear as integers in the
Digital Image Processing
405
range 0-255.

RGB (truecolor) images


An RGB image, sometimes referred to as
a truecolor image, is stored as an m-by-n-by-3 data
array that defines red, green, and blue color
components for each individual pixel.
The color of each pixel is determined by the
combination of the red, green, and blue intensities
stored in each color plane at the pixel's location.
Graphics file formats store RGB images as 24-bit
images, where the red, green, and blue components
are 8 bits each.
This yields a potential of 16 million colors.
The precision with which a real-life image can be
replicated has led to the nickname "truecolor image."
Jorma Kekalainen

Digital Image Processing

406

RGB (truecolor) images


An RGB Matlab array can be of class double, uint8, or uint16.
In an RGB array of class double, each color component is a value
between 0 and 1.
A pixel whose color components are (0,0,0) is displayed as black,
and a pixel whose color components are (1,1,1) is displayed as
white.
The three color components for each pixel are stored along the
third dimension of the data array.
For example, the red, green, and blue color components of the pixel
(10,5) are stored in RGB(10,5,1), RGB(10,5,2), and RGB(10,5,3),
respectively.
To display the truecolor image RGB, we can use imshow or
image function. E.g.,
image(RGB)

Jorma Kekalainen

Digital Image Processing

407

Read a truecolor image into


Matlab
Given one command at a time

Jorma Kekalainen

Digital Image Processing

408

Read a truecolor image into


Matlab
Given the whole command
block at the same time

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

410

RGB (truecolor) images


To determine the color of
the pixel at (2,3), look at
the RGB triplet stored in
(2,3,1:3).
Suppose (2,3,1) contains
the value 0.5176, (2,3,2)
contains 0.1608, and
(2,3,3) contains 0.0627.
The color for the pixel at
(2,3) is

The next figure shows an RGB


image of class double

0.5176 0.1608 0.0627


Jorma Kekalainen

Digital Image Processing

411

True color or RGB images


Matlab handles 24-bit RGB images in much the same way as
grayscale.
We can save the color values to a matrix and view the result:
>> a=imread('autumn.tif');
>> figure,imshow(a)

Jorma Kekalainen

Digital Image Processing

412

True color or RGB images


Note that the pixel values now consist of a list of three values,
giving the red, green and blue components of the color of the
given pixel.
An important difference between this type of image and a
grayscale image can be seen by the command
>> size(a)
ans =
206 345 3
Now a is a three-dimensional matrix, also called a
multidimensional array.
We can think of a as being a stack of three matrices, each of
the same size.
Jorma Kekalainen

Digital Image Processing

413

RGB values at a given location


To obtain any of the RGB values at a given location, we use
indexing.
For example
>> a(100,200,2)
returns the second color value (green) at the pixel in row
100 and column 200.
If we want all the color values at that point, we can use
>> a(100,200,1:3)
However, Matlab allows a convenient shortcut for listing all
values along a particular dimension; just using a colon on
its own:
>> a(100,200,:)
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

417

R, G, & B bands of a truecolor image


displayed with grayscale colormaps
F=imread('flowers.tif');
figure, imshow(F)
title('F=imread(''flowers.tif'')')
R=F(:,:,1); G=F(:,:,2); B=F(:,:,3); % Component images
figure, imshow(R)
title('R=F(:,:,1)')
figure, imshow(G)
title('G=F(:,:,2)')
figure, imshow(B)
title('B=F(:,:,3)')
GrayF=0.2989*R+0.5870*G+0.1140*B;
figure, imshow(GrayF)
title('GrayF=.2989*R+0.5870*G+0.1140*B')
Grayim=rgb2gray(F);
figure, imshow(Grayim)
Jorma Kekalainen
Digital Image Processing
title('Grayim=rgb2gray(F)')

418

True color image compared to


grayscale image
F=imread('flowers.tif');
figure, imshow(F)
title('F=imread(''flowers.tif'')')
R=F(:,:,1); G=F(:,:,2); B=F(:,:,3); % Component images
GrayF=.2989*R+0.5870*G+0.1140*B; % or GrayF=rgb2gray(F);
figure, imshow(GrayF)
title('GrayF=.2989*R+0.5870*G+0.1140*B')

Jorma Kekalainen

Digital Image Processing

419

Component images with grayscale


colormaps and grayscale image
R

Gray
B

Jorma Kekalainen

Digital Image Processing

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)

Sometimes, the pixels are floating point values.


These values are transformed to the interval [0,255] and
further through a colormap in the computer.
A true color image has 3 values per pixel.
These values are transformed individually through 3 color
maps in the computer and further to the red, green and
blue channel of the screen.
Jorma Kekalainen

Digital Image Processing

421

True color map

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.

Read a truecolor image into Matlab

Jorma Kekalainen

Digital Image Processing

423

Crop the image


First, select a
region using
the magnifier.

Jorma Kekalainen

Digital Image Processing

424

Crop the image


First, select a
region using
the magnifier.

Jorma Kekalainen

Digital Image Processing

425

Crop the image

Jorma Kekalainen

Digital Image Processing

426

Crop the image


>> F=imread('flowers.tif');
>> class(F)
ans =
Uint8
>> size(F)
ans =
362 500 3
Here
Here it
is: it is:
>> figure
>> image(F)
>> title('flowers.tif')
>> xlabel('Introduction to Image Processing')
>> truesize
>> R=F(40:190,330:480,:);
>> figure
>> image(R)
>> truesize
>>
Jorma Kekalainen

Digital Image Processing

Now close the


other image

427

Example

Jorma Kekalainen

Digital Image Processing

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.

Structure of an indexed image


The pixels in the image are represented by integers, which are pointers
(indices) to color values stored in the colormap.
The relationship between the values in the image matrix and the
colormap depends on the class of the image matrix. If the image
matrix is of class double, the value 1 points to the first row in the
colormap, the value 2 points to the second row, and so on. If the
image matrix is of class uint8 or uint16, there is an offsetthe
value 0 points to the first row in the colormap, the value 1 points
to the second row, and so on.
The offset is also used in
graphics file formats to
maximize the number of
colors that can be supported.
In the following image, the
image matrix is of class
double. Because there is no
offset, the value 5 points to
Jorma Kekalainen
the fifth
row of the colormap.

Digital Image Processing

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

Digital Image Processing

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

This figure depicts an


intensity image of class
double.

Digital Image Processing

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.

These values map to the full range of values in the current


colormap.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

434

Example

In this example, the left image maps


to the gray colormap using the
statements
>> load clown
figure
subplot(1,2,1)
imagesc(X)
colormap(gray)
axis image
title('Default CLim (= [1 81])')

Jorma Kekalainen

The right image has values between


10 and 60 scaled to the full range of
the gray colormap using the
statements
>> subplot(1,2,2)
clims = [10 60];
imagesc(X,clims)
colormap(gray)
axis image
title('CLim = [10 60]')

Digital Image Processing

435

Expanding midrange of grayscale


resolution

Jorma Kekalainen

Digital Image Processing

436

Display intensity image


Although it is
conventional to display
intensity images using a
grayscale colormap, it is
possible to use other
colormaps.
For example, the
following statements
display the intensity
image I in shades of
blue and green:
imagesc(I,[10 60]); colormap(winter);
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

438

Example

>> load clown


figure
imagesc(X)
colormap(map)

Jorma Kekalainen

Digital Image Processing

439

Example

Jorma Kekalainen

Digital Image Processing

440

imfinfo('clown.bmp')
imfinfo('clown.bmp')
ans =

Filename: 'C:\Users\ktkekjo\Documents\MATLAB\clown.bmp'

FileModDate: '17-Aug-2015 20:31:42'

FileSize: 65078

Format: 'bmp'

FormatVersion: 'Version 3 (Microsoft Windows 3.x)'

Width: 320

Height: 200

BitDepth: 8

ColorType: 'indexed'

FormatSignature: 'BM'

NumColormapEntries: 256

Colormap: [256x3 double]

Jorma Kekalainen

Digital Image Processing

441

Indexed color images


The command
>> imshow('ancienttrees.tif')
produces a nice color image of trees.
If we try saving it to a matrix first and then
displaying the result:
>> AT=imread('ancienttrees.tif');
>> figure,imshow(AT),
we obtain a dark, barely distinguishable image,
with single integer gray values, indicating that AT
is being interpreted as a single grayscale image.
Jorma Kekalainen

Digital Image Processing

442

Example
>> imshow('ancienttrees.tif')

Jorma Kekalainen

>> AT=imread('ancienttrees.tif');
>> figure,imshow(AT)

Digital Image Processing

443

Example
>> whos('AT')
Name
Size
AT
258x350

Jorma Kekalainen

Bytes Class Attributes


90300 uint8

Digital Image Processing

444

Example

load trees
>> T=ind2gray(X,map);
>> imshow(X,map)
>> figure,imshow(T)

Jorma Kekalainen

Digital Image Processing

445

Note

load trees
T=ind2gray(X,map);
imshow(X,map)
figure,imshow(T)
figure,imagesc(X)
figure,imagesc(X),colormap(map)

Jorma Kekalainen

Digital Image Processing

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

PhotometricInterpretation: 'RGB Palette'


StripOffsets: [1x12 double]
SamplesPerPixel: 1
RowsPerStrip: 23
StripByteCounts: [1x12 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: [256x3 double]
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 73930

Digital Image Processing

447

Note
AT=imread('ancienttrees.tif');
imshow(AT)

Jorma Kekalainen

[AT,cmap]=imread('ancienttrees.tif');
figure,image(AT)
colormap(cmap)

Digital Image Processing

448

Colormap and index


In fact the image ancienttrees.tif is an example of
an indexed image, consisting of two matrices: a
color map, and an index to the color map.
Assigning the image to a single matrix picks up
only the index; we need to obtain the color map
as well:
>> [AT,map]=imread('ancienttrees.tif');
>> figure,imshow(AT,map)
Matlab stores the RGB values of an indexed
image as values of type double, with values
between 0 and 1.
Jorma Kekalainen

Digital Image Processing

449

Colormap and index


>> whos
Name
AT
map

Jorma Kekalainen

Size
258x350
256x3

Bytes
90300
6144

Digital Image Processing

Class Attributes
uint8
double

450

Truecolor vs. colormapped image


>> ind(100,100,:) =200
>> ind(98:102,98:102,:)
200 198 200 198 200
198 200 198 103 200
198 200 200 200 200
198 198 200 200 200
198 200 200 103 103

Jorma Kekalainen

emap(ind(100,100,:)+1,:)=emap(201,:) = 0.9373 0.3725 0.5843


>> emap(199:203,:)
0.9294 0.4824 0.6902
0.2471 0.2353 0.3882
0.9373 0.3725 0.5843
0.8549 0.8157 0.8118
0.4196 0.3137 0.3451

Digital Image Processing

>> round(255*emap(199:203,:))
237 123 176
63 60 99
239 95 149
218 208 207
107 80 88

451

Colormapped vs. Truecolor in


Matlab

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

Digital Image Processing

Intensity values
are between 0
and 1.

*255 = [ 239 95 149 ]

452

Example

Jorma Kekalainen

24-bit truecolor

Digital Image Processing

8-bit colormapped

453

Colormapped image, indices, and colormap


>> [AT,map] =imread('ancienttrees.tif');
Indices contained in
AT(98:102,98:102)
95 105 95 93 105
112 105 105 105 105
117 117 112 105 105
112 114 112 112 105
97 111 117 112 112

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

Digital Image Processing

181
206
206
214
181

222
239
156
222
222
454

Example: Converting a colormapped image


to true color
AT is a mxnx1, 8-bit
image.
Each pixel has a value
between 0 and 255.

map is the colormap that is stored in


ancienttrees.tif along with image. map is a 256x3
type-double matrix, each row of which lists a color
in terms of its R, G, and B intensities, which are
given as fractions between 0 and 1.

>> [AT,map] = imread('ancienttrees.tif');


>> ATtrue = uint8(reshape(map(AT+1,:),[size(AT) 3])*255);

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.

By concatenating ATs columns, Matlab rearranges


AT into a m x n x 1 list. Each number in the list
(if it has 1 added to it) refers to a row of the
colormap. Then, map(AT+1,:) produces a m x n x 3
matrix of intensity values of type double between
0 and 1.
Digital Image Processing

455

Example: Converting a colormapped image


to true color
>> [AT,map] = imread('ancienttrees.tif');
ATtrue = uint8(reshape(map(AT+1,:),[size(AT) 3])*255);
class(ATtrue)
size(ATtrue)
figure,imshow(ATtrue)
title('Converted to the true color image')
ans =
uint8
ans =
258 350 3
255*map(112:116,:)
214
165
206
181
247

181
206
206
214
181

222
239
156
222
222

Jorma Kekalainen

Digital Image Processing

456

How to make colormaps

Jorma Kekalainen

Digital Image Processing

457

Color bands of a truecolor image


displayed with grayscale colormaps

Jorma Kekalainen

Digital Image Processing

458

Color bands of a truecolor image


displayed with grayscale colormaps
>> I = imread('flowers.tif');

>> Red = I(:,:,1);


>> colormap(kcm);

>> Green = I(:,:,2);


>> colormap(kcm);

>> Blue = I(:,:,3);


>> colormap(kcm);

Jorma Kekalainen

Digital Image Processing

459

Color bands of a truecolor image


displayed with tinted colormaps

Jorma Kekalainen

Digital Image Processing

460

Color bands of a truecolor image


displayed with tinted colormaps
>> I = imread('flowers.tif');

>> Red = I(:,:,1);


>> colormap(rcm);

>> Green = I(:,:,2);


>> colormap(gcm);

>> Blue = I(:,:,3);


>> colormap(bcm);

Jorma Kekalainen

Digital Image Processing

461

Saving images as files


Assuming that
I contains the image
of the correct class,
that
cmap is a colormap,
and that
image_name is the
file-name that you
want.

Jorma Kekalainen

Digital Image Processing

462

Example

Jorma Kekalainen

Digital Image Processing

463

Example: Scramble an image


D = imread('Dee.jpg');
r = randperm(333);%?
c = randperm(500);%?
Scr = D(r,c,:);
figure
image(Scr)
truesize
title('Scrambled Image')
xlabel('What is it?')

What are these


constants?
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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')

The image can be unscrambled


usingJorma
the Kekalainen
row and column
permutation vectors, r and c.

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

469

Obtaining information about


graphics files
The imfinfo function enables you to obtain information about
graphics files in any of the standard formats.
The information we obtain depends on the type of file, but it always
includes at least the following:
Name of the file, including the folder path if the file is not in the
current folder
File format
Version number of the file format
File modification date
File size in bytes
Image width in pixels
Image height in pixels
Number of bits per pixel
Image type: RGB (truecolor), intensity (grayscale), or indexed

Jorma Kekalainen

Digital Image Processing

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

PhotometricInterpretation: 'RGB Palette'


StripOffsets: [1x12 double]
SamplesPerPixel: 1
RowsPerStrip: 23
StripByteCounts: [1x12 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: [256x3 double]
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 73930

Digital Image Processing

471

Information about our image


Much of those information is not useful to us; but we
can see the size of the image in pixels, the size of the
file (in bytes), the number of bits per pixel (this is
given by BitDepth), and the color type (in those case
indexed).
For comparison, let's look at the output of a true
color file (showing only the first few lines of the
output):
>>imfinfo('flowers.tif')
and binary file
>> imfinfo('text.png')
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Bytes Class Attributes


65536 logical

Digital Image Processing

475

Data types and conversions


Elements in Matlab matrices may have a
number of different numeric data types; a few
of them are listed in table below.
These data types are also functions, we can
convert from one type to another.

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

Digital Image Processing

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

Digital Image Processing

479

uint8 function - converts to 8-bit


unsigned integer
intArray = uint8(array)
converts the elements of an array into unsigned 8-bit (1-byte) integers of
class uint8.
Input argument array is array of any numeric class, such as single or
double.
If array is already of class uint8, the uint8 function has no effect.
Output argument intArray is array of class uint8.
Values range from 0 to 28 1.
The uint8 function maps any values in array that are outside the limit to
the nearest endpoint.
For example,
>> uint8(2^8)
ans =
255
Jorma Kekalainen

Digital Image Processing

480

Converting between different


image types
We can convert images from one image type to
another. Table lists Matlab's functions for converting
between different image types.
Function

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

Digital Image Processing

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

Digital Image Processing

482

Note: 4 color

rgb2ind

indsize = 362 500


% RGB to indexed [y,map]=rgb2ind(x)
emap =
[ind,emap]=rgb2ind(I,6);
0.1451 0.1176
0.7451 0.2627
figure,imshow(ind,emap), indsize=size(ind),emap
0.7412 0.5451
title('rgb2ind'), xlabel('Indexed image with 6 colors') 0.2392 0.2588
0.8706 0.6941
0.8706 0.8431

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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.

Unique colormap - cmunique


Eliminate duplicate colors in colormap; convert grayscale or truecolor
image to indexed image
Syntax:
[Y,newmap] = cmunique(X,map)
[Y,newmap] = cmunique(RGB)
[Y,newmap] = cmunique(I)

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

494

Exercise 1
Test and study all the previously presented
Matlab command sequences.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

497

Exercise 4

Repeat the previous question with some


(a) a binary image,
(b) an indexed color image,
(c) a true color image.

Jorma Kekalainen

Digital Image Processing

498

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Displaying Images

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

Digital Image Processing

501

Imshow function and grayscale


images
We have seen that if x is a matrix of type uint8, then
the command
imshow(x)
will display x as an image.
This is reasonable, since the data type uint8 restricts
values to be integers between 0 and 255.
However, not all image matrices come so nicely
bundled up into this data type, and lots of Matlab
image processing commands produces output
matrices which are of type double.
Jorma Kekalainen

Digital Image Processing

502

Imshow function and grayscale


images
We have two choices with a matrix of this double type:
1. convert to type uint8 and then display,
2. display the matrix directly.
The second option is possible because imshow will
display a matrix of type double as a grayscale image as
long as the matrix elements are between 0 and 1.
Suppose we take an image and convert it to type
double:
c=imread('cameraman.tif');
cd=double(c);
imshow(c),figure,imshow(cd)
The results are shown in the following figure.
Jorma Kekalainen

Digital Image Processing

503

Example
c=imread('cameraman.tif');figure,imshow(c)
cd=double(c);figure,imshow(cd)

(a) Original image

Jorma Kekalainen

(b) After conversion to type double


504

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

510

double vs. im2double


It is important to make the distinction between the two
functions double and im2double: double changes the
data type but does not change the numeric values;
im2double changes both the numeric data type and
the values.
The exception, of course, is if the original image is of
type double, in which case im2double does nothing.
Although the command double is not of much use for
direct image display, it can be very useful for image
arithmetic as we have seen with scaling examples.

Jorma Kekalainen

Digital Image Processing

511

uint8 vs. im2uint8


Corresponding to the functions double and
im2double are the functions uint8 and im2uint8.
If we take our image cd of type double, properly
scaled so that all elements are between 0 and 1,
we can convert it back to an image of type uint8
in two ways:
c2=uint8(255*cd);
c3=im2uint8(cd);
Use of im2uint8 is to be preferred; it takes other
data types as input, and always returns a correct
result.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

514

Displaying images using the


imshow function
The following example reads an image into the Matlab workspace and
then displays the image in a MATLAB figure window.
moon = imread('moon.tif');
imshow(moon);

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

Digital Image Processing

515

Images displayed

Jorma Kekalainen

Digital Image Processing

516

Specifying the initial image


magnification
By default, imshow attempts to display an image in its
entirety at 100% magnification (one screen pixel for each
image pixel).
However, if an image is too large to fit in a figure window
on the screen at 100% magnification, imshow scales the
image to fit onto the screen and issues a warning message.
To override the default initial magnification behavior for a
particular call to imshow, specify the InitialMagnification
parameter.
E.g., to view an image at 150% magnification, use this code
pout = imread('pout.tif');
imshow(pout, 'InitialMagnification', 150)
Note: imshow attempts to honor the magnification we specify. However, if the image does not
fit on the screen at the specified magnification, imshow scales the image to fit and issues a
warning message. We can also specify the text string 'fit' as the initial magnification value. In
Jorma Kekalainen
Image Processing
517
this case,
imshow scales the image to fitDigital
the current
size of the figure window.

Initial magnification

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

519

Controlling the appearance of the


figure
By default, when imshow displays an image in a figure, it
surrounds the image with a gray border.
We can change this default and suppress the border using
the 'border' parameter, as shown in the following example
imshow('moon.tif', 'border','tight')

The 'border' parameters affect only the image being


displayed in the call to imshow.
If we want all the images that we display using imshow to
appear without the gray border, we set the Image
Processing Toolbox 'ImshowBorder' preference to 'tight'.
We can also use preferences to include visible axes in the
figure. For more information about preferences, see
iptprefs.
Jorma Kekalainen

Digital Image Processing

520

Displaying suppressed border

Jorma Kekalainen

Digital Image Processing

521

Displaying each image in a


separate figure
The simplest way to display multiple images is to display them in
separate figure windows.
Matlab does not place any restrictions on the number of images
you can display simultaneously.
imshow always displays an image in the current figure.
If we display two images in succession, the second image replaces
the first image.
To view multiple figures with imshow, use the figure command to
explicitly create a new empty figure before calling imshow for the
next image.
For example, to view the first three frames in an array of truecolor
images F,

F=imread('peppers.png');
imshow(F(:,:,1))
figure, imshow(F(:,:,2))
figure, imshow(F(:,:,3))

Jorma Kekalainen

Digital Image Processing

522

Each image displayed separately

Jorma Kekalainen

Digital Image Processing

523

Displaying multiple images in the


same figure
We can use the imshow function with the Matlab
subplot function or the Matlab subimage function to
display multiple images in a single figure window.
subplot divides a figure into multiple display regions.
The syntax of subplot is subplot(m,n,p).
This syntax divides the figure into an m-by-n matrix of
display regions and makes the pth display region active.

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.

Displaying multiple images in the


same figure
E.g., we can use this syntax to display two images side by side

[X1,map1]=imread('forest.tif');
[X2,map2]=imread('trees.tif');
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)

subimage converts images to truecolor before displaying them and


therefore circumvents the colormap sharing problem.
This example uses subimage to display the forest and the trees
images with better results

[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

Digital Image Processing

525

Multiple images displayed in the same


figure

[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

Digital Image Processing

526

Note: Image sequence


Some applications work with collections of images related by time, such as
frames in a movie, or by spatial location, such as magnetic resonance
imaging (MRI) slices.
These collections of images are referred to as image sequences, image
stacks, or videos.
The ability to create N-dimensional arrays can provide a convenient way to
store image sequences.
E.g., an m-by-n-by-p array can store an array of p two-dimensional images,
such as grayscale or binary images, as shown in the following figure.
An m-by-n-by-3-by-p array can store truecolor images where each image is
made up of three planes.
Multidimensional array containing
an image sequence

Jorma Kekalainen

Digital Image Processing

527

Note: Image sequence


The imshow function can display one frame at a
time, using standard Matlab array indexing
syntax.
To animate an image sequence or provide
navigation within the sequence, use the Video
Viewer app (implay).
The Video Viewer app provides playback controls
that you can use to navigate among the frames in
the sequence.
To get a static view of all the frames in an image
sequence at one time, use the montage function.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

532

Example: The least significant bit plane


(= 0th bit plane)

c=imread('Hedyg-scale.tif');
cd=double(c);
c0=mod(cd,2);
imshow(c0)

Jorma Kekalainen

Digital Image Processing

533

Example: The

st
1

bit plane

c1=mod(floor(cd/2),2); figure, imshow(c1)

Jorma Kekalainen

Digital Image Processing

534

Example: The

nd
2

bit plane

c2=mod(floor(cd/4),2); figure, imshow(c2)

Jorma Kekalainen

Digital Image Processing

535

Example: The

rd
3

bit plane

c3=mod(floor(cd/8),2); figure, imshow(c3)

Jorma Kekalainen

Digital Image Processing

536

Example: The

th
4

bit plane

c4=mod(floor(cd/16),2); figure, imshow(c4)

Jorma Kekalainen

Digital Image Processing

537

Example: The

th
5

bit plane

c5=mod(floor(cd/32),2); figure, imshow(c5)

Jorma Kekalainen

Digital Image Processing

538

Example: The

th
6

bit plane

c6=mod(floor(cd/64),2); figure, imshow(c6)

Jorma Kekalainen

Digital Image Processing

539

Example: The most significant


(=7st) bit plane
c7=mod(floor(cd/128),2); figure, imshow(c7)

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

541

Original image

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

544

All commands used in the


previous example

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))

Digital Image Processing

545

Exercise
Transform the image cameraman.tif into a
sequence of binary images by breaking them
up into their bit-planes.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

547

Least significant bit planes

Jorma Kekalainen

Digital Image Processing

548

Most significant bit planes

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

553

Exercise

Using a matrix A=floor(255*rand(5)) study the effect of command series


A0=rem(A,2)
A1=rem(floor(A/2),2)
A2=rem(floor(A/4),2)
A3=rem(floor(A/8),2)
A4=rem(floor(A/16),2)
A5=rem(floor(A/32),2)
A6=rem(floor(A/64),2)
A7=rem(floor(A/128),2)
AA=2*(2*(2*(2*(2*(2*(2*A7+A6)+A5)+A4)+A3)+A2)+A1)+A0
Ath=A>127
Compare A with AA and A7 with Ath.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

556

Digital Image Processing


Spatial Resolution

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

560

Example: Spatial resolution


>> A=floor(255*rand(6))
A=

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

Example: Spatial resolution


>> imresize(imresize(A,1/2, 'nearest'),2,'nearest')
ans =

240
240
146
146
59
59

Jorma Kekalainen

240 165 165 236 236


240 165 165 236 236
146 139 139 124 124
146 139 139 124 124
59 189 189 113 113
59 189 189 113 113
Digital Image Processing

562

Example: Spatial resolution


>> imresize(imresize(A,1/4, 'nearest'),4,'nearest')
ans =

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

Digital Image Processing

113
113
113
113

563

Example: Spatial resolution


>> imresize(imresize(A,1/8, 'nearest'),8,'nearest')
ans =

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

Digital Image Processing

159
159
159
159
159
159
159
159

564

Example: Spatial resolution


>>
Aold=imresize_old(imresize_old(
A,1/2),2)

>>
Aold1=imresize_old(imresize_old(
A,1/4),4)

Aold =

Aold1 =

240
240
146
146
59
59

240 165 165 236 236


240 165 165 236 236
146 139 139 124 124
146 139 139 124 124
59 189 189 113 113
59 189 189 113 113

Jorma Kekalainen

3
3
3
3

Digital Image Processing

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

Digital Image Processing

566

Effective resolution
By changing the parameters of imresize, we
can change the effective resolution of the
image to smaller amounts:

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

569

Example: Increasing resolution of


an image
x6=imresize(imresize(x,1/64, 'nearest'),64,'nearest');
figure,imshow(x6)

Jorma Kekalainen

Digital Image Processing

570

Example: Increasing resolution of


an image
x5=imresize(imresize(x,1/32, 'nearest'),32,'nearest');
figure,imshow(x5)

Jorma Kekalainen

Digital Image Processing

571

Example: Increasing resolution of


an image
x4=imresize(imresize(x,1/16, 'nearest'),16,'nearest');
figure,imshow(x4)

Jorma Kekalainen

Digital Image Processing

572

Example: Increasing resolution of


an image
x3=imresize(imresize(x,1/8 ,'nearest'),8,'nearest');
figure,imshow(x3)

Jorma Kekalainen

Digital Image Processing

573

Example: Increasing resolution of


an image
x2=imresize(imresize(x,1/4, 'nearest'),4,'nearest');
figure,imshow(x2)

Jorma Kekalainen

Digital Image Processing

574

Example: Increasing resolution of


an image
x1=imresize(imresize(x,1/2 ,'nearest'),2,'nearest');
figure,imshow(x1)

Jorma Kekalainen

Digital Image Processing

575

Example: Original image


figure,imshow(x)

Jorma Kekalainen

Digital Image Processing

Deepika Bollywood star

576

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Point Operations

Image processing operations


Any image processing operation transforms the
gray values of the pixels.
However, image processing operations may be
divided into into three classes based on the
information required to perform the
transformation.
From the most complex to the simplest, they are:
transforms
spatial filters
point operations
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

580

Spatial filters and point


operations
Spatial filters
To change the gray level of a given pixel we
need only know the value of the gray levels in
a small neighborhood of pixels around the
given pixel.
Point operations
A pixel's gray value is changed without any
knowledge of its surrounds.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

We can see how these operations affect an image


by looking at the graph of old gray values against
new values.
The following figure shows the result of adding or
subtracting 128 from each pixel in the image.
Jorma Kekalainen

Digital Image Processing

584

Adding and subtracting a


constant

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

Digital Image Processing

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

Digital Image Processing

587

Adding or subtracting a constant


Now we can view the results:
>> imshow(c1),figure,imshow(c2)

c1: Adding 128

Jorma Kekalainen

Digital Image Processing

c2: Subtracting 128

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

Digital Image Processing

589

Dividing with a constant


c3=immultiply(c,0.5); or c3=imdivide(c,2);

c3: y=x/2
Jorma Kekalainen

Digital Image Processing

590

Multiplicating with a constant


c4=immultiply(c,2);

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

592

Effect of mixed operations


c5=imadd(immultiply(c,0.5),128); or
c5=imadd(imdivide(c,2),128);

c5: y=x/2+128
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

594

Binary conversion
BI=im2bw(Deepiga,0.5);% Binary conversion with threshold
>> imshow(BI)
>> figure, imshow(~BI)

Jorma Kekalainen

Digital Image Processing

595

Note: im2bw

Convert image to binary image, based on


threshold
Syntax

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

with the value 1 (white) and replaces all other


pixels with the value 0 (black).
Specify level in the range [0,1].
This range is relative to the signal levels
possible for the image's class.
Therefore, a level value of 0.5 is midway
between black and white, regardless of class.

Jorma Kekalainen

To compute the level argument, you


can use the function graythresh.
If you do not specify level, im2bw
uses the value 0.5.
BW = im2bw(X, map, level) converts
the indexed image X with colormap
map to a binary image.
BW = im2bw(RGB, level) converts the
truecolor image RGB to a binary
image.
If the input image is not a grayscale
image, im2bw converts the input
image to grayscale, and then converts
this grayscale image to binary by
thresholding.

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

598

Solarization

Complementing only dark pixels


Jorma Kekalainen

Complementing only light pixels

Digital Image Processing

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

Digital Image Processing

600

Different contrasted images


Very low contrast, low contrast and medium
contrast images

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

602

Image and its histogram


pout.tif

Jorma Kekalainen

Digital Image Processing

603

Histogram spreading out


Since the gray values are all clustered together
in the center of the histogram, we would
expect the image to be poorly contrasted.
Given a poorly contrasted image, we would
like to enhance its contrast, by spreading out
its histogram.
There are two ways of doing this:
histogram stretching (contrast stretching)
histogram equalization.
Jorma Kekalainen

Digital Image Processing

604

Histogram stretching (Contrast


stretching)
Suppose we have an image with the histogram shown in
figure below, associated with a table of the numbers ni of gray
values:
Gray level i
ni

A histogram of a poorly contrasted image

Jorma Kekalainen

Digital Image Processing

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:

The equation yields:

where i is the original gray


level and j its result after the
transformation.
Jorma Kekalainen
Digital Image Processing

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

Digital Image Processing

607

Corresponding stretched
histogram
A stretched histogram indicates an image
with greater contrast than the original.

Jorma Kekalainen

Digital Image Processing

A histogram of a poorly
contrasted image

608

Use of imadjust in stretching


To perform histogram
stretching in Matlab the
imadjust function may
be used.
In its simplest form, the
command
imadjust(im,[a,b],[c,d])
stretches the image
according to the
function shown in
figure.

The stretching function


given by imadjust

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.

Use of imadjust in stretching


Since imadjust is designed to work equally well on images of
type double, uint8 or uint16 the values of a, b, and d must be
between 0 and 1; the function automatically converts the
image (if needed) to be of type double.
If either of [a,b] or [c,d] are chosen to be [0,1], the
abbreviation [] may be used.
Thus, for example, the command
>> imadjust(im,[],[])
does nothing, and the command
>> imadjust(im,[],[1,0])
inverts the gray values of the image,
to produce a result similar
to a photographic negative.
Jorma Kekalainen
Digital Image Processing
610

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

Digital Image Processing

611

Example

Jorma Kekalainen

Digital Image Processing

612

Example: Adjusting an RGB image

RGB2 = imadjust(RGB1,[.2 .3 0; .6 .7 1],[]);


figure, imshow(RGB1)
title('Original image ''football.jpg''')
RGB1 = imread('football.jpg');
figure, imshow(RGB2)
title('Adjusted image')

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

614

Gamma not equal to 1


The imadjust function with gamma not equal
to 1

concave upward
concave downward

Jorma Kekalainen

Digital Image Processing

615

Example
The function used is a slight variation on the standard
line between two points:

Use of the gamma value alone can be enough to


substantially change the appearance of the image.
For example:
t=imread('tire.tif');
th=imadjust(t,[],[],0.5);% =0.5
imshow(t),figure,imshow(th)
produces the result shown in the following figure.
Jorma Kekalainen

Digital Image Processing

616

Example
The tire image and after adjustment with the
gamma value =0.5.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

618

Stretching function
Stretching function used in the previous tire image.
=0.5 (<1 concave downward)

Jorma Kekalainen

Digital Image Processing

619

Piecewise linear stretching function


We can write our own function to perform piecewise linear
stretching as shown in figure below.
To do this, we will make use of the find function, to find the
pixel values in the image between ai and ai +1.
Since the line between the coordinates (ai, bj)
and (ai+1, bj+1) has the equation

the heart of our function will be the lines


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);
where im is the input image and out is the output image.
Jorma Kekalainen

Digital Image Processing

620

Matlab function for piecewise


linear stretching function
A simple procedure which takes as inputs
images of type uint8 or double.
function out = histpwl(im,a,b)
% HISTPWL(IM,A,B) applies a piecewise linear
%transformation to the pixel values
% of image IM, where A and B are vectors
% containing the x and y coordinates of the
% ends of the line segments.
%IM can be of type UINT8 or DOUBLE, and
% the values in A and B must be between
% 0 and 1.
% For example:
% histpwl(x,[0,1],[1,0])
% simply inverts the pixel values.
%
Jorma Kekalainen

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

and this number is rounded to the nearest


integer.
Jorma Kekalainen

Digital Image Processing

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)

We would expect this image to be uniformly bright,


with a few dark dots on it.
To equalize this histogram, we form running totals of
the ni , and multiply each by 15/360=1/24:
Jorma Kekalainen

Digital Image Processing

625

Example
Histogram indicating poor contrast

Jorma Kekalainen

Digital Image Processing

626

Example

To equalize this
histogram, we
form running
totals of the ni ,
and multiply each
by 15/360=1/24.

Jorma Kekalainen

Digital Image Processing

627

Example
We now have the following transformation of gray
values, obtained by reading off the first and last
columns in the previous table:

The histogram of the j values is shown in the


following figure.
This is far more spread out than the original
histogram, and so the resulting image should exhibit
greater contrast.
Jorma Kekalainen

Digital Image Processing

628

Example
The histogram before
equalization

Jorma Kekalainen

The histogram after


equalization

Digital Image Processing

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

Digital Image Processing

630

Results after histogram


equalization
The image and its histogram after equalization

Jorma Kekalainen

Digital Image Processing

631

Example: Very dark image


We can obtain a dark image by taking the index values only of
an indexed color image.
>> AT=imread('ancienttrees.tif');
We can display this matrix and its histogram with commands:
>> imshow(AT),figure,imhist(AT),axis tight We now have an index
matrix AT consisting of
low values only.

Jorma Kekalainen

Since the
index matrix
contains only
low values it
will appear
dark when
displayed.
Digital Image Processing

632

Example: After equalization


But we can apply histogram equalization to this image, and
display the results:
The image is no longer very dark and
>> ATh=histeq(AT);
the corresponding histogram is no
>> imshow(ATh),figure,
longer heavily clustered at the lower
end of the scale
>> imhist(ATh),axis tight

Jorma Kekalainen

Digital Image Processing

633

Example: Before and after


histogram equalization

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

635

Example
H=imread('he.tif');
figure,imshow(H),figure,imhist(H(:,:,1)),axis
tight

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

637

After equalization
hee=histeq(he(:,:,1));
figure, imshow(hee),
title('After equalization')
figure,imhist(hee),axis tight
title('After equalization')

Jorma Kekalainen

Digital Image Processing

638

Example

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

equalization section.

641

Digital Image Processing


Exercises

Exercise on image import and


export
(a) Read and display any image with very bad
contrast (Construct such an image if needed)
(b) Check the image appearance in the
workspace
(c) Improve image contrast
(d) Write the image to a disk file
(e) Check the contents of the newly written
file
Jorma Kekalainen

Digital Image Processing

643

(a) Read and display an image


First, we clear the Matlab workspace of any
variables and close open figure windows.
clear, close all
To read an image, we use the imread command.
Here we read an image file ex4-fig1.png in an
array named I.
I = imread('ex4-fig1.png');
An acronym png means Portable Network Graphics
(PNG) which is one of many graphics file formats.
Note: For the list of supported graphics file formats, see the imread
Jorma Kekalainen
Digital Image Processing
644
function
reference documentation.

(a) Read and display an image


>> imfinfo('ex4-fig1.png')
ans =

Filename: [1x46 char]

FileModDate: '07-Aug-2015 21:48:40'

FileSize: 16388

Format: 'png'

FormatVersion: []

Width: 357

Height: 221

BitDepth: 8

ColorType: 'grayscale'

FormatSignature: [137 80 78 71 13 10 26 10]

Colormap: []

Histogram: []

Jorma Kekalainen

Digital Image Processing

645

(a) Read and display an image


Now display the image.
The toolbox includes two image display functions:
imshow and imtool.
imshow is the toolbox's fundamental image display
function.
imtool starts the Image Tool which presents an
integrated environment for displaying images and
performing some common image processing tasks.
Note: The Image Tool provides all the image display capabilities of imshow but also
provides access to several other tools for navigating and exploring images, such as
scroll bars, the Pixel Region tool, Image Information tool, and the Contrast Adjustment
Digital
Image Processing
646
tool.Jorma
ForKekalainen
more information, see Image
Display
and Exploration Overview.

(a) Read and display an image


We can use either function to display an image.
Here we use imshow function
>> figure, imshow(I)
>> title('Grayscale image ex4-fig1.png')

Jorma Kekalainen

Digital Image Processing

647

(b) Check the image appearance in


the workspace
To see how the imread function stores the image data
in the workspace, we check the Workspace browser in
the Matlab desktop.
The Workspace browser displays information about all
the variables we create during a Matlab session.
The imread function returned the image data in the
variable I, which is a 221x357 element array of uint8
data.
Values of I are between 0 and 58.
Matlab can store images as uint8, uint16, or double
arrays.
Note: For more information about image storage classes, see Converting Between
Jorma Kekalainen
Digital Image Processing
648
Image
Classes.

(b) Check the image appearance in


the workspace
We can also get information about variables in
the workspace by calling the whos command.
>> whos
Name
I

Jorma Kekalainen

Size

221x357

Bytes Class Attributes


78897 uint8

Digital Image Processing

649

(c) Improve image contrast


The image ex4-fig1.png is too dark (0I 58) and so a low
contrast image.
To see the distribution of intensities in ex4-fig1.png, we can
create a histogram by calling the imhist function.
>> figure, imhist(I), title('Histogram of the image ex4-fig1')
Notice how the intensity range is
rather narrow. It does not cover
the potential range of [0, 255].
There are missing the medium
and high values that would result
in good contrast.
Note: Precede the call to imhist with the figure command so that the histogram does
Jorma Kekalainen
Digital Image Processing
650
not overwrite
the display of the image
I in the current figure window.

(c) Improve image contrast


Histogram equalization
The toolbox provides several ways to improve
the contrast in an image.
One way is to call the histeq function to
spread the intensity values over the full range
of the image.
This process called histogram equalization.

Jorma Kekalainen

Digital Image Processing

651

(c) Improve image contrast


We equalize the histogram giving a command
I2 = histeq(I);
Display the new equalized image, I2, in a new figure
window.
>> figure, imshow(I2)
>> title('Equalized image')

Jorma Kekalainen

Digital Image Processing

652

(c) Improve image contrast


Call imhist to create a histogram of the equalized
image I2.
>> figure, imhist(I2)
>>title('Histogram of the image after equalization')

Jorma Kekalainen

Digital Image Processing

653

(c) Improve image contrast


Comparison of histograms
After equalization process, the histogram of I2 is clearly more
spread out than the histogram of I (0 I 58) .
>> subplot(121),imhist(I);
>> title('Histogram of the image ex4-fig1');
>> subplot(122),imhist(I2);
>> title('Histogram of the image after equalization')

Jorma Kekalainen

Digital Image Processing

654

(c) Improve image contrast


Comparison of images

>> subplot(121),imshow(I)
>> title('Grayscale image ex4-fig1.png')
>> subplot(122),imshow(I2)
>> title('Equalized image')
>> truesize

Jorma Kekalainen

Digital Image Processing

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.

In addition, the toolbox includes an interactive tool,


called the Adjust Contrast tool, that we can use to
adjust the contrast and brightness of an image
displayed in the Image Tool.
To use this tool, call the imcontrast function or access
the tool from the Image Tool.
For more information, see Adjusting Image Contrast
Using the Adjust Contrast Tool.
Jorma Kekalainen

Digital Image Processing

656

(d) Write the image to a disk file


To write the newly adjusted image I2 to a disk
file, use the imwrite function.
If we include the filename extension '.png', the
imwrite function writes the image to a file in
Portable Network Graphics (PNG) format, but we
can also specify other formats, e.g.,
imwrite (I2, 'ex4-fig12.tif');
See the imwrite function reference page for a list
of file formats it supports.
See also Writing Image Data to a File for more
information about writing image data to files.
Jorma Kekalainen
Digital Image Processing
Note:
Tagged Image File Format
(TIFF) 'tiff' or 'tif'.

657

(e) Check the contents of the


newly written file
To see what imwrite wrote to the disk file, use
the imfinfo function.
imfinfo('ex4-fig12.tif')
The imfinfo function returns information
about the image in the file, such as its format,
size, width, and height.
See Getting Information About a Graphics File
for more information about using imfinfo.
Jorma Kekalainen

Digital Image Processing

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

>> imwrite (I2,'ex4-fig12.png');


>> imfinfo('ex4-fig12.png')
ans =
Filename: [1x47 char]
FileModDate: '07-Aug-2015 23:29:41'
FileSize: 17879
Format: 'png'
FormatVersion: []
Width: 357
Height: 221
BitDepth: 8
ColorType: 'grayscale'

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

664

Exercise
Create a dark image with
c=imread('cameraman.tif');
[x,map]=gray2ind(c);

The matrix x, when viewed, will appear as a


very dark version of the cameraman image.
Apply histogram equalization to it, and
compare the result with the original image.

Jorma Kekalainen

Digital Image Processing

665

Digital Image Processing


Thresholding

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

Digital Image Processing

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

Digital Image Processing

668

Example
The commands
>> r=imread('rice.tif');
>> imshow(r),figure,imshow(r>120)
will produce the images shown below.

Jorma Kekalainen

Digital Image Processing

The resulting image can then


be further processed to
find the number, or average
size of the grains.

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

Digital Image Processing

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

Digital Image Processing

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')

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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')

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Apply thresholding operation so that


white if its gray level is between 100 and 150
A pixel becomes
Jorma Kekalainen
Processing
blackDigital
if itsImage
gray
level is otherwise

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)
]

title('Image after thresholding (IM>100 & IM<150)')

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

Digital Image Processing

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

Digital Image Processing

687

Note
We can obtain similar results using im2bw:
imshow(im2bw(x,map,0.45)&~im2bw(x,map,0.5))

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

690

Example

R=rand(256)*128+127;
T=imread('text.png');figure,imshow(T)
TT=uint8(R.*double(not(T)));
figure,imshow(TT)

The first command simply uses the


rand function (which produces matrices
of uniformly generated random
numbers between 0.0 and 1.0), and
scales the result so that random
numbers are between 127 and 255.
We then read in the text image, which
shows white text on a dark background.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

692

Example
If we threshold this image and display the result with
>> imshow(TT>100)
the background has been completely removed.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

695

Solution

Superimpose the image text.png onto the image cameraman.tif:


t=imread('text.png');
c=imread('cameraman.tif');
m=uint8(double(c).*double(~t));
figure, imshow(m),figure, imshow(m>100)
figure, imshow(m>10),figure, imshow(m>5)

Ans. Yes, we can threshold this new image m


to isolate the text!

Jorma Kekalainen

Digital Image Processing

696

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Spatial Filtering

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

Digital Image Processing

699

Using a spatial mask on an image


The idea is to move a mask - a rectangle (usually with sides of
odd length) or other shape over the given image.
As we do this, we create a new image whose pixels have gray
values calculated from the gray values under the mask, as
shown below.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

701

Spatial filter
Suppose that the 3*5 mask
values are given by:

and that corresponding


pixel values are

We now multiply and add:

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

703

Spatial filtering schema

Jorma Kekalainen

Digital Image Processing

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:

where e is gray value of the current pixel in the original image,


and the average is the gray value of the corresponding pixel in
the new image.
Jorma Kekalainen
Digital Image Processing
705

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

Digital Image Processing

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

and so we can describe this filter by the matrix

Jorma Kekalainen

Digital Image Processing

710

Example
The filter

would operate on gray values as


a-2b+c-2d+4e-2f+g-2h+i

Jorma Kekalainen

Digital Image Processing

711

Edges of the image


There is an obvious problem in applying a filter at the
edge of the image, where the mask partly falls
outside the image.
In such a case, as illustrated below there will be a
lack of gray values to use in the filter function.
A mask at the edge of an image

Note: There is no single best approach; the method must be dictated by


the problem at hand; by the filter being used, and by the result
Jorma Kekalainen
Digital Image Processing
712
required.

Dealing with edge problem


Ignore the edges
That is, we only apply the mask to those pixels in
the image for with the mask will lie fully within
the image.
This means all pixels except for the edges, and
results in an output image which is smaller than
the original.
If the mask is very large, we may lose a significant
amount of information by this method.
We applied this method in our previous example.
Jorma Kekalainen

Digital Image Processing

713

Dealing with edge problem


Padding with zeros
We assume that all necessary values outside
the image are zero.
This gives us all values to work with, and will
return an output image of the same size as the
original, but may have the effect of
introducing unwanted artifacts (for example,
edges) around the image.
Note: There is no single best approach dealing with the edge problem; the method
must be dictated by the problem at hand; by the filter being used, and by the result
Jorma Kekalainen
Digital Image Processing
714
required.

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

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,'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

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

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

65.5556 67.7778 58.8889


108.8889 128.8889 105.5556
130.0000 150.0000 106.6667
151.1111 148.8889 85.5556
Digital Image Processing
107.7778 87.7778 38.8889

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

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
Digital Image Processing
50.0000
40.0000 12.2222 10.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

In practice, of course, conv2 computes the convolution for


finite intervals.
Note that matrix indices in Matlab software always start at
1 rather than 0.
Therefore, matrix elements A(1,1), B(1,1), and C(1,1)
correspond to mathematical quantities a(0,0), b(0,0), and
c(0,0).
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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))

Average filtering (3*3)

Original image

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

730

Comment on average filtering


Notice how the zero padding used at the edges has resulted in a dark
border appearing around the image.
This is especially noticeable when a large filter is being used.
If this is an unwanted artifact of the filtering; if for example it changes the
average brightness of the image, then it may be more appropriate to use
the 'valid' shape option.
The resulting image after these filters may appear to be much worse than
the original.
However, applying a blurring filter to reduce details in an image may the
perfect operation for autonomous machine recognition, or if we are only
concentrating on such aspects of the image as numbers of objects or
amount of dark and light areas.
In those cases, too much details may obscure the outcome.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

732

Low and high pass filters


A high pass filter passes over the high frequency components,
and reduces or eliminates low frequency components.
A low pass filter passes over the low frequency components,
and reduces or eliminates high frequency components.
E.g. the 3*3 averaging filter is low pass filter, as it tends to blur
edges.

The filter
Jorma Kekalainen

is a high pass filter.

Digital Image Processing

733

High pass filter


We note that the sum of the coefficients (i.e., the
sum of all elements in the matrix), in the high
pass filter is zero.
This means that in a low frequency part of an
image, where the gray values are similar, the
result of using this filter is that the corresponding
gray values in the new image will be close to zero.
High pass filters are of particular value in edge
detection and edge enhancement.
Jorma Kekalainen

Digital Image Processing

734

Example: High pass filtering


f=fspecial('laplacian')
f=

0.1667 0.6667 0.1667

0.6667 -3.3333 0.6667

0.1667 0.6667 0.1667


c=imread('cameraman.tif');
cf=filter2(f,c);
mshow(cf/100)
Jorma Kekalainen

Digital Image Processing

735

Example: High pass filtering


Application of the Laplacian of Gaussian (log) filter.
f1=fspecial('log')
f1 =

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

Digital Image Processing

736

Example
Lets consider block of similar values pixels,

and apply the previous high pass filter

to the central four:

The resulting values are


close to zero, which is the
expected result of applying a
high pass filter to a low
frequency component.

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

to the central four elements

Jorma Kekalainen

Digital Image Processing

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

Values outside the displayable


range
For image display we would like the gray
values of the pixels to lie between 0 and 255.
However, the result of applying a linear filter
may create values which lie outside this range.
There are ways of dealing with values outside
of this displayable range
Make negative values positive
Clip values
Scaling transformation.
Jorma Kekalainen

Digital Image Processing

740

Make negative values positive


This will certainly deal with negative values,
but not with values greater than 255.
Hence, this can only be used in specific
circumstances;
for example, when there are only a few negative
values, and when these values are themselves
close to zero.

Jorma Kekalainen

Digital Image Processing

741

Clip values

We apply the following thresholding type operation to the


gray values x produced by the filter to obtain a displayable
value y:

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

Digital Image Processing

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

Applying this transformation to all gray levels x produced by


the filter will result (after any necessary rounding) in an image
which can be displayed.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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)

The result will be a matrix of type double, with entries in


the range 0.0 1.0.
This can be viewed with imshow.
We can make it a uint8 image by multiplying by 255 first
imshow(uint8(255*cf2g))
Jorma Kekalainen

Digital Image Processing

747

Note: mat2gray function


The mat2gray function automatically scales the
matrix elements to displayable values for any matrix ,
it applies a linear transformation to its elements,
with the lowest value mapping to 0.0, and the
highest value mapping to 1.0.
This means the output of mat2gray is always of type
double.
The function also requires that the input type is
double.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

The mat2gray function automatically


scales the matrix elements to
displayable values.
For any matrix it applies a linear
transformation to its elements, with the
lowest value mapping to 0.0, and
the highest value mapping to 1.0.
This means the output of mat2gray is
always of type double.
The function also requires that the input
type is double.
Digital Image Processing
752

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

757

Digital Image Processing


Gaussian and Nonlinear
Filters

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

Digital Image Processing

759

One dimensional Gaussian


function
The Gaussian probability distribution function

where is the standard deviation: a large value of


produces to a flatter curve, and a small value leads to a
sharper curve.

Jorma Kekalainen

Digital Image Processing

760

Two dimensional Gaussian


function
A two dimensional Gaussian function is given by

The command fspecial('gaussian') produces a discrete version of this


function.
We can draw pictures of this with the surf function, and to ensure a nice
smooth result, we shall create a large filter (size 50*50) with different
standard deviations.
a=50;s=3;
g=fspecial('gaussian',[a a],s);
surf(1:a,1:a,g)
s=9;
g2=fspecial('gaussian',[a a],s);
figure,surf(1:a,1:a,g2)
Jorma Kekalainen

Digital Image Processing

761

Two dimensional Gaussian


functions
=3

Jorma Kekalainen

=9

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

765

(5*5)filter vs. (11*11)filter

Jorma Kekalainen

=0.5

=1

=2

=5

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

769

Maximum and minimum filter


Here as a simple example we define a filter which takes the
maximum value in a 3*3 neighborhood:
cmax=nlfilter(c,[3,3],'max(x(:))');
The nlfilter function requires three arguments: the image
matrix, the size of the filter, and the function to be applied.
The function must be a matrix function which returns a scalar
value.
A corresponding filter which takes the minimum value can be
just as easily applied:
cmin=nlfilter(c,[3,3],'min(x(:))');

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

773

Comparison of results
Filtered by maximum
filter

Jorma Kekalainen

Filtered by minimum
filter

Digital Image Processing

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

Digital Image Processing

775

Solution

Jorma Kekalainen

Digital Image Processing

776

Solution

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Median filtering effects here are


infinitesimal. However, median
filtering seems almost tailor-made for
removal of some type of noise.

Digital Image Processing

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

Digital Image Processing

781

Rank-order filters
For example, if we decided to use as a mask a (3*3)
cross shape:

then the median would be the third of these five


values when sorted.
The command to do this is
ordfilt2(c,3,[0 1 0;1 1 1;0 1 0]);
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

The same observations as in


the previous case of the
rectangular 3-by-3 mask.

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

Digital Image Processing

784

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Noises and Noise Cleaning
Methods

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Gaussian noise is white noise which is


normally distributed
Easy to use mathematically
Therefore often used in practice even if the model is
not perfect
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

797

Image corrupted by Gaussian and


speckle noise
Gaussian noise

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

Digital Image Processing

799

Periodic noise

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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')

Note: For example


>> [x,y]=meshgrid(1:3,1:3)
x=
1 2 3
1 2 3
1 2 3
y=
1 1 1
2 2 2
Jorma Kekalainen
3 3 3

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

805

Digital Image Processing


Cleaning Impulse Noise

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

Digital Image Processing

807

Effect of low pass filtering


Given that pixels corrupted by salt
and pepper noise are high
frequency components of an image,
we should expect a low-pass filter
should reduce them.
So we might try filtering with an
average filter:
a3=fspecial('average');
c_sp_a3=filter2(a3,c_sp);
The result is not noticeably better
than the noisy image.
The noise is not so much removed
as smeared over the image.
Note: Impulse noise = binary noise = salt and pepper
Kekalainen
Digital Image Processing
noiseJorma
= shot
noise.

808

Effect of low pass filtering


The effect is even more pronounced if
we use a larger averaging filter:
>> a7=fspecial('average',[7,7]);
>> c_sp_a7=filter2(a7,c_sp);
>> a11=fspecial('average',[11,11]);
>> c_sp_a11=filter2(a11,c_sp);
(5*5) averaging

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

811

Example
Using a 3*3 mask, the output value is the median of
the values in the mask.

We see that very large or very small values - noisy


values - will end up at the top or bottom of the
sorted list.
Thus the median will in general replace a noisy value
with one closer to its surroundings.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

Cleaning salt and pepper


noise with a median filter

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

Digital Image Processing

814

Using median filter on more noise


This would produce an

If we corrupt more pixels with noise:


image with 20% of its
>> c_sp2=imnoise(c,'salt & pepper',0.2); pixels corrupted by noise.
then medfilt2 still does a remarkably good job.
20% salt & pepper noise

Jorma Kekalainen

Digital Image Processing

After (3*3) median filtering

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

Digital Image Processing

816

Try to remove noise completely


To remove noise completely, we can either try a double
application of the 3*3 median filter, or try a 5*5 median filter
on the original noisy image:
medfilt2 takes an optional
parameter a 2 element vector
>> c_sp2_m5=medfilt2(c_sp2,[5,5]);
Using medfilt2 twice

Jorma Kekalainen

giving the size of the mask to


be used.
Using a (5*5) median filter

Digital Image Processing

817

Example

% Using 3*3 median filter twice


Cspm3twice=medfilt2(Cspm3);
figure, imshow(Cspm3twice)
title('Image filtered twice by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')

% Using 5*5 median filter


Csp2m5=medfilt2(Csp2,[5,5]);
figure, imshow(Csp2m5)
title('Image filtered by 5*5 median filter')
xlabel('Image with 20% of its pixels corrupted by binary noise')

Jorma Kekalainen

Digital Image Processing

818

Digital Image Processing


Cleaning Gaussian Noise
with Image Averaging and
Wiener Filtering

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

823

Example

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

826

Image averaging to remove


Gaussian noise
10 images

100 images

Jorma Kekalainen
Image Processing
Note:
This method only worksDigital
if the
Gaussian noise has zero mean.827

Example

%Cleaning Gaussian noise by averaging


images
%Gaussian noise
C=imread('cameraman.tif');
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')
% Create 10 image versions
s=size(C);
Cga10=zeros(s(1),s(2),10);
for i=1:10 Cga10(:,:,i)=imnoise(C,'gaussian');
end
%
Cga10av=mean(Cga10,3);
figure, imshow(Cga10av/255)
title('10 times average image')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')

Jorma Kekalainen

%100 times averaging


Cga100=zeros(s(1),s(2),100);
for i=1:100
Cga100(:,:,i)=imnoise(C,'gaussian'); end
Cga100av=mean(Cga100,3);
figure, imshow(Cga100av/255)
title('100 times average image')
xlabel('Image with additive Gaussian noise
with m=0,v=0.01')

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

830

Averaging filtering to remove


Gaussian noise
The results are not really particularly pleasing;
although there has been some noise
reduction, the smeary nature of the resulting
images is unattractive.
Image with additive Gaussian
noise with m=0,v=0.01

Jorma Kekalainen

(3*3) averaging

Digital Image Processing

(5*5) averaging

831

Least squares filtering


Given a degraded image M of some original image M and a
restored version R, what measure can we use to say whether
our restoration has done a good job?
Clearly we would like R to be as close as possible to the
correct image M.
One way of measuring the closeness of R to M is by adding
the squares of all differences:

(m

i, j

ri , j )

i, j

where the sum is taken over all pixels of R and M (which we


assume to be of the same size).
This sum can be taken as a measure of the closeness of R to
M.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

833

Why Wiener filter?


The Wiener filter is optimal in the sense
that it minimizes the mean square error
between the undistorted image g(x,y)
and the restored image (x,y).
i.e. it minimizes e2=E[|g(x,y)- (x,y)|2]

Jorma Kekalainen
Image Processing
Note:
MMSE(minimum mean Digital
square
error) filtering

834

Wiener filter reducing Gaussian


noise
This filter is a non-linear spatial filter; we move a mask across
the noisy image, pixel by pixel, and as we move, we create an
output image the gray values of whose pixels are based on the
values under the mask.
Since we are dealing with additive noise, our noisy image M
can be written as M=M+N, where M is the original correct
image, and N is the noise; which we assume to be normally
distributed with mean 0.
However, within our mask, the mean may not be zero;
suppose the mean is mf , and the variance in the mask is f2 .
Suppose also that the variance of the noise over the entire
image is known to be g2 .
Jorma Kekalainen

Digital Image Processing

835

Wiener filter reducing Gaussian


noise
Then the output value can be calculated as

where g is the current value of the pixel in the noisy


image.
In practice, we calculate mf by simply taking the
mean of all gray values under the mask, and f2 by
calculating the variance of all gray values under the
mask.
Jorma Kekalainen

Digital Image Processing

836

Wiener filter reducing Gaussian


noise
We may not necessarily know the value g2 (the
variance of the noise over the entire image) , so the
Matlab function wiener2 which implements Wiener
filtering uses a slight variant of the above equation:

where n is the computed noise variance, and is


calculated by taking the mean of all values of f2 (the
variance in the mask) over the entire image.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

838

Wiener filtering to remove Gaussian noise


(3*3) filtering

(5*5) filtering

(9*9) filtering

(7*7) filtering

Jorma Kekalainen

Digital Image Processing

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')

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

842

The image and its appearance


after Wiener filtering
The result is a clear improvement over the
original noisy image.

Jorma Kekalainen

Digital Image Processing

843

Example: whos command


>> whos
Name

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

Digital Image Processing

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:

Using the previous images transform them by


using a 3*3 averaging filter.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

846

Note: Im2uint8 convert image to 8-bit unsigned integers.

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

>> fsp=imnoise(f,'salt & pepper');%The amount


of noise added defaults to 10%
figure, imshow(fsp)
title('Image with 10% of its pixels corrupted by
binary noise')

Digital Image Processing

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

>> fspm3=medfilt2(fsp);% class: uint8


figure, imshow(fspm3)
title('Image filtered by 3*3 median filter')
xlabel('Image with 10% of its pixels corrupted
by binary noise')

Digital Image Processing

848

>> % Cleaning binary (salt & pepper noise)


noise with median filter
% 20% of pixel corrupted by noise
fsp20=imnoise(f,'salt & pepper',0.2);% 20% of
pixel corrupted by noise
fsp20m3=medfilt2(fsp20);% class: uint8
figure, imshow(fsp20)
title('Image with 20% of its pixels corrupted by figure, imshow(fsp20m3)
title('Image filtered by 3*3 median filter')
binary noise')
xlabel('Image with 20% of its pixels corrupted
by binary noise')

(b)

Jorma Kekalainen

Digital Image Processing

849

(c)% Using 5*5 median filter

% Using 3*3 median filter twice


fsp20m3twice=medfilt2(fsp20m3);
figure, imshow(fsp20m3twice)
title('Image filtered twice by 3*3 median filter')
xlabel('Image with 20% of its pixels corrupted
by binary noise')

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')

Digital Image Processing

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

Digital Image Processing

852

Continued on the following page

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

Digital Image Processing

853

Note

Jorma Kekalainen

Digital Image Processing

854

Note

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

856

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Edges and Differences

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.

Edge detection is used to identify the edges in an image.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

In the right hand block, there is a clear difference between


the gray values in the second and third columns.
This would be easily discernible in an image.
The human eye can pick out gray differences of this
magnitude with relative ease.
Our aim is to develop methods which will enable us to pick
out the edges of an image.
Jorma Kekalainen
Digital Image Processing

861

Edges
Suppose we follow the gray values of pixels in a line
crossing an edge, as illustrated below.

If we consider the gray values along this line, and


plot their values, we will have something similar to
that a step edge and a ramp edge shown in the
following figure.
Jorma Kekalainen

Digital Image Processing

862

Gray values across edges


A step edge

A ramp edge

A line

Jorma Kekalainen

A roof

Digital Image Processing

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

To see where this graph comes from, suppose that


the values of the ramp edge in the previous figure
are, from left to right:
Jorma Kekalainen

Digital Image Processing

864

Differences and edges


If we form the differences, by subtracting each value
from its successor, we obtain:
which are plotted above.
It appears that the difference tends to enhance
edges, and reduce other components.
So if we could difference the image, we would obtain
the effect we want.

Jorma Kekalainen

Digital Image Processing

865

Different differences
We can define the difference in three separate
ways:
the forward difference:
the backward difference:
the central difference:
Jorma Kekalainen

Digital Image Processing

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

where the subscripts in each case indicate the


direction of the difference.

Jorma Kekalainen

Digital Image Processing

867

Finding vertical edges

To see how we might use x

to determine edges in the x direction, consider the function


values around a point (x,y) and using the convention that x
values increase to the right, y values to the bottom:

To find the filter which returns the value x , we just compare


the coefficients of the function values in x with their position
in the array:
Jorma Kekalainen

Digital Image Processing

868

Finding vertical edges


This filter

thus will find vertical edges in an image.


However, the edges in the result can be a bit irregular or
jumpy; this can be overcome by smoothing the result in the
opposite direction by using the filter

Jorma Kekalainen

Digital Image Processing

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:

>> 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 Jorma
1 Kekalainen
0

Digital Image Processing

>> Px=conv2(A,B,'same')
Px =
-1 0 1
-1 0 1
870
-1 0 1

Finding horizontal edges


To find the filter which returns the value y ,
we just compare the coefficients of the function
values in y with their position in the previous
function array.

Jorma Kekalainen

Digital Image Processing

871

Finding horizontal edges

To see how we might use y

to determine edges in the y direction, consider the function


values around a point (x,y) and using the convention that x
values increase to the right, y values to the bottom:

To find the filter which returns the value y , we just compare


the coefficients of the function values in y with their position
in the array:
Jorma Kekalainen

0 -1 0
0 0 0
0 1 0
Digital Image Processing

872

Finding horizontal edges


This filter
0 -1
0 0
0 1

0
0
0

thus will find horizontal edges in an image.


However, the edges in the result can be a bit
irregular or jumpy; this can be overcome by
smoothing the result in the opposite direction by
using the filter
0
1
0
Jorma Kekalainen

0
1
0

0
1
0

Digital Image Processing

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

Digital Image Processing

>> 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

A=[0 0 0;-1 0 1;0 0 0]


B=[0 1 0;0 1 0;0 1 0]
>> A'
ans =
0 -1
0 0
0 1
0 -1
0 0
0 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

and its companion for finding horizontal edges

are the Prewitt filters for edge detection.


Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

If you need to emphasize vertical edges, transpose


the filter h'.
To find vertical edges, or for x-derivatives, use h'.
Jorma Kekalainen

Digital Image Processing

878

Applaying Prewitt filters


We are now in the position of having to apply two filters to an
image.
Each filter is applied independently, and formed the output
image from a function of the filter value produced.
So if px and py were the gray values produced by applying Px
and Py to an image, then the output gray value v can be
chosen by any of these methods:

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

An original image

880

Example
After filtering with the Prewitt filters
Figure (a)

Jorma Kekalainen

Figure (b)

Digital Image Processing

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

Digital Image Processing

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

>> px=[-1 0 1;-1 0 1;-1 0 1]


px =
-1 0 1
-1 0 1
-1 0 1
>> py=px'
py =
-1 -1 -1
0 0 0
1 1 1

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

% All the edges with:


edge_p=sqrt(icx.^2+icy.^2);
figure,imshow(edge_p/255)
title('All edges in the grayscale image ic.tif')
edge_t=im2bw(edge_p/255,0.
3);
figure,imshow(edge_t)
title('Binary image
thresholded from ic.tif')
xlabel('Threshold level=0.3')

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1
2
1

0
0
0

890

Sobel filter Sx
As we previously notice in Prewitts case this filter

will find vertical edges in an image.


However, the edges in the result can be a bit irregular or
jumpy; this can be overcome by smoothing the result in the
opposite direction by using the filter
0
0
0
Jorma Kekalainen

1
2
1

0
0
0

In the Sobel filters, the


smoothing gives slightly
more prominence to the
central pixel.

Digital Image Processing

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 will find vertical edges in an image.


>> A=[0 0 0;-1 0 1;0 0 0]
A=
0 0 0
-1 0 1
Jorma Kekalainen
0 0 0

>> B'=[0 1 0;0 2 0;0 1 0]


B' =
0 1 0
0 2 0Digital Image Processing
0 1 0

>> 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

will find vertical edges in an image.


However, the edges in the result can be a bit irregular or
jumpy; this can be overcome by smoothing the result in the
opposite direction by using the filter
0
1
0
Jorma Kekalainen

0
2
0

0
1
0

Digital Image Processing

In the Sobel filters, the


smoothing gives slightly
more prominence to the
central pixel.
893

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

%Roberts and Sobel filters


edge_r=edge(ic,'roberts');
figure,imshow(edge_r)
title('Edge finding of ic.tif using Roberts cross-gradient filters')
xlabel('Using command edge(ic,''proberts'');')
%
%and
%
edge_s=edge(ic,'sobel');
figure,imshow(edge_s)
title('Edge finding of ic.tif using Sobel filters')
xlabel('Using command edge(ic,''sobel'');')

Jorma Kekalainen

Digital Image Processing

899

Example

Jorma Kekalainen

Digital Image Processing

900

Digital Image Processing


Second Differences

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

Digital Image Processing

902

Note
We can define the difference in three separate
ways:
the forward difference:
the backward difference:
the central difference:
Jorma Kekalainen

Digital Image Processing

903

Note
An image is a function of two variables, so we
must generalize those definitions to include
both the x and y values

where the subscripts in each case indicate the


direction of the difference.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

905

Central second differences


To calculate a central second difference, take the backward
difference of a forward difference:

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

Digital Image Processing

906

Central second differences


To calculate a central second difference, take the backward
difference of a forward difference:

y2 = y y f (x, y ) = y [ f (x, y + 1) f (x, y )]

= [ 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

Digital Image Processing

907

Laplacian filter
The corresponding filters for second differences x2 in the x
direction and for second differences y2 in the y direction are

The sum of these two is written


and implemented by the filter, which is known as a discrete
Laplacian.

Jorma Kekalainen

Digital Image Processing

908

Example
Take the pixel values describing a ramp edge

If we form the differences, by subtracting each value from its


successor, we obtain:

Differences of the edge function

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

912

Result after filtering with a


discrete laplacian
Although the result is
adequate, it is very
messy when compared
to the results of the
Prewitt and Sobel
methods discussed
earlier.

Jorma Kekalainen

Digital Image Processing

913

Other Laplacian masks


Other Laplacian masks can be used, e.g.,:

In Matlab, Laplacians of all sorts can be generated using the


fspecial function, in the form fspecial('laplacian',alpha) which
produces the Laplacian

If the parameter alpha (which is optional) is omitted, it is


assumed to be 0.2.
Jorma Kekalainen
Digital Image Processing
Note:
The value 0 gives the Laplacian
developed earlier.

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

Digital Image Processing

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 =

0.4737 0.0526 0.4737

0.0526 -2.1053 0.0526

0.4737 0.0526 0.4737

>> fspecial('laplacian',0.8)

ans =

0.4444 0.1111 0.4444

0.1111 -2.2222 0.1111

0.4444 0.1111 0.4444

>> fspecial('laplacian',0.7)

ans =

0.4118 0.1765 0.4118

0.1765 -2.3529 0.1765

0.4118 0.1765 0.4118

>> fspecial('laplacian',0.6)

ans =

0.3750 0.2500 0.3750

0.2500 -2.5000 0.2500

0.3750 0.2500 0.3750

>> fspecial('laplacian',0.5)

ans =

0.3333 0.3333 0.3333

0.3333 -2.6667 0.3333


Jorma
Kekalainen

0.3333 0.3333 0.3333

>> 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

Digital Image Processing

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

Digital Image Processing

917

Definition of the zero crossings


We define the zero crossings
in such a filtered image to be
pixels which satisfy either of
the following:
1. They have both a negative
gray value and they are next to
(by four-adjacency) a pixel
whose gray value is positive,
2. They have both a value of
zero and they are between
negative and positive valued
pixels.

Jorma Kekalainen

Filtered image
Zero crossings are inside red rectangles

Digital Image Processing

918

Example
Zero crossings are inside red circles

Jorma Kekalainen

Digital Image Processing

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

Edges and second differences

Digital Image Processing

920

Locating zero crossings in an


image
Consider a small image (a), and the result after
filtering with a Laplacian mask in figure (b).
Zero crossings are colored with red.
(a) Small image
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

(b) After laplace filtering

-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

Digital Image Processing

921

Zerocross option of edge


We now have a further method of edge detection: take the
zero-crossings after a laplace filtering.
This is implemented in Matlab with the zerocross option of
edge, which takes the zero crossings after filtering with a
given filter:
>> l=fspecial('laplacian',0);
>> icz=edge(ic,'zerocross',l);
>> imshow(icz)

The result is not a very good - far too


many gray level changes have been
interpreted as edges by this method.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

924

fspecial('log', hsize, sigma)


h = fspecial('log', hsize, sigma) returns a
rotationally symmetric Laplacian of Gaussian
(LoG) filter of size hsize with standard
deviation sigma (positive).
hsize can be a vector specifying the number of
rows and columns in h, or it can be a scalar, in
which case h is a square matrix.
The default value for hsize is [5 5] and 0.5 for
sigma.
Jorma Kekalainen

Digital Image Processing

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

Using an LoG filter first and


finding then zero crossings

Digital Image Processing

926

Marr-Hildreth method
log=fspecial('log',13,2);
edge(ic,'zerocross',log);

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Filter the previous image with fspecial('laplacian',0) and


construct the corresponding zero crossing chart.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

933

Digital Image Processing


Edge Enhancement

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

(b) Image after unsharp masking

Digital Image Processing

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

Digital Image Processing

940

Unsharp masking filter


We can in fact perform the filtering and subtracting operation
in one command, using the linearity of the filter, and that the
3*3 filter

is the identity filter.


Hence unsharp masking can be implemented by a filter of the
form

where k is a constant chosen to provide the best result.


Jorma Kekalainen

Digital Image Processing

941

Unsharp masking filter


Alternatively, the unsharp masking filter may
be defined as

so then we are subtracting a blur from a


scaled version of the original.

Jorma Kekalainen

Digital Image Processing

942

Unsharp masking filter


The scaling factor may also be split between the identity and
blurring filters.
The unsharp option of fspecial function produces such filters;
the filter created has the form

where is an optional parameter which defaults to 0.2.


If =0.5 the filter is

Jorma Kekalainen

Digital Image Processing

943

Exercise
Verify with Matlab the previous equality

Jorma Kekalainen

Digital Image Processing

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

Figure after unsharp


masking, appears sharper
and cleaner than the
original.

After unsharp masking

Digital Image Processing

Note: Although we have


used averaging filters above,
we can use any low pass
946
filter for unsharp masking.

High boost filtering


Allied to unsharp masking filters are the high boost filters,
which are obtained by
high boost = A(original)-(low pass)
where A is an amplication factor.
If A=1, then the high boost filter becomes an ordinary high
pass filter.
If we take as the low pass filter the 3*3 averaging filter, then a
high boost filter will have the form
where z > 8

Jorma Kekalainen

Digital Image Processing

947

High boost filtering


If we put z=11 , we obtain a filtering very similar to the
unsharp filter above, except for a scaling factor.
Thus the commands:
>> f=[-1 -1 -1;-1 11 -1;-1 -1 -1]/9;
>> Cf=filter2(C,f);
>> imshow(Cf/90)
will produce a image similar to that in an earlier figure.
The value 90 was obtained by trial and error to
produce an image with similar intensity to the original.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

949

Example

Jorma Kekalainen

Digital Image Processing

950

Note
We can also write the high boost formula above as

Best results for high boost filtering are obtained if we multiply


the equation by a factor so that the filter values sum to 1;
this requires A- =1 or =1/(A-1).
So a general unsharp masking formula is

Jorma Kekalainen

Digital Image Processing

951

High boost filters


A different version of the high boost filter is

where for best results A taken so that 3/5 A5/6.


If we take A=3/5 , the formula becomes

If we take A=5/6

Jorma Kekalainen

Digital Image Processing

952

Special high boost filters


Using the identity and averaging filters, we can obtain high
boost filters by:
>>id=[0 0 0;0 1 0;0 0 0];
>>f=fspecial('average');
>>hb1=3*id-2*f
hb1 =
-0.2222 -0.2222 -0.2222
-0.2222 2.7778 -0.2222
-0.2222 -0.2222 -0.2222
>> hb2=1.25*id-0.25*f
hb2 =
-0.0278 -0.0278 -0.0278
-0.0278 1.2222 -0.0278
Jorma
Kekalainen
Digital Image Processing
-0.0278
-0.0278 -0.0278

953

Special high boost filters


id=[0 0 0;0 1 0;0 0 0]
f=fspecial('average')
id =
0 0 0
0 1 0
0 0 0
f=
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
Jorma Kekalainen

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

Digital Image Processing

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

(b) High boost filtering with hb2

Digital Image Processing

hb1 appears
to produce the
best result;
hb2 produces
an image not
very much
sharper than
955
the original.

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Fourier Transforms

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

Digital Image Processing

958

The one-dimensional discrete


Fourier transform
We can start with the observation that a periodic
function may be written as the sum of sines and
cosines of varying amplitudes and frequencies.
Some functions will require only a finite number of
functions in their decomposition; others will require
an infinite number.
For example, a square wave has the decomposition

Jorma Kekalainen

Digital Image Processing

959

A function and its trigonometric


decomposition

Jorma Kekalainen

Digital Image Processing

960

Square wave, its decomposition


and finite approximation

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

962

Example
Consider the discrete
sequence

Expressing a discrete function


as the sum of sines.

which we may take as a


discrete approximation to
the square wave .
This can be expressed as the
sum of only two sine
functions.
We shall see beside how to
obtain those sequences.
Jorma Kekalainen

Digital Image Processing

963

Discrete Fourier transform


(DFT)
The Fourier transform allows us to obtain
those individual sine waves which compose a
given function or sequence.
Since we shall be concerned with discrete
sequences (especially images), we shall
investigate only the discrete Fourier
transform (DFT).

Jorma Kekalainen

Digital Image Processing

964

Definition of the one dimensional


DFT and IDFT
Suppose
is a sequence of length N.
We define its discrete Fourier transform to be the sequence
where

The formula for the inverse DFT is very similar to the forward
transform:
Jorma Kekalainen

Digital Image Processing

965

IDFT vs. DFT


If we compare IDFT to DFT we will see that
there are really only two differences:
1. there is no scaling factor 1/N.
2. the sign inside the exponential function has
been changed to positive.

Jorma Kekalainen

Digital Image Processing

966

Note
The formulas given in equations

are not used by all authors.


The main change is the position of the scaling factor 1/N.
Some authors put it in front of the sum in the forward formula.
Some authors put it in front of the sum in the inverse formula.
Others put a factor of 1/(N) in front of both sums.
The point is the sums by themselves would produce a result (after both
forward and inverse transforms) which is too large by a factor of N.
So somewhere in the forward-inverse formulas a corresponding 1/N must
exist; it doesn't really matter where.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

968

Example
Express DFT of the sequence
1 , n = 0,1,2,6,7
x( n) =
3 n 5
0 ,

as compact as possible both with the help of


the complex exponentials and real cosine
functions. Calculate the both expressions in
Matlabs command window.

Jorma Kekalainen

Digital Image Processing

969

Example
Lets substitute the given sequence x
1 , n = 0,1,2,6,7
x( n) =
3 n 5
0 ,

and N=8 to the DFT formula


2
X( k ) = x( n) exp j kn

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

2.4142 + 0.0000i -1.0000 - 0.0000i -0.4142 + 0.0000i

Columns 5 through 8
Jorma Kekalainen

1.0000

Digital Image Processing


-0.4142 - 0.0000i
-1.0000 - 0.0000i 2.4142 + 0.0000i

971

FFT and IFFT


In Matlab, we can calculate the forward and
inverse transforms with fft and ifft.
Here fft stands for Fast Fourier Transform,
which is a fast and efficient method of
performing the DFT.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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;

Digital Image Processing

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
%

Digital Image Processing

976

Re{fft}-Re{muunnos1} and
Im {fft}-Im{muunnos1}

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

fft(x') vs. muunnos3('fft',x')


>> x =[1
x=
1 1
>> fft(x')
ans =
5.0000
2.4142
-1.0000
-0.4142
1.0000
-0.4142
-1.0000
2.4142
Jorma Kekalainen

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

ifft(XX) vs. muunnos3('ifft',XX)


XX =
5.0000
2.4142
-1.0000
-0.4142
1.0000
-0.4142
-1.0000
2.4142

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

Digital Image Processing

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

fft(a) vs. muunnos3('fft',a)


a=rand(1,8)-0.5+j*(rand(1,8)-0.5)
>> fft(a)
ans =
Columns 1 through 3
0.3162 + 1.4353i 0.7771 - 0.9257i 1.3794 + 1.4137i
Columns 4 through 6
1.4377 - 0.7349i -0.6110 + 0.3098i -1.6977 + 1.2294i
Columns 7 through 8
0.7038 + 0.4999i 0.2124 + 0.4326i
>> muunnos3('fft',a)
ans =
Columns 1 through 3
0.3162 + 1.4353i 0.7771 - 0.9257i 1.3794 + 1.4137i
Columns 4 through 6
1.4377 - 0.7349i -0.6110 + 0.3098i -1.6977 + 1.2294i
Columns 7 through 8
Jorma Kekalainen
Digital Image Processing
0.7038 + 0.4999i 0.2124 + 0.4326i

987

fft(a') vs. muunnos3('fft',a')


>> 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

Jorma Kekalainen

Digital Image Processing

>> 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

ifft(a) vs. muunnos3('ifft',a)


>> ifft(a)
ans =
Columns 1 through 3
0.0395 + 0.1794i 0.0265 + 0.0541i 0.0880 + 0.0625i
Columns 4 through 6
-0.2122 + 0.1537i -0.0764 + 0.0387i 0.1797 - 0.0919i
Columns 7 through 8
0.1724 + 0.1767i 0.0971 - 0.1157i
>> muunnos3('ifft',a)
ans =
Columns 1 through 3
0.0395 + 0.1794i 0.0265 + 0.0541i 0.0880 + 0.0625i
Columns 4 through 6
-0.2122 + 0.1537i -0.0764 + 0.0387i 0.1797 - 0.0919i
Columns 7 through 8
0.1724 + 0.1767i 0.0971 - 0.1157i
Jorma Kekalainen

Digital Image Processing

989

ifft(a') vs. muunnos3('ifft',a')


>> 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

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

Digital Image Processing

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

Digital Image Processing

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

1 Digital1 Image Processing

>> 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

Digital Image Processing

>> 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

Digital Image Processing

995

Properties of the one-dimensional


DFT: Linearity
This is a direct consequence of the definition of the DFT as a
matrix product.
Suppose f and g are two vectors of equal length, and p and q
are scalars, with h=pf+qg.
If F, G and H are the DFT's of f, g and h respectively, then
H=pF+qG
This follows from the definitions of

and the linearity of the matrix product.


Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

997

h=pf+qg and H=fft(h')


h=

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

Digital Image Processing

998

PF=fft(pf') and QG=fft(qg')


PF =
1.0e+02 *
1.0800 + 0.0000i
-0.1200 + 0.2897i
-0.1200 + 0.1200i
-0.1200 + 0.0497i
-0.1200 + 0.0000i
-0.1200 - 0.0497i
-0.1200 - 0.1200i
-0.1200 - 0.2897i
Jorma Kekalainen

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

Digital Image Processing


max(abs(H-H1))
= 1.4211e-14

1000

H1=PF+QG vs. pfft(f')+qfft(g')


>> H1
H1 =
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

>> 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

Properties of the one-dimensional


DFT: Shifting
Suppose we multiply each element xn of a
vector x by (-1)n .
In other words, we change the sign of every
second element.
Let the resulting vector be denoted x1.
Then the DFT X1 of x1 is equal to the DFT X of
x with the swapping of the left and right
halves.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

X5=-9.6569 + 4.0000i
X6=-4.0000 - 4.0000i
1005
X7=1.6569 - 4.0000i

The Fast Fourier Transform


Without an efficient method of calculating a DFT, it would
remain only of academic interest, and of no practical use.
However, there are a number of extremely fast and efficient
algorithms for computing a DFT.
Such an algorithm is called a Fast Fourier Transform, or FFT.
The use of an FFT vastly reduces the time needed to compute
a DFT.
One FFT method works recursively by dividing the original
vector into two halves, computing the FFT of each half, and
then putting the results together.
This means that the FFT is most efficient when the vector
length is a power of 2.
Jorma Kekalainen

Digital Image Processing

1006

FFT vs. direct method


For a vector of length 2n , the direct method takes
(2n)2=22n multiplications and the FFT only n2n.
The saving in time is thus of an order of 2n/n.
The advantage of using an FFT algorithm
becomes greater as the size of the vector
increases.
The following table shows that advantage gained
by using the FFT algorithm as opposed to the
direct arithmetic definition of transformations by
comparing the number of multiplications
required for each method.
Jorma Kekalainen

Digital Image Processing

1007

Comparison of FFT and direct


arithmetic of DFT

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1009

The two-dimensional DFT


In two dimensions, the DFT takes a matrix as input, and
returns another matrix, of the same size, as output.
If the original matrix values are f(x,y), where x an y are the
indices, then the output matrix values are F(u,v).
The matrix F
is the Fourier transform of f.
Then the original matrix f
is the inverse Fourier transform of F.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1012

Corrugations and their


measurements
The horizontal and vertical distances between consecutive wave bottoms

Jorma Kekalainen

Digital Image Processing

1013

Two-dimensional discrete Fourier


transform pair
The definition of the two-dimensional discrete Fourier
transform is very similar to that for one dimension.
The forward and inverse transforms for an M*N matrix

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

are not used by all authors.


The main change is the position of the scaling factor 1/(MN).
Some authors put it in front of the sums in the forward formula.
Others put a factor of 1/(MN) in front of both sums.
The point is the sums by themselves would produce a result (after both
forward and inverse transforms) which is too large by a factor of MN.
So somewhere in the forward-inverse formulas a corresponding 1/(MN)
Jormamust
Kekalainen
Digital Image
Processing
1015
exist; it doesn't really matter
where.

Properties of the two dimensional


Fourier transform
All the properties of the one-dimensional DFT transfer
into two dimensions.
First we notice that the forward and inverse transforms
are very similar, with the exception of the scale factor
1/(MN) in the inverse transform, and the negative sign
in the exponent of the forward transform (similarity
property).
This means that nearly the same algorithm, only very
slightly adjusted, can be used for both the forward an
inverse transforms.
But there are some further properties, which are of
particular use for image processing.
Jorma Kekalainen

Digital Image Processing

1016

DFT as a spatial filter


Note that the values

are independent of the values f or F.


This means that they can be calculated in advance, and only then
put into the formulas above.
It also means that every value F(u,v) is obtained by multiplying
every value of f(x,y) by a fixed value, and adding up all the results.
But this is precisely what a linear spatial filter does: it multiplies all
elements under a mask with fixed values, and adds them all up.
Thus we can consider the DFT as a linear spatial filter which is as big
as the image.

Jorma Kekalainen

To deal with the problem of edges (image


borders), we assume that the image is tiled
(duplicated) in all directions, so that the mask
Digital Image Processing
always has image
values to use.

1017

Separability
Notice that the Fourier transform filter elements can be
expressed as products:

The first product value


depends only on x and u, and is independent of y and v.
Conversely, the second product value

depends only on y and v, and is independent of x and u.


Jorma Kekalainen

Digital Image Processing

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.:

If we replace x and u with y and v we obtain the


corresponding formulas for the DFT of matrix columns.
These formulas define the one-dimensional DFT of a vector, or
simply the DFT.
Jorma Kekalainen

Digital Image Processing

1019

Calculating a 2-D DFT


The 2-D DFT can be calculated by using this property of
separability.
To obtain the 2-D DFT of a matrix, we first calculate the DFT of
all the rows, and then calculate the DFT of all the columns of
the result.
Since a product is independent of the order, we can equally
well calculate a 2-D DFT by calculating the DFT of all the
columns first, then calculating the DFT of all the rows of the
result.
Original
image
Jorma Kekalainen

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:

where k is scalar, and f and g are matrices.


This property is of great use in dealing with image
degradation such as noise which can be modeled as a sum:

d=f+n
where f is the original image; n is the noise and d is the
degraded image.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1026

DC coefficient
If we put u = v = 0 in the definition given in equation

we get

This term is equal to the sum of all terms in the original


matrix.
The value F(0, 0) of the DFT is called the DC coefficient.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

This is the conjugate symmetry property of


DFT.

Jorma Kekalainen

Digital Image Processing

1029

Conjugate symmetry in the DFT


Conjugate symmetry means that
half of the transform is a mirror
image of the conjugate of the
other half.
We can think of the top and
bottom halves, or the left and
right halves, being mirror images
of the conjugates of each other.
Figure demonstrates this
symmetry in a shifted DFT.
The black square shows the
position of the DC coefficient.
The symmetry means that its
information is given in just half
of a transform, and the other
half is redundant.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1031

Spectrum of the transform


One trouble is that the DC coefficient is generally
very much larger than all other values.
This has the effect of showing a transform as a single
white dot surrounded by black.
One way of stretching out the values is to take the
logarithm of |F(u, v)| and to display
log(1+ |F(u,v)|)
The display of the magnitude of a Fourier transform
is called the spectrum of the transform.
Jorma Kekalainen

Digital Image Processing

1032

Fourier transforms in MATLAB

The relevant MATLAB functions for us are:


fft which takes the DFT of a vector,
ifft which takes the inverse DFT of a vector,
fft2 which takes the DFT of a matrix.
ifft2 which takes the inverse DFT of a matrix,
fftshift which shifts a transform as shown in
the previous figure.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1038

Example

a = [100 200; 100 200]


a = repmat(a,4,4)
af = fft2(a)
%
a=
100 200
100 200
a=
100 200 100 200
100 200 100 200
100 200 100 200
100 200 100 200
100 200 100 200
100 200 100 200
100 200 100 200
100 200 100 200

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1041

Example: Shifted DFT


a = [zeros(8,4) ones(8,4)]
af=fftshift(fft2(a));

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

Digital Image Processing

1042

Example: Shifted DFT


Lets perform the Fourier transform with a shift, to place the
DC coefficient in the centre, and since it contains some
complex values, for simplicity we shall just show the rounded
absolute values:
The DC coefficient is the sum of all values
of a; the other values may be considered
>> af=fftshift(fft2(a));
to be the coefficients of the necessary sine
>> round(abs(af))
functions required to from an edge.
ans =

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1045

DFT of the single edge


a=[zeros(256,128) ones(256,128)];
afl=log(1+abs(af));
imshow(mat2gray(log(1+abs(af))))
imshow(afl/afl(129,129))

Jorma Kekalainen

Digital Image Processing

1046

Function for viewing transforms

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;

Digital Image Processing

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'')')

Digital Image Processing

1049

Example: DFT of the single edge

Jorma Kekalainen

Digital Image Processing

1050

Note
DFT of the single edge with fftshow(af,'abs')
Note that the line is seen only in enlarged scale!

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1052

Example: Box

Jorma Kekalainen

Digital Image Processing

1053

Example

A
rotated
box and
its DFT

Now we shall look at a box


rotated 45
>> [x,y]=meshgrid(1:256,1:256);
>> b=(x+y<329)&(x+y>182)&(xy>-67)&(x-y<73);
>> imshow(b)
>> bf=fftshift(fft2(b));
>> figure,fftshow(bf)
Note that the transform of
the rotated box is the rotated
transform of the original box.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

4.0000 4.1231 4.4721 5.0000


3.0000 3.1623 3.6056 4.2426
2.0000 2.2361 2.8284 3.6056
1.0000 1.4142 2.2361 3.1623
0 1.0000 2.0000 3.0000
1.0000 1.4142 2.2361 3.1623
2.0000 2.2361 2.8284 3.6056
3.0000 3.1623 3.6056 4.2426

1059

DFT of the small circle


[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<15);
figure, imshow(c)
title('Small circle (c<15)')
%
cf=fftshift(fft2(c));
figure, fftshow(cf,'abs')
title('DFT of the small circle')
xlabel('fftshow(cf,''abs'')')
figure, fftshow(cf,'log')
title('DFT of the small circle')
xlabel('fftshow(cf,''log'')')

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

%Circle with gentle cutoff or spatially blurred


circle
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
b=1./(1+(z./15).^2);
%
figure,imshow(b)
title('Circle with a gentle cutoff')
%
bf=fftshift(fft2(b));
figure, fftshow(bf,'abs')
title('DFT of the small circle with a gentle
cutoff')
xlabel('fftshow(cf,''abs'')')
figure, fftshow(bf,'log')
title('DFT of the small circle with a gentle
cutoff')
xlabel('fftshow(cf,''log'')')
Digital Image Processing
1062

Example

Jorma Kekalainen

Digital Image Processing

1063

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Frequency Domain
Filtering

Filtering in the frequency domain


We have seen previously that one of the
reasons for the use of the Fourier transform in
image processing is due to the convolution
theorem: a spatial convolution can be
performed by element-wise multiplication of
the Fourier transform by a suitable filter
matrix.
Here we shall explore some filtering by this
method.
Jorma Kekalainen

Digital Image Processing

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

where D is the cutoff radius.


Jorma Kekalainen

Digital Image Processing

1067

Ideal filter functions


Then the ideal high pass filters can be described similarly:

These functions are illustrated in

Low pass

Jorma Kekalainen

High pass

Digital Image Processing

1068

Ideal low pass filtering


Suppose we have a Fourier transform matrix F,
shifted so that the DC coefficient is in the
center.
Since the low frequency components are
towards the center, we can perform low pass
filtering by multiplying the transform by a
matrix in such a way that center values are
maintained, and values away from the center
are either removed or minimized.
Jorma Kekalainen

Digital Image Processing

1069

Ideal low pass filtering


One way to do this is to multiply the transform by an
ideal low-pass matrix, which is a binary matrix m
defined by:

Then the inverse Fourier transform of the elementwise product of F and m is the result we require:

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1071

Ideal filtering on the DFT


Now we can perform a low pass filter by multiplying the
transform matrix by the circle matrix (recall that dot asterisk is
the Matlab syntax for element-wise multiplication of two
matrices):
>> cfl=cf.*c;
>> figure,fftshow(cfl,'log')
and this is shown beside.

Jorma Kekalainen

Digital Image Processing

1072

Example

Jorma Kekalainen

Digital Image Processing

1073

Applying ideal low pass filtering


Now we can take the inverse transform and display the result:
>> cfli=ifft2(cfl);
>> figure,fftshow(cfli,'abs')
After inversion
and this is shown beside.

Note the ringing about the


edges in this image. This is a
direct result of the sharp cutoff
of the circle. The ringing is
Jorma Kekalainen
Digital Image Processing
transferred
to the image.

1074

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

DFT of a very small circle (c<5)

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

Digital Image Processing

1078

Effect of a very small circle (c<5)


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
Cfl=Cf.*c;% Lowpass filtering(symbol l)
figure,fftshow(Cfl,'log')
title('DFT of of the cameraman image multiplied by an ideal low-pass matrix c<5')
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(<5)')
xlabel('fftshow(Cfli,''abs'')')

Jorma Kekalainen

Digital Image Processing

1079

Effect of a very small circle (c<5)

Jorma Kekalainen

Digital Image Processing

1080

DFT of a larger circle (c<30)

%Effect of a larger circle (c<30)


r=30;
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<r);
figure, imshow(c)
title(['Circle c<', num2str(r)])
%
cf=fftshift(fft2(c));
figure, fftshow(cf,'log')
title(['DFT of the circle c<',num2str(r)])
xlabel('fftshow(cf,log)')

Jorma Kekalainen

Digital Image Processing

1081

Effect of a larger circle (c<30)

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1083

Effect of a gentle cutoff(c<30)

Jorma Kekalainen

Digital Image Processing

1084

Comparison

Jorma Kekalainen

Digital Image Processing

1085

Experiments with gentle cutoffs

%Experiments with gentle cutoffs


promt='Give cutoff value r=';
r=input(promt)
[x,y]=meshgrid(-128:127,-128:127);
z=sqrt(x.^2+y.^2);
c=(z<r);
%Circle with gentle cutoff or spatially blurred circle
b=1./(1+(z./r).^2);
%
figure,imshow(b)
title(['Circle c<',num2str(r),' with a gentle cutoff'])
%
bf=fftshift(fft2(b));
figure, fftshow(bf,'log')
title(['DFT of the circle c<',num2str(r),' with a
gentle cutoff'])
xlabel('fftshow(cf,log)')
%without function
%figure, imshow(mat2gray(log(1+abs(bf))))
%title('DFT of the circle c<30) with a gentle cutoff')
%

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)])

Digital Image Processing

1086

Digital Image Processing


High Pass Filtering

High pass filtering


Just as we can perform low pass filtering by
keeping the center values of the DFT and
eliminating the others, so high pass filtering
can be performed by the opposite: eliminating
center values and keeping the others.
This can be done with a minor modification of
the preceding method of low pass filtering.

Jorma Kekalainen

Digital Image Processing

1088

High pass filtering


First we create the circle:
>> [x,y]=meshgrid(-128:127,-128:127);
>> z=sqrt(x.^2+y.^2);
>> c=(z>15);
and multiply it by the DFT of the image:
>> cfh=cf.*c;
>> figure,fftshow(cfh,'log')

Jorma Kekalainen

Digital Image Processing

The DFT after


high pass filtering

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.

As with low pass filtering, the


size of the circle influences the
information available to the
inverse DFT, and hence the
final result.
Jorma Kekalainen

Digital Image Processing

1090

High pass filtered (c>15) image

Jorma Kekalainen

Digital Image Processing

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

Ideal high pass filtering with


different cutoffs
The following figures show some results of ideal high pass
filtering with different cutoffs.
If we have small cutoff, such as in the next figure, we are
only removing a small amount of the transform.
We would thus expect that only the lowest frequencies of
the image would be removed; then there is some grayscale
detail in the final image, but large areas of low frequency
are close to zero.
If the cutoff is large, then more information is removed
from the transform, leaving only the highest frequencies.
We also see this in the following figures; only the edges of
the image remain.

Jorma Kekalainen

Digital Image Processing

1093

Examples
Cutoffs: c>15, c>30, c>60

Cutoffs: c>1, c>2, c>5

Jorma Kekalainen

Digital Image Processing

1094

c>50

c>100

c>120
Jorma Kekalainen

Digital Image Processing

1095

Digital Image Processing


Butterworth Filtering

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

Digital Image Processing

1097

Butterworth filters
Butterworth filters are based on the following functions for
low pass filters and for high pass filters :

where the parameter n is called the order of the filter and D is


cutoff radius.
The size of n dictates the sharpness of the cutoff.
Butterworth filters with n = 2

Jorma Kekalainen

Digital Image Processing

1098

Comparison of Butterworth filters


Butterworth filters with n = 2

Butterworth filters with n = 4

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1100

Low pass Bu-filter


Here are the commands to produce a Butterworth low pass
filter of size 256*256 with cutoff circle D=15 and order n=2
and its 3D-plot.
[x,y]=meshgrid(-128:127,-128:127);
bl=1./(1+((x.^2+y.^2)/15^2).^2);
zl=bl;
figure, mesh(x,y,zl)
title('bl=1./(1+((x.^2+y.^2)/15^2).^2)')

Jorma Kekalainen

Digital Image Processing

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

DFT after Butterworth


low pass filtering

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1104

Compare

Jorma Kekalainen

Digital Image Processing

1105

Exercise
Study effects of Butterworth low pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image

Jorma Kekalainen

Digital Image Processing

1106

Solution

%Experiments with Butterworth low pass filter


promt1='Give filter order n=';
n=input(promt1)
promt2='Give cutoff value D=';
D=input(promt2)
%So to apply a Butterworth low pass filter to the DFT of the cameraman image
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')
%Apply a Butterworth low pass filter to the DFT of the cameraman image:
[x,y]=meshgrid(-128:127,-128:127);
bl=1./(1+((x.^2+y.^2)/D^2).^n);
Cfbl=Cf.*bl;
figure,fftshow(Cfbl,'log')
title(['Butterworth lowpass filtered (n=',num2str(n),' and D=',num2str(D),') DFT image'])
%Inverting and displaying the result:
Cfbli=ifft2(Cfbl);
figure,fftshow(Cfbli,'abs')
title(['Butterworth lowpass filtered (n=',num2str(n),' and D=',num2str(D),' ) image'])

Jorma Kekalainen

Digital Image Processing

1107

n=1,D=1,10,20,30

Jorma Kekalainen

Digital Image Processing

1108

n=1,D=40,50,100

Jorma Kekalainen

Digital Image Processing

1109

n=2,D=1,10,20,30

Jorma Kekalainen

Digital Image Processing

1110

n=2,D=40,50,100

Jorma Kekalainen

Digital Image Processing

1111

n=4,D=1,10,20,30

Jorma Kekalainen

Digital Image Processing

1112

n=4,D=40,50,100

Jorma Kekalainen

Digital Image Processing

1113

n=8,D=1,10,20,30

Jorma Kekalainen

Digital Image Processing

1114

n=8,D=40,50,100

Jorma Kekalainen

Digital Image Processing

1115

D=50,n=16,32,64,128

Jorma Kekalainen

Digital Image Processing

1116

Exercise
Study effects of Butterworth high pass filter
with different order (n) and cutoff radius (D)
on cameraman.tif image

Jorma Kekalainen

Digital Image Processing

1117

Solution: Bu high pass filters


n=1

D=1
D=5
D=10
D=20,
D=50,

Jorma Kekalainen

Digital Image Processing

D=100

1118

Bu high pass filters


n=2,D=10,50,100

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Solution: Low pass Butterworth


filter function
function lout=lbutter(im,d,n)
% LBUTTER(IM,D,N) creates a low-pass Butterworth filter
% of the same size as image IM, with cutoff D, and order N
%
% Use e.g.:
% x=imread('cameraman.tif');
% l=lbutter(x,25,2);
%
height=size(im,1);
width=size(im,2);
[x,y]=meshgrid(-floor(width/2):floor((width-1)/2),-floor(height/2):
...
floor((height-1)/2));
lout=1./(1+((x.^2+y.^2)/d^2).^n);

Jorma Kekalainen

Digital Image Processing

1122

Solution: High pass Butterworth


filter function
function hout=hbutter(im,d,n)
% HBUTTER(IM,D,N) creates a high-pass
%Butterworth filter of the same size as image IM,
%with cutoff D, and order N
%
% Use e.g.:
% x=imread('image.tif');
% l=hbutter(x,25,2);
%
hout=1-lbutter(im,d,n);
Jorma Kekalainen

Digital Image Processing

1123

Solution: Low or high pass Bufilter function

function [ out] = butter(im,d,n,type)


% A function to generate a low pass or high pass Butterworth filter
%function out=butter(im,d,n,type)
% BUTTER(IM,D,N,TYPE) creates a low-pass or high pass Butterworth filter
height=size(im,1);
width=size(im,2);
[x,y]=meshgrid(-floor(width/2):floor((width-1)/2),-floor(height/2): ...
floor((height-1)/2));
%
if strcmp(type,'low')
out=1./(1+((x.^2+y.^2)/d^2).^n);
elseif strcmp(type,'high')
out=1-1./(1+((x.^2+y.^2)/d^2).^n);
else
error('TYPE must be ''low'' or ''high''')
end

Jorma Kekalainen

Digital Image Processing

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

DFT after Butterworth


low pass filtering

Digital Image Processing

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

We can apply a Butterworth


high pass filter function
similarly, first by creating
the filter and applying it to
the image transform:
bh=hbutter(C,15,1);
cfbh=cf.*bh;
figure,fftshow(cfbh,'log')
then inverting and
displaying the result
cfbhi=ifft2(cfbh);
figure,fftshow(cfbhi,'abs')

Digital Image Processing

1126

Example

Jorma Kekalainen

Digital Image Processing

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

%Applying Butterworth high 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')
bh=hbutter(Cf,15,1);
Cfbh=Cf.*bh;
figure,fftshow(Cfbh,'log')
title('DFT of the cameraman image
multiplied by Bu-hpf (D=15, n=1)')
%inverting and displaying the result:
Cfbhi=ifft2(Cfbh);
figure,fftshow(Cfbhi,'abs')
title('Bu-highpass filtered (D=15, n=1)
image ')

Digital Image Processing

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

Digital Image Processing

1129

Example

Jorma Kekalainen

Digital Image Processing

1130

Example

Jorma Kekalainen

Digital Image Processing

1131

Digital Image Processing


Gaussian Filtering in
Frequency Domain

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

Digital Image Processing

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

Digital Image Processing

1135

Gaussian low pass filter


In each case, the final parameter of the fspecial function is
the standard deviation; it controls the width of the filter.
As we see, the larger the standard deviation, the wider the
function, and so the greater amount of the transform is
preserved.
The results of the transform on the original image can be
produced using the usual sequence of commands:
cgi1=ifft2(cg1);
cgi2=ifft2(cg2);
fftshow(cgi1,'abs');
fftshow(cgi2,'abs');
and the results are shown below.
Jorma Kekalainen

Digital Image Processing

1136

Applying a Gaussian low pass filter in


the frequency domain
=10
The larger the standard
deviation, the wider
the function, and so the
greater amount of the
transform is preserved.

=30

Jorma Kekalainen

Digital Image Processing

1137

High pass Gaussian filter


We can apply a high pass Gaussian filter easily; we create a
high pass filter by subtracting a low pass filter from 1.
h1=1-g1;
h2=1-g2;
ch1=cf.*h1;
ch2=cf.*h2;
ch1i=ifft2(ch1);
chi1=ifft2(ch1);
chi2=ifft2(ch2);
fftshow(chi1,'abs')
figure,fftshow(chi2,'abs')
Jorma Kekalainen

Digital Image Processing

1138

Applying a Gaussian high pass


filter in the frequency domain
=10

=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.

Gaussian hp filtered (size=256,


std=10 and std=30) DFT-image

Jorma Kekalainen

Digital Image Processing

1140

Digital Image Processing


Removal of Periodic Noise

Removal of periodic noise


Earlier we investigated a number of different
noise types which can affect images.
However, we did not look at 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 as may be caused
by an electric motor.
Jorma Kekalainen

Digital Image Processing

1142

Creating periodic noise


We can synthetically create periodic noise by overlaying an
image with a trigonometric function,e.g.,:
[x,y]=meshgrid(1:256,1:256);
s=1+sin(x+y/1.5);
Cp=(double(C)/128+s)/4;
where C is the cameraman image from previous sections.
The second line simply creates a sine function, and adjusts its
output to be in the range 0 2.
The last 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.
Jorma Kekalainen

Digital Image Processing

1143

Image with periodic noise, and its


transform
This can be viewed directly with imshow, and it is shown
below .
The extra two spikes away from the center
We can produce its DFT: correspond to the noise just added. In general
>> cpf=fftshift(fft2(cp)); the smaller the period of the noise, the further
from the center the two spikes will be.

Jorma Kekalainen

Digital Image Processing

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.

Removing noise spikes


We will now remove these extra spikes, and invert the result.
If we use data cursor and move it around the image, we find that the
spikes have row, column values of (156,170) and (102,88).
These have the same distance from the center: 49.0918.
We can check this by
>>z=sqrt(x.^2+y.^2);
>> z(156,170)
>> z(102,88)
ans =49.0918
ans =49.0918
There are two methods we can
use to eliminate the spikes
Band reject filtering
Jorma Kekalainen
Notch
filtering

Digital Image Processing

1145

Band reject filtering


We create a filter consisting of ones with a ring of zeroes; the
zeroes lying at a radius of 49 from the center:
br=(z < 47 | z > 51);
where z is the matrix consisting of distances from the origin.
This particular ring will have a thickness large enough to cover the
spikes.
Then as before, we multiply this by the transform:
cpfbr=cpf.*br
The result is that the spikes have been blocked out by this filter.
Taking the inverse transform produces the image
Cpfbri=ifft2(Cpfbr);
figure, fftshow(Cpfbri,'log')

Jorma Kekalainen

Digital Image Processing

1146

Removing periodic noise with a


band-reject filter
Note that not all the noise has gone, but a significant amount
has, especially in the center of the image.
A band-reject filter

Jorma Kekalainen

After inversion

Digital Image Processing

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

Digital Image Processing

1148

Removing periodic noise with a


notch filter
As before, much of the noise in the center has been removed.
Making more rows and columns of the transform zero would result in a
larger reduction of noise.
After inversion
A notch filter

Jorma Kekalainen

Digital Image Processing

1149

Digital Image Processing


Inverse Filtering

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1153

Conclusion and solution


The trouble is that some elements of the Butterworth matrix
are very small, so dividing produces very large values which
dominate the output.
We can deal with this problem in two ways:
1. Apply a low pass filter L to the division:

This should eliminate very low (or zero) values.


2. Constrained division: choose a threshold value d, and if
|F(i, j)| < d, we dont perform a division, but just keep our
original value:

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1155

n=10

It seems that using a low pass filter with a cutoff


round about 50 will yield the best results.
After we use larger cutoffs, the result degenerates.

D=30,40,50,60,70,80

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Inverse filtering using


constrained division
We can try the second method; to implement
it we simply make all values of the filter which
are too small equal to 1 e.g.:
d=0.01;
b=lbutter(C,15,2);b(find(b<d))=1;
C1=fftshift(fft2(Cba))./b;
C1a=abs(ifft2(C1));
imshow(mat2gray(C1a))
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1160

Inverse filtering using constrained


division
d<0.001

d<0.005

d<0.01

It seems that using a


threshold d in the
range 0.005d 0.01
produces reasonable
results.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1163

Motion blurred image


To blur it, we can use the blur parameter of the fspecial
function.
m=fspecial('motion',11,0);
bm=imfilter(b,m);
imshow(bm)

The result of the


blur has effectively
obliterated the text
on the image.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1166

Attempts at removing motion blur


To deblur the image, we need to divide its transform by the
transform corresponding to the blur filter.
This means that we first must create a matrix corresponding
to the transform of the blur:
m2=zeros(256,256);
m2(1,1:11)=m;
mf=fft2(m2);
Now we can attempt to divide by this transform.
bmi=ifft2(fft2(bm)./mf);
fftshow(bmi,'abs')
and the result is shown in the following figure.
Jorma Kekalainen

Digital Image Processing

1167

Attempts at removing motion blur


As with inverse
filtering, the result
is not particularly
good, because the
values close to zero
in the matrix mf
have tended to
dominate the
result.

Jorma Kekalainen

Straight division

Digital Image Processing

1168

Attempts at removing motion blur


As above, we can constrain the division by only dividing by
values which are above a certain threshold e.g.:
d=0.02;
mf=fft2(m2);mf(find(abs(mf)<d))=1;
bmi=ifft2(fft2(bm)./mf);
imshow(mat2gray(abs(bmi))*2)
where the last multiplication by 2 just brightens the result,
which is shown in the following figure.

Jorma Kekalainen

Digital Image Processing

1169

Attempts at removing motion blur


The writing,
especially in the
centre of the image,
is now quite legible.

Jorma Kekalainen

Constrained division

Digital Image Processing

1170

Compare
Original and
blurred

Jorma Kekalainen

Straight and constrained division

Digital Image Processing

1171

Example

%Suppose we take an image and blur it by a small amount.


bc=imread('board.tif');
bg=im2uint8(rgb2gray(bc));
b=bg(100:355,30:285);
figure, imshow(b)
title('Take the color image and make a grayscale version of data type uint8')
xlabel('and pick out a square subimage')
m=fspecial('motion',11,0);% Blurring filter
bm=imfilter(b,m);
figure, imshow(bm)
title('Blurred image with filter fspecial(''motion'',11,0)')
%To deblur the image, we need to divide its transform by the transform corresponding to the blur filter
m2=zeros(256,256);
m2(1,1:11)=m;%
mf=fft2(m2);% First create a matrix corresponding to the transform of the blur
%Now we can attempt to divide by this transform.
bmi=ifft2(fft2(bm)./mf);
figure, fftshow(bmi,'abs')
title('Recovery attempt of the original image through straight division')
d=0.02;% Discrimination threshold
mf=fft2(m2);mf(find(abs(mf)<d))=1;
bmi=ifft2(fft2(bm)./mf);
figure, imshow(mat2gray(abs(bmi))*2);% The last multiplication by 2 just brightens the result
title(['Constrained division using threshold d = ',num2str(d)])

Jorma Kekalainen

Digital Image Processing

1172

Digital Image Processing


Jorma Kekalainen

Digital Image Processing


Color Processing

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

Digital Image Processing

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

Digital Image Processing

1176

Example

Jorma Kekalainen

Digital Image Processing

1177

Built-in colormaps functions


Built-in colormap,
specified as a one of
the colormaps listed in
the table.
The default colormap is
parula.
Versions of MATLAB
prior to R2014b use jet
as the default.
The colormap function
applies the new
colormap with the
same number of colors
as the current
colormap. To change
the number of colors,
use the map input
argument.
Jorma Kekalainen

Digital Image Processing

1178

Example
load flujet
figure,image(X)
title('Using jet colormap')
figure,image(X)
colormap(lines)
title('Using lines colormap')

Jorma Kekalainen

Digital Image Processing

1179

Human visual system


The human visual system is particularly
attuned to two things: edges and color.
The human visual system is not particularly
good at recognizing small changes in gray
values.
For human beings, color provides one of the
most important descriptors of the world
around us.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1181

Physical aspects of color


Visible light is a small part of the electromagnetic spectrum:
EM radiation in which the energy takes the form of waves
of varying wavelength.
EM radiation range from cosmic rays of very short wavelength
to electric power which has very long wavelength.

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

Digital Image Processing

1182

Electromagnetic spectrum and


colors

Jorma Kekalainen

Digital Image Processing

1183

Primary and secondary colors


The human visual system tends to perceive color as being
made up of varying amounts of red, green and blue.
That is, human vision is particularly sensitive to those colors
called the primary colors.
If we add together any two primary colors we obtain the
secondary colors:

Jorma Kekalainen

Digital Image Processing

1184

Additive and subtractive color


mixing
The additive combination of
red, green and blue lights.

The subtractive combination of


yellow, purple and cyan pigments.

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

Digital Image Processing

1186

Primary and secondary colors of


light (Additive color mixing)
Here, secondary colors are mixtures of two primary colors.
yellow = red + green
cyan = green + blue
magenta = red + blue
CRT
LCD
plasma

Jorma Kekalainen

Digital Image Processing

1187

Primary and secondary colors of


pigments (Subtractive color mixing)
A primary color of pigment absorbs one primary color of light
and reflects the others.
red = yellow + magenta
green = cyan + yellow
blue = magenta + cyan
Painting colors

Color printing is a mixture


of additive and subtractive
color mixing
Jorma Kekalainen

Digital Image Processing

1188

Color matching experiment


The amounts of red, green, and blue which make
up a given color can be determined by a color
matching experiment.
In such an experiment, people are asked to match
a given color (a color source) with different
amounts of the additive primaries red, green and
blue.
Such an experiment was performed for the first
time in 1931 by the CIE (Commission
Internationale d'Eclairage), an organization
responsible for color standards.
Jorma Kekalainen

Digital Image Processing

1189

RGB color matching functions


(CIE, 1964)
Note that for some
wavelengths, various
of the red, green or
blue values are
negative.
This is a physical
impossibility, but it
can be interpreted by
adding the primary
beam to the color
source, to maintain a
color match.

Jorma Kekalainen

Digital Image Processing

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:

The inverse transformation is easily obtained by inverting the


matrix:

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1193

XYZ color matching functions

The XYZ color


matching functions
corresponding to
the R, G, B curves.

Jorma Kekalainen

Digital Image Processing

1194

Note on the previous curves


The previous curves were plotted from the data
originated in Colour & Vision Research Laboratories
database.
The CIE required that the Y component
corresponded with luminance, or perceived
brightness of the color.
That is why the row corresponding to Y in the first
matrix (that is, the second row) sums to 1, and also
why the Y curve is symmetric about the middle of the
visible spectrum.
Jorma Kekalainen

Digital Image Processing

1195

Color matching curves according to


CIExyz (1964)

load OmaCIExyz64.mat; % CIE data


w=OmaCIExyz64(:,1);
x=OmaCIExyz64 (:,2);
y=OmaCIExyz64 (:,3);
z=OmaCIExyz64 (:,4);
figure,plot(w,x,'r:*',w,y,'g:*',w,z,'b:*')
text(590,1.25,'X','Fontsize',14, 'Color', 'red')
text(545,1.1,'Y','Fontsize',14, 'Color', 'green')
text(440,2.15,'Z','Fontsize',14,'Color', 'blue')
title('Color matching curves CIE 1964 from ''OmaCIExyz64.mat''')
xlabel('Wavelength [nm]'), ylabel('Tristimulus value')

Jorma Kekalainen

Digital Image Processing

1196

Color matching curves according to


CIExyz (1964)

Jorma Kekalainen

Digital Image Processing

1197

Color matching curves (RGB)


load OmaCIExyz64.mat; % CIE data
x2r=[3.063 -1.393 -0.476;-0.969 1.876 0.042;0.068 -0.229 1.069];
xyz=OmaCIExyz64(:,2:4)';
rgb=x2r*xyz;
w=OmaCIExyz64(:,1);
figure,plot(w,rgb(1,:)','r:*',w,rgb(2,:)','g:*',w,rgb(3,:)','b:*')
text(465,2,'Blue','Fontsize', 14,'Color', 'blue')
text(500,1.7,'Green','Fontsize',14, 'Color', 'green')
text(640,1.7,'Red','Fontsize',14, 'Color', 'red')
title('Color matching curves CIE 1964 from ''OmaCIExyz64.mat''')
ylabel('Tristimulus value'), xlabel('Wavelength [nm]')

Jorma Kekalainen

Digital Image Processing

1198

Color matching curves (RGB)

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1201

Exercise
Show that given x, y, and Y, we can obtain the
tristimulus values X and Z:

Jorma Kekalainen

Digital Image Processing

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

Dividing equation (3) by equation (2 )

Jorma Kekalainen

Z z
=
Y y

z
Z= Y
y

z =1 x y

Digital Image Processing

1 x y
Y
y
1203

Exercise
Show that from equations
x+y+z=1

we obtain the normalized tristimulus values x, y, and


z.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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.

Chromaticity diagram (CIE 1964)

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.

Official CIE 1931 x,y chromaticity


diagram

Jorma Kekalainen

Digital Image Processing

1208

Digital Image Processing


Color models

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

Digital Image Processing

1210

RGB and color cube


In this model, each color is represented as three
values R, G and B , indicating the amounts of red,
green and blue which make up the color.
This model was originally used for computer
displays where three independent light source
are for the red, green and blue component of
each color.
We may imagine all the colors sitting inside a
color cube.
Jorma Kekalainen

Digital Image Processing

1211

RGB color cube

Backsides

Jorma Kekalainen

R=0

G=0

B=0

Digital Image Processing

1212

RGB color cube


The colors along the black-white diagonal, shown in the
diagram as a dotted line, are the points of the space where
all the R, G, B values are equal.
They are the different intensities of gray.
RGB is the standard for the display of colors on computer
monitors and TV sets.
But it is not a very good way of describing colors.
How, for example, would you define light brown using RGB?
Note also from the previous RGB color matching functions
that some colors require negative values of R, G or B.
These colors are not realizable on a computer monitor or
TV set, on which only positive values are possible.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1215

Computing the RGB gamut

load OmaCIExyz64.mat; % CIE data


x2r=[3.063 -1.393 -0.476;-0.969 1.876 0.042;0.068 -0.229 1.069];
for i=1:100,
for j=1:100,
cg(i,j,:)=x2r*[j/100 i/100 1-i/100-j/100]';
if min(cg(i,j,:))<0,
cg(i,j,:)=[1 1 1];
end;
end;
end;
figure, imshow(cg),line(x',y'),axis square,axis xy

title('RGB gamut')
Jorma Kekalainen

Digital Image Processing

1216

RGB gamut

Jorma Kekalainen

Digital Image Processing

1217

Chromaticity diagram and gamut

load OmaCIExyz64.mat; % CIE data


xyz=OmaCIExyz64(:,2:4);
xy=xyz./(sum(xyz')'*[1 1 1]);
x=100*xy(:,1)';
y=100*xy(:,2)';
figure,plot([x x(1)],[y y(1)]),axis square
hold on
xlabel('X coordinate'),ylabel('Y coordinate')
%
x2r=[3.063 -1.393 -0.476;-0.969 1.876 0.042;0.068 -0.229 1.069];
cg=zeros(100,100,3);
for i=1:100,
for j=1:100,
cg(i,j,:)=x2r*[j/100 i/100 1-i/100-j/100]';
if min(cg(i,j,:))<0,
cg(i,j,:)=[1 1 1];
end;
end;
end;
imshow(cg),line([x x(1)],[y y(1)]),axis square,axis xy,axis on,axis tight
title('Chromaticity diagram and RGB gamut')

Jorma Kekalainen

Digital Image Processing

1218

Chromaticity diagram and gamut

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1220

Hue, Saturation, Value (HSV)


HSV color model terms have the following
meanings:
Hue: The true color attribute (red, green, blue,
orange, yellow, and so on).
Saturation: The amount by which the color has
been diluted with white.
The more white in the color, the lower the saturation.
So a deep red has high saturation, and a light red (a
pinkish color) has low saturation.

Value: The degree of brightness: a bright color


has high intensity; a dark color has low intensity.
Jorma Kekalainen

Digital Image Processing

1221

HSV (Hue, Saturation, Value)


This model is a quite natural method of
describing colors.
Since the intensity (value) is independent
of the color information, this is a very
useful model for image processing.
We can visualize this model as a cone.
Any point on the surface represents a
purely saturated color.
The saturation is thus given as the
relative distance to the surface from the
central axis of the structure.
Hue is defined to be the angle
measurement from a pre-determined
axis.
Jorma Kekalainen
Digital Image Processing

1222

Illustration of the HSV color


space

Any point on the surface


represents a purely saturated
color. The saturation is thus
given as the relative distance to
the surface from the central axis
of the structure. Hue is defined
to be the angle measurement
from a pre-determined axis, say
red. Jorma Kekalainen

Digital Image Processing

1223

Convert from HSV to RGB color


space
The HSV color space (Hue, Saturation, Value) is often used by
people who are selecting colors (e.g., of paints or inks) from a color
wheel or palette, because it corresponds better to how people
experience color than the RGB color space does.
The functions rgb2hsv and hsv2rgb convert images between the
RGB and HSV color spaces.
As hue varies from 0 to 1.0, the corresponding colors vary from red
through yellow, green, cyan, blue, magenta, and back to red, so that
there are actually red values both at 0 and 1.0.
As saturation varies from 0 to 1.0, the corresponding colors (hues)
vary from unsaturated (shades of gray) to fully saturated (no white
component).
As value, or brightness, varies from 0 to 1.0, the corresponding
colors become increasingly brighter.

Jorma Kekalainen

Digital Image Processing

1224

Separated color planes of an HSV image


As the hue plane image in the next figure
illustrates, hue values make a linear
transition from high to low. If we compare
the hue plane image against the original
image, we can see that shades of deep
blue have the highest values, and shades
of deep red have the lowest values. (As
stated previously, there are values of red
on both ends of the hue scale.)
Saturation can be thought of as the purity
of a color. As the saturation plane image
shows, the colors with the highest
saturation have the highest values and are Value is roughly equivalent to
brightness, and you will notice that
represented as white. In the center of the
the brightest areas of the value
saturation image, notice the various
plane correspond to the brightest
shades of gray. These correspond to a
colors in the original image.
mixture of colors; the cyans, greens, and
Jorma Kekalainen
Digital Image Processing
1225
yellow shades are mixtures of true colors.

Separate color planes

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')

B = reshape(A,m,n) returns the m-by-n matrix B


whose elements are taken column-wise from A. An
Jorma Kekalainen
Digital Image Processing
error results if A does not have m*n elements.

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

Digital Image Processing

1227

Conversion between RGB and HSV


Suppose a color is specified by its RGB values.
If all the three values are equal, then the color
will be a gray scale; that is, an intensity of white.
Such a color, containing just white, will thus have
a saturation of zero.
Conversely, if the RGB values are very different,
we would expect the resulting color to have a
high saturation.
In particular, if one or two of the RGB values are
zero, the saturation will be one, the highest
possible value.
Jorma Kekalainen

Digital Image Processing

1228

Conversion between RGB and HSV


Hue is defined as the fraction around the circle starting from
red, which thus has a hue of zero.
Reading around the circle produces the following hues:
Color

Hue

Red

Yellow

1/6

Green

1/3

Cyan

1/2

Blue

2/3

Magenta

5/6

Jorma Kekalainen

Digital Image Processing

1229

Conversion between RGB and HSV


Suppose we are given three (R,G,B) values, which we
suppose to be between 0 and 1.
So if they are between 0 and 255, we first divide
each value by 255.
We then define:

Jorma Kekalainen

Digital Image Processing

1230

Conversion between RGB and HSV


To obtain a value for Hue, we consider several cases:

In the particular case (R,G,B) = (0,0,0), for which both V==0,


we define (H,S,V)=(0,0,0).
Jorma Kekalainen

Digital Image Processing

1231

Example

For example, let it be (R,G,B)=(0.1,0.3,0.5)


Now V=max(R,G,B)=0.5
=max(R,G,B)-min(R,G,B)=0.5-0.1=0.4,
S= /V=0.4/0.5= 0.8

Since B=V then

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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

1.0000 -0.2727 -0.6468


1.0000 -1.1037 1.7006

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

>> 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

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1246

Examples

Convert RGB image to NTSC and back.


RGB = imread('board.tif');
NTSC = rgb2ntsc(RGB);
RGB2 = ntsc2rgb(NTSC);
figure,imshow(RGB)
title('RGB')
figure,imshow(NTSC)
title('NTSC')
figure,imshow(RGB2)
title('RGB2')

Jorma Kekalainen

Digital Image Processing

1247

Example

Jorma Kekalainen

Digital Image Processing

1248

Color images in Matlab


Since a color image requires three separate
items of information for each pixel, a (true)
color image of size m*n is represented in
Matlab by an array of size m*n*3 which is a
three dimensional array.
We can think of such an array as a single
entity consisting of three separate matrices
aligned vertically.
Jorma Kekalainen

Digital Image Processing

1249

Three dimensional array for an


RGB image

Jorma Kekalainen

Digital Image Processing

1250

Convert from HSV to RGB color


space
The rgb2hsv function converts colormaps or
RGB images to the HSV color space.
hsv2rgb performs the reverse operation.
These commands convert an RGB image to the
HSV color space.
RGB = imread('peppers.png');
HSV = rgb2hsv(RGB);

Jorma Kekalainen

Digital Image Processing

1251

RGB color image and its


components

%Suppose we read in an RGB image:


f=imread('flowers.tif');
size(f)
%We can isolate each color component by the colon operator:
f(:,:,1); %The first, or red component
f(:,:,2); %The second, or green component
f(:,:,3); %The third, or blue component
%These can all be viewed with imshow:
imshow(f)
figure,imshow(f(:,:,1))
figure,imshow(f(:,:,2))
figure,imshow(f(:,:,3))

Jorma Kekalainen

Digital Image Processing

1252

RGB color image and its


Color image
components
Note how the colors with particular hues show up
with high intensities in their respective components.
For the bright flowers the red component shows a very high
intensity. The green and blue components show much lower
intensities. Similarly the green leaves show slightly higher
intensity in the green component than the other two.

Red component

Jorma Kekalainen

Green component

Digital Image Processing

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

Digital Image Processing

1254

RGB HSV

Hue

Saturation

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1256

YIQ components

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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);

Because luminance is one of the components of the NTSC format,


the RGB to NTSC conversion is also useful for isolating the gray level
information in an image.
In fact, the toolbox functions rgb2gray and ind2gray use the
rgb2ntsc function to extract the grayscale information from a color
image.
For example, these commands are equivalent to calling rgb2gray.
YIQ = rgb2ntsc(RGB);
I = YIQ(:,:,1);
Note:
InKekalainen
the YIQ color space, I is Digital
oneImage
of the
two color components, not the
Jorma
Processing
1259
grayscale component.

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

Digital Image Processing

1260

Example

Jorma Kekalainen

Digital Image Processing

1261

YCbCr color space


The YCbCr color space is widely used for digital video.
In this format, luminance information is stored as a
single component (Y), and chrominance information is
stored as two color-difference components (Cb and
Cr).
Cb represents the difference between the blue
component and a reference value.
Cr represents the difference between the red
component and a reference value.
YCbCr data can be double precision, but the color
space is particularly well suited to uint8 data.
Note:
YUV,
another color space widely
used
for digital video, is very similar to YCbCr
Jorma
Kekalainen
Digital
Image Processing
1262
but not identical.

YCbCr color space


For uint8 images, the data range for Y is [16, 235], and
the range for Cb and Cr is [16, 240].
YCbCr leaves room at the top and bottom of the full
uint8 range so that additional (nonimage) information
can be included in a video stream.
The function rgb2ycbcr converts colormaps or RGB
images to the YCbCr color space and ycbcr2rgb
performs the reverse operation.
For example, these commands convert an RGB image
to YCbCr format.
RGB = imread('peppers.png');
YCBCR = rgb2ycbcr(RGB);
Jorma Kekalainen

Digital Image Processing

1263

Digital Image Processing


Pseudocoloring

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

Digital Image Processing

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

Digital Image Processing

1266

Gray - color transformations


We have three functions fR(x), fG(x), fB(x)
which assign red, green and blue values to
each gray level x.
These values (with appropriate scaling, if
necessary) are then used for display.
Using an appropriate set of functions can
enhance a gray-scale image with impressive
results.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

1269

Color maps

hsv - Hue-saturation-value color map.


hot - Black-red-yellow-white color map.
gray - Linear gray-scale color map.
bone - Gray-scale with tinge of blue color map.
copper - Linear copper-tone color map.
pink - Pastel shades of pink color map.
white - All white color map.
flag - Alternating red, white, blue, and black
color map.
lines - Color map with the line colors.
colorcube - Enhanced color-cube color map.
vga - Windows colormap for 16 colors.
jet - Variant of HSV.
prism - Prism color map.
cool - Shades of cyan and magenta color map.
autumn - Shades of red and yellow color map.
spring - Shades of magenta and yellow color
map.
winter - Shades of blue and green color map.
summer - Shades of green and yellow color
map.

Jorma Kekalainen

Digital Image Processing

1270

Applying a color map to a grayscale


image
We created the color image with:
[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))''')

Jorma Kekalainen

Digital Image Processing

1271

Example: Image spine.tif

% 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

Digital Image Processing

1272

Jorma Kekalainen

Digital Image Processing

1273

Applying a color map to a grayscale


image

We create the color image with:


[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))''')

Jorma Kekalainen

Digital Image Processing

1274

Creating simple colormap


We can easily create our own colormap: it must be a matrix
with 3 columns, and each row consists of RGB values between
0.0 and 1.0.
Suppose we wish to create a blue, magenta, green, red
colormap.
Using the RGB values:

we can create our colormap with:


mycolormap=[0 0 1;1 0 1;0 1 0;1 0 0];
Jorma Kekalainen

Digital Image Processing

1275

Image colored with a handmade


colormap
Before we apply it to the image,
we need to scale the image
down so that there are only the
four grayscales 0, 1, 2 and 3:
b4=grayslice(b,4);
figure
imshow(b4,mycolormap)
The result is shown beside.

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1277

Jorma Kekalainen

Digital Image Processing

1278

imfinfo('palikat.png')
>> imfinfo('palikat.png')
ans =

Filename: 'C:\Users\ktkekjo\Documents\MATLAB\palikat.png'

FileModDate: '19-Aug-2015 01:49:20'

FileSize: 39932

Format: 'png'

FormatVersion: []

Width: 307

Height: 308

BitDepth: 8

ColorType: 'indexed'

FormatSignature: [137 80 78 71 13 10 26 10]

Colormap: [256x3 double]

Histogram: []

Jorma Kekalainen

Digital Image Processing

1279

Digital Image Processing


Processing of Color
Images

Processing of color images


There are two methods we can use:
1. we can process each R, G, B matrix separately,
2. we can transform the color space to one in which
the intensity is separated from the color, and process
the intensity component only.

Schemas for these are given in the following


diagrams.
We shall consider some of different image
processing tasks, and apply the above methods to
color images.
Jorma Kekalainen

Digital Image Processing

1281

RGB and intensity processing

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

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

Digital Image Processing

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);

Now we have to convert from RGB to YIQ, so as


to be able to isolate the intensity component:
cn=rgb2ntsc(c);
Jorma Kekalainen

Digital Image Processing

1285

RGB YIQ

Jorma Kekalainen

Digital Image Processing

1286

Histogram equalization to the


intensity component
Now we apply histogram Intensity processing
equalization to the
intensity component, and
convert back to RGB for
display:
cn(:,:,1)=histeq(cn(:,:,1));
c2=ntsc2rgb(cn);
imshow(c2)

The result is shown beside.

Jorma Kekalainen

Whether this is an improvement is


debatable, but contrast has enhanced and
Digital Image Processing
the green color has nearly washed1287
out

Intensity components before and


after histogram equalization

Jorma Kekalainen

Digital Image Processing

1288

imfinfo('forest.tif')

>> imfinfo('forest.tif')

ans =

Filename: [1x64 char]

FileModDate: '04-joulu-2000 14:57:58'

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'

PhotometricInterpretation: 'RGB Palette'

StripOffsets: [1x17 double]

SamplesPerPixel: 1

RowsPerStrip: 18

StripByteCounts: [1x17 double]

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));

Now we have to put them all back into a single 3 dimensional


array for use with imshow.
The cat function is what we want:
c3=cat(3,cr,cg,cb);
imshow(c3)

Note: cat function concatenates arrays along specified dimension


C = cat(dim, A1, A2, A3, A4, ...)
Digital Image Processing
1290
The Jorma
firstKekalainen
variable to cat is the dimension
along which we want our arrays to be joined.

Histogram equalization of a color


image
It seems that the green
color is totally washed
out.

Jorma Kekalainen

Using each RGB


component

Digital Image Processing

1291

Comparison

Jorma Kekalainen

Digital Image Processing

1292

Example: Histogram equalization

% Test with true color


c=imread('pep.jpg');
figure,imshow(c)
title('Original truecolor image ''pep.jpg''')
cn=rgb2ntsc(c);
figure, imshow(cn)
title('RGB image converted to YIQ image')
figure,imshow(cn(:,:,1))
title('Intensity component (Y=luminance)
before its histogram equalization')
% we apply histogram equalization to the
intensity component,
% and convert back to RGB for
cn(:,:,1)=histeq(cn(:,:,1));
figure,imshow(cn(:,:,1))
title('Intensity component (Y) after its
histogram equalization')
c2=ntsc2rgb(cn);
figure,imshow(c2)
title('Image after intensity component (Y)
equalization')

Jorma Kekalainen

title('Intensity component (Y) after its


histogram equalization')
c2=ntsc2rgb(cn);
figure,imshow(c2)
title('Image after intensity component (Y)
equalization')
% we try to apply histogram equalization to
each of the RGB components
cr=histeq(c(:,:,1));
cg=histeq(c(:,:,2));
cb=histeq(c(:,:,3));
% we have to put them all back into a single
3 dimensional array for use with imshow.
%The cat function is what we want
c3=cat(3,cr,cg,cb);
figure,imshow(c3)
title('Histogram equalization applied to all
RGB components')

Digital Image Processing

1293

Example
Original RGB image

Colors are considerably


washed out

Jorma Kekalainen

Digital Image Processing

Some false colors have


been introduced

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

Digital Image Processing

1295

Example

Jorma Kekalainen

Digital Image Processing

1296

High pass filtering


For a high pass filter, for
example an unsharp masking
filter, may sometimes be better
working with the intensity
component only:

cn=rgb2ntsc(c);
a=fspecial('unsharp');
cn(:,:,1)=filter2(a,cn(:,:,1));
cu=ntsc2rgb(cn);
imshow(cu)

The result is shown beside.


Jorma Kekalainen

Digital Image Processing

1297

Jorma Kekalainen

Digital Image Processing

1298

Jorma Kekalainen

Digital Image Processing

1299

Example

Jorma Kekalainen

Digital Image Processing

1300

Jorma Kekalainen

Digital Image Processing

1301

Conclusion: Spatial filtering of a


color image
In general, we will obtain reasonable results
using the intensity component only.
Although we can apply a filter to each of the
RGB components, as we did for the example
above, we cannot be guaranteed a good
result.
The problem is that any filter will change the
values of the pixels, and this may introduce
unwanted colors.
Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1303

Original and noisy image

Jorma Kekalainen

Digital Image Processing

1304

Noise on a color image


Salt & pepper
noise

Red
component

Green
component

Blue
component

Jorma Kekalainen

Digital Image Processing

1305

Denoising each RGB component


We can apply 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)

The result is beside.


Jorma Kekalainen

Digital Image Processing

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)

Noise has been clearly


diminished, but it is still
there. As we see from the
following slice.
Jorma Kekalainen

Digital Image Processing

1307

Jorma Kekalainen

Digital Image Processing

1308

Example: Noise reduction

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

Digital Image Processing

1309

Heavy noise

Jorma Kekalainen

Digital Image Processing

1310

Reduction of heavy noise heavy noise

Jorma Kekalainen

Digital Image Processing

1311

Example: Reduction of heavy noise

p=imread('pears.png');

figure, imshow(p)

title('Original image')

d=0.2;% Noise increased to d*100%

pn=imnoise(tw,'salt & pepper',d);

figure,imshow(tn)

title(['Noisy image (',num2str(d*100),')% from pixel corrupted'])

figure,imshow(pn(:,:,1))

title('Red component')

figure,imshow(pn(:,:,2))

title('Green component')

figure,imshow(pn(:,:,3))

title('Blue component')

% Apply 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')

% We remove the noise from Y only

pnn=rgb2ntsc(pn);

pnn(:,:,1)=medfilt2(pnn(:,:,1));

pm2=ntsc2rgb(pnn);
Jormafigure,imshow(pm2)
Kekalainen
Digital Image Processing

title('Median filtered luminance (Y) component only')

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

Digital Image Processing

1314

Continued on the following page

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

Digital Image Processing

1315

Note

Jorma Kekalainen

Digital Image Processing

1316

Note

Jorma Kekalainen

Digital Image Processing

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

Digital Image Processing

1318

Applying the edge function to the


intensity component only
To implement the first
method, we start with the
rgb2gray function:

fe1: Edges after rgb2gray

fg=rgb2gray(f);
fe1=edge(fg);
figure,imshow(fe1)

Recall that edge with no


parameters implements
Sobel edge detection.
The result is shown beside.

Jorma Kekalainen

Digital Image Processing

1319

Applying the edge function to


each of the RGB components
For the second method, we can join the
results with the logical or:
f1=edge(f(:,:,1));
f2=edge(f(:,:,2));
f3=edge(f(:,:,3));
fe2=f1 | f2 | f3;
figure,imshow(fe2)

Result is shown beside.


Jorma Kekalainen

Digital Image Processing

1320

Example

% Edge detection (Sobel method)


% An edge image will be a binary image containing the edges of the input image.
% We can go about obtaining an edge image in two ways
% To implement the first method, we start with the rgb2gray function
f=imread('pep.jpg');
figure,imshow(f)
title('Original')
fg=rgb2gray(f);% First method: Take the intensity component only,
fe1=edge(fg);% and apply the edge function to it
figure,imshow(fe1)
title('Edge applied to the intensity component only taking first rgb2gray conversion')
% Note edge with no parameters implements Sobel edge detection
% Second method: Apply the edge function to each of the RGB components
f1=edge(f(:,:,1));
f2=edge(f(:,:,2));
f3=edge(f(:,:,3));
fe2=f1 | f2 | f3;% Join the results with the logical "or"
figure,imshow(fe2)
title('Edge function is applied to each of the RGB components, and joined the results')

Jorma Kekalainen

Digital Image Processing

1321

Comment
The edge image fe2 is a
much more complete
edge image than fe1.

Jorma Kekalainen

Digital Image Processing

1322

Example

% Edge detection (Canny method)


% An edge image will be a binary image containing the edges of the input image.
% We can go about obtaining an edge image in two ways
% To implement the first method, we start with the rgb2gray function
f=imread('pep.jpg');
figure,imshow(f)
title('Original')
fg=rgb2gray(f);% First method: Take the intensity component only,
fe1=edge(fg, 'canny');% and apply the edge function to it
figure,imshow(fe1)
title('Edge applied to the intensity component only taking first rgb2gray conversion')
% Note edge with no parameters implements Sobel edge detection
% Second method: Apply the edge function to each of the RGB components
f1=edge(f(:,:,1), 'canny');
f2=edge(f(:,:,2), 'canny');
f3=edge(f(:,:,3), 'canny');
fe2=f1 | f2 | f3;% Join the results with the logical "or"
figure,imshow(fe2)
title('Edge function is applied to each of the RGB components, and joined the results')

Jorma Kekalainen

Digital Image Processing

1323

As a reference: Canny method

Jorma Kekalainen

Digital Image Processing

1324

Exam Up to This
Effective Image File
Compression Methods

You might also like