Post-processing transforms raw segmentation output into clean, measurement-ready object masks. The operations are rooted in mathematical morphology — a framework for analyzing and modifying shape using a small probe called a structuring element (SE) — and connected-component analysis for identifying and filtering individual objects (Gonzalez & Woods, §9.5–9.6).
Morphological Clean-Up
Erosion removes boundary pixels: the SE is placed at each pixel, and only pixels where the SE fits entirely within the foreground survive. Solomon & Breckon use the "prairie-fire" analogy — imagine setting fire to the boundary; the fire advances inward, consuming pixels layer by layer. Dilation adds boundary pixels: the SE is placed at each background pixel adjacent to the foreground, and all pixels it touches become foreground — the boundary grows outward.
Combining these yields compound operations: Opening (erosion followed by dilation) removes small protrusions and isolated noise pixels without significantly changing object size. Closing (dilation followed by erosion) fills small gaps and holes while preserving overall dimensions. The Smooth Boundaries operation applies opening then closing in sequence, producing regularized contours. The SE size controls the scale of features affected — a 3×3 SE smooths pixel-level noise; a larger SE smooths larger irregularities but risks merging nearby objects.
Distance Transforms & Watershed
For separating touching objects (not a BOM post-processing operation, but closely related in the pipeline), the Euclidean Distance Transform (EDT) computes each foreground pixel's minimum distance to the nearest background pixel. Local maxima of the distance map identify the centers of well-separated objects — these become seed points. The watershed algorithm then "floods" from these seeds: imagine the distance map as a topographic surface and water rising from each seed — where two flood regions meet, a watershed boundary is placed, segmenting touching objects. Over-segmentation (too many false boundaries) is a common failure; marker-controlled watershed mitigates this by restricting seeds to verified markers (Gonzalez & Woods, §10.5).
Connected Component Analysis
Connected-component labeling identifies distinct foreground regions by scanning the image and assigning unique integer labels to contiguous pixel groups. 4-connectivity considers only orthogonal neighbors (N/S/E/W); 8-connectivity includes diagonals — the choice affects whether diagonally-touching pixels are labeled as one or two objects. Once labeled, components can be filtered by properties: area filtering removes components below a pixel-count threshold (the Remove Small Objects operation); shape filtering can select components by circularity, aspect ratio, or other geometric criteria.
Pipeline Ordering
The order of post-processing operations matters because each step changes the input to the next. The recommended sequence — (1) Smooth boundaries → (2) Fill holes → (3) Remove small objects → (4) Label — maximizes cleanup effectiveness: smoothing may resolve some holes, filling may increase component sizes above the removal threshold, and final removal catches remaining artifacts before labeling. Reversing the order causes failures: removing small objects before filling holes can delete valid object fragments that would have grown above threshold after hole-filling. Similarly, labeling before cleanup creates spurious events that persist in downstream analysis even if the underlying mask is later corrected.
Post-processing cleans up the messy output of thresholding. Erosion shrinks objects (like a fire burning inward from edges), dilation grows them, and combining both in sequence (opening then closing) smooths boundaries without changing overall size. Fill holes closes interior gaps from uneven staining. Remove small objects eliminates noise fragments below a minimum size. The order matters: smooth first, fill holes second, remove small objects third, label last — each step makes the next one more effective.