Erosion (Morphological Operation) — Image Processing

Anshul Sachdev
6 min readMar 17, 2024

--

Visualizing the Code with Geekosophers

In this blog of the series Visualizing the Code with Geekosophers we are going to look at Erosion, a morphological operation in Image Processing.

What are Morphological Operations

Morphology means the study of shape of things. In Image Processing, the operations performed based on shape are called morphological operations. We apply structuring element to the input image and perform the operation to get the output image. The input and output images are of the same dimensions. The value of the pixel in the output image is based on the comparison of the corresponding pixel in the input image with its neighbours.

Before we dive into the topic, it is important to understand about the Structuring element.

What is Structuring Element

It is a 2D matrix which identifies the pixel to be processed and the neighbouring pixels used in the processing. We choose a structuring element according to the shape of the object we want to process in the input image.

Origin: Generally, the centre of the structuring element is called the origin. It identifies the pixel being processed. The origin need not always be the centre but might be even outside of the structuring element.

More about the structuring element can be found here.

Now that we are familiar with the basics of morphological operations, let’s dive into Erosion.

Erosion

Erosion is one of the most common morphological operation along with Dilation. Erosion is used to remove pixels from the boundary of the input image shrinking the object.

Erosion operator takes two inputs, one is the image and the other one is the structuring element. The structuring element determines the effect of erosion on the input image.

Key points about Erosion-

  1. Erosion is the dual of dilation.

2. Size of input and output image is same.

3. It shrinks the object in the input image.

4. The nature of thinning(shrinking) is determined by the structuring element.

5. Erosion can be performed on Binary images as well as Grayscale images.

  • Erosion of Binary Image- In a binary image, a pixel is set to 1 if all of the neighbouring pixels have the value 1.
  • Erosion of Grayscale Image- The value of the output pixel is the minimum value of all pixels in the neighbourhood.

Only those pixel in the input image whose corresponding kernel pixel is 1(True) is considered to be the neighbour pixel.

Applications of Erosion-

  1. Counting objects- The erosion process can be used to disconnect connected objects so that they can be separated and counted using labelling algorithms.

2. Removing Salt Noise- The erosion process can be used to remove salt noise(white dots) from an image. This affects the quality of the image, as a result opening process(erosion followed by dilation) is generally used for this purpose.

Erosion Animation

Study of any topic is incomplete without self practising the concept. To help you with the process, we have created an animation where you can view the Erosion process live. The animation is dynamically generated according to the user inputs providing flexibility over the content.

Play Animation

Want some more features in the animation? Don’t worry we have got that covered too. The animation code is open sourced so that you can tweak the code according to your needs to get a better understanding of the concept.

GitHub Link for the Source Code.

Show your appreciation for our efforts by starring the repo. Thanks :))

Erosion Code using OpenCV — Python

Key Points-

With Binary Image-

  • The code automatically uses the padding of ‘1’ while eroding the image.
  • Pixel value is ‘1’ if all the neighbouring pixels are ‘1’

With Grayscale Image-

  • The code automatically uses the padding of ‘255’ while eroding the image.
  • Pixel value is the minimum of all pixels in the neighbour.

Code-1. Binary Erosion- Using input image in the form of a numpy array.

import numpy as np
import cv2
img = np.array([[ 1, 1, 0, 0, 0],
[ 1, 1, 1, 0, 1],
[ 0, 1, 1, 0, 1],
[ 0, 1, 1, 0, 1],
[ 0, 0, 0, 0, 0],
[ 1, 1, 0, 0, 0]], np.uint8)
kernel = np.array([[1, 1, 0],
[1, 1, 0],
[1, 1, 0]], np.uint8)
eroded_image = cv2.erode(img,kernel,iterations = 1)
print(eroded_image)

Code-2. Binary Erosion- Using a real input image.

The following code takes input a 7x7 image and uses a 5x5 kernel( structuring element) and gives output a jpg image of size 7x7.

import numpy as np
import cv2
filepath = ‘Erosion/assets/erosion7x7sample2.jpg’
img = cv2.imread(filepath,0)
kernel = np.array([[0, 1, 0, 0, 0],
[1, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1]], np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
outpath = ‘Erosion/assets/resultErosion.jpg’
cv2.imwrite(outpath, erosion)
Input Image(left) and Output Image(right) (Size: 7 pixel X 7 pixel). The above images has been enlarged for better visualisation.

Code-3. Grayscale Erosion- Using input image in the form of a numpy array.

import numpy as np
import cv2
img = np.array([[3, 4, 8, 1, 7],
[9, 4, 2, 1, 6],
[7, 8, 8, 1, 1]], np.uint8)
kernel = np.array([[0, 1, 0],
[1, 1, 0],
[0, 1, 0]], np.uint8)
eroded_image = cv2.erode(img,kernel,iterations = 1)print(eroded_image)
################################################################### The code automatically creates a padding of ‘1’ around the image  # for binary erosion and a padding of '255' for grayscale erosion# Input Image (3X5):
# 1 0 1 0 0
# 1 1 0 0 1
# 0 0 1 1 0
# Padded Image for 3X3 kernel(Binary Erosion):
# 1 1 1 1 1 1 1
# 1 1 0 1 0 0 1
# 1 1 1 0 0 1 1
# 1 0 0 1 1 0 1
# 1 1 1 1 1 1 1
# Padded Image for 5X5 kernel(Grayscale Erosion):
# 1 1 1 1 1 1 1 1 1
# 1 1 1 1 1 1 1 1 1
# 1 1 1 0 1 0 0 1 1
# 1 1 1 1 0 0 1 1 1
# 1 1 0 0 1 1 0 1 1
# 1 1 1 1 1 1 1 1 1
# 1 1 1 1 1 1 1 1 1
##################################################################

That’s all for this blog. Hope you had a great time learning about Erosion process.

In the comment section: What do you think about all of this? Are you excited about Image Processing, pissed, or somewhere in between?

Call to action: Do follow us to read more on Image Processing and other tech-related blogs.

Want to contribute? Reach out to us at geekosophers@gmail.com.

--

--