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.

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:#fff

1. Dilation (Max Filter)

How it works

Two common 3x3 structuring elements:

Cross=010111010Square=111111111
Binary example

Using a 3x3 square SE:

Input=[0000000000001000000000000]DilationOutput=[0000001110011100111000000]

Because the filter is a 3×3, as it slides around the input image, there are exactly 9 different stopping positions where it will overlap that single, isolated 1.

Grayscale example

Step 1. Input (same 5x5 example as Min/Max filter)

202224262821200252729192326303118222428321721232733

Step 2. Apply grayscale Dilation (3x3 local max)
Output (3x3, valid mode):

[2002003120020032263033]

Step 3. Final result: local maxima emphasize bright regions and propagate the bright spike (200).

When to use?
When to avoid?

2. Erosion (Min Filter)

How it works
Binary example

Using a 3x3 square SE:

1111111111111111111111111erosion0000001110011100111000000
Grayscale example

Step 1. Input (same 5x5 example as Min/Max filter)

202224262821200252729192326303118222428321721232733

Step 2. Apply grayscale Erosion (3x3 local min)
Output (3x3, valid mode):

[192224182224172123]

Step 3. Final result: local minima suppress bright spikes, including the 200 outlier.

When to use?
When to avoid?

3. Opening (Erosion -> Dilation)

How it works
Binary example

Input has one isolated noise pixel and one larger object:

1000001110011100111000000openingisolated noise removed, larger object preserved
Grayscale example

Step 1. Input (same 5x5 example as Min/Max filter)

202224262821200252729192326303118222428321721232733

Step 2. Apply Erosion (local min)
Min Output (3x3):

[192224182224172123]

Step 3. Apply Dilation on Min Output (local max)
Max Output (1x1):

[24]

Step 4. Final result: Opening output is 24.

When to use?
Tip

Opening preserves structures larger than the SE and removes structures smaller than the SE.

4. Closing (Dilation -> Erosion)

How it works
Binary example

Input has a small hole in the center:

0000001110010100111000000closing0000001110011100111000000
Grayscale example

Step 1. Input (same 5x5 example as Min/Max filter)

202224262821200252729192326303118222428321721232733

Step 2. Apply Dilation (local max)
Max Output (3x3):

[2002003120020032263033]

Step 3. Apply Erosion on Max Output (local min)
Min Output (1x1):

[26]

Step 4. Final result: Closing output is 26.

When to use?
Caution

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.

Morphological Gradient(I)=Dilation(I,SE)Erosion(I,SE)
How it works
Binary example

Using a 3×3 square SE on a 5×5 image with a 3×3 filled square at the center:

Input=0000001110011100111000000

Step 1 — Dilation (expand by 1): every background pixel adjacent to any foreground pixel turns 1.

Dilation=1111111111111111111111111

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.

Erosion=0000000000001000000000000

Step 3 — Gradient (Dilation − Erosion): the shared interior drops out, leaving the boundary ring.

Gradient=1111111111110111111111111

The result is a 2-pixel-wide ring (1 pixel outward + 1 pixel inward from the original edge) that traces the object boundary.

Tip

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?
When to avoid?