Closing fills small holes and narrow gaps in the foreground of a binary mask. It applies Dilation first, then Erosion, using the same structuring element, also called a probe, for both steps.

Dilation expands the foreground into nearby background pixels, including holes and gaps. It also expands the outside boundary. Erosion then removes much of this outer growth, while filled regions with enough foreground around them remain filled.

Main idea

Fill small breaks first, then shrink the outside boundary back. The erosion step uses the dilation result as its input.

The final object usually stays closer to its original size than it would after dilation alone. MathWorks describes this sequence.


How Closing Works

Step 1: Dilate the Original Mask

Place the probe's anchor on each foreground pixel. Mark every position covered by its active cells as foreground in the dilation output.

After dilation:

  • Foreground expands into small holes
  • Nearby fragments can join across narrow gaps
  • The outside boundary also grows

Step 2: Erode the Dilation Result

At each position, check whether all active probe cells cover foreground in the dilated mask. Keep the output pixel at the anchor only when this condition is satisfied.

After erosion:

  • Much of the extra outer boundary is removed
  • Filled holes and gaps remain filled where the probe fits completely
  • The outer shape returns close to its original size

Use the dilation result

Finish dilation first, then erode that result. Eroding the original mask would shrink the foreground around its existing holes and gaps.


Worked Example: Fill a Small Hole

The input is a 3-by-3 foreground square with one background pixel at its centre. Use a centred 3-by-3 square probe, with all nine cells active.

Here, 1 means foreground and 0 means background. Treat positions outside the shown grid as 0.

Input         After dilation   After erosion
 
0 0 0 0 0     1 1 1 1 1        0 0 0 0 0
0 1 1 1 0     1 1 1 1 1        0 1 1 1 0
0 1 0 1 0     1 1 1 1 1        0 1 1 1 0
0 1 1 1 0     1 1 1 1 1        0 1 1 1 0
0 0 0 0 0     1 1 1 1 1        0 0 0 0 0

What happens:

  • Dilation: The centre hole fills, and the outer square grows from 3-by-3 to 5-by-5
  • Erosion: The outer boundary shrinks back to 3-by-3
  • Final result: The square keeps its original outer size, and the centre is now foreground

Why does erosion keep the filled hole?

Erosion checks the current dilated mask. At the centre, the entire 3-by-3 probe covers foreground, so the centre stays 1. At the outer edge, part of the probe reaches background, so that edge is removed.

This is why the two operations can change the original mask even though one grows foreground and the other shrinks it. SciPy also demonstrates hole filling by closing.


Worked Example: Connect a Horizontal Gap

Use a 1-by-3 horizontal probe, 1 [1] 1, with the middle cell as its anchor:

Input:            0 0 1 1 0 1 1 0 0
After dilation:   0 1 1 1 1 1 1 1 0
After erosion:    0 0 1 1 1 1 1 0 0

Dilation connects the two fragments. Erosion returns the outer ends to their original positions. The central gap remains filled because its left, centre, and right positions are now foreground.


Understanding the Equation

Meaning of each symbol:

SymbolMeaning
Original foreground region
Structuring element used in both steps
Closing
Dilation
Erosion

Evaluate the brackets first: dilate with , then erode that result with .


Closing Compared with Opening

OperationOrderMain purpose
ClosingDilation, then erosionFill small background holes and gaps
OpeningErosion, then dilationRemove small foreground regions and protrusions

Choose by the defect

Use closing for missing foreground, such as a break in a stroke. Use opening for extra foreground, such as an isolated speckle.


Choosing the Probe

Match the probe to the gap's direction and size:

  • A horizontal line connects short breaks along a horizontal structure
  • A vertical line connects short breaks along a vertical structure
  • A square fills small gaps in several directions and includes diagonal positions
  • A disk gives an approximately circular neighbourhood and can suit curved shapes

Uses and Limits

Typical uses:

  • Fill small holes inside object masks
  • Reconnect broken text strokes or road markings
  • Repair short breaks in detected structures

Nearby objects can merge

A probe that reaches across a wanted separation can join separate objects or strokes. Large probes can also fill meaningful holes and change boundary detail.

The gap must be compatible with the probe's shape and size. A filled position remains foreground only if it passes the erosion step.

For reliable results, use the same probe and anchor in both steps. Keep each step's input fixed while calculating its output.