Erosion: every, or nothing
Erosion keeps a foreground pixel only if every pixel under the structuring element is also foreground. One background pixel anywhere under the element and the centre is turned off.
The consequences follow directly. Boundaries retreat inwards by roughly the radius of the element, because a pixel near an edge always has some background under a large enough window. Small isolated specks vanish entirely, since a speck smaller than the element can never have the element fit inside it. Thin connections between larger blobs are cut, because a bridge one pixel wide fails the test everywhere along its length.
Select Erode above and raise the structuring element size. The bright speckles disappear on the first step; the shapes visibly thin; push it far enough and the thin bright bar at the bottom of the frame is gone entirely.
Dilation: any, and it counts
Dilation is erosion's mirror. A pixel becomes foreground if any pixel under the structuring element is foreground.
Boundaries advance outwards. Small holes inside an object fill, because a hole smaller than the element has foreground on all sides within reach. Nearby blobs merge as their expanding boundaries meet. The dark speckles that erosion could not touch disappear here instead.
Erosion and dilation are *duals*: eroding the foreground is exactly dilating the background. That is not a coincidence to be memorised, it is the same rule seen from the other side.
Open and close: the useful pair
Neither operation is much use alone, because both change the size of everything. Composing them fixes that.
Opening is erosion followed by dilation. The erosion removes small bright specks and thins everything; the dilation grows what survived back to roughly its original size. Specks that were destroyed do not come back — there is nothing left to grow. The net effect is *remove small bright things, leave everything else about where it was*.
Closing is dilation followed by erosion. The dilation fills small dark holes and joins nearby pieces; the erosion shrinks the result back down. Holes that were filled stay filled. The net effect is *remove small dark things, leave everything else about where it was*.
| Operation | Sequence | Removes | Boundary |
|---|
| Erode | — | bright specks | retreats |
| Dilate | — | dark holes | advances |
| Open | erode, dilate | bright specks | roughly unchanged |
| Close | dilate, erode | dark holes | roughly unchanged |
Set the control above to Open and compare against Erode at the same size. The speckles are gone in both, but the shapes under Open are still the size they started.
Why this always follows thresholding
A thresholded mask is almost never clean. Noise that happened to cross the threshold leaves scattered foreground pixels in the background; a highlight inside an object leaves a hole. Both are small, and both are exactly what open and close are for.
The standard sequence — threshold, open to despeckle, close to fill — is so common it is worth treating as one step. Only after it does counting connected components or tracing contours give sensible answers.
Choosing the element
The size is the parameter that matters, and there is a principle for it: the structuring element should be larger than the artefacts you want to remove and smaller than the features you want to keep. If a speck is 3 pixels across and a genuine object is 30, anything between will do. If they are 3 and 5, no size works and morphology is the wrong tool.
The shape matters less, but not never. A square element treats diagonals differently from axes; a disc is isotropic; a horizontal line erodes vertical strokes while leaving horizontal ones alone, which is how table rules are separated from text in document processing.
Where it goes wrong
Applying it to a greyscale image without thinking. Greyscale morphology is defined — erosion becomes a local minimum and dilation a local maximum — but it means something different, and results that look plausible may not be what was intended.
Using an element that is too large. It removes the noise and the detail together. Thin structures are lost first and cannot be recovered.
Opening when you meant closing. The mnemonic is that opening removes *bright* things and closing removes *dark* ones. If your foreground is dark against a light background, they swap.