Skip to content
AI360XpertBeta
Computer Vision

Visual explainer

Erosion, Dilation, Opening and Closing

Trace a binary structuring element through shrinking, growth, speck removal and hole filling, including the loss of thin structures.

Fit A Shape Template

Erosion keeps a location only when the entire structuring element fits the foreground.
Erosion keeps a location only when the entire structuring element fits the foreground.

Morphological operations change foreground shapes using a structuring element, a small template moved across a mask. Erosion requires every template position to be foreground; dilation requires at least one foreground overlap for our symmetric template. Neither operation averages pixel intensities.

The main examples use binary masks: foreground 1, background 0. Hatching marks foreground. The default template is a centered, all-ones three-by-three square; later beats explicitly compare a cross and extend the rules to grayscale. In the first drawing its dashed outline fits the left block exactly, so its center survives erosion. Shift it one pixel right, as on the right, and three template cells lie in background: that candidate center is removed. These local tests follow OpenCV's morphology definitions.

Erosion Retreats At Boundaries

A centered three-by-three square erodes a five-by-five block to three-by-three.
A centered three-by-three square erodes a five-by-five block to three-by-three.

Only the inner three rows and three columns of this five-by-five block can center a fully fitting template. The foreground count drops from 25 to 9. Its boundary retreats one pixel on every side. Both panels use 30 drawing units per pixel; the dashed outline on the right records the original extent and is not output foreground.

The outer rounded frames are view crops, not foreground boundaries. Assume background extends beyond every crop and enough space is retained for intermediate growth. Border values matter in real implementations; use explicit policies when an object touches the actual image edge.

Dilation Expands The Support

A centered three-by-three square dilates a three-by-three block to five-by-five.
A centered three-by-three square dilates a three-by-three block to five-by-five.

Start with a solid three-by-three block. Every location within one pixel horizontally and vertically has a template overlapping it. The result is five-by-five: 9 foreground pixels become 25. For a binary mask this is a local maximum over the template support; erosion is a local minimum. Grayscale morphology extends those min/max rules to intensity values.

This symmetric square reaches diagonal neighbors too. A cross-shaped template would produce a different outline. Template shape, size, anchor and repetitions define which structures the operation affects.

Opening Removes A Speck

Opening removes an isolated pixel while restoring a surviving three-by-three block.
Opening removes an isolated pixel while restoring a surviving three-by-three block.

Opening means erosion followed by dilation using the same template. This new fixture contains a three-by-three block and a separate single pixel: ten foreground pixels in total. Erosion leaves only the block's center, one pixel. Dilation expands that survivor back to a three-by-three block, nine pixels. No survivor remains at the isolated speck, so it cannot regrow.

The drawing now uses 16 units per pixel to fit all three stages, keeping that scale identical within the beat. Restoring this particular square does not mean opening restores every object's original shape. Protrusions narrower than the template may disappear.

Closing Fills A Hole

Closing fills a central one-pixel hole through dilation followed by erosion.
Closing fills a central one-pixel hole through dilation followed by erosion.

Closing reverses the order: dilation, then erosion. The input is a five-by-five block missing its central pixel, giving 24 foreground pixels. Dilation grows its outer extent to a solid seven-by-seven block and fills the hole, giving 49 pixels. Erosion then returns the outer boundary to five-by-five, but leaves its interior solid: 25 pixels.

The scale remains 16 units per pixel; these crops expand to nine-by-nine to show the temporary growth. Closing can bridge narrow gaps as well as fill small holes. It does not know whether a gap represents damage or a meaningful separation between objects.

Compare The Kernel Shape

Square and cross structuring elements reach different neighbors during dilation.
Square and cross structuring elements reach different neighbors during dilation.

Dilate a single foreground pixel, marked by a dot, with two centered templates. A full three-by-three square produces nine foreground pixels, including diagonals. A three-by-three cross contains only the center and its four horizontal/vertical neighbors, producing a five-pixel plus. The drawing uses 40 units per pixel for this comparison. Even kernels with the same bounding dimensions can change connectivity differently; choose their actual support, not just their width.

Repeat A Primitive

Two square dilation passes expand one pixel into a five-by-five footprint.
Two square dilation passes expand one pixel into a five-by-five footprint.

On an otherwise empty mask, one square dilation turns a single pixel into three-by-three; a second turns it into five-by-five. Counts go from 1 to 9 to 25. Every panel uses 24 units per pixel. Repeated three-by-three square dilation has the effective support of a larger square: after n passes its side is 2n+1 for this isolated input, assuming no border clipping.

Repeated primitives are also distinct from repeating a whole opening. Two erosions followed by two dilations use a larger effective support than one erosion followed by one dilation. Read the API's iteration semantics instead of assuming an iteration repeats the entire compound operation.

Extract A Boundary Band

Subtracting erosion from dilation creates a band around the original boundary.
Subtracting erosion from dilation creates a band around the original boundary.

For a solid five-by-five block, the square template produces a seven-by-seven dilation and a three-by-three erosion. Their difference is the morphological gradient: 49−9=40 foreground pixels. The transparent central hole removes the eroded interior; the dashed outline records the original boundary. Both panels use 24 units per pixel. This band straddles that boundary and is thicker than a one-pixel contour. Template size determines its thickness.

Extend To Grayscale

Grayscale erosion takes a local minimum and dilation takes a local maximum.
Grayscale erosion takes a local minimum and dilation takes a local maximum.

Switch explicitly to a grayscale row and a flat width-three support. For neighborhood [2,7,4], erosion returns 2 and dilation returns 7; the arithmetic mean would instead be 13/3. The bar heights share one intensity scale. Binary all/any tests are special cases of these min/max operations when values are restricted to 0 and 1. Grayscale morphology changes local extrema while preserving the distinction between intensity ordering and an average.

Where It Breaks

Opening deletes an isolated one-pixel-wide structure that the kernel cannot fit inside.
Opening deletes an isolated one-pixel-wide structure that the kernel cannot fit inside.

A real one-pixel-wide, five-pixel-long line contains no location where the three-by-three square fits. Erosion deletes it completely. Dilation of that empty result stays empty; the amber dashed outline marks the lost input, not surviving pixels. A cleanup operation can therefore erase a vessel, character stroke or wire along with unwanted specks. Choose the template by the structures you need to preserve, and inspect intermediate masks.

The Quick Version

  • Erosion keeps centers where the entire template fits foreground.
  • Erosion shrinks the five-by-five square to three-by-three.
  • Dilation grows support wherever the symmetric template overlaps foreground.
  • Opening erodes then dilates, removing unsupported foreground structures.
  • Closing dilates then erodes, filling some background gaps.
  • Kernel shape determines which neighbors can change.
  • Repeated primitive passes enlarge the effective support.
  • Dilation minus erosion produces a boundary band.
  • Grayscale erosion and dilation select local minima and maxima.
  • Thin meaningful structures can disappear during opening.