1 Machine vision Summary # 5: Mphological operations MORPHOLOGICAL OPERATIONS A real image has continuous intensity. It is quantized to obtain a digital image with a given number of gray levels. Different numbers such as 256 (most common) 512 are used. Higher numbers such as 4096 are used in me sensitive applications (medical imaging f example). In the early days of machine vision, stage memy computer power were very limited expensive. F this reason, design focused on binary images (two levels of gray). Although computer memy speed have improved considerably, binary images are still widely used in various applications. Applications of binary images: Chromosome analysis Optical character recognition Industrial parts recognition Advantages of binary images Smaller memy requirements Faster execution time Can be used in many applications Limitations Cannot be extended to 3D Losing intensity details (infmation). A. Thresholding operations Thresholding allows to obtain a binary image from a gray level image. Thresholding can be expressed as follows { 1 if f(i, j) < T A(i, j) = (1) { 1 if f(i, j) Z A(i, j) = where Z is a set of intensity levels f a given object. Matlab comm im2bw allows to obtain a binary image from a gray level image. Mphological operations are operations on binary images. They affect the shape structure of the object. Definition: Mphological operations: analysis processing of geometric structure. Mathematical mphology is mainly applied to digital images but it has other applications as well. Mathematical mphology was initially developed f binary images, later, it was extended to gray level images. It was introduced in 1964 by Martheron Serra, from Ecole des Mines de Paris. B. Basic concepts from set they 1) a is an element of A: a A 2) a is not an element of A: a A 3) A is a subset of B: A B 4) C is the union of A B: C = A B (2) 5) Union: A B = {p/p A p B} 6) C is the intersection of A B: C = A B 7) Intersection: A B = {p/p A p B} 8) Complement: Ω = {p/p Ω p A}. In a binary image this cresponds to interchanging 0s 1s. There are two basic mphological operations: dilation erosion. Dilation erosion are perfmed by laying the structuring element B on the image A, sliding it cross the image. Notations Black pixel: value 0. White pixel: value 1. The structuring element B is similar to a mask a filter. B can have any shape, typical shapes include: row(3), column(3), cross(3). Matlab function strel can be used to create the structuring element. The size shape of the structuring element are imptant facts that affect the output image cannot be neglected igned. C. Dilation Dilation can be defined as follows A B = {c/c = a + b, a A, b B} (3) where A is the image B is the structuring element. This operation has some similarities with the Minkowski sum. D. Dilation example The result of dilation is A = 1 0 1 0 1 1 0 1 1 (4) B = [ 1 0 1 ] (5) C = (6) Note the the result of convolution will be C = 0 2 0 (7) Thus, in mphological operations 1+1=1. E. Erosion Erosion is the dual of dilation. It is given by C = A B = {C/B c A} (8)
where B c is a shift by c units. The structuring element B is positioned with its igin at pixel (i, j) the new pixel value is determined using the rule { 1 if B fits in A C(i, j) = (9) Another way to write erosion is as follows C = A B = {x : B x A} (10) Therefe, C consists of all points x f which the translation of B by x fits inside A. F. Some properties of erosion dilation Dilation: is commutative Dilation is associative A B = B A (11) A (B C) = (A B) C (12) Erosion is not commutative G. Example of erosion dilation With Image A is given by Erosion: Dilation: A B B A (13) A = [ 1 0 0 0 0 1 1 ] (14) B = [ ] (15) C = [ 0 0 0 0 0 0 0 ] (16) C = [ 1 1 0 1 ] (17) H. Rules f padding images Processing pixels at image bder is done as follows: 1) Dilation: Pixels beyond the image bder are assigned minimum value, that is zero. 2) Erosion: Pixels beyond the image bder are assigned maximum value, that is 1 255 (in gray level). The structuring element can be of any size shape, but usually it is much smaller than the image, The structuring element is binary has a well defined igin (center). In general B is a 3 by 3 mask it is applied to every pixel in the image. I. Examples of structuring element The most common structuring elements can be divided into four groups: 1) SE of Type I (18) 2) SE of Type II: (19) 3) SE of Type III, B k, k = 1, 2,..., 4, (rotate 90 o ): 1 X X 1 0 X (20) 1 X X 4) SE of Type IV, B k, k = 1, 2,..., 8, (rotate 45 o ): 0 0 0 X 1 X (21) where X means don t care J. Effects of dilation erosion Dilation exps the white erosion shrinks the white. 1) Dilation can be used to exp object fill in holes connect disjoint objects 2) Erosion can be used to shrink object Dilation erosion are the most basic mphological operations. Assuming the background is black (0) the object is white (1), dilation exps the object, fills in holes, repairs breaks; erosion shrinks objects, splits apart joined objects, strips away extrusions. Erosion dilation are illustrated in figure 2. Both dilation erosion use a structuring element. Other mphological operations can be derived based on combinations of dilation erosion. K. Example F image A in figure 3, apply the following: 1) Dilation then erosion, 2) Erosion then dilation. The results are shown in figure 3. It is clear that Dilation(erosion(A)) A Erosion(dilation(A)) A Dilation(erosion(A)) Erosion(dilation(A)) Clearly, erosion dilation are not the inverse of each other, they do not cancel out. They are the dual of each other. Other definitions f erosion dilation: Dilation: A B = A b (22) b B where A b is a translation of A by b units. Erosion: A B = A b (23) b B where A b is a translation of A by b units. A b is the reverse translation of A b. 2
Fig. 1. (a) Original image, (b) structuring element (strel( diamond, 6), (c) after erosion, (d) after dilation a b A erode(dilate(a, B), B) c dilate(erode(a, B), B) B Fig. 2. (a) Original image, (b) after dilation (c) after erosion. The structuring element is given by B = strel( diamond,4) Fig. 3. Comparison between erosion dilation applied in different ders L. Example Relationship between dilation erosion can be written as A B = (A c B s ) c (24) where B s is the symmetric of B. In der to illustrate equation (24), we consider image A given by: A = 1 0 0 1 (25) 0 0 B = [ 1 0 1 ] (26) 3
In this case, the compliment of A is A c = 1 1 1 0 0 0 0 (27) 1 0 0 0 1 The symmetric of the structuring element is given by B s = [ 1 0 1 ] (28) (a) (b) finally A c B s = (A c B s ) c = 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 (29) (30) which is the same as A B. Dilation erosion are the most basic mphological operations. Other operations can be derived based on these operations M. Opening closing These are mphological operations derived from dilation erosion. Opening: Erosion followed by dilation using the same structuring element: open(a, B) = dilate [erode(a, B), B] (31) A B = (A B) B (32) Closing: Dilation followed by erosion using the same structuring element: close(a, B) = erode [dilate(a, B), B] (33) A B = (A B) B (34) Both closing opening use erosion dilation but in different ders. The der is imptant, it is what makes the difference. Opening closing are not opposite of each other, they are duals. They are related as follows where X s is the symmetric of X X c is the complement of X A B = (A c B s ) c (35) Closing: fills in holes small gaps, it can be used to remove noise. Opening: Remove small objects, can be used f template matching. (c) Fig. 4. Boundary detection. (a): iginal image, (b): using equation (39), (c): using equation (40), (d) using mphological gradient. The structuring element is given by B = strel( diamond,4) Both closing opening are idempotent, i.e., applying them multiple times does not change the result. (d) (A B) B = A B (36) (A B) B = A B (37) Opening closing satisfy the following Opening: A B A Closing: A A B N. Why opening closing? Erosion is used to remove little objects in the image but at the same time, after erosion the object is smaller. Applying dilation with the same structuring element leads to the iginal size of the objects. Dilation fills little holes, but it also enlarges the object. An erosion brings the object to the iginal size. O. Mphological gradient Mphological gradient acts in a similar way to the gradient, i.e., can be used to detect edges, i.e., boundaries in binary images. (A B) (A B) A = (38) diamb where B is a disk like mask with a small diameter: diam B. Other operats that can be used to extract boundaries are Method I (A B) A (39) Method II A (A B) (40) Boundary detection using mphological operations is ilsutrated in figure 4. Region filling Region filling can be accomplished by iteratively using dilation intersection. We begin by defining a point p = 1 4
The output image when the filter is applied to the center pixel with a 3 by 3 window is 1 0 1 A = 1 1 0 (45) Original After hit miss B 1 B 2 Fig. 5. (a) Original image, (b) after hit miss inside the hole, we perfm the following operation X k = (X k 1 B) A c (41) X 0 = p (42) where B is the cross structuring element. The algithm exits when X k = X k 1. Hit miss transfmation (hit miss) Hit miss is a method f template matching. It can be used to find the location of one shape among a set of shapes. Uses two structuring elements B 1, B 2, with B 1 B 2 = 0. The hit miss operation is A B = (A B 1 ) (A c B 2 ) (43) where B = (B 1, B 2 ). Hit miss is the intersection of two erosions, it uses a pair of positive negative template. The result is 1 when B 1 fits in A B 2 misses A (fits in A c ). The output is one A has all pixels in B 1 but none in B 2. Hit miss filter is widely used binary pattern recognition. An illustration of using hit miss to find a cross in A is shown in figure 5. Majity filter A majity filter is a special case of the median filter. In a similar way to the median filter, the majity filter is very efficient in removing salt pepper noise. An example is below: Original image: A = 1 0 1 1 0 0 (44) 5