You are on page 1of 57

EECE\CS 253 Image Processing

Richard Alan Peters II



Department of Electrical Engineering and
Computer Science

Fall Semester 2007
Lecture Notes
Lecture Notes on Mathematical Morphology:
Binary Images
This work is licensed under the Creative Commons Attribution-Noncommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or
send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.
5 August 2012 2 5 August 2012 2 1999-2007 by Richard Alan Peters II
What is Mathematical Morphology?
nonlinear,
built on Minkowski set theory,
part of the theory of finite lattices,
for image analysis based on shape,
extremely useful, yet not often used.
It is:
5 August 2012 3 5 August 2012 3 1999-2007 by Richard Alan Peters II
Uses of Mathematical Morphology
image enhancement
image segmentation
image restoration
edge detection
texture analysis
particle analysis
feature generation
skeletonization
shape analysis
image compression
component analysis
curve filling
general thinning
feature detection
noise reduction
space-time filtering

5 August 2012 4 5 August 2012 4 1999-2007 by Richard Alan Peters II
Notation and Image Def initions
An image is a mapping, I, from a set, S
P
, of pixel coordinates to a set, G, of
values such that for every coordinate vector, p = (r,c) in S
P
, there is a value
I(p) drawn from G. S
P
is also called the image plane.
A binary image has only 2 values. That is, G = {v
fg
, v
bg
}, where v
fg
, is called
the foreground value and v
bg
is called the background value.
Often, the foreground value is v
fg
= 0, and the background is v
bg
= . Other
possibilities are {v
fg
, v
bg
} = {0,}, {0,1}, {1,0}, {0,255}, and {255,0}.
In this lecture we assume that {v
fg
, v
bg
} = {255, 0}, although the fg is often
displayed in different colors for contrast.
5 August 2012 5 5 August 2012 5 1999-2007 by Richard Alan Peters II
Notation and Image Def initions
The foreground of binary image I is
i.e. the set of locations, p, where I(p) = v
fg
. Similarly, the background is
{ } ( ) ( )
{ }
P fg
FG I I , , I( ) , r c S v = = e = p p p
{ } ( ) ( )
{ }
P bg
BG I I , , I( ) . r c S v == = e = p p p
Note that
{ } { } I I BG I FG = { } { } = I BG I FG and C,
and that
{ } { } { }
C
I FG I BG = and { } { } { } . I BG I FG
C
=
The background is the complement of the foreground and vice-versa.
5 August 2012 6 5 August 2012 6 1999-2007 by Richard Alan Peters II
A Binary Image
This represents a digital image. Each square is one pixel.
foreground:
R e
=
c
c
where
) ( I p
0 ) I( = p
background
5 August 2012 7 5 August 2012 7 1999-2007 by Richard Alan Peters II
Support of an Image
( )
{ }
P f
supp I ( , ) I( ) .
g
r c S v = = e = p p
That is, the support of a binary image
is the set of foreground pixel
locations within the image plane.
The complement of the support is,
therefore, the set of background pixel
locations within the image plane.
The support of a binary
image, I, is
( ) { } { }
C
P b
supp I ( , ) I( ) .
g
r c S v = = e = p p
5 August 2012 8 5 August 2012 8 1999-2007 by Richard Alan Peters II
Structuring Element (SE)
A structuring element is a small image used as a moving window
whose support delineates pixel neighborhoods in the image plane.
It can be of any shape, size, or connectivity (more than 1 piece, have
holes). In the figure the circles mark the location of the structuring
elements origin which can be placed anywhere relative to its support.
E
x
a
m
p
l
e

S
E
s

F
G

i
s

g
r
a
y

;

B
G

i
s

w
h
i
t
e

5 August 2012 9 5 August 2012 9 1999-2007 by Richard Alan Peters II
Structuring Element
Let I be an image and Z a SE.
Z+p means that Z is moved so
that its origin coincides with
location p in S
P
.
Z+p is the translate of Z to
location p in S
P
.
The set of locations in the
image delineated by Z+p is
called the Z-neighborhood of
p in I denoted N{I,Z}(p).
5 August 2012 10 5 August 2012 10 1999-2007 by Richard Alan Peters II
Reflected Structuring Elements
( ) ( ) ( )
, , for all , . Z Z _ _ _ = eS
Let Z be a SE and let S be the square of pixel locations
that contains the set {(r,c), (r,c) | (r,c)esupp(Z)}. Then
is the reflected structuring element.
rotated by 180 around its origin. is Z Z
5 August 2012 11 5 August 2012 11 1999-2007 by Richard Alan Peters II
Dilation
5 August 2012 12 5 August 2012 12 1999-2007 by Richard Alan Peters II
Dilation of Binary Images
There are a number of equivalent definitions of dilation. Three of them
that apply to binary images are:
| | { }. I ) ( I
P
= + e = p p Z S Z

C
The set of all pixel locations, p, in the
image plane where the intersection of
+p with I is not empty.
{ }
. ) ( I
I supp

e
+ =
p
p Z Z
The union of copies of the SE, one translated to
each pixel location in the support of the image.
{ }
. ) I ( I
supp

Z
Z
e
+ =
p
p
The union of copies of the image, one translated
to each pixel location in the support of the SE.
5 August 2012 13 5 August 2012 13 1999-2007 by Richard Alan Peters II
Dilation
original image original / dilation dilated image
The locus of pixels peS
P
such that C. = + I ) ( p Z

SE = Z
8

This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 14 5 August 2012 14 1999-2007 by Richard Alan Peters II
Dilation using a Reflected SE
5 August 2012 15 5 August 2012 15 1999-2007 by Richard Alan Peters II
Fast Computation of Dilation
. I I J

Z
Z
e
+ = =
q
q
The fastest way to compute binary dilation is to use the union-of-
translates-of-the-image definition. That is, use
Assume the dimensions of I are RC, the dimensions of Z are NM, and
Zs origin is offset from the upper left hand corner (ULHC) by rows and
_ columns. Allocate a scratch image, T, that is (R+N1)(C+M1) and
initialized to zeros. Then, for each FG pixel loc (v, u) in Z (measured
from the ULHC of Z ) perform a logical OR between I+(v, u) and T. Put
the results in T. When done, copy to J the RC subarray of T starting at
(,_).
5 August 2012 16 5 August 2012 16 1999-2007 by Richard Alan Peters II
Dilation through Image Shifting
5 August 2012 17 5 August 2012 17 1999-2007 by Richard Alan Peters II
Dilation through Image Shifting
The red outlines indicate the positions of the features in the original images.
5 August 2012 18 5 August 2012 18 1999-2007 by Richard Alan Peters II
Erosion
5 August 2012 19 5 August 2012 19 1999-2007 by Richard Alan Peters II
Erosion of Binary Images
There are a number of equivalent definitions of erosion. Three of them
that apply to binary images are:
( )
{ }
. I I
Z supp


e
+ =
p
p Z
The intersection of copies of the image, one translated
to each pixel location in the support of the refl. SE.
The set of all pixel locations, p, in the
image plane where Z+p is contained
in I.
{ } I p p I
p
c + e = Z S Z
The intersection of copies of the refl. SE, one translated
to each pixel location in the support of the image.
( )
{ }
. I
I supp


e
+ =
p
p Z Z
5 August 2012 20 5 August 2012 20 1999-2007 by Richard Alan Peters II
original image erosion / original eroded image
The locus of pixels peS
P
such that . I c +p Z
Erosion
SE = Z
8

This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 21 5 August 2012 21 1999-2007 by Richard Alan Peters II
Fast Computation of Erosion
The fastest way to compute binary erosion is to use the intersection-of-
translates-of-the-image definition. That is, use
Assume the dimensions of I are RC, the dimensions of Z are NM, and
Zs origin is offset from the upper left hand corner (ULHC) by rows and
_ columns. Allocate a scratch image, T, that is (R+N1)(C+M1) and
initialized to zeros. Rotate
1
Z by 180. Then, for each FG pixel loc (v, u)
in (measured from the ULHC of ) perform a logical OR between
I+(v, u) and T. Put the results in T. When done, copy to J the RC
subarray of T starting at (N, M_).
. I I J

Z
Z
e
+ = =
q
q
1
In matlab the fastest way to rotate Z by 180 is
Zrefl = flipud(fliplr(Z));
5 August 2012 22 5 August 2012 22 1999-2007 by Richard Alan Peters II
Comparison of Erosion and Dilation
erosion / original / dilation erosion / original
original contains erosion
original / dilation
dilation contains original
SE = Z
8

This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 23 5 August 2012 23 1999-2007 by Richard Alan Peters II
Erosion from Dilation / Dilation from Erosion
Dilation and erosion are duals of each other with respect to complementation:
That is, dilation with the reflected SE of the complement of a binary image is
the complement of the erosion. Erosion with the reflected SE of the
complement of the image is the complement of the dilation. It follows that,
erosion can be performed with dilation and vice versa. That implies that only
one or the other must be implemented directly.
{ }
c
c
Z Z I I =

{ } . I I
c
c
Z Z =

and
{ }
c
c
Z Z

= I I

{ } , I I
c
c
Z Z

=
and
5 August 2012 24 5 August 2012 24 1999-2007 by Richard Alan Peters II
Opening and Closing
Opening is erosion by Z followed by dilation by Z.
Closing is dilation by followed by erosion by .
( ) Z Z I Z I =
( ) Z Z I Z I

= -
The opening is the best approximation of the image FG that can be made
from copies of the SE, given that the opening is contained in the original.
I Z contains no FG features that are smaller than the SE.
The closing is the best approximation of the image BG that can be made
from copies of the SE, given that the closing is contained in the image BG.
I Z contains no BG features that are smaller than the SE.
5 August 2012 25 5 August 2012 25 1999-2007 by Richard Alan Peters II
Opening
5 August 2012 26 5 August 2012 26 1999-2007 by Richard Alan Peters II
Opening is Erosion Followed by Dilation
opening / original erosion / opening
dilate the erosion
erosion / original
erode the original
SE = Z
8

dilated erosion
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 27 5 August 2012 27 1999-2007 by Richard Alan Peters II
original erosion opening
SE = Z
8

Opening is Erosion Followed by Dilation
original image eroded image dilated erosion
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 28 5 August 2012 28 1999-2007 by Richard Alan Peters II
original image opening / original open image
The union of translates of Z such that . I c +p Z
The opening of I by Z is the best approximation of I that can be made
by taking the union of translated copies of Z, subject to the constraint
that the opening be contained by the original image.
SE = Z
8

Opening
5 August 2012 29 5 August 2012 29 1999-2007 by Richard Alan Peters II
Closing
5 August 2012 30 5 August 2012 30 1999-2007 by Richard Alan Peters II
Closing is Dilation Followed by Erosion
1

closing / original
to get the closing
closing / dilation
erode the dilation
original / dilation
SE = Z
8

1
using the reflected SE,

original image
5 August 2012 31 5 August 2012 31 1999-2007 by Richard Alan Peters II
original dilation closing
SE = Z
8

1
using the reflected SE,

Closing is Dilation Followed by Erosion
1

original image dilated image eroded dilation
5 August 2012 32 5 August 2012 32 1999-2007 by Richard Alan Peters II
Duality Relationships
| |
| |
| |
| |
C
C
C
C
C
C
C
C
Z Z
Z Z
Z Z
Z Z

I I
I I
I I
I I
= -
- =
=
= Erosion in terms of dilation:
Dilation in terms of erosion:
Opening in terms of closing:
Closing in terms of opening:
I
C
is the complement of I and is the reflected SE.
5 August 2012 33 5 August 2012 33 1999-2007 by Richard Alan Peters II
Binary Ops with Asymmetric SEs
L shaped SE
O marks origin
Foreground: white pixels
Background: black pixels
Cross-hatched
pixels are
indeterminate.
5 August 2012 34 5 August 2012 34 1999-2007 by Richard Alan Peters II
C
I
- C
SE
+1
Since morph. ops.
are neighborhood
ops., there is a
band of pixels
around the border
of the resultant
image where the
values are
indeterminate.
Border Effects
deterministic region
co
SE
C
I
- C
SE
+co
SE
r
o
S
E

R
I

-

R
S
E

+
r
o
S
E

Erosion & Dilation
R
I

-

R
S
E

+
1

C
SE

R
SE

The actual values of pixels in
the indet. region depend on
the specific algorithm used.
5 August 2012 35 5 August 2012 35 1999-2007 by Richard Alan Peters II
Since opening &
closing iterate
erosion & dilation,
the boundaries of
the deterministic
region are 2x as
far from the
image border as
are those of
erosion or
dilation.
Border Effects
Opening & Closing
C
I
- 2C
SE
+1
deterministic
region
C
SE
C
I
- C
SE
+1

R
S
E

R
I

-

R
S
E

+
1

R
I

-

2
R
S
E

+
1

5 August 2012 36 5 August 2012 36 1999-2007 by Richard Alan Peters II
Boundary Extraction
original
binary image
erosion by square
8-connected SE
difference
4-conn inside bdry
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 37 5 August 2012 37 1999-2007 by Richard Alan Peters II
Boundary Extraction
original
binary image
erosion by plus
4-connected SE
difference
8-conn inside bdry.
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 38 5 August 2012 38 1999-2007 by Richard Alan Peters II
Boundary Extraction
original
binary image
dilation by plus
4-connected SE
difference
8-conn outside bdry.
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 39 5 August 2012 39 1999-2007 by Richard Alan Peters II
Boundary Extraction
original
binary image
dilation by square
8-connected SE
difference
4-conn outside bdry
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 40 5 August 2012 40 1999-2007 by Richard Alan Peters II
Boundary Extraction
original
binary image
in erosion by plus
erosion by square is
in 4-conn inside bdry
8-conn inside bdry is
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 41 5 August 2012 41 1999-2007 by Richard Alan Peters II
Boundary Extraction
original
binary image
in dilation by square
dilation by plus is
in 4-conn outside bdry
8-conn outside bdry is
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 42 5 August 2012 42 1999-2007 by Richard Alan Peters II
Boundary Extraction
8-bdry/4-bdry/orig
inside boundaries
all 4 boundaries
are disjoint from
orig/8-bdry/4-bdry
outside boundaries
This is a piece of a larger image. Boundary effects are not apparent
5 August 2012 43 5 August 2012 43 1999-2007 by Richard Alan Peters II
Conditional Dilation
original image mask over original dilated original
dilation inside a mask
5 August 2012 44 5 August 2012 44 1999-2007 by Richard Alan Peters II
Conditional Dilation
mask
over
dilated
masked
dilated
masked
dilated
union with
original
conditionally
dilated with
respect to
mask
5 August 2012 45 5 August 2012 45 1999-2007 by Richard Alan Peters II
Connected Component Extraction
tag
dilated
tag
masked
dilated
tag
tags
dilated
tags
masked
dilated
tags
5 August 2012 46 5 August 2012 46 1999-2007 by Richard Alan Peters II
Connected Component Extraction
tags
dilated
tags
masked
tags
dilated
tags
masked
dilated
tags
connected
component
5 August 2012 47 5 August 2012 47 1999-2007 by Richard Alan Peters II
Binary Reconstruction
Removes of small regions that are disjoint from larger objects
without distorting the small features of the large objects.
original opened reconstructed
Used after opening to grow back pieces of the original image
that are connected to the opening.
5 August 2012 48 5 August 2012 48 1999-2007 by Richard Alan Peters II
Algorithm for Binary Reconstruction
1. J = I o Z, where Z is any SE.
2. T = J,
3. J = J Z
k
, where k=4 or k=8,
4. J = I AND J,[Take only those pixels from J that are also in I .]
5. if J = T then go to 2,
6. else stop; [ J is the reconstructed image. ]
This is the same as connected component extraction with the opened
image, J, containing the tags. The choice of Z
k
determines the
connectivity of the result.

5 August 2012 49 5 August 2012 49 1999-2007 by Richard Alan Peters II
Algorithm for Binary Reconstruction
1. J = I o Z, where Z is any SE.
2. T = J,
3. J = J Z
k
, where k=4 or k=8,
4. J = I AND J,[Take only those pixels from J that are also in I .]
5. if J = T then go to 2,
6. else stop; [ J is the reconstructed image. ]
This is the same as connected component extraction with the opened
image, J, containing the tags. The choice of Z
k
determines the
connectivity of the result.

Usually a program for reconstruction
will take both J and I as inputs. E.g,

K = ReconBin(I,J,Z);

Then the algorithm starts at step 2.
5 August 2012 50 5 August 2012 50 1999-2007 by Richard Alan Peters II
Skeletonization
Let Skel(I,r) be the set of pixels in I such that if p _ Skel(I,r)
then D
p
(r), is a maximal disk of radius r in I. That is, Skel(I,r)
is the locus of centers of maximal disks of radius r in I. Then
) , I ( Skel
0
r S
r

=
=
That is, the skeleton of I is the union of all the sets of centers of
maximal disks.
Note that for any actual image I , the union will not be infinite,
since I is bounded (not infinite in extent).
5 August 2012 51 5 August 2012 51 1999-2007 by Richard Alan Peters II
Skeletonization
Original shape Raw skeleton (red)
is the locus of centers of
maximal disks.
Pruned and connected
skeleton
5 August 2012 52 5 August 2012 52 1999-2007 by Richard Alan Peters II
Skeletonization: Maximal Disks
maximal disks (red)
non max disks (blue)
non max & max disks
over skeleton
non maximal disks
disks are squares
The maximal disk at pixel loc p is the largest disk in the fg that includes p.
5 August 2012 53 5 August 2012 53 1999-2007 by Richard Alan Peters II
Skeletonization: Maximal Disks
maximal disks (red)
non max disks (blue)
non max & max disks
over skeleton
non maximal disks
disks are squares
The maximal disk at pixel loc p is the largest disk in the fg that includes p.
5 August 2012 54 5 August 2012 54 1999-2007 by Richard Alan Peters II
Computation of the Skeleton
o
r
i
g
i
n
a
l

e
r
o
d
e

n
=
0

e
r
o
d
e

n
=
1

e
r
o
d
e

n
=
2

e
r
o
d
e

n
=
3

o
p
e
n

a
b
o
v
e


w
/

Z

(
n
=
1
)

o
p
e
n

a
b
o
v
e


w
/

Z

(
n
=
1
)

o
p
e
n

a
b
o
v
e


w
/

Z

(
n
=
1
)

o
p
e
n

a
b
o
v
e


w
/

Z

(
n
=
1
)

t
o
p

-

m
i
d
d
l
e

t
o
p

-

m
i
d
d
l
e

t
o
p

-

m
i
d
d
l
e

t
o
p

-

m
i
d
d
l
e

u
n
i
o
n

o
f

a
l
l

4


t
o

t
h
e

r
i
g
h
t

s
k
e
l
e
t
o
n

SE = Z
8
= = Sq(3)

n = 0: SE = 1 pixel
n = 1: SE = Sq(3)
n = 2: SE = Sq(5)
n = 3: SE = Sq(7)
Note that the
result is
disconnected
and has
spurious points. S
k
e
l
(

I
,

0

)

S
k
e
l
(

I
,

1

)

S
k
e
l
(

I
,

2

)

S
k
e
l
(

I
,

3

)

5 August 2012 55 5 August 2012 55 1999-2007 by Richard Alan Peters II
Skeletonization: Delete Spurious Pixels
def. spurious pixels as
conn. comp. of < 3 pix.
pruned skeleton
raw less spurious
raw skeleton
has spurious pixels
5 August 2012 56 5 August 2012 56 1999-2007 by Richard Alan Peters II
Skeletonization: Reconnect Components
2 other components
of pruned skel dilated.
Intersection of
dilated components.
2 components of
pruned skel. dilated.
5 August 2012 57 5 August 2012 57 1999-2007 by Richard Alan Peters II
Skeletonization
raw skeleton pruned skeleton reconnected skeleton

You might also like