Lecture 4: Morphological Image Processing
Lecture 4: Morphological Image Processing
Harvey Rhody
Chester F. Carlson Center for Imaging Science
Rochester Institute of Technology
[email protected]
Abstract
We will develop morphological image processing techniques that
use combinations and sequences of operations. These techniques are
useful in a wide range of applications.
DIP Lecture 4
Morphological Algorithms
• Boundary Extraction
• Region Filling
• Connected Components
• Hit-or-Miss Transform
• Convex Hull
DIP Lecture 4 1
Region Filling
Develop an algorithm to fill in bounded regions of background color.
until Xk = Xk−1.
DIP Lecture 4 2
Region Filling Program
sa=Size(A,/dim)
X=BytArr(sa[0],sa[1]) & X[start]=1b
Y=BytArr(sa[0],sa[1])
Ac=A EQ 0
count=0
WHILE ((min(X EQ Y) EQ 0) AND (count LT max iterations)) DO BEGIN
count=count+1
Y=X
X=Dilate(Y,B) AND Ac
ENDWHILE
RETURN,X
END
DIP Lecture 4 3
Example: Connected Regions in Maze
DIP Lecture 4 4
Example: Connected Regions in Maze
DIP Lecture 4 5
Connected Components
We have used a labeling technique to find connected components. Here we will examine a
morphological technique based on set operations.
until Xk = Xk−1
DIP Lecture 4 6
Example: G&W Figure 9.17
DIP Lecture 4 7
Connected Components Program
Function morph cc,A,start,filter=filter,max iterations=max iterations,count=count
DIP Lecture 4 8
Connected Components Search
Let A be a N × M binary image.
1. Initialization
(a) Set B = A
(b) Set C = 0 (an N × M array of zeros.)
(c) ib=Where(B NE 0)
(d) k=1
2. Iteration: While Min(ib) GE 0
(a) X=morph cc(B,ib[0])
(b) C=C+kX & k=k+1
(c) B=B AND (X EQ 0)
(d) ib=Where(B NE 0) (returns ib=-1 when B is empty)
DIP Lecture 4 9
Finding All Connected Components
Shown below is an original image, a color-coded image showing the connected components,
and an index color palette.
DIP Lecture 4 10
Hit-or-Miss Transform
The morphological hit-or-miss transform is a basic tool for shape detection.
The transform is produced by eroding an image with one structuring element and its
complement with another structuring element and then combining the results.
If A is an image and B = (B1, B2) is a pair of structuring elements, then the H-M
transform is
c
(A ~ B) = (A B1) ∩ (A B2)
DIP Lecture 4 11
Hit-or-Miss Transform
Shown below left is an image A and below right is a pair of structuring elements designed
to detect the middle-sized box with the H-M transform.
Structuring elements B1
Image with three shapes and B2
DIP Lecture 4 12
Hit-or-Miss Transform
(Ac B2 ) (A ~ B) = (A B1 ) ∩ (Ac B2 )
DIP Lecture 4 13
Example 1
Use the H-M transform to find the small squares in the image below. Assume that B1 and
B2 are of the exact size to fit the small squares.
DIP Lecture 4 14
Example 1 (cont)
Original Image A C1 = (A B1 ) c
A
DIP Lecture 4 15
Example 2
Find the three different kinds of objects in the image on the left.
To find the small boxes use B = (S1, S2), to find the hollow boxes use C = (S2, S3)
and to find the large boxes use D = (S4, S5).
S1
S2
S3
S4
S5
Original image Search results
DIP Lecture 4 16
Convex Hull
A set A is said to be convex if any two elements can be joined by a straight path that
does not go outside the set.
The convex hull H of a set S is the smallest convex set such that S ⊆ H .
The convex set and convex deficiency are needed in many applications.
DIP Lecture 4 17
Convex Hull Algorithm
The convex hull algorithm uses the H-M transform with four sets of structuring elements,
(Bi, C) as given by the arrays below.
DIP Lecture 4 18
Example: G&W Figure 9.19
The set shown below corresponds to G&W Figure 9.19. The goal is to find the convex hull
H for this set.
DIP Lecture 4 19
Example (cont)
Shown below is the sequence of arrays Xk1. It converges in four steps.
DIP Lecture 4 20
Example (cont)
Shown below is the sequence of arrays Xk2. It converges in two steps.
DIP Lecture 4 21
Example (cont)
Shown below is the sequence of arrays Xk3. It converges in six steps.
DIP Lecture 4 22
Example (cont)
Shown below is the sequence of arrays Xk4. It converges in two steps.
DIP Lecture 4 23
Example (cont)
The union of the four converged arrays is shown below left. The original set is in white
and the convex set is in red. The convex set may be trimmed to the bounding box of the
original set to form the convex hull H as shown on the right in green and white.
DIP Lecture 4 24
Morphological Thinning
Thinning removes pixels from a set until only a narrow set remains. It is used to reveal set
structure in recognition applications.
Y = Xn
X0 = Xn
i c
Xi = Xi−1 ∩ (Xi−1 ~ B ) for i = 1, 2, . . . , n
DIP Lecture 4 25
Morphological Thinning
Each structuring element B i consists of a hit element S i and a miss element T i. A system
of eight pairs B i = (S i, T i) is shown below.
S1 S2 S3 S4 S5 S6 S7 S8
000 000 100 110 111 011 001 000
010 110 110 110 010 011 011 011
111 110 100 000 000 000 001 011
T1 T2 T3 T4 T5 T6 T7 T8
111 011 001 000 000 000 100 110
000 001 001 001 000 100 100 100
000 000 001 011 111 110 100 000
DIP Lecture 4 26
Example: G&W Figure 9.21
The original image is shown in the upper left. One pass of the algorithm is then made,
with each structuring element used one time. For each B i, the S i is shown in white and
the T i is shown in red.
A X1 X2 B1 B2
X3 X4 X5 B3 B4 B5
X6 X7 X8 B6 B7 B8
DIP Lecture 4 27
Example: G&W Figure 9.21 (cont)
the second and third pass with the same algorithm are shown below. There is no change
in X8 from Pass 2 to Pass 3 so the algorithm will stop there.
X0 X1 X2
X3 X4 X5
X6 X7 X8
DIP Lecture 4 28
M THIN IDL Program
FUNCTION M_THIN,A,max_iterations=max_iterations,count=count
;+
;B=m_thin(A)
;performs morphological thinning on a binary image.
;
;KEYWORDS:
;MAX_ITERATIONS (default=20) Set this value to the maximum
; number of iterations to allow.
;
;COUNT returns the number of iterations actually used.
;
;REFERENCE:
;Gonzalez and Woods, Digital Image Processing (2nd Ed), Sec 9.5.5
;
;HISTORY:
;Written for SIMG-782 class by H. Rhody, Sept. 2002.
;-
sa=Size(A)
IF sa[0] NE 2 THEN MESSAGE,’A must be 2D’
IF N_Elements(max_iterations) LE 0 THEN max_iterations=20
B=BytArr(sa[1]+4,sa[2]+4)
B[2:sa[1]+1,2:sa[2]+1]=A NE 0
DIP Lecture 4 29
M THIN (cont)
;STRUCTURING ELEMENTS
S1=[[0b,0b,0b],[0b,1b,0b],[1b,1b,1b]]
T1=[[1b,1b,1b],[0b,0b,0b],[0b,0b,0b]]
S2=[[0b,0b,0b],[1b,1b,0b],[1b,1b,0b]]
T2=[[0b,1b,1b],[0b,0b,1b],[0b,0b,0b]]
S3=Reverse(Transpose(S1),1)
T3=Reverse(Transpose(T1),1)
S4=Reverse(Transpose(S2),1)
T4=Reverse(Transpose(T2),1)
S5=Reverse(S1,2)
T5=Reverse(T1,2)
S6=Reverse(S4,1)
T6=Reverse(T4,1)
S7=Reverse(S3,1)
T7=Reverse(T3,1)
S8=Reverse(S2,1)
T8=Reverse(T2,1)
DIP Lecture 4 30
M THIN (cont)
X=B
Y=BYTARR(sa[1]+4,sa[2]+4)
count=0
WHILE MAX(X NE Y) GT 0 AND count LT max_iterations DO BEGIN
Y=X
X=Y AND NOT Morph_HitOrMiss(X,S1,T1)
X=X AND NOT morph_HitOrMiss(X,S2,T2)
X=X AND NOT morph_HitOrMiss(X,S3,T3)
X=X AND NOT morph_HitOrMiss(X,S4,T4)
X=X AND NOT morph_HitOrMiss(X,S5,T5)
X=X AND NOT morph_HitOrMiss(X,S6,T6)
X=X AND NOT morph_HitOrMiss(X,S7,T7)
X=X AND NOT morph_HitOrMiss(X,S8,T8)
count=count+1
ENDWHILE
RETURN,X[2:sa[1]+1,2:sa[2]+1]
END
DIP Lecture 4 31
Example: Thin the Blobs
The basic structure is captured by the thinned objects (red). The small ends could be removed by further processing to refine the
result.
DIP Lecture 4 32
Morphological Thickening Transform
Thickening is the morphological dual of thinning and is defined by
A B = A ∪ (A ~ B)
The algorithm can be programmed directly from the definition. However, it has been found to be more effective to do thickening
by thinning Ac .
1. Let C = Ac
2. Let D be thinned (using m thin, for example)
3. T = D c is the thickened object.
DIP Lecture 4 33
Example: G&W Figure 9.22
The steps in the thickening algorithm are illustrated in the figure below.
A C = Ac
D T = Dc
DIP Lecture 4 34