07 - Morphology
07 - Morphology
07 - Morphology
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
3 What Is Morphology?
• Morphological image processing (or morphology)
describes a range techniques for extracting image
components that are useful in the representation &
description of region shape such as boundaries,
skeletons etc.
• Some of the operations are frequently applied as
post-processing to remove imperfections introduced
during segmentation, and so typically operate on
binary images.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
4 Quick Example 1
Problem here
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
5 Quick Example 2
om Gonzalez & Woods, Digital Image Processing (2002)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
6 Preliminaries
Language of mathematical morphology is set theory
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
7 Preliminaries
Logic operations involving binary images
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
8 Preliminaries
Reflection and Translation:
ˆ = {w | w −b,
B for b B}
( A) z = {c | c a + z , for a A}
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
9 Structuring Elements
• Structuring elements are small sets/sub-images
used to probe an image under study
• For each SE, define its origin
• Shape and size must be adapted to geometric
properties for the objects
– For simplicity we will use rectangular structuring elements
with their origin at the middle pixel
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
10 Structuring Elements, Hits & Fits
B Structuring Element
Fit: All on pixels in the
structuring element cover
A on pixels in the image
C
Hit: Any on pixel in the
structuring element covers
an on pixel in the image
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
11 Fitting & Hitting
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1
0 0 0 1 1 0 0 0 0 0 0 0
1 1 1
0 0 1 B1 1 1 1 0 C
0 0 0 0
1 1 1
0 1 1 1 1 1 1 1 0 0 0 0
Structuring
0 1 1 1 1 1 1 1 0 0 0 0 Element 1
0 0 1 1 1 1 1 1 0 0 0 0 0 1 0
0 0 1 1 1 1 1 1 1 0 0 0 1 1 1
0 0 1 1 1 1 1 A1 1 1 1 0 0 1 0
0 0 0 0 0 1 1 1 1 1 1 0 Structuring
0 0 0 0 0 0 0 0 0 0 0 0 Element 2
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
12 Fundamental Operations
• Fundamentally morphological image processing is
very much like spatial filtering
• The structuring element is moved across every
pixel in the original image to give a pixel in a new
processed image
• The value of this new pixel depends on the
operation performed
• There are two basic morphological operations:
erosion and dilation
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
13 Erosion
Erosion of image A by structuring element B represented
as sets in Z2, is defined as:
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
15 Erosion Example
Original Image Processed Image With Eroded Pixels
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
16 Erosion Example 1
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
17 What Is Erosion For?
Erosion can split apart joined objects
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
19 Erosion Example 3
After erosion
Original with a disc of
image radius 15
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
20 Dilation
Dilation of image A by structuring element B is given by:
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
22 Dilation Example
Original Image Processed Image With Dilated Pixels
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
23 Dilation Example 1
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
24 Dilation Example 2
Original image After dilation
Structuring element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
25 Dilation Example 3
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
26 Quiz:
What is the result of this dilation?
0 0 0 0 0 1 0
0 1 1 0 ⊕ 0 1 1 =
0 0 0 0 0 0 0
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
27 Quiz:
0 0 0 0 0 1 0
0 1 1 0
0 1 1 0 ⊕ 0 1 1 = 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
28 What Is Dilation For?
Dilation can repair breaks
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
31 Opening
The opening of image f by structuring element s,
denoted f ○ s is simply an erosion followed by a
dilation
f ○ s = (f s) s
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
33 Opening Example
Original Image Processed Image
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
34 Opening Examples
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
35 Closing
The closing of image f by structuring element s,
denoted f • s is simply a dilation followed by an erosion
f • s = (f s)s
Structuring Element
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
37 Closing Example
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
38 Morphological Processing Example
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
39 Morphological Algorithms
Using the simple technique we have looked at so far
we can begin to consider some more interesting
morphological algorithms such as:
– Boundary extraction
– Region filling
– Extraction of connected components
– Hit or Miss Transformation
– Convex Hull
– Thinning/thickening
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
40 Boundary Extraction
Extracting the boundary (or outline) of an object is
often extremely useful
The boundary can be given simply as
β(A) = A – (AB)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
41 Boundary Extraction Example
A simple image and the result of performing boundary
extraction using a square 3*3 structuring element
6 Connected Components
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
43
Connectivity
Two points in an image are 'connected' if a path
can be found for which the value of the image
function is the same all along the path.
P1 connected to P2
P3 connected to P4
P1
P1 not connected to P3 or P4
P2 not connected to P3 or P4
P2
P3 not connected to P1 or P2
P4
P4 not connected to P1 or P2
P3
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
44 Finding Connected Components
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
46 Recursive algorithm for CCL
Pseudo code:
4. Go to step 1.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
47 Sequential algorithm for CCL
First Pass
• Intialize CC=0 and scan pixels across rows. So when we visit pixel p, pixel l
and t have been visited and labeled.
t
• If p=0: no action; l p
• If p=1: examine l and t.
– both l and t = 0; CC++ and assign CC to p.
– only one of l and t is 1; assign its label to p.
– both l and t are 1:
* same label => assign it to p;
* different label => assign lowest no. to p and
Mark labels in neighbours as equivalent (i.e
they are the same) and store in an equivalence
table Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
48 Sequential algorithm for CCL
First Pass
• Intialize CC=0 and scan pixels across rows. So when we visit pixel p, pixel l
and t have been visited and labeled.
t
• If p=0: no action; l p
• If p=1: examine l and t.
– both l and t = 0; CC++ and assign CC to p.
– only one of l and t is 1; assign its label to p.
– both l and t are 1:
* same label => assign it to p;
* different label => assign lowest no. to p and
Mark labels in neighbours as equivalent (i.e
they are the same) and store in an equivalence
table Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
49 Sequential algorithm for CCL
First Pass
• Intialize CC=0 and scan pixels across rows. So when we visit pixel p, pixel l
and t have been visited and labeled.
12
• If p=0: no action;
13
• If p=1: examine l and t.
– both l and t = 0; CC++ and assign CC to p.
– only one of l and t is 1; assign its label to p.
– both l and t are 1:
* same label => assign it to p;
* different label => assign lowest no. to p and
Mark labels in neighbours as equivalent (i.e
they are the same) and store in an equivalence
table Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
50 The Re-Labeling Process
Second Pass: simply replace the label with equivalent
label stored in the table
12
13
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
51 Why Equivalence Resolution?
14
89
56
25
37
7 10
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
52 Using morphology
X k = ( X k −1 B) A
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
53 Application: Blood cell counting
• Thresholded/binary Red
blood cell image
• Many blood cells are
separate objects
• Many touch – bad!
• Salt and pepper noise from
thresholding
• What kind of analysis can be
done ?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
54 Results of analysis
• 63 separate objects detected
after CCL
• Compute features like area, BB,
centroid etc.
• Single cells have area about 50
• Noise spots
• Gobs of cells
• What should be done to get
better results?
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
55 Region Filling
Given a pixel inside a boundary, region filling attempts
to fill that boundary with object pixels (1s)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
56 Why Region filling?
Helpful in the
later stages of
processing
skeleton before skeleton after
closing closing
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
57 Area fill and closing
Areafill Operation: extract connected components of dark regions and check
their area . If the area is less than the threshold value they will be treated as
holes. Then the dark holes can be removed by changing their intensity into
light/white color.
Another criteria can be compactness (defined as perimeter^2 /area)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
58 Region Filling (Floodfill Operation)
The key equation for region filling is
X k = ( X k −1 B) Ac k = 1,2,3.....
Where X0 is simply the starting point inside the
boundary, B is a simple structuring element and Ac is
the complement of A
This equation is applied repeatedly until Xk is equal to
Xk-1
Finally the result is unioned with the original boundary
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
59 Region Filling Step By Step
0
1
X0=p
X k = ( X k −1 B) Ac
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
60 Region Filling Example
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
63 The Hit-or-Miss Transformation
• Useful as a very basic tool for shape detection
• To find the location of a shape B1 in an image A
– Erosion of A Ө B1 gives all places where B1 fits in A
• But B1 fits in any sufficiently large shape
• So also require the boundary (B2) around the
shape, to be empty
– Erosion of Ac Ө B2 gives all places where B2 fits in empty places of A
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
64 The Hit-or-Miss Transformation
B = ( AOX ) Ac O(W − X )
A* B = ( X ,W − X )
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
65 The Hit-or-Miss Transformation
B = ( AOX ) Ac O(W − X )
A* B = ( X ,W − X )
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
66 Corner Detection
• Method can also be used to look for particular patterns of
foreground and background pixels
• In such case background is not required and hit or miss transform
reduces to simple erosion
• Using a Group of Structuring Elements, containing 0s, 1s
and sometimes don’t cares(x!)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
67 Corner Detection
Structuring Elements representing four corners
Contains 0s, 1s and don’t care’s
x x x x x x
x x x x x x
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
68 Convex Hull
• A set A is said to be convex if the straight
line segment joining any two points in A
lies entirely within A.
• The convex hull H of an arbitrary set S is
the smallest convex set containing S.
• Application in shape analysis etc.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
69 Convex Hull
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
70 Convex Hull
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
71 Skeletonization
• Is a concise representation of a shape - the set of
all points that are equally distant from two closest
points of the object boundary
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
72 Skeletonization
Results are
sensitive to small
perturbations in
the boundary
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
73 Thining
1. Used to remove selected foreground pixels
from binary images
2. After edge detection, lines are often thicker
than one pixel.
3. Thinning can be used to thin those line to one
pixel width.
4. It is particularly useful for skeletonization
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
74 Thining
The thinning of a set A by a structuring element B,
defined: A B = A − ( AO * B)
= A ( AO
* B )c
A more useful expression for thinning A symmetrically
is based on a sequence of structuring elements:
B = B1 , B 2 , B 3 ,..., B n
where B i is a rotated version of B i -1
The thinning of A by a sequence of structuring element {B}
A {B} = ((...(( A B1 ) B 2 )...) B n )
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
75 Thining
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
76 Grayscale Morphology
• Instead of binary images, assume we have gray
values f(x,y), where x,y are integer pixels
• Let b(x,y) be the gray level structuring element
b(x,y) 250
200
150
100
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
77 Grayscale Morphology
• Definition of dilation with flat structuring element
(f b)(x, y) = maxf (x − s, y − t)
(s,t)b
• Reflect b(s,t), slide it past f(x,y); at each position, take the maximum
of f(x,y) within the window of b(s,t)
b(s,t)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
78 Grayscale Morphology
• Definition of dilation with flat structuring element
(f b)(x, y) = maxf (x − s, y − t)
(s,t)b
• Reflect b(s,t), slide it past f(x,y); at each position, take the maximum
of f(x,y) within the window of b(s,t)
b(s,t)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
79 Gray Scale Erosion
• Definition of erosion (with flat SE)
(f b)(x, y) = (s,tmin
)b
f (x + s, y + t)
• Slide b(s,t) past f(x,y); at each position, take the minimum of f(x,y)
within that window
b(s,t)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
80 Gray Scale Erosion
• Definition of erosion (with flat SE)
• Slide b(s,t) past f(x,y); at each position, take the minimum of f(x,y)
within that window
b(s,t)
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
81 Example
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
82 Opening and Closing
• Similar to binary case
f b = ( f θb) b
– Opening is erosion followed by
dilation
– Closing is dilation followed by f •b = ( f b)θb
erosion
• Geometric interpretation of opening
– Push the SE up from below against the
underside of f
– Take the highest values achieved at
every point
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
83 Opening and Closing
• Geometric interpretation of closing
– Push the SE down from above against the topside of f
– Take the lowest values achieved at every point
Closing removes
small, dark details
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
84 Example
• Segment the coins from the image
I = imread('pennies.png');
I = rgb2gray(I);
imshow(I,[]);
% Pick a disk that will just fit inside the pennies
SE = strel('disk', 6, 0);
I2 = imopen(I,SE);
figure, imshow(I2,[]);
% Threshold to get the pennies
B = Iopen > 150;
figure, imshow(B);
L = bwlabel(B);
props = regionprops(L);
figure(1);
for i=1:length(props)
rectangle('Position', props(i).BoundingBox, 'EdgeColor', 'r’);
end
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
85 Top Hat Transform
• Open the image with a structuring element, subtract it
from the original h = f − ( f b)
• Leaves only details smaller than the structuring element
• Matlab example
I = imread('rice.png');
% Pick structuring element larger than a rice grain
background = imopen(I,strel('disk',15));
subplot(1,2,1), imshow(I);
subplot(1,2,2), imshow(background);
I2 = I-background;
figure, imshow(I2, []);
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
86 Quiz
• Describe a morphological algorithm to count the number
of objects with holes and the number of objects without
holes in below image
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
87 Summary
• The purpose of morphological processing is
primarily to remove imperfections added during
segmentation
• The basic operations are erosion and dilation
• Using the basic operations we can perform
opening and closing
• More advanced morphological operation can then
be implemented using combinations of all of these
• Analogous things could be done with grayscale
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT