You are on page 1of 45

ME4821 Digital

Image Processing

Morphological
Image

Processing_II
Dr. Faraz Junejo
Morphological
Algorithms
Using the simple technique we have
looked at so far we can begin to
consider some more interesting
morphological algorithms
We will look at:
Boundary extraction
Region filling
Thinning
Thickening
Convex Hull
Boundary Extraction
Extracting the boundary (or outline)
of an object is often extremely useful
es taken from Gonzalez & Woods, Digital Image Processing (2008))

The boundary can be given simply


as
(A) = A (A B)
Boundary Extraction
(contd.)
(A) = A (A B)

The boundary of a set A is obtained by first eroding A


by structuring element B and then taking the set
difference of A and its erosion.

The resultant image after subtracting the eroded image


from the original image has the boundary of the objects
extracted.

The thickness of the boundary depends on the size of


the structuring element. For example, in previous
example using a SE of 5 by 5 would result in a
boundary between 2 and 3 pixels thick.
Boundary Extraction:

Example
A simple image and the result of
es taken from Gonzalez & Woods, Digital Image Processing (2008))

performing boundary extraction using a


square 3*3 1s matrix as structuring element

Original Image Extracted Boundary


Boundary
Extraction:Example
a=imread('banana.jpg');
b=rgb2gray(a);
figure, imshow(b), title('Org Banana');
b(b<225)=0; % setting any pixel values below 225 i.e. bananas to
s=strel('disk',4,0); %Structuring element
c=~im2bw(b); %binary Image
figure, imshow(c), title('thresholded Banana');
d=imerode(c,s);%Erode the image by structuring element
Now taking difference between binary image and Eroded image
e=c-d; % boundary extracted image
figure,imshow(e);title('Boundary extracted Image');

% Now getting rid of salt noise and text using erosion


S1=[1 1 1;1 1 1;1 1 1];
f=imerode(e,s1);
figure,imshow(f);title(Final Boundary extracted Image');
Boundary Extraction
(contd.)
External Boundary: Consists of the pixels
outside A (object) that sit immediately next
to A.

Morphological Gradient: Consists of the


combination of internal and external
boundaries.

where B is a suitable structuring element


Boundary Extraction:
MATLAB
The IPT function bwperim returns a binary image
containing only the perimeter pixels of objects in
the input image.

Implementing extraction of internal boundary


extraction of a small (5 12) object using a 3 3
square as SE.
a = ones(5,12)
a (1:2,1)=0
a (4:5,5)=0
a (1:2,9)=0
b = bwperim(a,8) % use of bwperim with 8-connectivity, to
be
consistent with the SE in Figure
Region Filling
Given a pixel inside a boundary, region filling
attempts to fill that boundary with object pixels (1s)
es taken from Gonzalez & Woods, Digital Image Processing (2008)

Given a point
inside here, can
A hole may be defined as background weregion
fill surrounded
the whole by a
connected border of foreground pixels.
circle?
Algorithm is based on set dilation, complementation and intersection
for filling holes.
Region Filling (contd.)
The key equation for region filling is
es taken from Gonzalez & Woods, Digital Image Processing (2008)

X k ( X k 1 B ) A c
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
Region Filling (contd.)
Forming an array X0 of 0s (the same size as the
array containing A), except the locations in X0
corresponding to the given point in each hole,
which we set to 1.

2. Xk = (Xk-1 + B) Ac k=1,2,3,

Stop the iteration if Xk = Xk-1


Start form p inside
boundary.
Region Filling Step By
Step
Note !!
The dilation would fill the entire area if left
unchecked.

However, the intersection at each step with the


complement of A limits the result to inside the
region of interest.

This is an example of how a morphological


process can be conditioned to meet a desired
property.

Hence region filling can be called a conditional


dilation.
Region Filling: Example
es taken from Gonzalez & Woods, Digital Image Processing (2008))

Original Image One Region All Regions


Filled Filled
The IPT function imfill implements region filling.
It can be used in an interactive mode (where the
user clicks on the image pixels that should be used
as starting points) or by passing the coordinates of
Convex Hull
The smallest convex region enclosing a specified
group of points

In two dimensions, the convex hull is found


conceptually by stretching a rubber band around
the points so that all of the points lie within the
band.
Convex Hull Masks
There are four convex hull masks,
where 1 represents black, 0 represents
white and X represents a pixel that
doesnt matter, either 0 or 1.
Convex Hull Function
4
C ( A) U D i

i 1
Iteratively applying the hit-or-miss transform to A with
the 4 masks.

This means that a structuring element is said to have


found a match in A if the 3 by 3 region of A under the SE
mask at that location matches the pattern of the mask.

When the masks produce no more changes, the union is


performed between all 4 masks and the result becomes
Di, i.e. yielding D1 through D4 for B1 through B4.

The union of four resulting Ds constitute the convex hull


of A.
Convex Hull Masks
If any of these masks are found in
the image, then that middle pixel
on the image is replaced with a 1
or black respectively.

To achieve the complete convex


hull image, these masks must be
applied repeatedly until no further
changes occur
Convex Hull Application
Original Black and White Image
Convex Hull Application
(contd.)
Mask B1 applied to BW Image
Convex Hull Application
(contd.)
Mask B2 applied to BW Image
Convex Hull Application
(contd.)
Mask B3 applied to BW Image
Convex Hull Application
(contd.)
Mask B4 applied to BW Image
Convex Hull Application
(contd.)
Initial Result from Union are as follows:
Convex Hull (contd.)
Shortcoming of this algorithm: convex hull can grow beyond
the minimum dimensions required to guarantee convexity

Possible solution: Limit growth so that it does not extend


past the vertical and horizontal dimensions of the original
set of points.
Convex Hull: Application
The HitorMiss transformation
Revisited
Used to look for particular patterns of
foreground and background pixels i.e. it is a
basic tool for shape detection
Concept: To detect a shape:
Hit object
Miss background
Very simple object recognition
Input:
Binary Image
Structuring Element, containing 0s and 1s!!
Hit-or-Miss
Transformation (HMT)
Find location of one shape among a set of
shapes template matching

Composite SE: object part (B1) and


background part (B2)
does B1 fits the object while,
simultaneously, B2 misses the object, i.e.,
fits the background?
Example
Thinning
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.
Definition of Thinning
The thinning of a set A by a structuring
element B, can be defined by terms of the hit-
and-miss transform:

If foreground and background fit the


structuring element exactly, then the pixel at
the origin of the SE is set to 0

Note that the value of the SE at the origin is 1


or dont care!
Thinning =>
Thinning: Process
The process is to thin by one pass with
B1 , 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:
Thinning: Example
We use two Hit-and-miss
Transforms
Thickening
Used to grow selected regions of foreground
pixels
Thickening is a morphological dual of
thinning

If foreground and background match exactly


the SE, then set the pixel at its origin to 1!
Note that the value of the SE at the origin is
0 or dont care!
Thickening: Example
Thickening: Example
Used to grow selected regions of
foreground pixels
MATLAB Exercise: BASIC
MORPHOLOGICAL
Goal
ALGORITHMS
The goal of this exercise is to learn how to how to
implement basic morphological algorithms in MATLAB.
Objectives
Learn how to perform boundary extraction.
Explore the bwperim function.
Learn how to fill object holes using the imfill function.
Explore object selection using the bwselect function.
Learn how to label objects in a binary image using the
bwlabel function.
Explore the bwmorph function to perform thinning,
thickening, and skeletonization.
Matlab Commands
imdilate: dilates an image
imerode: erode an image
imclose: Morphologically close image
imopen: Morphologically open image
bwhitmiss: Binary hit-miss operation
bwperim : used for boundary extraction
imfill: Fill image regions and holes
Matlab Commands
(contd.)
bwlabel: label connected components
in binary image
bwmorph: Morphological operations
on binary images
bwareaopen: Morphologically open
binary image (i.e. remove small
objects)
bwselect: Select objects in binary
image
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 ,closing,
region filling, convex hull etc.

You might also like