The idea: change a shape with a small probe

Morphology works on the shape of things in a binary image: each pixel is 1 (part of the object) or 0 (background). It uses a small pattern called the structuring element B. B has an origin, here its centre pixel. Put the origin on a pixel p and look at the image pixels under B. There are only two basic questions, and every other operation is built from them:

  • Does B fit? Every pixel under B is 1. That is erosion, written A ⊖ B.
  • Does B hit? At least one pixel under B is 1. That is dilation, written A ⊕ B.
A 5 by 5 blue square on a grid with a 3 by 3 structuring element placed at three spots: inside the square, where all 9 cells are 1 (fits: the pixel stays in the erosion and is in the dilation); on the square's edge, where 3 of 9 are 1 (hits but does not fit: removed by erosion, kept by dilation); and outside, where 0 of 9 are 1 (misses: in neither)
Fit → the pixel is in the erosion. Hit → it is in the dilation. Miss → in neither.

The page uses a 12 × 12 image so every pixel is visible. Pixel (x, y) is column x, row y, counted from 0 with y going down. B is the 3 × 3 square (the 8 neighbours and the centre) or the 3 × 3 cross (the 4 side neighbours and the centre). Pixels outside the image count as 0. Panel A is the input, step 1 and step 2 hold the results. A green + marks a pixel that became 1, a red × one that became 0.

Erosion: keep only where B fits

A pixel stays 1 only if B, centred on it, lies completely inside the object. So every object loses its outer layer, as thick as B reaches (one pixel for a 3 × 3 B). Anything too small to hold B disappears completely. On the square with specks, the 6 × 6 square becomes 4 × 4 and the four specks vanish: 41 pixels become 16, and 5 pieces become 1 (Demo 1: erosion shrinks, specks vanish).

Dilation: add wherever B hits

A pixel becomes 1 if B, centred on it, touches the object anywhere. Every object grows by one layer shaped like B, and gaps narrower than B close. The same image grows from 41 to 98 pixels. Each speck is only one empty pixel away from the square, so its grown copy touches the grown square, and the 5 pieces become 1 (Demo 2: dilation grows the shape). Another way to see dilation: stamp a copy of B on every 1-pixel. (For a B that is not symmetric, dilation uses B mirrored through its origin; the square and the cross are the same mirrored.)

The two are duals. Eroding the object is the same as dilating the background and taking the complement: (A ⊖ B)ᶜ = Aᶜ ⊕ B for a symmetric B. What one does to the object, the other does to the background.

Opening: erode, then dilate

Opening A ∘ B = (A ⊖ B) ⊕ B. The erosion throws away everything B cannot fit into. The dilation then grows what is left back to its old size. The result is the union of every place where B fits inside A. So opening removes small things and keeps large ones as they were:

image, BAafter erosionopeningwhat happened
square + specks, square41, 5 pieces1636, 1 piecethe specks are gone, the 6 × 6 square is back exactly (Demo 3)
two blobs + bridge, square42, 1 piece1240, 2 piecesthe 2-pixel bridge is cut, the blobs come back (Demo 5)
square + specks, cross411632the square loses its 4 corners (Demo 6)

The last row shows that the shape of B matters. A cross that fits inside the square can never cover the square's corner pixel, so the opening rounds the corners. With a 3 × 3 square B, the square fits B everywhere and comes back whole.

Closing: dilate, then erode

Closing A • B = (A ⊕ B) ⊖ B, the same two steps in the other order. The dilation fills holes, cracks and notches smaller than B; the erosion then shrinks the outline back to where it was. The block with holes has 60 pixels: a one-pixel hole, a two-pixel hole and a notch on its top edge (3 hole pixels in all, since the notch opens to the outside). The dilation gives 100 pixels, the erosion 64: the full 8 × 8 block with no holes (Demo 4: closing fills holes).

Two rows of small pixel grids. Top row, opening: a 4 by 4 square with a speck (17 pixels), its erosion (4 pixels, speck gone), then the dilation of that (the 16-pixel square back). Bottom row, closing: a 6 by 6 block with a one-pixel hole (35 pixels), its dilation (64 pixels, hole filled), then the erosion of that (the 36-pixel block back, no hole)
Opening removes what is smaller than B; closing fills gaps smaller than B. Both leave large shapes unchanged.

Two useful facts. Opening only removes pixels and closing only adds them: A ∘ B ⊆ A ⊆ A • B. And both are idempotent: opening an opened image again changes nothing, and the same for closing. Opening then closing is a common cleanup filter for a noisy binary mask: remove the specks, then fill the pinholes.

Boundary extraction

The erosion removes exactly the outer layer of each object. So the boundary is the image minus its erosion: β(A) = A − (A ⊖ B). For the two blobs with the bridge, 42 − 12 = 30 pixels: a one-pixel outline of each blob and the bridge, which is all boundary because B fits nowhere on it (Demo 7: boundary = A minus its erosion). With the square B the outline is 4-connected (it steps around corners); with the cross it is thinner, 8-connected. This outline comes from the shape alone. Edges in a grey image, found from the brightness gradient, are on Canny Edge Detection.

A binary image usually comes from thresholding a grey image, often after histogram equalization. Erosion and dilation slide a window over the image like a convolution, but they take a logical AND (fit) or OR (hit) instead of a weighted sum. After cleanup the pieces are counted or labelled with a flood fill, which is how this page counts its "pieces".

See also Thresholding and Otsu's Method for how a grey image becomes the black-and-white image that erosion and dilation work on.

What the page leaves out

Larger and other structuring elements (disks, lines at an angle, B with the origin off-centre), and B that is not symmetric. Border rules: OpenCV's erode treats pixels outside the image as 1 by default, so objects touching the border are not eaten from outside; this page uses 0 for both. Grey-scale morphology (erosion = local minimum, dilation = local maximum), the morphological gradient, top-hat, hit-or-miss transform, thinning, skeletons, reconstruction and the distance transform are not shown.

On the mind map

Open the Machine Learning mind mapMachine LearningLearning setup: what feedback, how to scoreLearning setupLinear models: weighted sum w·x + bLinear modelsOther classic models: trees, margins, neighboursOther classic modelsUnsupervised: find structure without labelsUnsupervisedTraining: loss, gradient, small stepTrainingNeural networks: layers of σ(W·a + b)Neural networksArchitectures: how layers share informationArchitecturesLanguage models: predict the next tokenLanguage modelsLLM speed and tuning: cheaper to run, safer to useLLM speed and tuningRetrieval and agents: give the model data and toolsRetrieval and agentsConvolutionConvolutionslide a small kernel over the imageCNN layer by layerCNN layer by layerfc1 holds 78% of the weightsCanny Edge DetectionCanny Edge DetectionBlur, gradient, thin, two thresholds, linkHarris CornersHarris CornersR = det − k·trace² large: cornerHistogram EqualizationHistogram Equalizationrunning sum of counts = new levelsOtsu ThresholdingOtsu ThresholdingMax between-class variance picks tHough TransformHough TransformCollinear pixels' curves cross in one cellErosion and DilationErode = B fits, dilate = B hitsCNN vs TransformerCNN vs Transformerlocal field vs attention to allRNN, LSTM, GRURNN, LSTM, GRUhidden state; gates fight vanishingSelf-attentionSelf-attentionsoftmax(QKᵀ/√d_k) weights the VDiffusion modelDiffusion modellearn to predict the added noiseVAE vs GANVAE vs GANELBO loss vs generator-discriminator gameContrastive learning, CLIPContrastive learning, CLIPmatch pairs in one space, InfoNCE lossResidual connectionsResidual connectionsx + F(x) lets gradients skip layersOpen the Machine Learning mind mapMachine Learninghow layers share informationArchitecturesConvolutionConvolutionslide a small kernel over the imageCNN layer by layerCNN layer by layerfc1 holds 78% of the weightsCanny Edge DetectionCanny Edge DetectionBlur, gradient, thin, two thresholds, linkHarris CornersHarris CornersR = det − k·trace² large: cornerHistogram EqualizationHistogram Equalizationrunning sum of counts = new levelsOtsu ThresholdingOtsu ThresholdingMax between-class variance picks tHough TransformHough TransformCollinear pixels' curves cross in one cellErosion and DilationErode = B fits, dilate = B hitsCNN vs TransformerCNN vs Transformerlocal field vs attention to allRNN, LSTM, GRURNN, LSTM, GRUhidden state; gates fight vanishingSelf-attentionSelf-attentionsoftmax(QKᵀ/√d_k) weights the VDiffusion modelDiffusion modellearn to predict the added noiseVAE vs GANVAE vs GANELBO loss vs generator-discriminator gameContrastive learning, CLIPContrastive learning, CLIPmatch pairs in one space, InfoNCE lossResidual connectionsResidual connectionsx + F(x) lets gradients skip layers
This page on the Machine Learning mind map · 81 pages in 10 branches · open it to see the whole map and test yourself in recall mode.