OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Erosion and dilation are fundamental operations in the field of mathematical morphology. They are frequently employed in image processing to enhance or remove certain features from an image.
The basic idea behind erosion is similar to soil erosion �C it erodes away the boundary of the foreground object. It works by sliding a kernel (usually a simple shape like a rectangle or circle) across the image. For the pixel in the original image to be considered as 1 (or white), all pixels inside this kernel must be 1 in the original image; otherwise, the pixel is set to 0 (or black).
This operation reduces the size of the foreground object and is especially useful for removing small noise.
Dilation is the opposite of erosion. It slides a kernel across the image and, for the original pixel to be considered as 1, at least one pixel under the kernel must be 1. This enlarges the boundary of the foreground object.
Dilation is particularly useful for adding size to the foreground object or joining broken parts of an object.
Setup: First, ensure you've installed OpenCV:
pip install opencv-python
Erosion and Dilation using OpenCV:
import cv2 import numpy as np # Load the image img = cv2.imread('path_to_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply binary thresholding _, binary_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # Define the kernel. Let's use a 5x5 square kernel = np.ones((5, 5), np.uint8) # Erosion erosion = cv2.erode(binary_img, kernel, iterations=1) # Dilation dilation = cv2.dilate(binary_img, kernel, iterations=1) # Display the results cv2.imshow('Original', binary_img) cv2.imshow('Erosion', erosion) cv2.imshow('Dilation', dilation) cv2.waitKey(0) cv2.destroyAllWindows()
Run the Code: Execute the code, and you'll see the original binary image, the result after erosion, and the result after dilation displayed side by side.
The effect of erosion and dilation is controlled by the size and shape of the structuring element or kernel. A larger kernel will have a more pronounced effect.
The iterations
parameter specifies how many times the operation is applied. Applying multiple iterations of erosion or dilation will amplify the effect.
Erosion and dilation are often used in sequence to achieve various results, such as opening (erosion followed by dilation) and closing (dilation followed by erosion). These combined operations can help in noise reduction, gap filling, and more.
Sample code for erosion and dilation of images in OpenCV:
import cv2 import numpy as np # Read a binary image img = cv2.imread('binary_image.png', 0) # Define a structuring element (kernel) kernel = np.ones((5, 5), np.uint8) # Apply erosion erosion = cv2.erode(img, kernel, iterations=1) # Apply dilation dilation = cv2.dilate(img, kernel, iterations=1) # Display the results cv2.imshow('Original', img) cv2.imshow('Erosion', erosion) cv2.imshow('Dilation', dilation) cv2.waitKey(0) cv2.destroyAllWindows()