Morphological Erosion Explained
Erosion keeps a foreground pixel only when a small probe fits fully inside the object. Shapes shrink, specks vanish, and thin bridges snap.
Why Does This Exist?
Binary masks arrive messy: salt specks around objects, thin bridges gluing things that should be separate, ragged borders from thresholding. A linear blur would smear the shapes while cleaning them. Erosion instead probes shape directly with a structuring element, deleting exactly the pixels where the probe does not fit and leaving the rest untouched.
It is one of the two primitives in morphological operations, paired with dilation. Erode-then-dilate sequences build opening, the standard speck remover that keeps large shapes at full size.
Think of It Like This
Parking a van in a lot
The structuring element is a van and the foreground is legal parking space. Erosion keeps only the spots where the whole van fits without touching the lines, so the legal area shrinks inward from every edge and a motorcycle-sized speck of space fails everywhere and disappears. The van's size decides everything: a bigger van erases more.
Where it stops: a van cannot park half on the lines, but grayscale erosion softens this to a local minimum instead of a hard veto, so the analogy is exact only for binary images.
How It Actually Works
Slide a structuring element, often a square, over the binary image. At each position the output center stays foreground () only if every in the element overlaps a in the image, so a single background pixel under the probe flips the center to . One pass therefore strips a one-pixel layer off every boundary, fills nothing, and deletes any object the probe cannot cover. On grayscale images the same idea becomes a local minimum filter, darkening each window to its dimmest pixel.
Worked example
A window holds eight s and one at the corner:
With a full probe the corner overlaps the probe, so the output center is : one background pixel vetoes the whole window. The same veto shrinks a row. Eroding with a 3-wide probe tests each interior seat: seat 1 sees and fails, seat 2 sees and survives, seat 3 sees and fails, giving . Three wide becomes one wide, and a lone speck would fail everywhere and vanish.
Code
Pure stdlib Python replays the row shrink from the worked example: each output seat survives only when its full 3-wide neighbourhood is foreground.
row = [0, 1, 1, 1, 0]out = [1 if row[i - 1] == 1 and row[i] == 1 and row[i + 1] == 1 else 0 for i in range(1, 4)]print(out)# -> [0, 1, 0]The three-wide bar narrows to one survivor, exactly the prose result. Real masks run the same test in two dimensions, which is why the window above collapses to on a single corner zero.
Watch Out For
Foreground and background swapped
Erosion shrinks whatever you call foreground, usually white. Run it on dark text over a bright page and the bright background shrinks instead, so the text grows fatter, the opposite of the plan. The symptom is objects thickening under so-called erosion. Invert the image first so your targets are the bright foreground.
Probe larger than the target
A structuring element bigger than the objects themselves erases everything, survivors included. The symptom is an empty output after one innocent-looking pass. Size the probe to the noise you want gone, and keep it smaller than the smallest object you must keep.
The Quick Version
- Erosion keeps a pixel only when the whole probe fits on foreground.
- Objects shrink, specks vanish, and thin bridges between objects snap.
- The worked window outputs because one corner zero vetoes all nine cells.
- The worked row narrows from to .
- On grayscale it acts as a local minimum filter.
- Invert first so targets are bright, and size the probe between the noise scale and the smallest kept object.