Sets and structuring elements
A binary image is a set : the positions of its foreground pixels. The structuring element (SE) is a small binary matrix, i.e. a set of offsets around an origin, for example a square, a cross or a disk. denotes moved to position , and its reflection through the origin (for the symmetric shapes here, ).
Dilation and erosion
Dilation keeps every position where touches the object: objects grow by the shape of , small holes and gaps close, and nearby objects merge. Erosion keeps only the positions where fits completely inside: objects shrink, and specks and thin lines disappear. The two are dual: eroding the foreground is the same as dilating the background, .
Both are max and min filters: dilation takes the maximum of the image under , erosion the minimum. Like the median, they are non-linear. Here pixels outside the image count as background for dilation and do not constrain erosion.
Opening and closing
Opening erodes and then dilates. Everything that survives the erosion grows back, so large objects keep their shape, but whatever cannot fit into (specks, thin lines, narrow bridges) is gone. The opening is the union of all copies of that fit inside . Closing does the opposite: it fills holes, gaps and bays that are smaller than without growing the objects. Applying either one a second time changes nothing.
The boundary keeps the pixels removed by erosion: an outline as thick as the radius of .
Connected components
Two foreground pixels belong to the same component if a path of neighboring foreground pixels connects them. With 4-connectivity, neighbors share an edge; with 8-connectivity, diagonal neighbors count too. A diagonal line is one object with 8-connectivity but falls apart into single pixels with 4-connectivity.
The classic two-pass algorithm scans the image row by row. Each foreground pixel gets the smallest label of its already visited neighbors (left and above), or a new label if there is none. When different labels meet, as at the bottom of a U, they are recorded as equivalent in a union-find structure. A second pass replaces every label by the representative of its set. The labels then give each object's area, bounding box or centroid.
Try this
- Apply an opening with the 3 × 3 square: the two disks joined by a bridge become two components, and the specks, the diagonal line and the spike disappear.
- Apply a closing: the small holes in the rectangle, the gap in the ring and the notch in the disk are filled. The 5 × 5 hole is only filled from radius 3 on, when no longer fits into it.
- Dilate with radius 3 and compare the shapes of : the square keeps the corners of the rectangle, the disk rounds them, and the cross leaves them notched.
- Choose the boundary operation and increase the radius: the outlines get thicker.
- Set the radius to 0, so that every operation leaves unchanged, and switch between 4- and 8-connectivity: the diagonal line is one component or 17.
- Paint a diagonal line of your own and switch the connectivity again.
- Paint two shapes that almost touch and dilate them: they merge into one component, and the count in the values panel drops.