A binary mask does not contain a label explaining which small foreground feature is useful or which background gap is an error. Morphological operations change that mask according to a declared structuring element. Their results depend on geometry and order, rather than an understanding of the physical objects.

Opening performs erosion followed by dilation. Closing reverses those two stages. Applying a complete opening and then a complete closing is also generally different from applying them in the opposite order.

OpenCV's Morphological Transformations tutorial defines these combinations and discusses structuring-element shape. SciPy's binary_opening reference states the erosion-then-dilation sequence and exposes a specified structure and origin. The small sets below are original examples, not reproduced library demonstrations or executed software outputs.

Define foreground on an integer line

Use an infinite one-dimensional integer grid. A finite set A lists the foreground locations; every other integer is background. This avoids an unstated crop-edge rule.

Choose a centered, symmetric structuring element B = {−1, 0, 1}. It covers three consecutive positions. Its origin is the center, zero.

Dilation forms all locations a + b with a in A and b in B. Thus every foreground location contributes itself and its immediate neighbors. Erosion retains a center x only when all three locations x − 1, x, and x + 1 belong to A.

These definitions fix the meaning of each operation. A different element, origin convention, or finite-image boundary treatment can define a different calculation.

Begin with the foreground set:

A = {0, 1, 2, 5, 6, 10}.

It has a three-position run, a two-position run, and a singleton. This is an invented digital pattern without a claim about whether any run represents noise, a mark, or a physical part.

Opening retains what survives the fit test

Eroding A by B retains only the center 1. Its neighborhood {0, 1, 2} fits entirely within A.

The two-position run at 5 and 6 has no center whose three required positions are foreground. The singleton at 10 has none either. Thus the eroded set is {1}.

Dilating {1} by B produces {0, 1, 2}. The opening is therefore:

O(A) = {0, 1, 2}.

The dilation stage restores the surviving three-position run's extent in this case. It cannot recover the two-position run or singleton because they left no surviving centers after erosion.

This is not an algorithmic determination that those deleted features were undesirable. If location 10 represented a genuine small target, the same operation would still delete it.

Closing fills particular gaps

Dilating the original A first produces two runs:

{−1, 0, 1, 2, 3, 4, 5, 6, 7} and {9, 10, 11}.

The first run combines the expansions of {0, 1, 2} and {5, 6}. Their expanded supports overlap or meet. The expansion around 10 remains separated by background at 8.

Eroding these runs retains centers 0 through 6 from the first run and center 10 from the second. Therefore:

C(A) = {0, 1, 2, 3, 4, 5, 6, 10}.

The missing positions 3 and 4 have become foreground. The three-position gap at 7, 8, and 9 has not disappeared. Closing also retained the original singleton at 10.

The final set has eight positions rather than the original six. The operation has altered the data. It is not merely assigning different component labels to unchanged foreground.

Compose the complete operations in both orders

First open A, obtaining {0, 1, 2}. Closing that set dilates it to {−1, 0, 1, 2, 3} and erodes it back to {0, 1, 2}. Opening followed by closing therefore leaves three foreground positions.

Now close A first, obtaining {0, 1, 2, 3, 4, 5, 6, 10}. Opening this result erodes the seven-position run to centers 1 through 5 and removes the singleton. Dilation restores the run from 0 through 6.

Closing followed by opening therefore leaves seven positions:

{0, 1, 2, 3, 4, 5, 6}.

The outputs differ because closing the original mask created a long foreground run before opening tested which element placements fit. When opening came first, the deleted run at 5 and 6 was unavailable for the later closing to join.

There is no universal “cleanup” result independent of this ordering. The meaningful question is which transformation matches the intended interpretation of the mask.

Change the element, not the data

Keep the original A but replace B with the centered five-position element {−2, −1, 0, 1, 2}.

No five-position run fits inside A, so erosion produces the empty set and opening remains empty.

For closing, dilation now produces every integer from −2 through 12. The expanded supports bridge both original gaps. Erosion retains centers 0 through 10, giving eleven foreground positions.

The same input has now opened to nothing or closed to a continuous eleven-position run. Neither result follows from the operator name alone; the element is part of the definition.

In two dimensions, shape adds another distinction. A three-pixel horizontal line and a three-pixel vertical line have equal area, but a three-position horizontal structuring element fits the former and not the latter. Counting foreground pixels cannot substitute for that geometric fit.

Keep the interpretation attached to the transformation

The operations here treat foreground as a set. They do not estimate grayscale brightness, average measurements, or establish the physical identity of a region. A later component-labeling operation would analyze whichever modified mask it receives.

A claimed hole-removal or speck-removal benefit consequently needs the relevant feature geometry. A narrow legitimate feature can fail the same fit test as an unwanted feature. A gap that should separate objects can be filled under the same rule that joins a broken contour.

For a robot vision explanation, preserving the starting mask, foreground convention, structuring element, origin, boundaries, and operation sequence makes the result reproducible. The original set example shows the practical reason: changing any of those definitions can change which foreground locations exist before a later measurement or count is made.

Sources checked October 23, 2026: OpenCV 4.12.0, Morphological Transformations, official versioned tutorial: https://docs.opencv.org/4.12.0/d9/d61/tutorial_py_morphological_ops.html

SciPy documentation displayed as v1.18.0, binary_opening, structure, origin, and operation-sequence definitions: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.binary_opening.html

All foreground sets and calculations are original hypothetical examples. No image-processing library was executed to generate the article's outputs.

Illustration note: A generic robot camera or vision-workstation concept image may accompany this explanation. It does not depict the invented masks, a verified morphology result, or a validated inspection process.