Computer Vision Lab Lessons Morphological processing
Morphological processing
Erosion, dilation, opening and closing for cleaning binary masks.
This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.
Introduction
After thresholding, noise dots and small holes remain. Morphological operations deal with them by understanding shape, not value.
Learning objectives
- Understand the structuring element.
- Distinguish opening from closing.
- Extract boundaries with the morphological gradient.
Prerequisites
Thresholding and classical segmentation.
Scientific explanation
Erosion takes the smallest value under the structuring element, so white objects shrink, and dilation takes the largest value, so they grow. Opening removes objects smaller than the element, and closing fills holes smaller than it. Top-hat highlights small bright details, and Black-hat highlights dark ones.
In-depth explanation
Morphology, the language of shapes
Mathematical morphology treats a binary image as a set of points, and the structuring element B as a "probe" that we move over it. Erosion asks: "where does the probe fit entirely inside the shape?", and dilation asks: "where does the probe touch the shape?". All other operations are built from these two questions.
A\ominus B = \{z : B_z\subseteq A\},\qquad A\oplus B = \{z : (\hat B)_z\cap A\neq\varnothing\}Important properties
- Dilation and erosion are dual: eroding the shape = the complement of dilating the background.
- Opening and closing are idempotent: applying opening twice = applying it once.
- Opening shrinks the shape (
A\circ B\subseteq A), and closing enlarges it (A\subseteq A\bullet B).
Worked example
A 10×6-pixel rectangle and a 3×3 square structuring element. Erosion removes one pixel from each side, so it becomes 8×4. Dilation of the original makes it 12×8. Opening (erosion then dilation) brings it back to exactly 10×6, because the rectangle has no protrusions smaller than the element; an isolated one-pixel noise dot, however, disappears with erosion and dilation does not bring it back.
Useful derived operations
| Operation | Formula | Use |
|---|---|---|
| Morphological gradient | (A\oplus B)-(A\ominus B) | Boundaries |
| Top-hat | A - (A\circ B) | Small bright details on a varying background |
| Black-hat | (A\bullet B) - A | Small dark details |
| Skeleton | Successive erosions with opening | Centre lines of vessels and roads |
| Watershed with the distance transform | — | Separating touching objects (cells) |
In deep learning pipelines
Morphology is often used after segmentation models: removing small spurious components, filling holes inside organs, and keeping the largest connected component (for example in liver segmentation). This post-processing must be chosen on the validation set and reported in the methodology.
from scipy import ndimage as ndi
import numpy as np
filled = ndi.binary_fill_holes(mask)
lab, n = ndi.label(filled)
sizes = ndi.sum(filled, lab, range(1, n + 1))
largest = lab == (np.argmax(sizes) + 1)Review questions
- Why does opening not restore the small dots that erosion removed?
- Which operation would you choose to fill thin cracks in an object's boundary?
- How do you separate two touching cells?
Mathematical equations
Opening = (A ⊖ B) ⊕ B, Closing = (A ⊕ B) ⊖ B.
Practical example
The coins mask after Otsu has holes inside the coins; closing with a 7×7 elliptical element fills them.
Implementation steps
- Apply Otsu, then add it to the chain.
- Add a Closing operation and change the element size.
- Try Gradient to see the boundaries.
Expected results
The chain "threshold → closing" gives a clearly cleaner mask.
Common mistakes
Forgetting that the order of operations matters, or using an element larger than the objects themselves.
Practical applications
Cleaning medical segmentation masks, separating touching cells, document analysis.
References
- OpenCV (2024).OpenCV documentation: Image Processing (imgproc module). docs.opencv.org. docs.opencv.org
- R. Szeliski (2022).Computer Vision: Algorithms and Applications (2nd ed.). Springer, Texts in Computer Science. doi:10.1007/978-3-030-34372-9
