EE409 – Digital Image Processing 2017-2018 Spring
5. Morphological Operations
by: Dr. Gökhan Koray GÜLTEKİN
Introduction -- Morphology Operations
Morphology is the science of form and structure of animals/plants In DIP Terminology: Morphology: a broad set of image processing operations that process images based on structures. Morphological operations apply a structuring element to an input image Application Area: Morphological operations simplify images, and quantify and preserve the main shape characteristics of objects.
1
Dilation & Erosion
Basic operations
Are dual to each other: Erosion
shrinks foreground(e.g. white objects), enlarges Background(black background)
Dilation
enlarges foreground (e.g. white objects), shrinks background(black background)
Dilation Dilation expands the connected sets of 1s of a binary image. It can be used for 1. growing features
2. filling holes and gaps
2
What Is Dilation For? Dilation can repair breaks
Dilation can repair intrusions(girinti/çıkıntı)
Watch out: Dilation enlarges objects
Dilation
Dilation allows objects to expand, then potentially filling in small holes and connecting disjoint object. Consider each pixel in the input image If
the structuring element touches the foreground image, write a “1” at the origin of the structuring element!
Input: Binary
Image!
3
Dilation Does the structuring element hit the set? dilation of a set A by structuring element B: all z in A such that B hits A when origin of B=z
A B { z|( Bˆ )z A Φ }
grow the object
7
Example for Dilation Input image
1
0
0
Structuring Element
1
1
1
Output Image
0
1
1
1
0
1
1
1
4
Example for Dilation Input image
1
0
0
0
Structuring Element
1
1
1
Output Image
1
0
1
1
1
0
1
1
1
1
0
1
1
1
1
1
1
1
Example for Dilation Input image
1
0
0
0
1
Structuring Element
Output Image
1
0
1
1
1
1
5
Example: Dilation
Dilation is an important morphological operation
Applied Structuring Element:
Another Dilation Example
White = 1, black = 0 Image get lighter after dilation
6
Illustration of dilation
Imdilate function in MATLAB IM2 = IMDILATE(IM,NHOOD) dilates the image IM, where NHOOD is a matrix of 0s and 1s that specifies the structuring element neighborhood. This is equivalent to the syntax IIMDILATE(IM, STREL(NHOOD)). IMDILATE determines the center element of the neighborhood by FLOOR((SIZE(NHOOD) + 1)/2). >> se = imrotate(eye(3),90) se = 0 0 1
0 1 0
1 0 0
>> center=floor(size(se)+1)/2 center = 2 2
7
Example of Dilation in Matlab 0.5
1
1.5
2
2.5
>> I = zeros([13 19]); >> I(6,6:8)=1; >> I2 = imdilate(I,se);
3
3.5 0. 5
2
2
4
4
6
6
8
8
10
10
12
12
2
4
6
8
10
12
14
16
18
2
4
1
1.5
2
6
2.5
3
8
3.5
10
12
14
16
18
MATLAB Dilation Example1 I = zeros([13 19]); I(6, 6:12)=1; SE = imrotate(eye(5),90); I2=imdilate(I,SE); figure, imagesc(I) figure, imagesc(SE) figure, imagesc(I2)
8
MATLAB Dilation Example1
INPUT IMAGE
DILATED IMAGE
SE
MATLAB Dilation Example 2 I(6:9,6:13)=1; figure, imagesc(I) I2=imdilate(I,SE); figure; imagesc(I2);
9
MATLAB Dilation Example 2
I
I2
I(6:9,6:13)=1; figure, imagesc(I) I2=imdilate(I,SE); figure; imagesc(I2);
SE
I SE = 1 1 1
1 1 1
I2
1 1 1
10
MATLAB Dilation Example I=imread(‘coins.png’); I=im2bw(I,0.5); Imshow(I); Se=strel(‘ball’, 8, 8); I2=imdilate(I, se); Imshow(I2);
Dilation : Bridging gaps
11
Dilation: Edge Detection ď Ž 1. 2. 3.
Edge Detection Dilate input image Subtract input image from dilated image Edges remain!
Example: Edge Detection
24
12
Erosion Erosion shrinks the connected sets of 1s of a binary image. It can be used for 1. shrinking features
2. Removing bridges, branches and small protrusions
What Is Erosion For? Erosion can split apart joined objects
Erosion can split apart
Erosion can strip away extrusions(çıkıntı)
Watch out: Erosion shrinks objects
13
Erosion
Erosion shrinks objects by etching away (eroding) their boundaries. Erosion is the set of all points in the image, where the structuring element “fits into”. Consider each foreground pixel in the input image If
the structuring element fits in, write a “1” at the origin of the structuring element!
Input: Binary
Image!
Erosion Does the structuring element fit the set? erosion of a set A by structuring element B: all z in A such that B is in A when origin of B=z
A B {z|(B)z A} shrinks the object 28
14
Example for 1D Erosion Input image
1
0
0
Structuring Element
1
1
1
0
1
1
1
0
1
1
1
1
0
1
1
0
Output Image
Example for Erosion Input image
1
0
0
0
Structuring Element
1
1
1
Output Image
0
0
1
15
Example for Erosion Input image
1
0
0
0
1
1
1
Structuring Element
Output Image
0
0
0
0
1
0
0
1
1
1
1
1
0
0
Illustration of erosion
16
A first Example: Erosion
Erosion is an important morphological operation
Applied Structuring Element:
Another example of erosion
White = 1, black = 0 image as a result of erosion gets darker
17
MATLAB Erosion Example 2 pixel wide
I2
I3=imerode(I2,SE);
SE = 3x3
MATLAB Erosion Example I=imread(‘coins.png’); I=im2bw(I,0.5); Imshow(I); Se=strel(‘ball’, 8, 8); I2=imerode(I, se); Imshow(I2);
18
Counting Coins Counting coins is difficult because they touch each other! ď Ž Solution: Binarization and Erosion separates them! ď Ž
Removing Small Particles & Narrow Connections
19
Erosion Example
Original image
After erosion with a disc of radius 5
After erosion with a disc of radius 10
After erosion with a disc of radius 20
Other Morphological Operations
Dilation and Erosion are the basic operations, can be combined into more complex sequences.
Erosion and dilation are not invertible operations ---if an image is eroded and dilated, the original image is not reobtained.
The combination of Erosion and dilation constitutes new operations ----opening and closing. They are the most useful morphological filtering.
20
Opening & Closing Important operations Derived from the fundamental operations
Dilatation Erosion
Usually applied to binary images, but gray value images are also possible Opening and closing are dual operations
Opening erosion followed by dilation, denoted ∘
A B ( A B) B eliminates protrusions breaks necks smoothes contour
42
21
Opening The opening of image A by structuring element B, denoted A ○ B is simply an erosion followed by a dilation A ○ B = (A B) B
Original shape
After erosion
After dilation (opening)
Note a disc shaped structuring element is used
Opening Example Original Image
Image After Opening
22
Opening
Opening consists of an erosion followed by a dilation
Can be used to eliminate all pixels in regions that are small to contain the structuring element.
Similar to Erosion
Spot and noise removal
Less destructive
Uses the same structuring element for both erosion and dilation.
Input:
Binary Image
Opening
Take the structuring element (SE) and slide it around inside each foreground region. All
pixels which can be covered by the SE with the SE being entirely within the foreground region will be preserved.
All
foreground pixels which can not be reached by the structuring element without overlapping the edge of the foreground object will be eroded away!
Opening is idempotent: Repeated application has no further effects!
23
Opening ď Ž
Structuring element: 3x3 square
Opening Steps 1.
Erosion: Given the following binary image with squares on size 1,3,5,7,9 and 15. You can get rid of all the squares less than size of 15 by erosion followed by dilation of a structuring element of 13x13.
B 13x13 structuring element
A
Ae B Erosion of A by B
24
Opening Steps 2. Dilation: Cont. from the previous slide. Note that erosion followed by dilation helps to perform filtering.
B 13x13 structuring element
Ae B Erosion of A by B
•
( A e B) B Dilation by B
Opening of A by B A B
Erosion of A by B, followed by the dilation of the result by B Erosion- if any element of structuring element overlaps with background output is zero
FIRST - EROSION >> se = strel('square', 20);fe = imerode(f,se);figure, imagesc(fe),title('fe')
25
Dilation of Previous Result Outputs 1 at center of SE when at least one element of SE overlaps object
SECOND - DILATION >> se = strel('square', 20);fd = imdilate(fe,se);figure, imagesc(fd),title('fd')
Opening in Matlab FO=imopen(f,se); figure, imagesc(FO),title('FO')
26
Opening in Matlab What if we increased size of SE for DILATION operation??
se = 25
se = 30
se = strel('square', 25);fd = imdilate(fe,se);figure, imagesc(fd),title('fd') se = strel('square', 30);fd = imdilate(fe,se);figure, imagesc(fd),title('fd')
Opening Example ď Ž
Opening with a 11 pixel diameter disc
27
Opening Example
3x9 and 9x3 Structuring Element 3*9
9*3
Use Opening for Separating Blobs Use large structuring element that fits into the big blobs Structuring Element: 11 pixel disc
28
Closing
Closing consists of a dilation followed by an erosion. Connects objects that are close to each other. It can be used to fill in holes and small gaps. Closing is defined as a Dilatation, followed by an Erosion using the same structuring element for both operations. Dilation next erosion! Input: Binary
Image
Closing Dilation followed by erosion, denoted •
A B ( A B) B Smooth contour Fuse narrow breaks and long thin gulfs Eliminate small holes Fill gaps in the contour
29
Closing The closing of image A by structuring element B, denoted A • B is simply a dilation followed by an erosion A • B = (A B)B
Original shape
After dilation
After erosion (closing)
Note a disc shaped structuring element is used
Closing Example Original Image
Image After Closing
30
Closing
Take the structuring element (SE) and slide it around outside each foreground region. All
background pixels which can be covered by the SE with the SE being entirely within the background region will be preserved.
All
background pixels which can not be reached by the structuring element without lapping over the edge of the foreground object will be turned into a foreground.
Opening is idempotent: Repeated application has no further effects!
Closing
Structuring element: 3x3 square
31
Closing Example Closing operation with a 22 pixel disc ď Ž Closes small holes in the foreground ď Ž
Closing Example 1. 2.
Threshold Closing with disc of size 20
Thresholded
closed
32
Useful: open & close
Closing of A by B ďƒ A B 1.
Dilation of A by B
Outputs 1 at center of SE when at least one element of SE overlaps object
se = strel('square', 20); fd = imdilate(f,se); figure; imagesc(fd); title('fd');
33
Erosion of the prev. result by B 2. Erosion- if any element of structuring element overlaps with background output is zero
ORIGINAL
OPENING
CLOSING
34
Questions
?
35