Morphological Operations — A Cleaning Technique in Image Processing

Morphological Operation applied to the Letter J | ResearchGate

Image processing is an interesting yet rigorous undertaking in the field of computing. What makes it more painstaking is the presence of nuances in the structure of these images undergoing analysis. Nuances may come in the form of noise, texture, and structure imbalance to mention a few. But do not fret image processing enthusiasts, for our researchers have found a way to account for these image imperfections and that is what we are going to talk about today — Morphological Operations.

Morphological Operations

Morphological image processing is an assembly of non-linear operations related to the morphology (shape) of features in an image. These operations rely only on the relative ordering of their pixels and not on their numerical values. This tell us that these operations are especially suited to the processing of binary images. These operations, alongside filtering techniques, are useful for cleaning images especially in preparation for segmentation analysis.

Next, let us define a structuring element.

Structuring Element
Similar to spatial filters, morphological operations transform each pixel of an image to a value based on the values of its neighbors selected by a smaller matrix known as a structuring element.

Examples of 3x3 structuring elements

Our structuring elements would be useless if we do not apply them in morphological operations which are responsible for transforming our images. These operations are:

  • Dilation
  • Erosion

In a general sense, the dilation process adds pixels to the boundaries of objects in an image, while the erosion process on the other hand removes pixels on object boundaries. Dilation emphasizes the image further, hence including substantive information. Erosion removes salt and pepper noise which can interrupt the analysis if not taken care of. Always remember that the number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image.

Now, we shall see dilation and erosion in action. But before that, import these essential libraries to witness the image transformation for yourself.

import numpy as np
from import imread, imshow
from skimage.color import rgb2gray
from skimage.morphology import erosion
from skimage.morphology import dilation

For the sake of a simple illustration, we shall use a 3x3 structuring element applied on built-in binary images in the skimage library.

from skimage.draw import ellipse
ellps = np.zeros((25, 25), dtype=np.uint8)
rr, cc = ellipse(13, 13, 5,8, rotation=np.deg2rad(0))
ellps[rr, cc] = 1
imshow(ellps, cmap='gray')
An ellipse structuring element

Now, we apply dilation and erosion respectively and see how it affected the binary image.

Applying Erosion

from skimage.morphology import erosion
imshow(erosion(ellps, structuring_elem));
Eroded ellipse image

As per the definition, erosion removes information from our images. And it did significantly remove pixels from the ellipse as seen in the output image above. Let us see how it would like if we dilated this.

Dilated ellipse image

You guessed right! It made the ellipse larger because it added more pixels to our image. This has added more information to the image which made the ellipse more defined the original one.

You might ask, is it possible to dilate and erode (and vise versa) at the same time? Fortunately, it is possible!

Chaining morphological operations
A good thing to know about morphological operations is that you can chain operations in a single run. It could be a combination of both or a pair up of the same operation. It is up to you on how you would use it, as long as you arrive at your desired state.

Here, I will depict three different implementations of chained morphological operations. See all three and compare the difference.

imshow(erosion(erosion(ellps, structuring_elem), structuring_elem));
Chained erosion operation

The image became significantly smaller as we have applied another layer of erosion than before. Comparing it from the original image, this lost more information and it is hard to recognize the image as an ellipse now.

imshow(dilation(dilation(ellps, structuring_elem), structuring_elem));
Chained dilation operation

On the other hand, applying another layer of dilation to the image made the output look more refined as it added more pixels to the boundaries of the ellipse.

imshow(dilation(erosion(ellps, structuring_elem), structuring_elem));
Erosion x Dilation Operation

Then finally, the erosion/dilation combo. Basically, it just neutralized the image since the pixels that have been eroded has been recovered by the pixels that were dilated (and vise versa).

And there you have it. You are becoming more and more knowledgeable about image processing techniques. Now, you learned how to clean images! Wonderful. Stay updated to more articles coming your way. Cheers!

A kid who uses data to make a difference.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store