Computer Vision Morphological Operations
Morphological operations reshape the white regions of a black-and-white image. They grow shapes, shrink shapes, fill gaps, and wipe away specks. Vision engineers use them right after thresholding to clean a rough mask into a tidy one.
The Structuring Element
Every operation slides a small pattern called a structuring element, or kernel, across the image. Think of the kernel as a tiny stamp. At each position the stamp checks which neighboring pixels are white and decides the fate of the center pixel.
Rectangle 3x3 Cross 3x3 Ellipse 5x5
1 1 1 0 1 0 0 0 1 0 0
1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 0 1 1 1 1 1
1 1 1 1 1
0 0 1 0 0
Bigger kernels cause stronger effects. Kernel shape decides which directions the effect favors.
Erosion: Shrinking Shapes
Erosion keeps a white pixel only when every pixel under the kernel is white. Edges peel away, thin lines vanish, and small specks disappear. Watch a 3x3 kernel work on a small block.
Original After Erosion 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Only the three middle pixels have a full white neighborhood, so only those survive.
Dilation: Growing Shapes
Dilation turns a pixel white when any pixel under the kernel is white. Shapes swell, gaps shrink, and broken strokes reconnect.
Original After Dilation 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1
Opening: Remove Specks
Opening applies erosion and follows with dilation. Erosion deletes small white noise, and dilation restores the size of the shapes that survived. Think of it as scrubbing dust off a window without moving the furniture.
Noisy mask: [BIG BLOB] . . ' . Opened mask: [BIG BLOB]
Closing: Fill Holes
Closing applies dilation and follows with erosion. Dilation seals small black holes and narrow cracks, and erosion trims the outer edge back to its original size. Closing repairs a coin whose center turned black during thresholding.
Mask with holes: [BLOB with o o] Closed mask: [SOLID BLOB]
Other Useful Operations
Morphological Gradient
The gradient subtracts the eroded image from the dilated image. The result outlines each shape with a thin border.
Top Hat
Top hat subtracts the opened image from the original. Small bright details on an uneven background stand out, such as bright text on a shadowed page.
Black Hat
Black hat subtracts the original from the closed image. Small dark details pop out, such as dark cracks on a bright surface.
Code Example
import cv2
import numpy as np
mask = cv2.imread("mask.png", cv2.IMREAD_GRAYSCALE)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
eroded = cv2.erode(mask, kernel, iterations=1)
dilated = cv2.dilate(mask, kernel, iterations=1)
opened = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
edges = cv2.morphologyEx(mask, cv2.MORPH_GRADIENT, kernel)Choosing the Right Operation
Problem Operation ------------------------------ -------------- Tiny white specks Opening Small black holes in objects Closing Touching objects need a gap Erosion Broken letter strokes Dilation Need only the outline Gradient
Practical Tips
- Start with a 3x3 kernel and grow it only when results look weak
- Apply opening before closing when a mask has both specks and holes
- Keep iterations low, since each pass changes shape sizes
- Run these tools on binary images for predictable results
Real-World Walkthrough: Counting Coins
A camera photographs coins on a dark table. Thresholding turns the coins white, yet shiny centers often break into black holes and dust creates tiny white dots. A clean mask needs two repairs.
Threshold mask -> Opening (remove dust) -> Closing (fill coin centers) -> Count blobs
Opening wipes out the dust dots without shrinking the coins. Closing fills the black holes inside each coin. Contour counting reports the correct number of coins. The pipeline uses two lines of morphology and saves hours of manual cleanup.
Common Mistakes
- Using a kernel larger than the objects, which erases them completely
- Running erosion without dilation and leaving shapes smaller than reality
- Applying morphology to grayscale photos and expecting binary-style results
- Forgetting that white pixels count as the foreground in OpenCV
Practice Exercise
Draw a white letter on a black canvas and sprinkle random white dots over it. Run opening with a 3x3 kernel and watch the dots vanish. Cut a few black gaps into the letter and run closing to repair them. Change the kernel to a cross shape and compare edges.
