Morphological Operations

The four classical mathematical-morphology operations — Erosion, Dilation, Opening, Closing — with an elliptical structuring element, live preview, and mask integration. Core workhorses for mask editing (grow or shrink a star mask, close small holes in a selection, open away speckle), small-feature removal, and binary-image clean-up. OpenCV engine underneath; everything else is the dialog, live preview, and document mask integration.

1. Opening the dialog

Runs on the active document's image. Non-modal and stays open after Apply so you can keep tweaking or hop to another image. Opens with a live preview at the default settings (Erosion, kernel 3, one iteration).

2. The four operations

Erosion

Shrinks bright features by setting each pixel to the minimum value within its kernel neighbourhood. Thin bright structures (single-pixel noise, hot pixels, isolated hair-thin features) disappear completely when they're smaller than the kernel.

Dilation

The dual of erosion: each pixel takes the maximum within the kernel neighbourhood, growing bright features outward. Dark gaps smaller than the kernel get filled in.

Opening

Erosion followed by dilation. The sequence removes bright features smaller than the kernel without shrinking the larger features that survive the erosion step.

Closing

Dilation followed by erosion. The dual of opening: fills dark holes smaller than the kernel without growing the already-bright features outward.

3. Parameters

Operation

Dropdown with the four choices above. Reset returns to Erosion.

Kernel size

SpinBox, 1–31, odd only (stepping by 2). The diameter of the elliptical structuring element in pixels. Even numbers are auto-rounded up to the next odd value because morphology needs a centre pixel to be well-defined. Larger kernels affect larger features but also take proportionally longer to compute.

Iterations

SpinBox, 1–10. Number of times the operation is applied in sequence. For Erosion and Dilation this is equivalent to using a larger structuring element but runs faster for low iteration counts (two iterations of a 3×3 erode cover approximately the same reach as a single 5×5 erode). For Opening and Closing each iteration is a full open/close pair, which has a slightly different effect from using a bigger kernel — the intermediate dilate/erode steps happen at the smaller scale.

4. Preview, zoom, and masks

The main area shows a live preview that updates with a 200 ms debounce after any parameter change. Zoom In / Zoom Out / Fit to Preview buttons sit above the Apply row, and the view supports cursor-anchored wheel zoom with the standard SASpro behaviour. If the active document carries a mask, the result is blended through it so only masked regions receive the operation — both in the live preview and the final Apply.

5. Apply / Reset / Cancel and preset grip

Apply runs the final full-resolution computation and commits as an undoable edit named “Morphology”. All three slider values are recorded in the preset so Replay Last Action reproduces the exact run. The dialog stays open so you can switch to another document and apply with the same settings.

Reset returns to Erosion / kernel 3 / 1 iteration; Cancel discards. The grip at the bottom-left carries the current operation + kernel + iterations and can be dragged onto the canvas to make a shortcut tile, or dropped directly on an image to apply those settings headlessly. Double-clicking a shortcut reopens this dialog pre-seeded with the saved values.

6. Technical details

All processing on float32 in [0, 1] with intermediate uint8 for OpenCV's morphology functions. Mono inputs operate on the single channel; RGB inputs are processed per-channel independently (OpenCV handles this natively).

6.1 The four operations as set operations

Morphology operates on grey-level images via the min/max over a structuring-element neighbourhood K. Erosion takes the pixelwise minimum over that neighbourhood:

(I \ominus K)(x, y) \;=\; \min_{(i, j) \in K} I(x + i,\; y + j)

Dilation takes the maximum:

(I \oplus K)(x, y) \;=\; \max_{(i, j) \in K} I(x + i,\; y + j)

Opening and Closing compose erosion and dilation in the two orderings:

I \circ K \;=\; (I \ominus K) \oplus K
I \bullet K \;=\; (I \oplus K) \ominus K

Opening removes bright features smaller than the kernel because the initial erosion kills them completely and the following dilation can only grow back what's still there. Closing fills dark holes smaller than the kernel symmetrically — the initial dilation spans them, the following erosion restores the outer edges but leaves the newly-filled interior intact.

6.2 Erosion-dilation duality

The two primitives are duals under image complement: eroding an image is equivalent to dilating its negative (with a reflected kernel) and complementing back.

(I \ominus K)^c \;=\; I^c \oplus \hat{K}
(I \oplus K)^c \;=\; I^c \ominus \hat{K}

The elliptical kernel used here is point-symmetric so K = K̂, and the duality reduces to “eroding bright is the same as dilating dark”. This is why Erosion grows the dark regions of your image as a side effect of shrinking the bright ones, and vice versa.

6.3 Elliptical structuring element

SASpro uses OpenCV's MORPH_ELLIPSE structuring element throughout — a filled disc inscribed in a k×k bounding box with radius:

r \;=\; \frac{k - 1}{2}
K \;=\; \{(i, j) \,:\, i^2 + j^2 \leq r^2\}

Discs are the natural choice because they are isotropic — the operation has no preferred direction, so a round star stays round after erosion rather than becoming square or diamond-shaped the way it would with a rectangular kernel. OpenCV approximates the disc on the integer pixel grid, which is why the smallest meaningful kernel is 3 px (a 1×1 kernel is an identity).

6.4 Iterations

The Iterations parameter applies the chosen operation n times in succession:

I ⊖ₙ K = I ⊖ K ⊖ K ⊖ ... ⊖ K (n applications)

For Erosion and Dilation this is equivalent to a single operation with a larger structuring element — specifically, n iterations of a kernel with radius r cover approximately the same reach as a single kernel with radius nr. The multi-iteration form is faster when r is small and n is small, slower when n is large (because OpenCV's single-kernel morphology scales better than linearly). For Opening and Closing, each iteration is a full erode-dilate (or dilate-erode) pair, so the behaviour differs subtly from using a larger kernel: the intermediate step happens at the smaller scale, which can affect how features interact.

6.5 RGB vs mono handling

Mono inputs ((H, W) or (H, W, 1)) are flattened to a 2-D array and processed directly, then reshaped back to the original ndim on output. RGB inputs are handed to OpenCV's morphology functions as a single multi-channel array; OpenCV applies the operation independently to each channel using the same kernel. Because morphology is a nonlinear order-statistic operation, per-channel processing can produce slight colour shifts in features with narrow colour-channel differences — expected behaviour, not a bug; convert to luminance first if you want strictly colour-invariant morphology.

6.6 Document mask integration

If the active document carries a mask m ∈ [0, 1], the morphology result is blended with the untouched source through the mask:

\mathrm{out} \;=\; (1 - m)\,\mathrm{src} \;+\; m\,\mathrm{morph}(I)

applied after the full-image morphology computation. Both the live preview and the final Apply respect this mask identically. The mask is resampled to the processed image's dimensions if needed. Note that this gates where the morphology applies; the morphology itself still operates on the whole image, so a feature near a mask boundary can be affected by pixels across the boundary during the erode/dilate step before the blend restores the outside to its original state.

Credits. Mathematical morphology is Jean Serra & Georges Matheron's framework from the 1960s and 70s at the École des Mines de Paris; the standard references are Serra's Image Analysis and Mathematical Morphology (Academic Press, 1982) and Soille's Morphological Image Analysis (Springer, 1999/2003). The grey-level extension from the original binary formulation via min/max over structuring-element neighbourhoods is Haralick, Sternberg & Zhuang (1987). The implementations here are OpenCV's cv2.erode, cv2.dilate, and cv2.morphologyEx, which use efficient van Herk-Gil-Werman separable kernels for rectangular and ellipse-approximate shapes. SASpro's additions are the mono/RGB dispatcher, the live-preview pipeline with its 200 ms debouncing, the document-mask integration, and the preset editor and grip system.