morphological operations
Morphological operations are a family of shape-based transformations, originally for binary (black-and-white) images, that probe an image with a small template called a structuring element and reshape regions according to whether that template fits. Think of the structuring element as a tiny cookie cutter or eraser you slide across the image; the rules for how it adds or removes pixels let you clean up shapes: remove specks, fill holes, smooth jagged boundaries, thin lines to skeletons, or separate touching objects. Unlike convolution, the underlying operations are min and max, not weighted sums.
The two atoms are erosion and dilation. Erosion, written A erode B, keeps a foreground pixel only if the structuring element B, centered there, fits entirely inside the foreground; it shrinks bright regions and deletes anything smaller than B. Dilation, A dilate B, sets a pixel if B touches any foreground; it grows bright regions and fills small gaps. The two are dual under complement (eroding the foreground equals dilating the background). Composing them gives the workhorses: opening (erode then dilate) removes small bright specks and thin protrusions while preserving the overall size of larger shapes; closing (dilate then erode) fills small dark holes and bridges narrow gaps. Both are idempotent, applying them twice changes nothing more.
These extend naturally to grayscale images, where erosion becomes a local minimum and dilation a local maximum over the structuring element. Richer compound operators build on the atoms: the morphological gradient (dilation minus erosion) outlines edges; the top-hat transform (image minus its opening) extracts small bright features and corrects uneven illumination; the black-hat does the dark-feature equivalent; and skeletonization, the hit-or-miss transform, and the watershed segmentation all rest on this algebra. Morphology is the standard post-processing step after thresholding in document analysis, medical imaging, and industrial inspection.
After thresholding a scanned form, opening with a 3x3 square wipes out isolated ink specks, then closing with the same element reconnects the broken strokes of letters, leaving clean, solid characters ready for OCR.
Pitfall: order matters, opening and closing are not the same and neither equals doing nothing. Pick opening to remove bright noise and closing to fill dark gaps, and size the structuring element to the features you want gone; too large and you erase the objects you meant to keep.