Morphological Transformations of Images using OpenCV | Image Processing Part-2

Ravjot Singh
Analytics Vidhya
Published in
4 min readAug 11, 2020

In this Article, we will learn how can we perform some more image processing algorithms in Python language.

For Introduction to Image Processing in Python, What is Image Processing, How to install OpenCV on your system, you can refer to my article-

Geometric Transformations of Images using OpenCV|Image Processing Part-1

Let’s start using OpenCV Library-

1. Importing the required libraries:

cv2- For OpenCV (Used for Image Processing)

matplotlib- For Matplotlib (Used fot Plotting and Visualization)

numpy- For Numpy (Used for Scientific Computing)

pandas- For Pandas (Used for Data Analysis)

Libraries required

2. Reading an image using OpenCV library:

We can read an image by using OpenCV and Matplotlib library.

Opening an image

3. Morphological Transformations on Image:

Theory -

Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, second one is called structuring element or kernel which decides the nature of operation. Two basic morphological operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also comes into play.

Goal-

  • We will learn different morphological operations like Erosion, Dilation, Opening, Closing etc.
  • We will see different functions like : cv2.erode(), cv2.dilate(), cv2.morphologyEx() etc.

3A) EROSION

The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of foreground object. The kernel slides through the image (as in 2D convolution). A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel is 1, otherwise it is eroded (made to zero).

Here, as an example, I would use a 3x3 kernel with full of ones. Let’s see it how it works:

3B) DILATION

It is just opposite of erosion. Here, a pixel element is ‘1’ if atleast one pixel under the kernel is ‘1’. So it increases the white region in the image or size of foreground object increases. Normally, in cases like noise removal, erosion is followed by dilation. Because, erosion removes white noises, but it also shrinks our object. So we dilate it. Since noise is gone, they won’t come back, but our object area increases. It is also useful in joining broken parts of an object.

3C) OPENING

Opening is just another name of erosion followed by dilation. It is useful in removing noise, as we explained above. Here we use the function, cv2.morphologyEx().

3D) CLOSING

Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small holes inside the foreground objects, or small black points on the object.

3E) MORPHOLOGICAL GRADIENT

It is the difference between dilation and erosion of an image.

The result will look like the outline of the object.

3F) TOP HAT

It is the difference between input image and Opening of the image. Below example is done for a 5x5 kernel.

3G) BLACK HAT

It is the difference between the closing of the input image and input image.

This brings us to the end of this article. I hope you have understood the basics of Image Processing with OpenCV in Python clearly. Make sure you practice as much as possible.

Do look out for other Jupyter notebooks in the series which will explain the various other aspects of Image Processing with OpenCV in Python.

If you wish to check out more resources related to Data Science and Machine Learning you can refer to my Github account.

Hope you like the post.

--

--

Ravjot Singh
Analytics Vidhya

A Tech enthusiast || Dedicated and hardworking with a passion for Data Science || Codes in Python & R.