Morphological Operations

In image processing, there are many morphological operations. Some examples of these operations are union, intersection, complement, difference, reflection, translation, etc. These operations operate under the assumption that the structures or shapes are in binary.

The focus of this activity is the other two types of morphological operations namely: dilation and erosion. The dilation of a shape A with a structuring element B is adding A with a set Z where Z are the translations of a reflected B that when intersected with with A is not the empty set. The erosion of a shape A with a structuring element B is the set of all points Z such that B translated by Z is contained in A.

To show dilation and erosion, I generated four shapes and five structuring elements. The shapes are: a 5×5 square, a triangle (base of 4 and height of 3), a 10×10 hollow square (width of 2) and a plus sign (length of 5 each line). The structuring elements are: 2×2 ones, 2×1 ones, 1×2 ones, cross (3 pixels long, 1 pixel thick) and a diagonal line (2 boxes long). Figure 1-5 shows the four shapes being eroded and dilated (my assumption).

5x5 square
Figure 1. 5×5 square being eroded and dilated by the five structuring elements
Figure 2. Triangle being eroded and dilated by the five structuring elements
Figure 2. Triangle being eroded and dilated by the five structuring elements
Figure 3. A 10x10 hollow square being eroded by the five structuring elements
Figure 3. A 10×10 hollow square being eroded by the five structuring elements
Figure 4. 10x10 hollow square being dilated by the five structuring elements
Figure 4. 10×10 hollow square being dilated by the five structuring elements
Figure 5. Plus sign being eroded and dilated by the five structuring elements
Figure 5. Plus sign being eroded and dilated by the five structuring elements

To check if the outputs are correct, I performed the morphological operations in Scilab. Figures 6-9 shows the results of the morphological operations.

sqf1
Figure 6. (top) Erosion and (bottom) dilation of a 5×5 square in the order same as in Figures 1-5.
trf1
Figure 7. (top) Erosion and (bottom) dilation of a triangle in the order same as in Figures 1-5.
hsf1
Figure 8. (top) Erosion and (bottom) dilation of a 10×10 hollow square in the order same as in Figures 1-5.
psf1
Figure 9. (top) Erosion and (bottom) dilation of a plus sign in the order same as in Figures 1-5.

We see that each assumption matched with the Scilab-performed output. But I will not discuss everything in detail anymore because it just follows a certain pattern. To summarize, in eroding, when the structuring element is not found within the shape on a certain pixel, that certain pixel is eroded (zeroed out). In dilating, when the structuring element is not found within the shape on a certain pixel, the shape is expanded following the shape of the structuring element.

After discussing the morphological operations, we now proceed to discussing some applications namely: Open, Close and TopHat. Open operation is an operation in which:

open(shape,element) = dilate(erode(shape,element))

Close operation is an operation in which:

close(shape,element) = erode(dilate(shape,element))

TopHat operation is an operation in which:

tophat(shape,element) = shape – open(shape,element)

To examine these operations, I will also find an application to perform simultaneously. For the application, I am to isolate the “cancer cells” (big circles) from a group of “normal cells” (small circles) as shown in Figure 10.

Circles with cancer
Figure 10. Group of “cells” composed of “normal cells” and “cancer cells”

To do this, I first have to acquire a separate image to calibrate and get an approximation of the area of “normal cells”. Figure 11 shows the image to perform approximation.

Figure 11. Image of
Figure 11. Image of “normal cells” used to approximate its area

I separated Figure 11 into 12 subimages each having a size of 256×256. This is to get more data points and thus leading to a better approximation of the area. After separating into subimages, I will use one to discuss the effect of the three operations discussed earlier (Open, Close, TopHat). Figure 12 shows one of the subimages which will be used. Figure 13 shows the effect of the open operation, Figure 14 shows the effect of the close operation and Figure 15 shows the effect of the tophat operation.

BW
Figure 12. Subimage to be used.
openI
Figure 13. Result after opening the Figure 12.
closeI
Figure 14. Result after closing Figure 12.
tophatI
Figure 15. Result after performing tophat on Figure 12.

Notice that in Figure 12, we see small unwanted white spots. These white spots are clearly not part of the circles. In performing the morphological operations, I used a structuring element in the shape of a circle with a radius of 10. In Figure 13, we see that the unwanted white part have disappeared after opening the image. In Figure 14, the image have been completely distorted and only a few white circles are observable. In Figure 15, the unwanted white sport have not disappeared and to make things worse, the white circles have disappeared and only the outline remains. It is clear what the better option for the operation to be used is here. The properly clean the image, we need to use the open operation.

Going back to isolating the cancer cells, we now have a method to clean the given image in Figure 11. For each subimage, after cleaning with the open operation, we searched for blobs or a group of white pixels enclosed by black pixels. We label each blob and extract their area in terms of number of pixels. We then plot the histogram of the number of blobs based on blob area. The histogram is shown in Figure 16.

Figure 16. Histogram of blob count based on blob area
Figure 16. Histogram of blob count based on blob area for Figure 11

We see that the histogram is high at values in between 300 pixels and 600 pixels. Areas below that will be too small to be considered as a “normal cell” while values above that will be considered as a clump of cells which will then be rejected because it cannot be of any help in the approximation of the area of a “normal cell”. We then get the mean area and its corresponding standard deviation of those blobs above 300 and below 600 pixels. These values will be used to find the “cancer cells” in Figure 10.

In Figure 10, we perform the same binarization process and clean the image using the same open operation but now with a structuring element in the shape of a circle of radius 13. We then searched the whole image for blobs and labelled each. The histogram for blob count using blob area is then plotted. Figure 17 shows the said histogram.

histcancer
Figure 17. Histogram of blob count using blob area of Figure 10

After cleaning, only a few circles now remain. We notice that there are peaks less than 600. These peaks will resemble the area of the “normal cells”. A group of peaks are then observed above 800 pixels. This will resemble the area of the “cancer cells” thus when removing those blobs covered by the range (mean – standard deviation) to (mean + standard deviation) which are calculated earlier. Figure 18 shows the resulting image of the isolated “cancer cells”.

isolated
Figure 18. Isolated “cancer cells” in black and white

If we compare Figure 18 with Figure 10, we see that they match with the known observed “cancer cells”. This means that this process is effective in isolating shapes based on area.

For my above and beyond study, I will try to find a situation in which the tophat operation is used. I will take an image as shown in Figure 19. It shows a text with something that looks like a spilled ink.

abv
Figure 19. Text with spilled ink. Retrieved from http://www.rnib.org.uk/sites/default/files/unclear_vision_0.jpg

My first step is to take the negative of the image and perform the tophat operation using a square structuring element with a side length of 6. Figure 20 shows the resulting image.

TopHat
Figure 20. Result after performing tophat operation

We can now see that the color of the ink stain is now starting to merge with the background while the color of the text remain white (due to applying negative). From here, we can completely remove the stain by segmenting by threshold and the result is shown in Figure 21.

Figure 21. Resulting image after thresholding
Figure 21. Resulting image after thresholding

Although the ink stain is gone, we now observe several unwanted white patches around the letters. To fix this, we return to Figure 20 and apply a limit to thresholding. We create a patch similar to that of Figure 22 and apply lower thresholding to where the patch is white. We apply higher thresholding to the rest. The resulting image will be shown in Figure 23.

Figure
Figure 22. Patch used to perform operations
Figure 23. Result after separate thresholding
Figure 23. Result after separate thresholding

To further enhance the image, we can use the earlier images (make use of black background) to arrive to a final enhance image shown in Figure 24. The image will then be reinverted to return to black text.

Figure 24. Final enhanced image
Figure 24. Final enhanced image

Review:

First of all, I would like to thank my classmate Ralph Aguinaldo for sharing ideas with me while figuring out how to perform this activity. I would also like to thank Maam Jing for the guidance and for confirming my results.

thanks6Secondly, I would like to say that this activity really exposed the artist (or non-artist) in me. This activity was fun but the above and beyond part really made me think hard. For that, I would like to give myself a grade of 12 for this activity.

12_3References:

[1] http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html

[2] https://www.cs.auckland.ac.nz/courses/compsci773s1c/lectures/ImageProcessing-html/topic4.htm

Leave a comment