You are on page 1of 60

Chapter 9

Morphological Image Processing


Morphological Algorithms

Lecture # 11

©Dr J.Iqbal
Structuring Elements

Structuring Element
B
Fit: All of the pixels in the
structuring element cover on
pixels in the image
A
C Hit: Any one pixel in the
structuring element covers an
on pixel in the image

All morphological processing operations are based on these simple ideas

©Dr J.Iqbal
Hit-or-Miss Transform
A tool for shape detection

Basic Idea:
 Suppose we have a binary image that contains certain
shapes (circles, squares, lines, etc,….) called image A.
 We use another image or matrix to search image A for a
particular pattern of bits. We will call this pattern “shape B”.
 We then search image A for shape B.

 Whenever there is a ‘hit’, we indicate where the center of


shape B was on image A.

©Dr J.Iqbal
Hit-or-Miss Transform
Structuring Element

So far we have considered the SEs where 0s are


treated as Don’t Cares i.e. we focus on the 1s only

1 0 1 0
1 1 1 1 1 1
1 0 1 0

©Dr J.Iqbal
Hit-or-Miss Transform
Extended Structuring Element

Now we will distinguish between the 0s and the Don’t cares

1 1 1
× 0 ×

× 0 ×

e.g. For a ‘Hit’ the 0s of SE should match with 0s


of the underlying image

©Dr J.Iqbal
Hit-or-Miss Transform
Extended Structuring Element: Example

0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0
0 0 0 0 1 1 1 0 1 1 1
×
0 ×

0 1 0 0 1 1 1 1 ×
0 ×

0 1 1 1 1 1 0 1
0 0 1 1 1 1 0 1

Slide the SE on the image and look for the ‘Hit’

How many ‘HITs’ are there

Note: In the book the symbol for the Hit-or-Miss transform is *


©Dr J.Iqbal
Hit-or-Miss Transform

Extended Structuring Element: Example

0 0 0 0 0 0 0 0 ‘Hit’ encountered
0 0 1 1 1 0 0 0
0 0 0 0 1 1 1 0 1 1 1
×
0 ×

0 1 0 0 1 1 1 1 ×
0 ×

0 1 1 1 1 1 0 1
0 0 1 1 1 1 0 1

©Dr J.Iqbal
Hit-or-Miss Transform

Extended Structuring Element: Example

0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0
0 0 0 0 1 1 1 0 1 1 1
×
0 ×

0 1 0 0 1 1 1 1 ×
0 ×

0 1 1 1 1 1 0 1
0 0 1 1 1 1 0 1
‘Hit’ encountered

©Dr J.Iqbal
Hit-or-Miss Transform
Extended Structuring Element: Example

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0
×
0 ×
=
0 1 0 0 1 1 1 1 ×
0 × 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0
0 0 1 1 1 1 0 1 0 0 0 0 0 0 0 0

What we actually did???

©Dr J.Iqbal
Hit-or-Miss Transform

We have searched the pattern in the structuring element in the image.

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
Output: The center of the pattern is 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 and rest everything is 0
0 0 0 0 0 0 0 0

©Dr J.Iqbal
Hit-or-Miss Transform
Example: Search a 100x100 pixel square in an image

How do we search?
 Take an image of size 100x100 (B)
representing a white square
 We search the pattern in the input
image (A)
 If found, we have a “fit”. We mark
the center of the “Hit” with a
white pixel
 In the above example, there would be
only 1 fit

©Dr J.Iqbal
Hit-or-Miss Transform
Try to find Shape B in Image A
Do you find any problem with this?
If we search for a 100x100 pixel square in an image, we
will have a positive response for all squares greater
than 100x100 as well

Solution ?

Shape B

Image A The pattern will fit at different places

©Dr J.Iqbal
Hit-or-Miss Transformation
 A basic tool for shape detection
Consider the example shown in figure:
 Objective is to find X in the space A
 Background of X is given by (W-X)
 The set of locations for which X
exactly fits inside A is the intersection
of erosion of A by X and the erosion
of Ac by (W-X), i.e.,
𝐴 * 𝐵 = (𝐴 𝑋) ∩ 𝐴𝑐 (𝑊 − 𝑋)

Black = 1, White =0
©Dr J.Iqbal
Morphological Boundary Extraction
The boundary of set A denoted by 𝛽(𝐴) is obtained by first eroding
A by a suitable structuring element B and then taking the difference
between A and its erosion, i.e.
𝛽(𝑎) = 𝐴 − (𝐴 𝐵).

Black = 1, White =0

©Dr J.Iqbal
Morphological Boundary Extraction (example)

𝛽(𝑎) = 𝐴 − (𝐴 𝐵) ⇒ 9.5 − 1
Black = 0, White =1

©Dr J.Iqbal
Region Filling
Given a pixel inside a boundary, region filling attempts to fill that
boundary with object pixels (1s)

Given a point inside here,


can we fill the whole
circle?

©Dr J.Iqbal
Region Filling
 Let A is set containing a subset whose elements are 8-connected
boundary points of a region.
If all boundary points are labeled 1 and non boundary points are
labeled 0, the following procedure fills the region:
– Start from a known point p and taking X0 = p,
– Then taking the next values of Xk as:
𝑋𝑘 = (𝑋𝑘−1 ⊕ 𝐵) ∩ 𝐴𝑐 , 𝑘 = 1,2,3, . . .
B is a suitable structuring element
– Terminate iterations if Xk = Xk-1
– The set union of Xk and A contains the filled set and its
boundaries.
Example: Consider the figure (next page)

©Dr J.Iqbal
Region Filling

0 0 0 0 0 0 0 1 1 1 1 1 1 1
0 1 1 1 1 1 0 1 0 0 0 0 0 1
0 1 0 0 0 1 0 1 0 1 1 1 0 1 0 1 0

0 1 0 0 0 1 0 1 0 1 1 1 0 1 1 1 1
0 1 0
0 1 0 0 0 1 0 1 0 1 1 1 0 1
0 1 1 1 1 1 0 1 0 0 0 0 0 1 B
0 0 0 0 0 0 0 1 1 1 1 1 1 1

A Ac

©Dr J.Iqbal
Region Filling

0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 1 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 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 ⊕ 𝐵)
𝑋𝑘 = (𝑋𝑘−1 ⊕ 𝐵) ∩ 𝐴𝑐
𝑘 = 1,2,3, ⋯

©Dr J.Iqbal
Region Filling

0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0

0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0

0 1 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 1 0 0 0
0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0

𝐴𝑐 𝑋1 = (𝑋0 ⊕ 𝐵) ∩ 𝐴𝑐
𝑋1 = (𝑋0 ⊕ 𝐵)
𝑋𝑘 = (𝑋𝑘−1 ⊕ 𝐵) ∩ 𝐴𝑐
𝑘 = 1,2,3, ⋯

©Dr J.Iqbal
Region Filling
The set union of Xk and A contains the filled set and its boundaries.
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 0 0 1 1 1 0 0

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

©Dr J.Iqbal
Region Filling (Example)
𝑋𝑘 = (𝑋𝑘−1 ⊕ 𝐵) ∩ 𝐴𝑐 , 𝑘 = 1,2,3, . . .

NOTE:
The
intersection of
dilation and the
complement of
A limits the
result to inside
the region of
interest
Black = 1, White =0
©Dr J.Iqbal
Region Filling

Original Image One Region Filled All Regions Filled

©Dr J.Iqbal
Convex Hull (Binary Images)

A set A is said to be convex if any straight line segment


joining two points of A lies within A
Example: R1 is convex as line segment pq lies within set
R1

©Dr J.Iqbal
Convex Hull (Binary Images)

Convex hull H of a set S is the smallest convex set


containing S
Rubber band example:

Convex Hull

©Dr J.Iqbal
Convex Hull (Binary Images)
Convex set: A set A is said to be convex if any straight line
segment joining two points of A lies within A.
Convex Hull: Convex hull H of a set S is the smallest convex
set containing S.
To find the Convex Hull C(A) of a set A the following simple
morphological algorithm can be used:
– Let Bi, where i = 1, 2, 3, 4, represent four structuring elements
– Implement: 𝑋𝑘𝑖 = (𝑋𝑘−1 𝑖 𝑖
* 𝐵𝑖 ) ∪ 𝑋𝑘−1 , 𝑖 = 1,2,3,4 𝑘 = 1,2,3, . . .
– Starting with 𝑋0𝑖 = 𝐴.
– Repeat 2nd step until convergence, i.e.4 𝐷𝑖 = 𝑋𝑐𝑜𝑛𝑣𝑖
→ 𝑋𝑘𝑖 = 𝑋𝑘−1𝑖
.
– Convex Hull of A is given as: 𝐶(𝐴) = 𝐷𝑖
𝑖=1

Equation given in the book is wrong (Errata)

©Dr J.Iqbal
Convex Hull (4 x Structuring Elements)

Black = 1, White =0
©Dr J.Iqbal
Convex Hull (Binary Images)
 Pick the first SE and scan whole image for HIT
 Replace center with one in case of HIT
 Keep iterating until convergence
 Select next SE and repeat above three steps

Start At:

𝑋01 = 𝐴

Find:
𝑋11 = (𝑋01 1 1
* 𝐵 ) ∪ 𝑋0
𝑋21 = (𝑋11 * 𝐵1 ) ∪ 𝑋11 i=SA number
⋮ K= no of iteration
𝐴

Until Convergence 𝑋𝑘𝑖 = (𝑋𝑘−1


𝑖 𝑖 𝑖
* 𝐵 ) ∪ 𝑋𝑘−1
©Dr J.Iqbal
Convex Hull (Binary Images)
𝑋11 = (𝑋01 1 1
* 𝐵 ) ∪ 𝑋0 𝑋𝑘1 = (𝑋𝑘−1
1 1 1
* 𝐵 ) ∪ 𝑋𝑘−1

𝑋21 = (𝑋11 * 𝐵1 ) ∪ 𝑋11


Convergence after four iterations


𝑋𝑘𝑖 = (𝑋𝑘−1
𝑖 𝑖 𝑖
* 𝐵 ) ∪ 𝑋𝑘−1

Call it D1

 Pick the first SE and scan whole image for HIT


i=SE number  Replace center with one in case of HIT
K= no of iteration  Keep iterating until convergence ©Dr J.Iqbal
Convex Hull (Binary Images)

Repeat the same process for B2, B3 and B4

𝑋𝑘𝑖 = (𝑋𝑘−1
𝑖 𝑖 𝑖
* 𝐵 ) ∪ 𝑋𝑘−1 , 𝑖 = 1,2,3,4 𝑘 = 1,2,3, . . .

D2 D3 D4

©Dr J.Iqbal
Convex Hull (Binary Images)

Take the union of all Di to get the convex hull of A

𝐶(𝐴) = 𝐷𝑖
𝑖=1

©Dr J.Iqbal
Convex Hull (Example)

©Dr J.Iqbal
Thinning (Binary Images)
Thinning: Thinning of a set A by the structuring element B can be
defined in terms of hit-or-miss transform as:
𝐴 ⊗ 𝐵 = 𝐴 − (𝐴 * 𝐵) = 𝐴 ∩ (𝐴 * 𝐵)𝑐

where structuring element B is given by the sequence:


{𝐵} = {𝐵1 , 𝐵2 , 𝐵3 , . . . . . . , 𝐵𝑛 }

where Bi is the rotated version of Bi-1.


The thinning can also be defined by a sequence of structuring
elements as: 𝐴 ⊗ {𝐵} = ((. . . . ((𝐴 ⊗ 𝐵1 ) ⊗ 𝐵2 ). . . . . ) ⊗ 𝐵𝑛 ).
The process is to thin A by ONE pass with 𝐵1 , then thin the result
with one pass of 𝐵2 and so on, until A is thinned with one pass of 𝐵𝑛
The entire process is repeated until no further changes occur.

©Dr J.Iqbal
• Thin A by ONE pass with
B1, then thin the result
with one pass of B2 and
so on
• No Iteration with same
SE as we did in convex
Hull
• The entire process is
repeated (iterated) (B1 to
Bn ) until no further
changes occur.
• SE are rotated version
• Zero padded boundry
𝐴 ⊗ 𝐵 = 𝐴 − (𝐴 * 𝐵)

• Grey = 1
• White = 0

• Mythinmain.m

©Dr J.Iqbal
Thinning (Binary Images)

©Dr J.Iqbal
Thinning (Binary Images)

©Dr J.Iqbal
Thickening (Binary Images)

Thickening: As in thinning, thickening of a set A by the structuring


element B can be defined in terms of hit-or-miss transform as:
𝐴 . 𝐵 = 𝐴 ∪ (𝐴 * 𝐵)
In terms of a sequence of structuring elements it is defined as:

𝐴 . {𝐵} = ((. . . . ((𝐴 . 𝐵1) . 𝐵2). . . . . ) . 𝐵𝑛 ).


In practice, the usual procedure is to thin the background of the set
and then complement the result.

To thicken a set A, we form C=Ac , thin C and then form Cc

©Dr J.Iqbal
Thickening (example)
To thicken a set A,
• C=Ac
• thin C
• Cc (complement C)
• Remove
disconnected points

©Dr J.Iqbal
Thickening (example)

• Mythickmain.m

©Dr J.Iqbal
Thickening (example)
Error in the book (Errata)

©Dr J.Iqbal
Skeletons (Binary Images)
To understand the skeleton consider the following figure:

The skeleton S(A) of a set A is set of


all points z, such that
 The largest disk (D)z centered at
point z, and contained in A,
touches the boundary of A at two
or more different points
 One cannot find a larger disk
containing (D)z and included in A.

©Dr J.Iqbal
Skeletons (Binary Images)
In terms of morphological processes (erosion and openings) the
skeletons can be computed as the union of skeleton subsets:
𝐾

𝑆(𝐴) = 𝑆𝑘 (𝐴)
𝑘=0

where the skeleton subsets are: 𝑆𝑘 (𝐴) = (𝐴 𝑘𝐵) − (𝐴 𝑘𝐵) ∘ 𝐵.


Here B is the structuring element and (A kB) indicates the k
successive erosions of A, i.e. (𝐴 𝑘𝐵) = (. . . (𝐴 𝐵) 𝐵). . . . ) 𝐵.
K is the last iterative step before A erodes to an empty set.
A can be reconstructed from skeleton subsets by using the
𝐾
equation: 𝐴= 𝑆 (𝐴) ⊕ 𝑘𝐵
𝑘
𝑘=0

where 𝑆𝑘 (𝐴) ⊕ 𝑘𝐵 denotes k successive dilations of Sk(A), that is,


𝑆𝑘 (𝐴) ⊕ 𝑘𝐵 = ((. . . (𝑆𝑘 (𝐴) ⊕ 𝐵) ⊕ 𝐵) ⊕. . . ) ⊕ 𝐵.
©Dr J.Iqbal
𝑆𝑘 (𝐴) = (𝐴 𝑘𝐵) − (𝐴 𝑘𝐵) ∘ 𝐵.

𝐴 ∘ 𝐵 = (𝐴 𝐵) ⊕ 𝐵.
(opening)
𝐾

𝑆(𝐴) = 𝑆𝑘 (𝐴)
𝑘=0

©Dr J.Iqbal
Pruning
a. Thinning and skeletonizing tend to leave parasitic components.
b. Pruning methods are essential complement to thinning and
skeletonizing procedures

X1  A {B} (1) Thinning

X 2    X1 * Bk 
8
(2) Hit or Miss
k 1

X3   X2  H   A (3) Dilation &


Intersection Applied
H : 3  3 structuring element after each step
of 1s

X 4  X1  X 3 (4) Union

©Dr J.Iqbal
Pruning:
Example (1) Thinning
𝑋1 = 𝐴 ⊗ {𝐵}

𝐴 ⊗ 𝐵 = 𝐴 − (𝐴 * 𝐵) = 𝐴 ∩ (𝐴 * 𝐵)𝑐
{𝐵} = {𝐵1 , 𝐵2 , 𝐵3 , . . . . . . , 𝐵𝑛 }

©Dr J.Iqbal
Pruning:
Example
X 2    X1 * Bk 
8

k 1

(2) Hit or Miss

𝑋1 = 𝐴 ⊗ {𝐵} (1)Thinning
(DONE)
𝐴 ⊗ 𝐵 = 𝐴 − (𝐴 * 𝐵) = 𝐴 ∩ (𝐴 * 𝐵)𝑐
{𝐵} = {𝐵1 , 𝐵2 , 𝐵3 , . . . . . . , 𝐵𝑛 }

©Dr J.Iqbal
Pruning:
Example

𝑋3 = 𝑋 2⊕
(2) Hit𝐻or∩ 𝐴 (3) Dilation (3 times) &
𝐻: 3 × 3 structuring
Miss (DONE) element Intersection after each
step
X 2    X1 * Bk 
8

k 1

©Dr J.Iqbal
Pruning:
Example

(3) Dilation (3 times)


𝑋4 = 𝑋1 ∪ 𝑋3 & Intersection after
each step (DONE)
𝑋3 = 𝑋2 ⊕ 𝐻 ∩ 𝐴
𝐻: 3 × 3 structuring element
©Dr J.Iqbal
Pruning:
Example

©Dr J.Iqbal
Morphological Reconstruction
It involves two images:-
a. One image contains the starting points for the
transformation (The image is called marker)
b. Another image (mask) constrains the transformation
c. Processing is based on connectivity that is defined by
the structuring element
d. Processing repeats until stabilization

©Dr J.Iqbal
Geodesic Dilation
Let F denote the marker image and G the mask image,
F ⊆ G. The geodesic dilation of size 1 of the marker image
(1)
with respect to the mask, denoted by DG (F), is defined as
(1)
DG (F) = F ⊕ B ∩ G

The geodesic dilation of size n of the marker image F


(n)
with respect to G, denoted by DG (F), is defined as
(n) (1) (n−1)
DG (F) = DG DG (F)
(0)
with DG (F) = F.

©Dr J.Iqbal
Geodesic Dilation
(1)
DG (F) = F ⊕ B ∩ G

©Dr J.Iqbal
Geodesic Erosion
Let F denote the marker image and G the mask image.
The geodesic erosion of size 1 of the marker image with
(1)
respect to the mask, denoted by EG (F), is defined as
(1)
EG (F) = F−B ∪G

The geodesic erosion of size n of the marker image F


(n)
with respect to G, denoted by EG (F), is defined as
(n) (1) (n−1)
EG (F) = EG EG (F)
(0)
with EG (F) = F.

©Dr J.Iqbal
Geodesic Erosion
(𝟏)
𝐄𝐆 (𝐅) = 𝐅−𝐁 ∪𝐆

Errata ©Dr J.Iqbal


Reconstruction
Morphological reconstruction byby Dilation
dialtion of a mask image
G from a marker image F, denoted RG (F), isof
D
Morphological reconstruction by dialtion defined
a maskas
image
the geodesic
G from dilation
a marker imageof F,
F with respect
denoted to G,
RDG (F), iteratedas
is defined until
stability is achieved;
the geodesic dilationthat is, respect to G, iterated until
of F with
stability is achieved; that is,
R
D G
D
(F) =DG (F)
(k)
(k)
R G (F) = DG (F)

with k such that DG(k) (F) =DG(k-1) (F).

©Dr J.Iqbal
Reconstruction by Dilation

©Dr J.Iqbal
Reconstruction by Dilation

©Dr J.Iqbal
Reconstruction
Morphological reconstruction byby Erosion
erosion of a mask image
GMorphological
from a markerreconstruction
image F, denoted RG (F), is of
E
by erosion defined
a maskasimage
the geodesic
G from erosion
a marker of FF,with
image respect
denoted to G,
REG (F), is iterated until
defined as
stability is achieved;
the geodesic erosionthat
of F iwith
s, respect to G, iterated until
stability is achieved; that is,
R
E G
E
(F) = EG (F)
(k)
(k)
R G (F) = EG (F)

with k such that EG(k) (F) = EG(k-1) (F).

©Dr J.Iqbal
Opening by Reconstruction
The opening by reconstruction of size n of an image F is
The opening by reconstruction of size n of an image F is
defined as the reconstruction by dilation of F from the
defined as the reconstruction by dilation of F from the
erosion
erosionofofsize
sizennofofF;F;that
thatisis
(𝑛)(n)
𝑂𝑅OR(𝐹) D𝐷
(F) = RF𝐹
= 𝑅 F𝐹-nB 
− 𝑛𝐵

where F -nB  indicates n erosions of F by B.

©Dr J.Iqbal
Opening by Reconstruction
Mask (G) Marker (F)

4x4 SE

©Dr J.Iqbal

You might also like