opencv
  1. opencv-erosion-dilation

Erosion & Dilation - ( Template Matching and Transformations )

Heading h2

Syntax

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

cv2.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])

Example

import cv2
import numpy as np

# read the image
img = cv2.imread('image.jpg', 0)

# create a kernel
kernel = np.ones((5,5), np.uint8)

# erosion
erosion = cv2.erode(img, kernel, iterations=1)

# dilation
dilation = cv2.dilate(img, kernel, iterations=1)

# display the output
cv2.imshow('Original', img)
cv2.imshow('Erosion', erosion)
cv2.imshow('Dilation', dilation)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

The output of the above code will be three images - the original image, the eroded image, and the dilated image.

Explanation

Erosion and dilation are two basic operations in digital image processing used for morphology, which is the study of the shape and structure of objects. Erosion and dilation are usually performed on binary images, but can also be performed on grayscale images.

Erosion is a morphological operation that removes pixels at the edges of an object to make it smaller. It is implemented using a structuring element or kernel which slides across the image and compares each pixel with the corresponding kernel pixel. If any of the pixels in the kernel is 0, the output pixel is set to 0, otherwise, it is set to 1.

Dilation is the opposite of erosion. It adds pixels at the edges of an object to make it larger. It is also implemented using a structuring element or kernel which slides across the image and compares each pixel with the corresponding kernel pixel. If any of the pixels in the kernel is 1, the output pixel is set to 1, otherwise, it is set to 0.

In the above example, we read an image, create a kernel using the np.ones() function, and apply erosion and dilation operations using the cv2.erode() and cv2.dilate() functions respectively. The iterations parameter specifies the number of times each operation is performed.

Use

Erosion and dilation operations are commonly used in digital image processing for various applications such as edge detection, noise reduction, morphological analysis, and template matching.

Important Points

  • Erosion is a morphological operation that removes pixels at the edges of an object to make it smaller
  • Dilation is the opposite of erosion, it adds pixels at the edges of an object to make it larger
  • Both operations are usually performed on binary images, but can also be performed on grayscale images
  • A kernel is used to perform erosion and dilation operations
  • The iterations parameter specifies the number of times each operation is performed

Summary

In conclusion, erosion and dilation are important operations in digital image processing used for morphology. They are used for various applications such as edge detection, noise reduction, morphological analysis, and template matching. OpenCV provides the cv2.erode() and cv2.dilate() functions to apply these operations, using a structuring element or kernel.

Published on: