Image Erosion with OpenCV in Python

Python OpenCV – Image Erosion

Python OpenCV - Erode image with multiple iterations

What is image erosion?

Erosion is a process that removes pixels from the boundaries of objects in an image.

What does erosion do to an image?

Erosion removes small objects, to separate objects that are too close to each other, and to make objects more uniform in shape.

What does erosion do to pixels in an image?

In erosion, the value of a pixel is changed to the minimum value of its neighbouring pixels.

How to erode an image with OpenCV?

To erode an image with OpenCV in Python, you can use the cv2.erode() function.

In this tutorial, you will learn how to use the cv2.erode() function to erode a given image, with examples.

Syntax of cv2.erode()

The syntax of cv2.erode() function to erode an image is

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

where

ParameterDescription
srcInput/source image array.
kernelA matrix. Kernel used for erosion.
dts[Optional] Output image array of the same size and type as src.
anchor[Optional] Position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
iterations[Optional] The number of times erosion is applied.
borderType[Optional] Pixel extrapolation method.
borderValue[Optional] Border value in case of a constant border.

In the examples that we cover, we do not use the optional parameters. We shall cover some examples covering iterations parameter.

cv2.erode(src, kernel, iterations=1)

Steps to erode an image

  1. Read an image into an array using cv2.imread() function.
  2. Define a kernel for erosion. Kernel is a matrix, usually with an odd number of rows and odd number of columns.
  3. Call cv2.erode() function and pass the input image array and kernel as arguments. The function returns the eroded image array.
  4. Save the eroded image using cv2.imwrite() function.

Examples

In the following examples, we shall use the below input image for erosion.

test_image_house.jpg

1. Erode image using 3×3 kernel

In the following program, we erode the given image with a 3×3 kernel of ones.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

# Define the structuring element
kernel = np.ones((3,3),np.uint8)

# Perform image erosion
eroded = cv2.erode(img, kernel)

# Save eroded image
cv2.imwrite('eroded.jpg', eroded)

eroded.jpg

Python OpenCV - Erode image using 3x3 kernel

2. Erode image using 5×5 kernel

In the following program, we erode the given image with a 5×5 kernel of ones.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

# Define the structuring element
kernel = np.ones((5,5),np.uint8)

# Perform image erosion
eroded = cv2.erode(img, kernel)

# Save eroded image
cv2.imwrite('eroded.jpg', eroded)

eroded.jpg

Python OpenCV - Erode image using 5x5 kernel

3. Erode image using kernel with different number of rows and columns

In the following program, we take a kernel of size 3×7, and apply erosion to the given image.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

# Define the structuring element
kernel = np.ones((3,7),np.uint8)

# Perform image erosion
eroded = cv2.erode(img, kernel)

# Save eroded image
cv2.imwrite('eroded.jpg', eroded)

eroded.jpg

Python OpenCV - Erode image using kernel with different number of rows and columns

4. Erode image with multiple iterations

By default, only one iteration of erosion is done. But, we can increase the number of iterations, by specifying the required iterations (integer) value to the cv2.erode() function.

Python Program

import cv2
import numpy as np

# Load the image
img = cv2.imread('test_image_house.jpg')

# Define the structuring element
kernel = np.ones((5,5),np.uint8)

# Perform image erosion
eroded = cv2.erode(img, kernel, iterations=3)

# Save eroded image
cv2.imwrite('eroded.jpg', eroded)

eroded.jpg

Python OpenCV - Erode image with multiple iterations

Summary

In this Python OpenCV Tutorial, we did image erosion on a given image using cv2.erode() function, with the help of examples.

Code copied to clipboard successfully 👍