Table of Contents
show
Introduction
- Basic morphological operations are dilation and erosion
- Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels in object boundaries
- The number of pixels added or removed from the objects in an image depends on the size and shape of the kernel used to process the image
- Erosion:
- The structuring element is placed on the image and the center of the element is positioned over each pixel
- If all the pixels in the image under the kernel, corresponding to 1 in kernel are also 1, the pixel at the center remains 1. Otherwise 0
- Dilation:
- The structuring element is placed on the image and the center of the element is positioned over each pixel.
- If any of the pixels in the image under the kernel corresponding to 1 in the kernel is 1, the pixel at center become 1
Erosion
cv2.erode(src, kernel, dst=None, anchor=(-1, -1), iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=0)
- src: The input image (must be a binary or grayscale image).
- kernel: The structuring element used for erosion (typically created using np.ones() or cv2.getStructuringElement()).
- dst: The output image (optional; if not provided, the result will be returned).
- anchor: The position of the anchor within the kernel (-1, -1) by default, meaning the center of the kernel.
- iterations: The number of times the erosion is applied (default is 1).
- borderType: The pixel extrapolation method for borders (optional).
- borderValue: The value used in case of a constant border (optional).
Anchor
Anchor Position: The anchor point determines which pixel in the kernel corresponds to the pixel being evaluated in the input image.
Default Value: The default anchor position in OpenCV is (-1, -1), which automatically places the anchor at the center of the structuring element (kernel).
- This is typically the most commonly used setting, as it ensures that the operation is centered on the pixel being processed.
Border Types
- BORDER_CONSTANT: Pads with a constant value.
- BORDER_REPLICATE: Repeats the edge pixels.
- BORDER_REFLECT: Mirrors the pixels (excluding the edge pixel).
- BORDER_REFLECT_101: Mirrors the pixels (including the edge pixel).
- BORDER_WRAP: Wraps the border around from the opposite edge
Dilation
cv2.dilate(src, kernel, iterations=1, borderType=cv2.BORDER_CONSTANT, borderValue=None)
- src: The source image on which dilation is applied. This should be a binary or grayscale image.
- kernel: The structuring element used for dilation. This is typically a 2D array or matrix. Commonly used kernels are rectangular, elliptical, or cross-shaped. You can create a kernel using cv2.getStructuringElement().
- iterations: The number of times dilation is applied. Default is 1. Increasing this value will further expand the boundaries.
- borderType: (Optional) The border type used when the kernel extends beyond the edges of the image. Default is cv2.BORDER_CONSTANT.
- borderValue: (Optional) The value used for the border pixels when borderType is cv2.BORDER_CONSTANT. Default is None.
Applications of Erosion
- Removing small objects or thin structures from an image.
- Separating overlapping objects in an image.
- Finding boundaries of objects in an image.
Applications of Dilation
- Filling small holes or gaps in an image.
- Enlarging objects or structures in an image.
- Joining broken parts of an object in an image.

Opening and Closing
Opening: It is an erosion operation followed by a dilation. This process helps remove small noise from the foreground (removes small objects).


Closing: It is a dilation operation followed by an erosion. This process helps close small holes in the foreground objects.
Views: 0