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]]]]])
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:
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 explanation
Here’s a breakdown of the code:
Lines 1–3: These lines import the necessary libraries.
cv2for OpenCV functions,numpyfor numerical operations (especially to create the structuring element), andmatplotlib.pyplotfor visualizing the images.Line 6: This line reads in the image
‘image.png’as a grayscale image (the0argument inimreadfunction), and assigns it to the variablepic.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 variablestruct_elem.Line 12: This line applies the
cv2.erode()function to the image stored inpic, using the kernelstruct_elemand an iteration of 1. The resulting eroded image is then stored in the variablepic_eroded.Line 15: This line creates a new
matplotlibfigure with two subplots arranged in one row and two columns, with a figure size 10x5.fig_objandaxs_objare assigned the figure and axes objects, respectively.Lines 18–19: These lines display the original image on the first subplot (index 0) and set its title to
‘Original Picture’.Lines 22–22: These lines display the eroded image on the second subplot (index 1) and set its title to
‘Eroded Picture’.Lines 26–28: This loop goes through each subplot in
axs_objand removes the x and y ticks using theset_xticksandset_yticksfunctions.Line 31: This line displays the
matplotlibfigure 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
What is the primary function of cv2.erode() in OpenCV?
To enlarge the boundaries of an object in an image
To move the boundaries of an object in an image
To shrink the boundaries of an object in an image
None of the above
Free Resources