cv2.erode() OpenCV

OpenCV, or Open Source Computer Vision, is a library of programming functions mainly aimed at real-time computer vision. One such function, cv2.erode(), is used for image erosion process.

This Answer will delve into the cv2.erode() function, explaining its purpose, syntax, parameters, and its application in code examples.

What is the cv2.erode() function?

Erosion is a critical process in digital image processing, used to minimize objects like bright areas of the image getting thinner, whereas the dark zones get bigger. The OpenCV function that implements this operation is cv2.erode().

The primary objective of this function is to shrink the borders of an object in an image. Erosion is achieved by convolving a kernel with an image, altering the value of a particular pixel to the smallest value of all pixels in the neighborhood defined by the kernel.

Syntax and parameters of cv2.erode()

Let's have a look at the syntax of cv2.erode().

cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Syntax of cv2.erode() function

The function uses these parameters:

  • src: The source image or the image to be processed.

  • kernel: The structuring element utilized for erosion. If not specified, a 3x3 rectangle is the default.

  • dst: The resulting image, which is of the same size and type as src.

  • anchor: The anchor's location within the element. The default is (-1, -1), indicating that the anchor is at the element's center.

  • iterations: The number of repetitions of erosion. The default is 1.

  • borderType: The method of pixel extrapolation.

  • borderValue: The border's value in a constant border situation. The default is 0.

Example of using cv2.erode()

The following code shows how to use the cv2.erode() function. The code reads an image, applies the erosion operation using a 5x5 kernel, and then displays both the original and the eroded image.

This is the image we will be using:

Original image
Original image
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the picture
pic = cv2.imread('fb.png', 0)

# Create the structuring element
struct_elem = np.ones((5,5),np.uint8)

# Employ cv2.erode()
pic_eroded = cv2.erode(pic, struct_elem, iterations=1)

# Set up the matplotlib figure and subplots
fig_obj, axs_obj = plt.subplots(1, 2, figsize=(10, 5))

# Show the original picture
axs_obj[0].imshow(pic, cmap='gray')
axs_obj[0].set_title('Original Picture')

# Show the eroded picture
axs_obj[1].imshow(pic_eroded, cmap='gray')
axs_obj[1].set_title('Eroded Picture')

# Clear the x and y ticks
for each_axis in axs_obj:
    each_axis.set_xticks([])
    each_axis.set_yticks([])

# Display the figure
plt.show()
Code example of cv2.erode() function

Code explanation

Here’s a breakdown of the code:

  • Lines 13: These lines import the necessary libraries. cv2 for OpenCV functions, numpy for numerical operations (especially to create the structuring element), and matplotlib.pyplot for visualizing the images.

  • Line 6: This line reads in the image ‘image.png’ as a grayscale image (the 0 argument in imread function), and assigns it to the variable pic.

  • Line 9: This line creates the structuring element used for the erosion operation. np.ones((5,5), np.uint8) generates a 5x5 matrix (or kernel) of ones. This kernel is assigned to the variable struct_elem.

  • Line 12: This line applies the cv2.erode() function to the image stored in pic, using the kernel struct_elem and an iteration of 1. The resulting eroded image is then stored in the variable pic_eroded.

  • Line 15: This line creates a new matplotlib figure with two subplots arranged in one row and two columns, with a figure size 10x5. fig_obj and axs_obj are assigned the figure and axes objects, respectively.

  • Lines 1819: These lines display the original image on the first subplot (index 0) and set its title to ‘Original Picture’.

  • Lines 2222: These lines display the eroded image on the second subplot (index 1) and set its title to ‘Eroded Picture’.

  • Lines 2628: This loop goes through each subplot in axs_obj and removes the x and y ticks using the set_xticks and set_yticks functions.

  • Line 31: This line displays the matplotlib figure and its subplots, thus visualizing the original and eroded images side by side.

Wrapping up

In the realm of digital image processing, the cv2.erode() function in OpenCV is an invaluable asset. It's widely used for tasks such as noise removal, object separation, and simplification of geometry. With a few simple lines of code, it enables the reduction of object boundaries in images, making it a crucial function for those intrigued by computer vision and image processing tasks.

Quick Quiz

1

What is the primary function of cv2.erode() in OpenCV?

A)

To enlarge the boundaries of an object in an image

B)

To move the boundaries of an object in an image

C)

To shrink the boundaries of an object in an image

D)

None of the above

Question 1 of 30 attempted

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved