Morphological Transformation using OpenCV in Python

In this tutorial, we are going to learn about morphological transformation in Python using OpenCV. Let’s learn what is morphological transformation and how we can imply it in Python.

We are going to do this using OpenCV and NumPy packages if you have already installed open CV and NumPy then you can ignore otherwise you have to install open CV and NumPy.

How to install OpenCV and NumPy?

To install OpenCV You have to download the OpenCV program and run it on your computer and install it as normal installation.

To install NumPy you have to run the following comment in your command prompt or terminal (for Linux or Mac users) then it will be installed.

pip install numpy

Morphological Transformation in Python using OpenCV

Morphological transformation is basically some simple operations performed on a binary image. And the binary image is basically an image that contains two colors usually black and white.

There are main two operations in Morphological Transformation:
1.Erosion
2.dilation

  • Erosion: In erosion, we are just omitting the boundaries of the front image or the object image that is in the process we are thinning the object. Here we use cv2.erode() function.
  • Dilation:In the dilation process we are just going to thick the boundaries of a binary image. The bright area of the binary image dilates around the black regions of the background. It’s actually the reverse process of Erosion.Here we use cv2.dilate() function.
  • With the help of erosion and dilation we are going to perform the other five operations – Opening, Closing, Gradient, tophat, Blackhat. We are going to do this operation using the cv2.morphologyEx() function.
  • Opening: In the opening, we are going to do in the erosion but the process is the method of dilation. We use the opening to clear the basis of the object.
  • Closing: In closing, we are going to perform dilation by the process of erosion. We use the closing to remove the small holes in the object.
  • Gradient: And gradients basically the difference between the erosion process and the dilation process.
  • Tophat and Blackhat show the difference between the input image to the opening image and closing image respectively.
  • Now its time to display the image. To display the image we are using cv2.imshow() function.

The Python code for Morphological Transformation:

import cv2
import numpy as np
#performing Morphological Transformation
morph_img = cv2.imread('image.png',0)
morph_kernel = np.ones((6,6),np.uint8)
morph_erosion = cv2.erode(morph_img,kernel,iterations = 1)
morph_dilation = cv2.dilate(morph_img,kernel,iterations = 1)
morph_opening = cv2.morphologyEx(morph_img, cv2.MORPH_OPEN, kernel)
morph_closing = cv2.morphologyEx(morph_img, cv2.MORPH_CLOSE, kernel)
morph_gradient = cv2.morphologyEx(morph_img, cv2.MORPH_GRADIENT, kernel)
morph_tophat = cv2.morphologyEx(morph_img, cv2.MORPH_TOPHAT, kernel)
morph_blackhat = cv2.morphologyEx(morph_img, cv2.MORPH_BLACKHAT, kernel)
#fro displaying the image
cv2.imshow('EROSION', morph_erosion)
cv2.imshow('DILATION', morph_dilation)
cv2.imshow('OPENING', morph_opening)
cv2.imshow('CLOSING',morph_closing)
cv2.imshow('GRADIENT', morph_gradient)
cv2.imshow('TOPHAT', morph_tophat)
cv2.imshow('BLACKHAT', morph_blackhat)

Output

Now we are showing the outputs of the basic two operations Erosion and Dilation.

Morphological Transformations using OpenCV in Python

Study the images carefully, Youll definitely understands the differences between these two. Please try other operations on your own in your machine, It’ll be interesting. Thank you.

Also read:

Leave a Reply

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