Choose a morphological operation according to how the foreground should change. Then choose a structuring element, also called a probe, whose shape and size suit that change.

Main idea

Choose the operation for the required effect, then choose the probe for the geometry. The same operation can produce different results with different probes.


Choose the Operation

In a binary mask, foreground pixels represent the selected object and background pixels represent everything else.

What should change?OperationWhat it does
Expand the foreground or thicken a structureDilationAdds foreground around existing foreground
Keep an object's interior and remove boundary pixelsErosionKeeps positions where the probe fits completely inside foreground
Remove small unwanted foreground regions while retaining larger objectsOpeningErodes first, then dilates the remaining regions
Fill small holes or breaks while keeping the outside size close to the originalClosingDilates first, then erodes the expanded regions

Opening and closing use the same probe in both steps. MathWorks describes these four operations.

Identify the unwanted pixels

Extra foreground, such as speckles, suggests opening. Missing foreground, such as small holes or broken strokes, suggests closing.


Choose the Probe Shape

Match the probe to the directions and structures involved:

ProbeUseful forMain effect of its shape
Aligned lineStraight cracks, scratches, or strokesActs along the line's direction
3-by-3 crossTesting direct neighboursUses the centre, top, bottom, left, and right positions
3-by-3 squareCompact regions and small defects in several directionsUses all nine positions, including the corners
DiskCurved boundaries and compact objectsGives an approximately circular neighbourhood
EllipseElongated regions with curved boundariesExtends farther along one axis

A line probe can be horizontal, vertical, or set at another angle. Choose its direction to match the structure. MathWorks explains how the probe defines the selected neighbourhood.

A cross and a square test different positions

A 3-by-3 cross ignores the four corners. During erosion, its centre can pass even when those corners are background. A square requires foreground at the corners too.


Choose the Probe Size

Consider both the unwanted feature and the useful detail:

  • For opening, the probe should fit inside wanted structures and fail to fit inside unwanted regions
  • For closing, it should reach across unwanted gaps while keeping wanted separations open
  • For dilation, its extent sets the added margin
  • For erosion, its extent determines which boundary positions are removed

Size depends on width and shape, as well as length. A long thin scratch can contain an aligned line probe while failing to contain a square probe.

Larger probes affect more detail

Opening or erosion can remove useful thin structures. Closing or dilation can join objects that should remain separate. Check the smallest useful feature and the narrowest wanted separation.


Practical Choices

Required resultOperation and probeWhy it fitsMain risk
Add a roughly uniform margin around a person's maskDilation + diskExpands the mask around its boundaryNearby masks can merge
Repair breaks in curved letteringClosing + small diskBridges short gaps, then reduces outer growthSeparate strokes can join
Keep a long scratch and remove small round detectionsOpening + aligned lineThe line fits inside the scratch but cannot fit inside the small detectionsShort, curved, or differently aligned scratches can disappear
Keep pixels farther inside a cell maskErosion + diskRemoves a band around the boundarySmall cells and narrow branches can disappear

Check the Result

After choosing the operation and probe:

  1. Check that the intended speckles, holes, or gaps changed
  2. Check that useful small structures remain where required
  3. Check whether separate objects joined or one object split
  4. Compare the object's final size with the intended result

Adjust the probe's shape, direction, or size according to the observed problem.