0
0
Computer Visionml~5 mins

Morphological operations (erosion, dilation, opening, closing) in Computer Vision

Choose your learning style9 modes available
Introduction
Morphological operations help clean and change shapes in images by adding or removing pixels. They make it easier to find important parts or fix small mistakes in pictures.
Removing small noise dots from a scanned document image.
Making objects in a photo thicker or thinner to see their shape better.
Separating two objects that are touching in a picture.
Filling small holes inside objects in a black and white image.
Smoothing the edges of shapes in a binary image for better analysis.
Syntax
Computer Vision
cv2.erode(image, kernel, iterations=1)
cv2.dilate(image, kernel, iterations=1)
cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
image: Input image, usually black and white (binary) or grayscale.
kernel: A small matrix (like 3x3) that defines the shape of the operation.
iterations: How many times to apply the operation.
Examples
This makes the white parts in the image smaller by removing pixels at the edges.
Computer Vision
kernel = np.ones((3,3), np.uint8)
eroded = cv2.erode(image, kernel, iterations=1)
This makes the white parts bigger by adding pixels around edges, done twice.
Computer Vision
kernel = np.ones((5,5), np.uint8)
dilated = cv2.dilate(image, kernel, iterations=2)
Opening removes small white noise by eroding then dilating.
Computer Vision
kernel = np.ones((3,3), np.uint8)
opened = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
Closing fills small black holes by dilating then eroding.
Computer Vision
kernel = np.ones((3,3), np.uint8)
closed = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
Sample Model
This code creates a small black and white image with a white square and one noise pixel. It then shows how erosion, dilation, opening, and closing change the image.
Computer Vision
import cv2
import numpy as np

# Create a simple binary image with noise
image = np.zeros((7,7), dtype=np.uint8)
image[2:5, 2:5] = 255  # a white square
image[1,1] = 255      # noise pixel

kernel = np.ones((3,3), np.uint8)

# Apply erosion
eroded = cv2.erode(image, kernel, iterations=1)

# Apply dilation
dilated = cv2.dilate(image, kernel, iterations=1)

# Apply opening (remove noise)
opened = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

# Apply closing (fill holes)
closed = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)

print("Original image:\n", image)
print("Eroded image:\n", eroded)
print("Dilated image:\n", dilated)
print("Opened image:\n", opened)
print("Closed image:\n", closed)
OutputSuccess
Important Notes
Erosion shrinks white areas, useful to remove small white noise.
Dilation grows white areas, useful to fill small black holes.
Opening is erosion followed by dilation, good for removing noise without changing shape much.
Closing is dilation followed by erosion, good for filling holes inside objects.
Summary
Morphological operations change image shapes by adding or removing pixels.
Erosion shrinks white parts; dilation grows them.
Opening removes noise; closing fills holes.