Morphological Image Processing: Outline
Morphological Image Processing: Outline
Outline Contents
1 Preliminaries 2 Dilation and Erosion 2.1 Dilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Erosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Opening and Closing 1 3 3 5 5
4 Labeling Connected Components 11 Morphological image processing tools help us to extract image components that are useful in the representation and description of region shape, such as boundaries, skeletons, and convex hull. There are morphological techniques for pre- and postprocessing, such as morphological ltering, thinning, and pruning.
Preliminaries
Let Z be a set of integers. The sampling process used to produce the digital image may be viewed as a partitioning in the x y -plane in to a grid, with coordinates of the center of each grid being a pair of elements from the Cartesian product Z 2 . In the terminology of set theory the function f (x , y ) is said to be a digital image if (x , y ) are integers from Z 2 and f is a mapping that assigns an intensity value in the set of real numbers, R, to each distinct pair of coordinates (x , y ) . If the elements of R also are integers, a digital image then becomes a two-dimensional function, whose coordinates and the amplitude values are integers. Let A be a set in Z 2 , the elements of which are pixel coordinates (x , y ). If w = (x , y ) is an element of A, then we write w A. Similarly, if w is not an element of A, we write w A. / A set of B pixels satisfying a particular condition is written as B = {w |condition} . 1
B A A
B A
(a)
(b) A B
(c) A B
B A A
(d) A
(e) A B
Figure 1: Set operations. For example, the set of all pixel coordinates that do not belong to A, denoted by A , is given by A = {w |w A} . / This set is called the complement of A. The union of two sets, denoted by C =AB is the set of all elements that belong to wither A or B , or both. Similarly, the intersection of two sets A and B is the set of elements that belong to both sets, denoted by C = A B. The difference of sets A and B , denoted by A B , is the set of all elements that belong to A but not to B : A B = {w |w A, w B } . / Figure 1 illustrates these set operations. In addition to the basic operations, morphological operations often require two operations that are specic to sets whose elements are pixel coordinates. The reec tion of set B , denoted by B , is dened as B = {w |w = b, forb B } . The translation of set A by z = (z x , z y ), denoted by (A)z , is dened as (A)z = {c |c = a + z , fora A} . A binary image can be viewed as a bivariate function of x and y . Morphological theory views a binary image as a set of it foreground (1-valued) pixels, the elements of which are in Z 2 . Set operations such as union and intersection can be applied directly to binary image sets. For example, if A and B are binary images, then C = A B is also a binary image, where a pixel in C is a foreground pixel if either or both of the corresponding pixels in A and B are foreground pixels. Table 1 shows Matlabs logical operations that perform set operations on binary images. 2
(A)z
(a) Translation
(b) Reection
Figure 2: Translation and reection. Dot identies the origin. Table 1: Set operations for binary images in Matlab. Set operation MATLAB expression Name AB A&B AND AB A| B OR A ~A NOT AB A &~ B DIFFERENCE
The operations of dilation and erosion are fundamental to morphological image processing. Many of the algorithms are based on these operations.
2.1
Dilation
Dilation is an operation that grows or thickens objects in a binary image. The specic manner and the extent of this thickening is controlled by a shape referred to as a structuring element. Dilation is a process that translates the origin of the structuring element throughout the domain of the image and checks to see whether it overlaps with 1-valued pixels. The output image is 1 at each location of the origin of the structuring element if the structuring element overlaps at least one 1-valued pixel in the input image. Mathematically, dilation is dened in terms of set operations. The dilation of A by B , denoted by A B , is dened as A B = z |( B )z A = ,
where is the empty set and B is the structuring element. In words, the dilation of A by B is the set consisting of all the structuring element origin locations where the reected and translated B overlaps at least some portion of A. Dilation is commutative, i.e., A B = B A. It is a convention in image processing to let the rst operand of A B be the image and the second operand be the structuring element, which is usually much smaller than the image. The Matlab image processing toolbox function imdilate performs dilation. The syntax is C = imdilate (A , B) where A and C are binary images and B is a matrix of 0s and 1s that specify the structuring element. 3
Example 1. Write a Matlab program to dilate the image A with the structuring element B shown below.
0 1 0
Structuring Element Decomposition Dilation is associative. That is A (B C ) = (A B ) C . Suppose that structuring element B can be represented as a dilation of two structuring elements B 1 and B 2 : B = B1 B2. Then, A B = A (B 1 B 2 ) = (A B 1 ) B 2 . In other words, dilating A with B is the same as rst dilating A with B 1 , and then dilating the result with B 2 . We say that B can be decomposed into the structuring elements B 1 and B 2 . The associative property is important because the time required to compute dilation is proportional to the number of nonzero pixels in the structuring element. Consider for example, dilation with a 5 5 array of 1s: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1. 1 1
This structuring element can be decomposed into a ve-element row of 1s and a le-element column of 1s: 1 1 1 1 1 1 1 1 . 1 1 The number of elements in the original structuring element is 25, but the total number of elements in the row-column decomposition is only 10. This means that dilation with the row structuring element rst, followed by dilation with the column structuring element can be performed approximately 2.5 times faster that dilation with the 5 5 array of 1s.
The strel Function Matlab image processing toolbox function strel constructs structuring elements with a variety of shapes and sizes. Its basic syntax is B = s t r e l ( shape , parameters ) This returns a special quantity called an strel object. The function imdilate is able to use the decomposed from of the structuring element. Example: B = s t r e l ( diamond , 5 ) creates a at diamond sharped structuring element such that the distance from the structuring elements origin to the extreme points of the diamond is 5. There are other shapes such as disk, line, rectangle, etc. The following code shows how to use the structuring element generated by the strel function is used to carry out dilation. A = imread ( image . t i f ) ; B = s t r e l ( diamond , 5 ) C = imdilate (A, B ) ; imshow (C)
2.2
Erosion
Erosion shrinks or thins in a binary image. As in dilation, the manner and extent of shrinking is controlled by the structuring element. The output of erosion has a value 1 at each location of the origin of the structuring element, such that the structuring element overlaps only 1-valued pixels of the input image. The mathematical denition of erosion is similar to that of dilation. The erosion of A by B , denoted by A B , is dened as A B = z |(B )z A = .
In other words, erosion of A by B is the set of structuring element origin locations where the translated B has no overlap with the background of A. The Matlab image processing toolbox function imerode performs erosion. The syntax is C = imerode ( A , B ) where A and C are binary images and B is a matrix of 0s and 1s that specify the structuring element.
In practical image processing applications, dilation and erosion are used most often in various combinations. We can apply a series of dilation and erosion operations to an image, either using the same structuring element or, sometimes, a different one. Two such common operations are opening and closing.
Morphological Opening The morphological opening of A by B , denoted by A B , is simply erosion of A by B followed by the dilation of the result by B : A B = (A B ) B.
An alternative mathematical formulation of opening is A B = {(B )z |(B )z A} where {} denotes the union of all sets inside braces, and the notation C D means that C is a subset of D. This formulation has a simple geometric interpretation: A B is the union of all translations of B that t entirely within A.
contain the structuring element, smoothes the object contours, breaks thin connections, and removes the protrusions. Morphological Closing The morphological closing of A by B , denoted by A B , is a dilation followed by an erosion: A B = (A B ) B. Geometrically, A B is the complement of the union of all translations of B the do not overlap A.
Like opening, morphological closing tends to smooth the contours of objects. Unlike opening, however, it generally joins narrow breaks, lling long thin gulfs, and lls holes smaller than the structuring element.
Matlab Functions for Opening and Closing Opening and closing are implemented in the toolbox with functions imopen and imclose. The following carries out opening: C = imopen ( A , B ) Closing is carried out as follows: C = imclose ( A , B ) In these code snippets, A is a binary image, and B is matrix of 0s and 1s that species the structuring element. A strel object, SE can be used instead of B. Example 2. Write a Matlab program to carry out morphological opening on an image using a square structuring element.
Example 3. Write a Matlab program to carry out morphological closing on an image using a square structuring element.
Function bwmorph Image processing toolbox function bwmorph implements a variety of useful operations based on combinations of dilations, erosions, and lookup table operations. g = bwmorph( f , operation , n ) where f is an input binary image, operation is a string specifying the desired operation, and n is a positive integer specifying the number of times the operation is to be repeated. The operation can be open, close, dilate , erode, thin , skel, and several others. Thinning, (thin) means reducing binary objects or shapes in an image to strokes that are a single pixel wide. Skeletonisation (skel) is another way to reduce binary image objects to a set of thin strokes that retain the important information about the shapes of the original objects. Example 4. Write a Matlab program to carry out skeletonisation.
10
0 0 x0
y0
w 1 y 0 x0
y0
w 1 y
h 1 x
h 1 x
(a) 4-neighborhood
(b) 8-neighborhood
Figure 3: Neighborhoods.
Using connected component analysis we can extract individual objects or connected components in an image. In order to do this, we must dene connectedness A pixel p at coordinates (x 0 , y 0 ) has two horizontal and two vertical neighbors whose coordinates are This set is known as 4-neighbors of p , denoted by N 4 (p ). In a
similar manner we can dene N 8 (p ) as well. Figure 3 shows this. Two pixels p and q are said to be 4-adjacent if q N 4 (p ). Similarly, two pixels p and q are said to be 4-adjacent if q N 8 (p ). A path between pixels p 1 and p n is a sequence of pixels p 1 , p 2 , . . . , p n such that p k is adjacent to p k +1 , for 1 k < n . A path can be 4-connected or 8-connected depending on the denition of adjacency used. Two foreground pixels p and q are said to be 4-connected is there exists a 4connected path between them, containing entirely foreground pixels. Similarly, two foreground pixels p and q are said to be 8-connected is there exists a 8-connected path between them, containing entirely foreground pixels. Example 5. Identify if the pixels p and q are 4-connected or 8-connected.
11
Connected Components For any foreground pixel p , the set of all foreground pixels connected to it is called the connected component containing p . The nature of the connected component depends on the denition of adjacency we choose between 4- and 8-adjacency.
IPT Function bwlabel Matlab Image Processing Toolbox Function bwlabel computes all the connected components in a binary image. [ L , num] = bwlabel ( f , conn ) where f is a binary image and conn species the desired connectivity (either 4 or 8). Output L is called the label matrix, and num (optional) gives the total number of connected components found.
12