Thursday, July 28, 2011

A9 - Morphological Operation

Today, we'll all talk about shapes... shapes... shapes...

When I hear the word morphology, the first thing that comes to my mind is the biological context related to the study of structures and forms of plants and animals. Basically, this instinctive definition of mine can be stretched out to the image processing context. Practically, it means the same thing but instead of organisms, we talk about anything with no life like shapes.

Moving one step forward, morphological operations are then operations on an image that transform it to another for further processing and information extraction. These operations apply a structuring element (sort of a basis image) to an input image, creating an output image of the same size as the structuring element.

The most common type of morphological operations are dilation and erosion.


Dilation
The dilation of shape A by shape B (structuring element) is defined as

 

This will be the set of all z locations wherein the intersection of the reflection of B with A is not an empty. In simple terms, you reflect B and scan through all possible locations of a particular pixel (we can call this as the origin) in B . If B overlaps with A at a particular location for the origin, even though the overlap is just a single pixel, the pixel of that instantaneous location of the origin will be lighten up. This would cause an expansion of A in the shape of B. Figure 1 illustrates this concept.

 

 Figure 1. Dilation of A by the structuring element B.
  
Erosion
The erosion of shape A by the structuring element B is mathematically defined as

 

This is the collection of all z points wherein the the translation of B is entirely-contained in A. Basically, you scan through all possible locations of the origin, if B is entirely in A on that particular origin location, then that pixel location will be lighten up. This would cause a reduction of A by the shape of B. An illustration is shown in figure 2.
 
Figure 2. Erosion of A by the structuring element B.


Going through the concepts behind two basic morphological operations, we implement these in free-hand form and in Scilab. So we first create shapes (A1, A2, A3, A4) to be dilated/eroded as shown in figure 3.

 
Figure 3. (a) A1 : 5x5 square, (b) A2 : triangle with base = 4 and height = 3,
(c) A3 : 2 boxes thick hollow 10x10 square, (d) A4 : one box thick plus sign
with 5 boxes along each line.

Then we create shapes serving as structuring elements (B1, B2, B3, B4, B5) as shown in figure 4.

 
Figure 4. (a) B1 : 2x2 square, (b) B2 : 2x1 rectangle, (c) B3 : 1x2 rectangle,
(d) B4 : one box thick plus sign with 3 boxes along each line, 
(e) B5 : diagonal of two boxes long.

An initial must do is to designate the origin for each input image and structuring element. For A1, A2 and A3, the top left white pixel is the origin while for A4 the top white pixel of the cross is the origin. For the structuring elements B1, B2 and B3, the top left white pixel is the origin. For B4 the top white pixel of the cross is the origin while for B5, the top right white pixel of the diagonal is the origin.

We first predict the output of the dilation and erosion of A by structuring element B manually (hand-draw). The resulting dilations are shown in figure 5 while the erosions are shown in figure 6.

Figure 5. Resulting image prediction when dilation is employed.
A column uses the same input image with different structuring elements.
A row uses the same structuring element for different input images.
 (NOTE: The region enclosed by the black lines is the dilated version  and 
the region enclosed by red lines is the original image. If there are no black
lines, it means that the origin is pure black image-wise.)

 
Figure 6. Resulting image prediction when erosion is employed.
A column uses the same input image with different structuring elements.
A row uses the same structuring element for different input images.
 (NOTE: The region enclosed by the black lines is the dilated version  and 
the region enclosed by red lines is the original image. If there are no black
lines, it means that the origin is pure black image-wise.)


We then verify the predictions using Scilab. In Scilab, we can do dilation by using the function dilate(image, structuring element, [origin of structuring element]) and erosion by using erode(image, structuring element, [origin of structuring element]). Results are shown in figures 7 and 8 for dilation and erosion, respectively.
 
Figure 7. Dilation of an image using Scilab.
(A column uses the same input image with different structuring elements. 
A row uses the same structuring element for different input images.)

 
Figure 8. Erosion of an image using Scilab.
(A column uses the same input image with different structuring elements. 
A row uses the same structuring element for different input images.)

The results for the predictions and using Scilab for both dilation and erosion match convincingly. The only flaw is that the predictions for dilation were shifted as compared to the results of Scilab. This issue came up because during prediction stage, I forgot to reflect the structuring element before scanning its overlap with the input image. Nonetheless, the structures formed, which is more important, match.


There are other morphological operations available in Scilab such as thin and skel

The thin function with syntax thin(image) uses the Zhang-Suen technique in thinning binary objects by border detection [2]. Basically the resulting image is the skeletion, however not always connected, of the input image.

The skel function, on the other hand, with syntax [skl,dt,lbl] = skel(img [,side, algorithm]) skeletonization of a binary object. Here, skl is the multiscale skeletion image, dt is the euclidean transform of the image, lbl is the discrete Voronoi Diagram of the boundary pixels of the object, img is the binary image considered, side is the desired skeletion (can be either 'interior' for internal skeleton , 'exterior' for external skeleton or 'both') and algorithm is the skeletonization algorithm used (can be either 'fast euclidean' or 'exact euclidean') [2].

The results when these operators are acted on A1, A2, A3 and A4 (from figure 3) are shown below.

 
Figure 9. Using the thin function on input image (a) A1, 
(b) A2, (c) A3 and (d) A4.

 
Figure 10. Performing the skel function on the input image. 
First row outputs the interior skeleton. Second row outputs
the exterior skeleton. Third row outputs both the interior and
 exterior skeletons.

Great! The activity's over! Actually, I had fun doing this! I felt like being a child again especially in the prediction stage because of the opportunity to draw shapes. 

Overall, I think I did well in predicting the outcome of using dilation and erosion morphological operators. I was able to verify my predictions using Scilab and amazingly the results match with only little glitches. With that, I think I would give myself a grade of 10.0.

References:
[1] 'Morphological Operations', 2010 Applied Physics 186 manual by Dr. Maricor Soriano
[2] Scilab help browser

No comments:

Post a Comment