Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

07 - Morphology

Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

Image Processing & Computer vision

Morphological Image Processing


2 Contents
In this topic we will consider
– What is morphology?
– Simple morphological operations
– Compound operations
– Morphological algorithms
– Applications

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

How do we fill “missing pixels”?

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
5 Quick Example 2
om Gonzalez & Woods, Digital Image Processing (2002)

Image after segmentation Image after segmentation and


morphological processing

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:

The set of all points z such that B, translated by z, is contained by A.


Informally, the structuring element B is positioned with its
origin at (x, y) and the new pixel value is determined using
the rule:

1 if B fits A
g ( x, y ) = 
0 otherwise
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
14 Erosion Example
Original Image
Processed Image

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

Original image Erosion by 3*3 Erosion by 5*5


square structuring square structuring
element element

Watch out: In these examples a 1 refers to a black pixel!

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
17 What Is Erosion For?
Erosion can split apart joined objects

Erosion can strip away extrusions

Watch out: Erosion shrinks objects


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
18 Example 2

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

After erosion After erosion


with a disc of with a disc of
radius 11 radius 45

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
20 Dilation
Dilation of image A by structuring element B is given by:

The set of all displacements z, such that and A


overlap by at least one element.
Informally: The structuring element B (if symmetric with
origin at center, else reflected) is positioned with its
origin at (x, y) and the new pixel value is determined
using the rule: g ( x, y ) = 

1 if B hits A
0 otherwise
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
21 Dilation Example
Original Image
Processed Image

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

Original image Dilation by 3*3 Dilation by 5*5


square structuring square structuring
element element

Watch out: In these examples a 1 refers to a black pixel!

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:

What is the result of this dilation?

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

Dilation can repair intrusions

Watch out: Dilation enlarges objects


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
29 What Is Dilation For?
Combined with erosion: eliminate irrelevant details

structuring element B = 13x13 pixels of gray level 1


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
30 Compound Operations
More interesting morphological operations can be
performed by performing combinations of erosions
and dilations
The most widely used of these compound operations
are:
– Opening
– Closing

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

Note: a disc shaped structuring element is used


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
32 Opening Example
Original Image
Processed Image

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

Original shape After dilation After erosion


(closing)

Note a disc shaped structuring element is used


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
36 Closing Example
Original Image Processed Image

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 – (AB)

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

Original Image Extracted Boundary


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
42 Connected Components

Binary image with multiple 'objects'


Separate 'objects' must be labeled
individually

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

Pick any pixel in the image and assign it a label


Assign same label to any neighbor pixel with the
same value of the image function
Continue labeling neighbors until no neighbors
can be assigned this label
Choose another label and another pixel not
already labeled and continue
If no more unlabeled image points, stop.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
45 Connected Components Labelling

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
46 Recursive algorithm for CCL
Pseudo code:

1. Scan the image to find an unlabeled unity valued pixel and


assign it a new label L.

2. Recursively assign a label L to all its unity valued neighbors.

3. Stop if there are no more unlabeled unity valued pixels.

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.
12
• If p=0: no action;
13
• 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

12
13

– But resolving equivalences is necessary between the two


passes. Different methods:
• Union-Find disjoint sets and operations
• Obtain transitive closure using Floyd-Warshall algorithm

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
51 Why Equivalence Resolution?

14
89
56
25
37
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)

Given a point inside


here, can we fill the
whole circle?

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

Original Image One Region All Regions


Filled Filled

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

Ex: Find all the


right angle convex
corners of a region
in a given image
as shown below

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

Apply each Structuring Element


Use OR operation to combine the four results

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

Let B i , i = 1, 2, 3, 4, represent the four structuring elements.


The procedure consists of implementing the equation:
X ki = ( X k −1O
* Bi )  A
i = 1, 2, 3, 4 and k = 1, 2, 3,...
with X 0i = A.
When the procedure converges, or X ki = X ki −1 , let D i = X ki ,
the convex hull of A is
4
C ( A) =  D i
i =1

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

• We will only focus on flat (constant 50

value) structuring elements 0


0 50 100 150 200 250 300 350 400
Distance along profile

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) = maxf (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) = maxf (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)

(f  b)(x, y) = minf (x + s, y + t)


(s,t )b

• 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

Opening removes small,


bright details

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

You might also like