Morphological Operators


In the previous tutorial of OpenCV, we have discussed the blurring of the images with different operations on them. In this tutorial, we shall discuss the morphological operators, which are very much necessary to show the variety of effects by reducing the noise on the image. Few operations in this tutorial are good to reduce the pixel with the value of the black color on white background. These morphological operations can be applied on to the text data, to apply the dilation and erosion effects.

Now, let us look into the working on Jupyter notebook by applying the various filters to the image.

Initially, import the necessary libraries that are required to perform the operations on the image.

Create a function definition of creating an image with a black background by containing the text in the white foreground colour.

Then define another function to display the respective created image in the above function

Now, load the image and display the image. You will get the image displayed accordingly.


Erosion effect will clear the boundary region of the foreground image objects. In simple terminology, this effect will shrink the region of the foreground object. Let us apply the erosion effect on this image.

To apply the effect of erosion, create a kernel with specific size storing ones, that is of black color and apply the erode function to the image.

The image looks in this way when it is displayed

When the number of iterations in the argument of the erode function is increased then the shrinkage of the image will be increased, thus displays the image with less thickness of the foreground objects.


The Opening is the dilation effect applied after the erosion. This effect will remove the structural pixels of the image at the boundary regions.

To apply the opening effect, we need a noisy image. Let us create some random noise on our image and remove it by applying the opening effect. Create a white and black noisy image by using the random creation function of the NumPy library.

Now, multiply the maximum value with 255 to store the array in the image format.

Create a noisy image, by adding a clear image with the created noisy image.

Now apply the morphological operation of opening on to the noisy image to reduce the noise and display the clear image without any noise.


Closing is another important filter or effect of removing the noise from the foreground image. In closing operation, the structural image pixels are filled accordingly from the image boundaries.

Let us load the image with the noise on the foreground objects and display it.

By applying this closing effect of the morphological operation, the noise on the foreground objects are cleared and displays the clear image. The black dots on the white foreground image is cleared.

Morphological Gradient

The morphological gradient is the difference between dilation and erosion effects on the image.

Let us load the clear image and display it.

Now, let us apply the morphological gradient operation and display the resultant image, which will be the difference between the dilation and erosion effects on to the image.

This is all about the end of this tutorial about the Morphological operators and application of different operations on to the image. For any sort of doubts or the questions related to this tutorial, you can reach out through the comment box.

Related Tutorials

Image Blurring


Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *