OpenCV Tutorial – Erosion and Dilation of Image

Introduction

In this tutorial, we will learn the morphological operation of erosion and dilation. We will then see its OpenCV implementation of cv2.erode() and cv2.dilate() along with example.
Let us first understand about morphological operations.

Morphological Operations

Morphological operation is a set of non-linear operations that process images based on shapes morphology of features in an image. It applies structuring element to an input image and generate an output image.

The structuring element is a small binary image, i.e. a small matrix of pixels, each with a value of zero or one:

  • The matrix dimensions specify the size of the structuring element.
  • The pattern of ones and zeros specifies the shape of the structuring element.
  • An origin of the structuring element is usually one of its pixels, although generally, the origin can be outside the structuring element.

A common practice is to have odd dimensions of the structuring matrix and the origin defined as the center of the matrix. Structuring elements are similar to convolution kernels in linear image filtering.

The applications of these operations are –

  1. Removing noise.
  2. Isolation of individual elements and joining disparate elements in an image.
  3. Finding of intensity bumps or holes in an image.
The most basic morphological operations are Erosion and Dilation. Let us understand these operations in more details.

Erosion

It erodes away the boundaries of the foreground object and removes small-scale details from an image but simultaneously reduces the size of regions of interest.

In this operation, a convolution kernel of any shape of odd size is convolved across the image and if all the pixels under the kernel are 1 then the pixel in the original image (either 1 or 0) will be considered 1, otherwise, it is eroded i.e made to zero.

So what happens is that all the pixels near the boundary will be discarded depending upon the size of kernel. So the thickness or size of the foreground object decreases or simply white region decreases in the image.

It is useful for removing small white noises and detach two connected objects.

Basically it computes a local minimum over the area of given kernel.So the thickness or size of the foreground object decreases or in other words white region decreases in the image.

OpenCV has a specific function erode() for this operation. Let us see that in more detail.

OpenCV Erosion : cv2.erode()

Syntax

cv2.erode(src, kernel, dst,anchor,iterations,borderType,borderValue)

src: It is the image which is to be eroded.

kernel: A structuring element used for erosion. If element = Mat(), a 3 x 3 rectangular structuring element is used. Kernel can be created using getStructuringElement.

dst: It is the output image of the same size and type as src.

anchor: It is a variable of type integer representing anchor point and it’s default value Point is (-1, -1) which means that the anchor is at the kernel center.

borderType: It depicts what kind of border to be added. It is defined by flags like cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, etc.

iterations: It is number of times erosion is applied.

borderValue: It is border value in case of a constant border.

Return Value: It returns an image.

Example1: Erosion with cv2.erode()

In [1]:
import cv2
import numpy as np

In [2]:

#reading sample image
img=cv2.imread("lena.png")
In [3]:
#Display image
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

Sample Image

Sample Image - Lena
Sample Image – Lena
In [4]:
#5x5 kernel with full of ones
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
In [5]:
#Display image
window_name='erosion'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output
cv2.erode() Example -1
cv2.erode() : Example -1

Example2: Erosion with cv2.erode()

In this example, you are able to see the thickness or size of the foreground object decreases.

In [6]:
#reading an image
img_second=cv2.imread("j.png")
In [7]:
#Display image
window_name='imagesecond'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_second)
cv2.waitKey(0)
cv2.destroyAllWindows()

Sample Image

Sample Image - J
Sample Image – J
In [8]:
#5x5 kernel with full of ones.
kernel = np.ones((5,5),np.uint8)
erosion_1 = cv2.erode(img_second,kernel,iterations = 1)
In [9]:
#Display image
window_name='erosion'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,erosion_1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

cv2.erode() Example -2
cv2.erode() : Example -2

Dilation

The dilation operation usually uses a structuring element for probing and expanding the shapes contained in the input image. This operation is opposite to erosion

In this operation, a convolution kernel of any shape of odd size is convolved across the image and a pixel element is ‘1’ if at least one pixel under the kernel is ‘1’.

So it increases the white region in the image or size of the foreground object increases.

For 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.

OpenCV Dilation: cv2.dilate()

Syntax

cv2.dilate(src, kernel, dst,anchor,iterations,borderType,borderValue)

src: It is the image which is to be eroded.

kernel: A structuring element used for erosion. If element = Mat(), a 3 x 3 rectangular structuring element is used. Kernel can be created using getStructuringElement.

dst: It is the output image of the same size and type as src.

anchor: It is a variable of type integer representing anchor point and it’s default value Point is (-1, -1) which means that the anchor is at the kernel center.

borderType: It depicts what kind of border to be added. It is defined by flags like cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, etc.

iterations: It is number of times erosion is applied.

borderValue: It is border value in case of a constant border.

Return Value: It returns an image.

OpenCV has a specific function dilate() for this operation. Let us see that in more detail.

 

[adrotate banner=”3″]

Example1: Dilation with cv2.dilate()

In [10]:
#reading an image
img_third=cv2.imread("lena.png")
In [11]:
#Display image
window_name='imagethird'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_third)
cv2.waitKey(0)
cv2.destroyAllWindows()

Sample Image

Sample Image - Lena
Sample Image – Lena
In [12]:
kernel_3 = np.ones((5,5),np.uint8)
dilation = cv2.dilate(img_third,kernel_3,iterations = 1)
In [13]:
#Display image
window_name='imagedialte'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,dilation)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

cv2.erode() Example -1
cv2.erode() : Example -1

Example2: Dilation with cv2.dilate()

In [14]:
#reading an image
img_fourth=cv2.imread("j.png")
In [15]:
#Display image
window_name='imagefourth'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_fourth)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

Sample Image

Sample Image - J
Sample Image – J
In [16]:
kernel_3 = np.ones((5,5),np.uint8)
dilation_1 = cv2.dilate(img_fourth,kernel_3,iterations = 1)
In [17]:
#Display image
window_name='imagedilate_1'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,dilation_1)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

cv2.dilate() Example -2
cv2.dilate() : Example -2

Conclusion

Reaching the end of this tutorial, we learned how we can erode image using cv2.erode() , dilate image using cv2.dilate() in-built functions of opencv library. With the help of syntax and examples, we got a deeper understanding of these functions.
  • Sachin Mohan

    My name is Sachin Mohan, an undergraduate student of Computer Science and Engineering. My area of interest is ‘Artificial intelligence’ specifically Deep learning and Machine learning. I have attended various online and offline courses on Machine learning and Deep Learning from different national and international institutes My interest toward Machine Learning and deep Learning made me intern at ISRO and also I become the 1st Runner up in TCS EngiNX 2019 contest. I always love to share my knowledge and experience and my philosophy toward learning is "Learning by doing". So thats why I believe in education which have include both theoretical as well as practical knowledge.

    View all posts

Follow Us

Leave a Reply

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