Chapter 9 Morphological Image Processing1
Chapter 9 Morphological Image Processing1
1 UCSM
Contents
Basic Concepts in Set Theory
Logic Operations Involving Binary Pixels and Images
Dilation and erosion
Opening and closing
The Hit-or-Miss Transformation
Some basic morphological algorithms
2 UCSM
Morphological Image Processing
“Morphology “ – a branch in biology that deals with the form
and structure of animals and plants.
“Mathematical Morphology” – as a tool for extracting image
components, that are useful in the representation and
description of region shape.
The language of mathematical morphology is – Set theory.
Unified and powerful approach to numerous image processing
problems.
In binary images , the set elements are members of the 2-D
integer space – Z2, where each element (x,y) is a coordinate of a
black (or white) pixel in the image.
3 UCSM
Basic Concepts in Set Theory
Subset
Union
Intersection
4 UCSM
Logic Operations Involving Binary Pixels
and Images
The principal logic operations used in image processing
are: AND, OR, NOT (COMPLEMENT).
These operations are functionally complete.
Logic operations are preformed on a pixel by pixel basis
between corresponding pixels (bitwise).
Other important logic operations :
XOR (exclusive OR), NAND (NOT-AND)
Logic operations are just a private case for a binary set
operations, such : AND – Intersection , OR – Union,
NOT-Complement.
5 UCSM
6 UCSM
Logical expressions in Matlab for set
operations
Set Operation Matlab expression Name
for Binary images
A B A& B AND
A B A| B OR
Ac ~A NOT
A-B A& ~B DIFFERENCE
7 UCSM
Dilation
Dilation is used for expanding an element A by using
structuring element B
Dilation of A by B and is defined by the following equation:
8 UCSM
Dilation – Example 1
9 UCSM
Dilation – Example 2
10 UCSM
Dilation – A More interesting Example
11 UCSM
Dilation –Example
A=imread(‘broken_text.tif’);
B=[0 1 0;1 1 1;0 1 0];
A2=imdilate(A, B);
imshow(A2)
12 UCSM
The strel function
IPT function strel constructs structuring elements with a
variety of shapes and sizes.
se=strel(shape,parameters)
shape –string specifying the desired shape
parameters– list of parameters
se=strel(‘diamond’,5) //diamond-shaped structuring element
return diamond-shaped structuring element that extends ±5
pixels along the horizontal and vertical axes
Some shapes that strel create are:
se=strel(‘disk’,R)
se=strel(‘line’,LEN,DEG)
se=strel(‘square’,W)
13 UCSM
Erosion
Erosion is used for shrinking of element A by using
element B
Erosion for Sets A and B in Z2, is defined by the following
equation:
14 UCSM
Erosion – Example 1
15 UCSM
Erosion – Example 2
16 UCSM
Duality between dilation and erosion
Dilation and erosion are duals of each other with respect to set
complementation and reflection. That is,
Proof;
Erosion- (AB)c {z | (B)z A}c
If set (B)z is contained in set A, then (B)z ∩ Ac =Ø in which case
the preceding equation becomes
( AB) c {z | (B) z Ac }c
18 UCSM
Example
A=imread(‘wire.tif’);
se=strel(‘disk’,10);
A2=imerode(A,se);
imshow(A2)
19 UCSM
Image Dilation without using 'imdilate' function
Steps to be performed
1.Consider a matrix A=
20 UCSM
Con’t
5.If the values are zero, then, update the matrix D with zero on the
position (1,1)
6.Now slide the structuring element to the next position and
perform logical AND operation.
7. If the values after the logical AND operation is not zero, and
then update the matrix D with one.
D=
21 UCSM
Cont’d
8.Repeat this process until all the elements in the matrix A
are visited.
9.The final result after dilation using the structuring
element B is
22 UCSM
Matlab code (Example 1)
A=[1 0 0 0 1; 1 0 1 0 0; 1 1 1 0 0;0 0 1 1 1];
%Structuring element
B=[1 0 0; 0 1 0; 0 0 1];
%Pad zeros on all the sides
C=padarray(A,[1 1]);
%Intialize a matrix of matrix size A with zeros
D=false(size(A));
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Perform logical AND operation
D(i,j)=sum(sum(B&C(i:i+2,j:j+2)));
end
end
display(D);
23 UCSM
Explanation
24 UCSM
Matlab code (Example 2)
A=imread('text.png');
A=im2bw(A);
%Structuring element
B2=getnhood(strel('line',7,90));
m=floor(size(B2,1)/2);
n=floor(size(B2,2)/2);
%Pad array on all the sides
C=padarray(A,[m n]);
D=false(size(A));
for i=1:size(C,1)-(2*m)
for j=1:size(C,2)-(2*n)
Temp=C(i:i+(2*m),j:j+(2*n));
D(i,j)=max(max(Temp&B2));
end
end
figure,imshow(D);
25 UCSM
Image Erosion without using MATLAB
function 'imerode'
Steps to be performed
1.Consider a matrix A=
A=
5.Place the structuring element B on the matrix A and check whether the ones in
the structuring element overlaps only the ones in the matrix A.
A= B B=[1 1 0]
26
UCSM
Cont’d
6.If the condition is true, then update matrix D with one. And slide the widow to
next position.
D=
7.The ones in the structuring element do not overlap with the ones in the matrix A,
update the matrix D with 0.
27 UCSM
Cont’d
A=[1 0 1 1 1; 1 0 1 0 0; 1 1 1 0 0;0 0 1 1 1];
B=[1 1 0]; %Structuring element
C=padarray(A,[0 1],1); %Pad array with ones on both sides
D=false(size(A)); %Intialize the matrix D of size A with zeros
for i=1:size(C,1)
for j=1:size(C,2)-2
In=C(i,j:j+2);
%Find the position of ones in the structuring element
In1=find(B==1);
%Check whether the elements in the window have the value one in the
%same positions of the structuring element
if(In(In1)==1)
D(i,j)=1;
end
end
end
28
display(D); UCSM
Erosion
29 UCSM
Opening And Closing
Opening – smoothes contours , eliminates protrusions
30 UCSM
Opening And Closing
Opening –
eliminates protrusions
breaks necks
smoothes contour
First – erode A by B, and then dilate the result by B
In other words, opening is the unification of all B objects
entirely contained in A
C=imopen (A, B)
31 UCSM
Opening
A
B
)
(
A
BB
A
B
{(
B)
|
z(
B
)
zA}
32
33
UCSM
Opening And Closing
Closing –
smooth contour
fuse narrow breaks and long thin gulfs
eliminate small holes
fill gaps in the contour
First – dilate A by B, and then erode the result by B
In other words, closing is the group of points, which the
intersection of object B around them with object A – is not
empty
C=imclose (A, B)
34 UCSM
Closing
A
B
(
A
B
)B
35
36 UCSM
Use of opening and closing for morphological filtering
37 UCSM
Example
f=imread(‘figerprint.tif’);
se= strel(‘square’,3);
fo=imopen(f,se);
imshow (fo)
foc= imclose((fo,se);
imshow(foc)
38 UCSM
Useful: open and close
39
The Hit-or-Miss Transformation
A basic morphological tool for shape detection and can be
used to look for particular patterns of foreground and
background pixels in an image.
Let the origin of each shape be located at its center of gravity.
If we want to find the location of a shape , say – X , at (larger)
image, say – A :
Let X be enclosed by a small window, say – W.
The local background of X with respect to W is defined as
the set difference (W - X).
Apply erosion operator of A by X, will get us the set of
locations of the origin of X, such that X is completely
contained in A.
It may be also view geometrically as the set of all locations
40 of the origin of X at which X found a match (hit) in A. UCSM
The Hit-or-Miss Transformation
Apply erosion operator on the complement of A by the local
background set (W – X).
Notice, that the set of locations for which X exactly fits inside
A is the intersection of these two last operators above.
This intersection is precisely the location sought.
Formally:
If B denotes the set composed of X and it’s background –
B = (B1,B2) ; B1 = X , B2 = (W-X).
The match (or set of matches) of B in A, denoted is:
41
UCSM
Hit-or-Miss exp:
42
UCSM
Basic Morphological Algorithms
1 – Boundary Extraction
2 – Region Filling
3 – Extraction of Connected Components
4 – Convex Hull
5 – Thinning
6 – Thickening
7 – Skeletons
43 UCSM
Boundary Extraction
First, erode A by B, then make set difference between A
and the erosion
The thickness of the contour depends on the size of
constructing object – B
44
UCSM
Boundary Extraction
45 UCSM
Region Filling
This algorithm is based on a set of dilations, complementation
and intersections
Begin p, is the point inside the boundary, to fill the entire region
with value of 1
X(k) = (X(k-1) xor B) conjunction with complemented A
The process stops when X(k) = X(k-1)
The result that given by union of A and X(k), is a set contains
the filled set and the boundary
46
UCSM
Application: region filling
The first filled
region Fill all regions
Original image
Extraction of Connected Components
48 UCSM
This shows automated
inspection of chicken-
breast, that contains
bone fragment
50 UCSM
Convex hull
(morphological
algorithm) - example
51 UCSM
Convex hull (morphological algorithm) - example
52 UCSM
Thinning
The thinning of a set A by a structuring element B, can
be defined by terms of the hit-and-miss transform:
53 UCSM
Thinning (cont’d)
The process is to thin by one pass with B 1 , then thin the
result with one pass with B2, and so on until A is
thinned with one pass with Bn.
The entire process is repeated until no further changes
occur.
Each pass is preformed using the equation:
54 UCSM
Thinning example
B8
55 UCSM
(a)Sequence of rotated
structuring elements used for
thinning (b) Set A (c)Result
of thinning with the first
element (d)-(i) Results of
thinning with the next seve
elements (there was no
change between the seventh
and eight elements) (j)Result
of using the first four
elements again (there was no
change between the seventh
and eight elements)(k) Result
after covergence (l)
56 UCSM Conversion to m-
connectivity
Thickening
Thickening is a morphological dual of thinning.
Thickening is used to grow selected regions of foreground
pixels in binary images, somewhat like dilation or closing.
Definition of thickening .
As in thinning, thickening can be defined as a sequential
operation:
57 UCSM
Thickening (cont’d)
A separate algorithm for thickening is often used in practice,
Instead the usual procedure is to thin the background of the set
in question and then complement the result.
58 UCSM
Thickening example
59 UCSM
Skeleton
The notion of a skeleton S(A) of a set A is intuitively
defined,
60 UCSM
Skeleton
The skeleton of A is defined by terms of erosions and openings:
with
Where B is the structuring element and indicates k
successive erosions of A:
• k times, and K is the last iterative step before A erodes to an empty set, in
other words:
61 UCSM
Skeleton
A can be also reconstructed from subsets Sk(A) by
using the equation:
62 UCSM
Skeleton
Maximum disk: largest disk included in A, touching the
boundary of A at two or more different places
63
UCSM
Skeleton
64 UCSM
Skeleton Example
BW1 = imread('circbw.tif');
BW2 = bwmorph(BW1,'skel',Inf);
imshow(BW1)
figure, imshow(BW2)
65 UCSM
Some Matlab Functions
bwmorph
Morphological operations on binary images
Remove interior pixels to leave an outline of the shapes.
BW2 = bwmorph(BW,'remove');
BW3 = bwmorph(BW,'skel',Inf);
imshow(bwmorph(BW, 'thin', Inf));
imshow(bwmorph(BW, 'thicken', Inf));
imshow(bwmorph(BW, 'tophat', Inf));
imfill
Fill image regions and holes
BW2 = imfill(BW,locations)
BW2 = imfill(BW,'holes')
I2 = imfill(I) //fills holes in the grayscale image I.
66 UCSM
Gray-Scale Images
In gray scale images on the contrary to binary images we
deal with digital image functions of the form f(x,y) as an
input image and b(x,y) as a structuring element.
(x,y) are integers from Z*Z that represent a coordinates in
the image.
f(x,y) and b(x,y) are functions that assign gray level value
to each distinct pair of coordinates.
For example the domain of gray values can be 0-255,
whereas 0 – is black, 255- is white.
67 UCSM
Dilation – Gray-Scale
Equation for gray-scale dilation is:
68 UCSM
Dilation – Gray-Scale (cont)
Illustrate the previous equation in terms of 1-D. and we
will receive an equation for 1 variable:
69 UCSM
Dilation – Gray-Scale (cont)
The general effects of performing dilation on a gray scale
image is twofold:
70 UCSM
Erosion – Gray-Scale
Gray-scale erosion is defined as:
71 UCSM
Erosion– Gray-Scale (cont)
General effect of performing an erosion in grayscale images:
1. If all elements of the structuring element are positive, the
output image tends to be darker than the input image.
2. The effect of bright details in the input image that are smaller
in area than the structuring element is reduced, with the
degree of reduction being determined by the grayscale values
surrounding by the bright detail and by shape and amplitude
values of the structuring element itself.
Similar to binary image, grayscale erosion and dilation are
duals with respect to function complementation and
reflection.
72 UCSM
Dilation & Erosion– Gray-Scale
73 UCSM
Opening And Closing
Similar to the binary algorithms
Opening –
Closing –
In the opening of a gray-scale image, remove small light
details, while relatively undisturbed overall gray levels and
larger bright features
In the closing of a gray-scale image, we remove small dark
details, while relatively undisturbed overall gray levels and
larger dark features
74 UCSM
Opening And Closing
75 UCSM
Some Applications of Gray-Scale Morphology
Morphological smoothing: opening followed by closing
76 UCSM
Some Applications of Gray-Scale Morphology
Morphological gradient: difference between dilation and
erosion
77 UCSM
Some Applications of Gray-Scale Morphology
Top-hat transformation: difference between original
and opening
78
UCSM
Some Applications of Gray-Scale Morphology
Texture segmentation:
1.Closing with a larger and larger s.e. until the small particles
disappear
2. Opening with a s.e. larger than the gaps between large particles
3. Gradient separation contour
79 UCSM