Erosion and Dilation

Shrink a shape, grow a shape, and combine the two to remove speckles or fill holes without moving the boundary.

Overview

Operating on shape, not brightness

Every filter so far has treated the image as a field of numbers. Morphological operations treat it as a set of pixels — the foreground — and ask geometric questions about that set. They are defined for binary images, which is why the visualisation above thresholds first and why the binarise control sits alongside the others.

The tool is a structuring element: a small shape, usually a square or a disc, that gets placed over every pixel in turn. What happens next depends on which of two rules you apply.

Erosion and Dilation

Input
Output

Worth knowing

Morphology operates on shape. It is defined for binary images, which is why the threshold control comes first.
Erosion keeps a pixel only if every pixel under the structuring element is foreground. Shapes shrink and thin features vanish.
Dilation keeps a pixel if any pixel under the element is foreground. Shapes grow and small gaps close.
Open removes small bright specks; close fills small dark holes. Both leave the overall size roughly unchanged.

Erosion and Dilation

The two operations that shape a binary mask, and the four things you build from them.

Erosion: every, or nothing

Erosion keeps a foreground pixel only if every pixel under the structuring element is also foreground. One background pixel anywhere under the element and the centre is turned off.

The consequences follow directly. Boundaries retreat inwards by roughly the radius of the element, because a pixel near an edge always has some background under a large enough window. Small isolated specks vanish entirely, since a speck smaller than the element can never have the element fit inside it. Thin connections between larger blobs are cut, because a bridge one pixel wide fails the test everywhere along its length.

Select Erode above and raise the structuring element size. The bright speckles disappear on the first step; the shapes visibly thin; push it far enough and the thin bright bar at the bottom of the frame is gone entirely.

Dilation: any, and it counts

Dilation is erosion's mirror. A pixel becomes foreground if any pixel under the structuring element is foreground.

Boundaries advance outwards. Small holes inside an object fill, because a hole smaller than the element has foreground on all sides within reach. Nearby blobs merge as their expanding boundaries meet. The dark speckles that erosion could not touch disappear here instead.

Erosion and dilation are *duals*: eroding the foreground is exactly dilating the background. That is not a coincidence to be memorised, it is the same rule seen from the other side.

Open and close: the useful pair

Neither operation is much use alone, because both change the size of everything. Composing them fixes that.

Opening is erosion followed by dilation. The erosion removes small bright specks and thins everything; the dilation grows what survived back to roughly its original size. Specks that were destroyed do not come back — there is nothing left to grow. The net effect is *remove small bright things, leave everything else about where it was*.

Closing is dilation followed by erosion. The dilation fills small dark holes and joins nearby pieces; the erosion shrinks the result back down. Holes that were filled stay filled. The net effect is *remove small dark things, leave everything else about where it was*.

OperationSequenceRemovesBoundary
Erode—bright specksretreats
Dilate—dark holesadvances
Openerode, dilatebright specksroughly unchanged
Closedilate, erodedark holesroughly unchanged

Set the control above to Open and compare against Erode at the same size. The speckles are gone in both, but the shapes under Open are still the size they started.

Why this always follows thresholding

A thresholded mask is almost never clean. Noise that happened to cross the threshold leaves scattered foreground pixels in the background; a highlight inside an object leaves a hole. Both are small, and both are exactly what open and close are for.

The standard sequence — threshold, open to despeckle, close to fill — is so common it is worth treating as one step. Only after it does counting connected components or tracing contours give sensible answers.

Choosing the element

The size is the parameter that matters, and there is a principle for it: the structuring element should be larger than the artefacts you want to remove and smaller than the features you want to keep. If a speck is 3 pixels across and a genuine object is 30, anything between will do. If they are 3 and 5, no size works and morphology is the wrong tool.

The shape matters less, but not never. A square element treats diagonals differently from axes; a disc is isotropic; a horizontal line erodes vertical strokes while leaving horizontal ones alone, which is how table rules are separated from text in document processing.

Open and close, and what each removes

Erosion keeps a foreground pixel only where *every* pixel under the structuring element is foreground; dilation keeps it where *any* is. Alone, each changes the size of everything — erosion retreats boundaries inward and deletes specks smaller than the element, dilation advances them out and fills holes — so they are composed in pairs that despeckle without resizing:

operationsequenceremovesnet effect on size
openingerode then dilatesmall bright specksroughly unchanged
closingdilate then erodesmall dark holesroughly unchanged

Opening erodes first, destroying specks too small to contain the element, then dilates what survived back to about its original size — and a speck that was destroyed cannot grow back, so the net effect is "remove small bright things, leave the rest where it was". Closing is the mirror: fill small holes, then shrink back, so holes stay filled. This is why the standard pipeline after [thresholding](thresholding.html) is open-then-close — despeckle the background, fill the object — before counting components or tracing contours. The one parameter that matters is the element size, and the principle is exact: it must be larger than the artefacts to remove and smaller than the features to keep, so if a speck is 3 pixels and a real object is 30, anything between works, and if they are 3 and 5, morphology is the wrong tool.

Where it goes wrong

Applying it to a greyscale image without thinking. Greyscale morphology is defined — erosion becomes a local minimum and dilation a local maximum — but it means something different, and results that look plausible may not be what was intended.

Using an element that is too large. It removes the noise and the detail together. Thin structures are lost first and cannot be recovered.

Opening when you meant closing. The mnemonic is that opening removes *bright* things and closing removes *dark* ones. If your foreground is dark against a light background, they swap.

Shrinking and growing shapes with min and max

Erosion takes the minimum over a neighbourhood, dilation the maximum. Two operations, and every other morphological tool is a composition of them -- which this builds one at a time, then measures on a shape designed to have something for each of them to catch.

Check yourself

0 of …

Answer without scrolling back up.

  1. Under erosion, when does a foreground pixel survive?

  2. Why does opening remove small specks without shrinking the larger shapes?

  3. How should the structuring element be sized?

Cheat sheet

Erosion and Dilation

Every filter so far has treated the image as a field of numbers. Morphological operations treat it as a set of pixels — the foreground — and ask geometric questions about that set. They are defined for binary images, which is why the visualisation above thresholds first and why the binarise control sits alongside the others.

COMPUTER VISION · AI.Cruz.in/computer_vision/erosion_and_dilation.html

About the author

AI.Cruz builds and maintains AI.Cruz. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.