Morphological & Non-Linear Filters
These filters operate on the shape and structure of image regions rather than only raw pixel intensity. They are commonly applied to binary images, and the same ideas extend to grayscale images.
- Binary images: pixels are usually 0 (background) or 1 (foreground).
- Grayscale images: pixels are intensity values (for example 0 to 255).
The filter never changes the input grid directly. It reads from the input and writes each result to an output grid.
Classification of Morphological Filters
graph TD
A[Morphological & Non-Linear Filters] --> B[Basic Operations]
A --> C[Compound Operations]
A --> D[Derived Operations]
B --> B1[Dilation - Max Filter]
B --> B2[Erosion - Min Filter]
C --> C1[Opening - Erosion then Dilation]
C --> C2[Closing - Dilation then Erosion]
D --> D1[Morphological Gradient]
D --> D2[Top Hat Transform]
D --> D3[Black Hat Transform]
style A fill:#4A90D9,color:#fff
style B fill:#7BB661,color:#fff
style C fill:#E8A04C,color:#fff
style D fill:#C586C0,color:#fff1. Dilation (Max Filter)
How it works
- Binary rule: if at least one pixel under the structuring element (SE) is 1, the output center pixel becomes 1. Otherwise it is 0.
- Grayscale rule: output center pixel becomes the maximum value under the SE.
Two common 3x3 structuring elements:
Binary example
Using a 3x3 square SE:
Because the filter is a
, as it slides around the input image, there are exactly 9 different stopping positions where it will overlap that single, isolated 1.
- When the filter is top-left of the dot, the dot is in its bottom-right corner. It sees the 1, so it writes a 1 to the output center.
- When the filter slides over, the dot shifts to the bottom-middle of the filter. It still sees the 1, so it writes another 1 to the output center.
Grayscale example
Step 1. Input (same 5x5 example as Min/Max filter)
Step 2. Apply grayscale Dilation (3x3 local max)
Output (3x3, valid mode):
Step 3. Final result: local maxima emphasize bright regions and propagate the bright spike (200).
When to use?
- Filling small holes inside bright regions.
- Connecting nearby disconnected components.
- Expanding detected features before further analysis.
When to avoid?
- When fine boundaries must be preserved.
- When close objects must remain separated.
2. Erosion (Min Filter)
How it works
- Binary rule: output center pixel is 1 only if all required SE positions overlap foreground (1). If any required position is 0, output is 0.
- Grayscale rule: output center pixel becomes the minimum value under the SE.
Binary example
Using a 3x3 square SE:
Grayscale example
Step 1. Input (same 5x5 example as Min/Max filter)
Step 2. Apply grayscale Erosion (3x3 local min)
Output (3x3, valid mode):
Step 3. Final result: local minima suppress bright spikes, including the 200 outlier.
When to use?
- Removing small isolated bright noise.
- Separating weakly connected regions.
- Shrinking detected features to cores.
When to avoid?
- When small meaningful structures must survive.
- When thin lines are important.
3. Opening (Erosion -> Dilation)
How it works
- Binary rule: apply erosion first, then dilation, with the same SE.
- Grayscale rule: apply local minimum filter first, then local maximum filter, with the same SE.
Binary example
Input has one isolated noise pixel and one larger object:
Grayscale example
Step 1. Input (same 5x5 example as Min/Max filter)
Step 2. Apply Erosion (local min)
Min Output (3x3):
Step 3. Apply Dilation on Min Output (local max)
Max Output (1x1):
Step 4. Final result: Opening output is 24.
When to use?
- Removing small bright noise while preserving larger objects.
- Min Filter: remove salt noise (random bright spikes), darken tiny bright artifacts.
- Max Filter: remove pepper noise (random dark spikes), fill tiny dark gaps.
- Smoothing boundaries without large area change.
- Fast preprocessing step before thresholding or morphology.
Opening preserves structures larger than the SE and removes structures smaller than the SE.
4. Closing (Dilation -> Erosion)
How it works
- Binary rule: apply dilation first, then erosion, with the same SE.
- Grayscale rule: apply local maximum filter first, then local minimum filter, with the same SE.
Binary example
Input has a small hole in the center:
Grayscale example
Step 1. Input (same 5x5 example as Min/Max filter)
Step 2. Apply Dilation (local max)
Max Output (3x3):
Step 3. Apply Erosion on Max Output (local min)
Min Output (1x1):
Step 4. Final result: Closing output is 26.
When to use?
- Filling small holes or gaps inside objects.
- Connecting slightly broken bright regions.
If the SE is too large, opening and closing can distort meaningful object shapes.
5. Morphological Gradient (Dilation − Erosion)
The Morphological Gradient detects the boundary of objects by computing the difference between the dilated and eroded image. Think of it as a morphological edge detector — it highlights the shell of pixels sitting right on the border between foreground and background.
How it works
- Dilation pushes the object's boundary outward by 1 pixel (for a 3×3 SE).
- Erosion pulls the boundary inward by 1 pixel.
- Subtracting the eroded image from the dilated image removes the shared interior and leaves only the fringe — the boundary region.
Binary example
Using a 3×3 square SE on a 5×5 image with a 3×3 filled square at the center:
Step 1 — Dilation (expand by 1): every background pixel adjacent to any foreground pixel turns 1.
Step 2 — Erosion (shrink by 1): a pixel stays 1 only if its full 3×3 neighborhood is all 1. Only the center pixel qualifies.
Step 3 — Gradient (Dilation − Erosion): the shared interior drops out, leaving the boundary ring.
The result is a 2-pixel-wide ring (1 pixel outward + 1 pixel inward from the original edge) that traces the object boundary.
To get a boundary only on the inside of the object, compute I − Erosion(I).
To get only the outside fringe, compute Dilation(I) − I.
When to use?
- Detecting object boundaries in binary or segmented images without converting to grayscale.
- Fast alternative to Sobel or Canny when the data is already binary.
- Measuring object perimeters in binary masks (e.g., counting boundary pixels).
When to avoid?
- When you need a precise 1-pixel-wide edge — the standard morphological gradient produces a 2-pixel-wide boundary.
- On grayscale images with subtle intensity transitions — gradient-based methods (Sobel, Canny) handle those better.