import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
n = 80
y, x = np.mgrid[-1:1:complex(n), -1:1:complex(n)]
mask = (x**2 / 0.45**2 + y**2 / 0.30**2) <= 1
mask[(x > 0.05) & (x < 0.25) & (y > -0.1) & (y < 0.15)] = False
mask[(x + 0.65)**2 + (y - 0.55)**2 < 0.04**2] = True
structure = np.ones((5, 5), dtype=bool)
dilated = ndi.binary_dilation(mask, structure=structure)
eroded = ndi.binary_erosion(mask, structure=structure)
opened = ndi.binary_opening(mask, structure=structure)
closed = ndi.binary_closing(mask, structure=structure)
fig, axes = plt.subplots(1, 5, figsize=(12, 3))
for ax, img, title in zip(
axes,
[mask, dilated, eroded, opened, closed],
["original", "dilation", "erosion", "opening", "closing"]
):
ax.imshow(img, cmap="gray")
ax.set_title(title)
ax.axis("off")
plt.tight_layout()
plt.show()10 Morphology and Shape
Chapter goal. This chapter develops the mathematical language for binary morphology, grayscale morphology, connected components, skeletons, distance transforms, and shape descriptors. The emphasis is on clean definitions, algebraic properties, statistical interpretation, and the way these classical ideas still appear inside modern segmentation pipelines and AI vision systems.
Computer vision often begins with an image, but many tasks end with a shape. A medical segmentation model outputs a tumor region. A document-analysis system extracts characters. A satellite-image model finds roads and buildings. A robotics system estimates the silhouette of an object. In all these examples, the output is not merely a probability value; it is a subset of the image domain with geometry and topology.
Mathematical morphology is the part of image analysis that studies shapes through set operations. It was developed to describe how objects grow, shrink, merge, break apart, and simplify under local probes called structuring elements (Matheron 1975; Serra 1982; Soille 2003). In modern computer vision, morphology is still important because learned segmentation masks usually need geometric interpretation, topological checking, and post-processing. The tools in this chapter connect naturally to graph theory, metric geometry, topology, probability, and deep learning.
10.1 Learning objectives
After studying this chapter, you should be able to:
- represent a binary image as a subset of a pixel grid or as an indicator function;
- define dilation, erosion, opening, closing, boundary extraction, and morphological gradients;
- prove basic algebraic properties such as monotonicity, duality, and idempotence of opening/closing;
- compute connected components using graph adjacency;
- explain distance transforms, medial axes, and skeletons;
- compute shape descriptors from moments and covariance matrices;
- interpret morphology as max-plus/min-plus local filtering;
- understand how morphology appears in segmentation post-processing, medical imaging, and differentiable AI layers.
10.2 9.1 Binary images as sets
A grayscale image can be viewed as a function
\[ I: \Omega \subset \mathbb{R}^2 \to \mathbb{R}. \]
A binary image is a special image whose values are either 0 or 1. Equivalently, it is the indicator function of a subset:
\[ \mathbf{1}_A(x)= \begin{cases} 1, & x\in A,\\ 0, & x\notin A. \end{cases} \]
Here \(A\subset \Omega\) is the foreground object. On a finite pixel grid, one usually writes
\[ \Omega_h=\{0,1,\ldots,m-1\}\times \{0,1,\ldots,n-1\}, \]
and a binary image is a subset \(A\subseteq \Omega_h\).
This set viewpoint is useful because many shape operations can be expressed without mentioning pixel intensities. For example, “make the object thicker” becomes an operation on the set \(A\). “Remove small holes” becomes an operation on the complement \(A^c\). “Keep only large connected pieces” becomes a graph-theoretic operation on the adjacency graph of foreground pixels.
10.2.1 Indicator-function viewpoint
The same binary image can be viewed in three equivalent ways:
| Representation | Mathematical object | Typical use |
|---|---|---|
| Set | \(A\subseteq \Omega_h\) | morphology, geometry, topology |
| Indicator function | \(\mathbf{1}_A:\Omega_h\to\{0,1\}\) | image processing, arrays |
| Random field | \(Y_s\in\{0,1\}\) for each pixel \(s\) | statistics, segmentation uncertainty |
For MA Applied Math and MS Statistics students, the third viewpoint is especially useful. A neural segmentation network may output probabilities
\[ P_\theta(Y_s=1\mid I),\qquad s\in \Omega_h, \]
and the final binary mask is often obtained by thresholding:
\[ \widehat{A}_\tau=\{s\in\Omega_h: P_\theta(Y_s=1\mid I)\ge \tau\}. \]
Morphology then acts on \(\widehat{A}_\tau\) to enforce shape constraints.
10.3 9.2 Structuring elements
A structuring element is a small set \(B\subseteq \mathbb{Z}^2\) used to probe the image. Common examples are:
\[ B_{\text{cross}}=\{(0,0),(1,0),(-1,0),(0,1),(0,-1)\}, \]
and
\[ B_{3\times 3}=\{-1,0,1\}\times\{-1,0,1\}. \]
The first gives 4-neighbor geometry; the second gives 8-neighbor geometry. More generally, one may use disks, lines, ellipses, or anisotropic elements that reflect prior knowledge about the shape.
For \(z\in\mathbb{Z}^2\), define the translated structuring element
\[ B_z=\{b+z:b\in B\}. \]
The reflected structuring element is
\[ \check{B}=\{-b:b\in B\}. \]
Many morphology formulas use reflection. If \(B\) is symmetric, then \(B=\check{B}\).
10.4 9.3 Dilation and erosion
The two fundamental operations are dilation and erosion.
10.4.1 Dilation
The dilation of a set \(A\) by a structuring element \(B\) is
\[ A\oplus B=\{a+b:a\in A,\ b\in B\}. \]
Equivalently,
\[ A\oplus B=\{z: (\check{B})_z\cap A\neq \emptyset\}. \]
Dilation expands the foreground. A pixel \(z\) is included in \(A\oplus B\) if the structuring element placed at \(z\) touches the original object.
10.4.2 Erosion
The erosion of \(A\) by \(B\) is
\[ A\ominus B=\{z:B_z\subseteq A\}. \]
Erosion shrinks the foreground. A pixel \(z\) remains if the entire structuring element placed at \(z\) fits inside the object.
10.4.3 Intuition
| Operation | Local rule | Effect |
|---|---|---|
| Dilation | include if neighborhood intersects foreground | expands objects, fills small gaps |
| Erosion | include if neighborhood lies inside foreground | shrinks objects, removes thin parts |
The two operations are not inverses. Erosion can delete small objects completely; dilation cannot recover them without additional information.
10.4.4 Dilation as Minkowski addition
Dilation is exactly a Minkowski sum:
\[ A\oplus B=A+B. \]
This connects morphology to convex geometry and mathematical analysis. If \(A\) is a continuous planar set and \(B_r\) is a disk of radius \(r\), then
\[ A\oplus B_r=\{x\in\mathbb{R}^2: d(x,A)\le r\}, \]
where
\[ d(x,A)=\inf_{a\in A}\|x-a\|_2. \]
Thus dilation by a disk is the \(r\)-neighborhood of a set.
10.4.5 Erosion as distance from the complement
For a disk \(B_r\),
\[ A\ominus B_r=\{x\in A: d(x,A^c)>r\}. \]
So erosion keeps points that are at least distance \(r\) away from the background.
If a predicted segmentation mask has boundary uncertainty of roughly \(r\) pixels, then dilation and erosion by a radius-\(r\) structuring element produce outer and inner uncertainty bands. This is useful in medical imaging when the exact boundary is uncertain.
10.5 9.4 Interactive: dilation and erosion on a binary grid
The following interactive shows how dilation and erosion act on a synthetic binary object. Increase the radius to see how the set expands or shrinks.
Dilation and erosion on a binary grid
Choose a morphological operation and a square structuring-element radius.
10.6 9.5 Duality, monotonicity, and algebraic properties
Morphological operations have clean algebraic structure. Let \(A,C\subseteq \Omega\) and let \(B\) be a structuring element.
10.6.1 Monotonicity
If \(A\subseteq C\), then
\[ A\oplus B\subseteq C\oplus B \]
and
\[ A\ominus B\subseteq C\ominus B. \]
Proof. If \(z\in A\oplus B\), then \(z=a+b\) for some \(a\in A\) and \(b\in B\). Since \(A\subseteq C\), \(a\in C\), so \(z\in C\oplus B\). For erosion, if \(z\in A\ominus B\), then \(B_z\subseteq A\subseteq C\), hence \(z\in C\ominus B\). \(\square\)
10.6.2 Duality
Dilation and erosion are dual under complementation:
\[ (A\oplus B)^c=A^c\ominus \check{B}, \]
and
\[ (A\ominus B)^c=A^c\oplus \check{B}. \]
For symmetric \(B\), this simplifies to
\[ (A\oplus B)^c=A^c\ominus B, \qquad (A\ominus B)^c=A^c\oplus B. \]
This explains why dilation of the foreground is equivalent to erosion of the background.
10.6.3 Translation equivariance
For a shift \(t\in\mathbb{Z}^2\), define
\[ A+t=\{a+t:a\in A\}. \]
Then
\[ (A+t)\oplus B=(A\oplus B)+t, \]
and
\[ (A+t)\ominus B=(A\ominus B)+t. \]
This resembles the translation equivariance of convolution. In fact, dilation and erosion can be viewed as nonlinear translation-equivariant local operators.
10.7 9.6 Opening and closing
Two important composite operations are opening and closing.
10.7.1 Opening
The opening of \(A\) by \(B\) is
\[ A\circ B=(A\ominus B)\oplus B. \]
Opening erodes first and then dilates. It removes small foreground components and thin protrusions while preserving larger shapes.
10.7.2 Closing
The closing of \(A\) by \(B\) is
\[ A\bullet B=(A\oplus B)\ominus B. \]
Closing dilates first and then erodes. It fills small background holes and narrow gaps.
10.7.3 Properties
Opening is:
- anti-extensive: \(A\circ B\subseteq A\);
- monotone: if \(A\subseteq C\), then \(A\circ B\subseteq C\circ B\);
- idempotent: \((A\circ B)\circ B=A\circ B\).
Closing is:
- extensive: \(A\subseteq A\bullet B\);
- monotone: if \(A\subseteq C\), then \(A\bullet B\subseteq C\bullet B\);
- idempotent: \((A\bullet B)\bullet B=A\bullet B\).
Idempotence means that applying the operation twice gives no further change. This is a hallmark of a shape simplification operator.
10.7.4 Opening as a shape filter
Opening can be interpreted as keeping only those parts of \(A\) into which a translated copy of \(B\) can fit. Formally,
\[ A\circ B=\bigcup_{z:B_z\subseteq A} B_z. \]
Thus opening approximates \(A\) by a union of copies of the structuring element.
Use opening to remove small false-positive islands. Use closing to fill small false-negative holes. Use both carefully: too large a structuring element can remove real anatomical or geometric details.
10.8 9.7 Interactive: opening and closing
Opening and closing as shape filters
Opening removes small bright structures; closing fills small dark holes and gaps.
10.9 9.8 Boundary extraction and morphological gradients
The internal boundary of \(A\) with respect to \(B\) is
\[ \partial_B A=A\setminus(A\ominus B). \]
It consists of foreground pixels that do not survive erosion. The external boundary can be written
\[ \partial_B^{\text{ext}} A=(A\oplus B)\setminus A. \]
The morphological gradient is
\[ G_B(A)=(A\oplus B)\setminus(A\ominus B). \]
For grayscale images, a morphological gradient is often defined as
\[ G_B f(x)=(f\oplus B)(x)-(f\ominus B)(x), \]
where grayscale dilation and erosion are introduced below. This acts like an edge detector but uses local maxima and minima rather than local derivatives.
10.10 9.9 Grayscale morphology
For a grayscale image \(f:\Omega_h\to\mathbb{R}\) and a flat structuring element \(B\subseteq \mathbb{Z}^2\), define grayscale dilation by
\[ (f\oplus B)(x)=\max_{b\in B} f(x-b), \]
and grayscale erosion by
\[ (f\ominus B)(x)=\min_{b\in B} f(x+b). \]
These are max and min filters. They are nonlinear analogues of convolution.
10.10.1 Non-flat structuring elements
A non-flat structuring element is a function \(g:B\to\mathbb{R}\). Then dilation can be defined as
\[ (f\oplus g)(x)=\max_{b\in B}\{f(x-b)+g(b)\}, \]
and erosion as
\[ (f\ominus g)(x)=\min_{b\in B}\{f(x+b)-g(b)\}. \]
This places morphology in max-plus algebra and min-plus algebra. These algebraic systems replace ordinary addition and multiplication with operations such as maximum plus addition. They appear in optimization, dynamic programming, and shortest-path problems.
10.10.2 Relation to pooling in neural networks
Max pooling in CNNs is a grayscale dilation over a local window followed by subsampling. Min pooling can be implemented as
\[ \min_{b\in B} f(x+b)=-\max_{b\in B}\{-f(x+b)\}. \]
Thus erosion is negative max pooling applied to the negative image.
10.11 9.10 Connected components and adjacency graphs
A binary image \(A\subseteq\Omega_h\) can be turned into a graph. Let
\[ G_A=(V_A,E_A), \]
where
\[ V_A=A \]
and where two foreground pixels are connected by an edge if they are neighbors. With 4-connectivity,
\[ (i,j)\sim(i',j') \quad \Longleftrightarrow \quad |i-i'|+|j-j'|=1. \]
With 8-connectivity,
\[ (i,j)\sim(i',j') \quad \Longleftrightarrow \quad \max\{|i-i'|,|j-j'|\}=1. \]
A connected component is a connected component of this graph. Component labeling is a standard operation in post-processing segmentation masks.
10.11.1 Why connectivity matters
The choice of foreground connectivity affects topology. If diagonal pixels count as connected under 8-connectivity, then two diagonal pixels belong to the same component. Under 4-connectivity, they do not.
To avoid paradoxes, one often uses complementary choices:
| Foreground connectivity | Background connectivity |
|---|---|
| 4-connected foreground | 8-connected background |
| 8-connected foreground | 4-connected background |
This prevents ambiguous diagonal connections.
10.11.2 Euler characteristic
For a planar binary shape, the Euler characteristic is
\[ \chi(A)=\#\{\text{connected components}\}-\#\{\text{holes}\}. \]
In topological language,
\[ \chi(A)=\beta_0(A)-\beta_1(A), \]
where \(\beta_0\) counts connected components and \(\beta_1\) counts one-dimensional holes.
This is a simple but powerful topological descriptor. In medical image segmentation, for example, one may expect a certain anatomical structure to have one connected component and no holes. A segmentation mask that violates this expectation may need correction.
Morphology modifies the topology of a binary set. Dilation can merge components and fill holes. Erosion can split components and create holes. Persistent homology tracks how connected components and holes change over a scale parameter.
10.12 9.11 Distance transforms
For a foreground set \(A\subseteq\Omega_h\), the distance transform assigns to each pixel its distance to the nearest foreground pixel:
\[ D_A(x)=\min_{a\in A} d(x,a). \]
Common choices include:
\[ d_1(x,a)=|x_1-a_1|+|x_2-a_2|, \]
\[ d_2(x,a)=\sqrt{(x_1-a_1)^2+(x_2-a_2)^2}, \]
and
\[ d_\infty(x,a)=\max\{|x_1-a_1|,|x_2-a_2|\}. \]
For points inside the foreground, one often computes distance to the background:
\[ D_{A^c}(x)=\min_{y\in A^c}d(x,y),\qquad x\in A. \]
This value is large near the center of thick regions and small near the boundary.
10.12.1 Signed distance function
A signed distance function is
\[ \phi_A(x)= \begin{cases} -d(x,A^c), & x\in A,\\ d(x,A), & x\notin A. \end{cases} \]
The boundary is represented by the zero level set:
\[ \partial A=\{x:\phi_A(x)=0\}. \]
Signed distance functions connect morphology to level-set methods, shape optimization, and PDE-based vision.
10.13 9.12 Skeletons and medial axes
The medial axis of a shape is the set of centers of maximal balls contained in the shape. Informally, it is the set of points that have at least two closest boundary points.
For a continuous shape \(A\subset\mathbb{R}^2\), a disk \(B(x,r)\) is maximal in \(A\) if
\[ B(x,r)\subseteq A \]
and it is not strictly contained in a larger disk contained in \(A\). The medial axis consists of the centers of these maximal disks.
A skeleton should preserve the main topology and geometry of the shape while reducing it to a thin centerline. This is useful for roads, vessels, plant roots, handwriting, and shape matching.
10.13.1 Morphological skeleton
One classical morphological skeleton is defined using erosions and openings:
\[ S_k(A)=(A\ominus kB)\setminus\big((A\ominus kB)\circ B\big), \]
where \(kB\) denotes repeated dilation of \(B\) with itself. The full skeleton is
\[ S(A)=\bigcup_{k\ge 0} S_k(A). \]
The original shape can be approximately reconstructed from skeleton layers by
\[ A=\bigcup_{k\ge 0} \left(S_k(A)\oplus kB\right), \]
under suitable discrete assumptions.
10.14 9.13 Interactive: distance transform and skeleton intuition
Distance transform and skeleton intuition
The skeleton is approximated by ridges of the distance-to-boundary function.
10.15 9.14 Shape descriptors
A shape descriptor maps a set \(A\) to a numerical feature vector:
\[ \Psi(A)\in\mathbb{R}^d. \]
Good descriptors should be stable, interpretable, and relevant to the task. Some descriptors should be invariant to translation, rotation, or scale; others should preserve orientation and size.
10.15.1 Area and perimeter
The area of a binary shape is the number of foreground pixels:
\[ |A|=\sum_{(i,j)\in\Omega_h}\mathbf{1}_A(i,j). \]
Perimeter can be approximated by counting boundary pixels, or more accurately by using contour length estimates. A simple boundary-count estimate is
\[ P_B(A)=|A\setminus(A\ominus B)|. \]
10.15.2 Centroid
The centroid is
\[ \bar{x}=\frac{1}{|A|}\sum_{x\in A}x. \]
Writing \(x=(x_1,x_2)^T\), this gives
\[ \bar{x}=\begin{pmatrix}\bar{x}_1\\ \bar{x}_2\end{pmatrix}. \]
The centroid is translation equivariant: shifting the shape shifts the centroid by the same amount.
10.15.3 Covariance matrix of a shape
The shape covariance matrix is
\[ \Sigma_A=\frac{1}{|A|}\sum_{x\in A}(x-\bar{x})(x-\bar{x})^T. \]
The eigenvectors of \(\Sigma_A\) give principal axes. The eigenvalues describe spread along those axes. If
\[ \lambda_1\ge \lambda_2\ge 0, \]
then an eccentricity-type descriptor is
\[ e(A)=\sqrt{1-\frac{\lambda_2}{\lambda_1}}. \]
This is close to 0 for round shapes and close to 1 for elongated shapes.
10.15.4 Moment descriptors
Raw moments are
\[ m_{pq}=\sum_{(x,y)\in A} x^p y^q. \]
Central moments are
\[ \mu_{pq}=\sum_{(x,y)\in A}(x-\bar{x})^p(y-\bar{y})^q. \]
Normalized central moments are
\[ \eta_{pq}=\frac{\mu_{pq}}{\mu_{00}^{1+(p+q)/2}}. \]
Moment invariants combine normalized central moments to obtain descriptors invariant under translation, scaling, and sometimes rotation. These are useful for classical shape recognition but can be unstable for noisy boundaries.
10.15.5 Compactness and circularity
A common compactness measure is
\[ C(A)=\frac{4\pi |A|}{P(A)^2}. \]
For an ideal disk, \(C(A)=1\). For elongated or irregular shapes, \(C(A)\) is smaller.
Pixel-based perimeter estimates can be biased by grid orientation. For statistical analysis, use consistent definitions across the dataset, and be careful when comparing shapes at different resolutions.
10.16 9.15 Interactive: moments and principal axes
Shape covariance and principal axes
Change the elongation and rotation of a synthetic ellipse. The covariance eigenvectors estimate the principal axes.
10.17 9.16 Fourier descriptors for shape contours
Suppose the boundary of a planar shape is sampled as an ordered sequence of points
\[ z_k=x_k+iy_k,\qquad k=0,1,\ldots,N-1. \]
The discrete Fourier transform of the contour is
\[ \widehat{z}_\ell=\sum_{k=0}^{N-1} z_k\exp\left(-2\pi i k\ell/N\right). \]
Low-frequency coefficients describe the coarse shape, while high-frequency coefficients describe fine boundary details. A smoothed contour can be reconstructed from a truncated Fourier series:
\[ z_k^{(K)}=\frac{1}{N}\sum_{|\ell|\le K}\widehat{z}_\ell\exp\left(2\pi i k\ell/N\right). \]
Fourier descriptors make shape analysis look like signal processing on closed curves. Translation is controlled by the zero-frequency coefficient, scaling by coefficient magnitudes, and rotation by phase.
10.18 9.17 Interactive: Fourier shape reconstruction
Fourier descriptors of a closed contour
Low frequencies preserve the coarse shape. Higher frequencies add boundary details.
10.19 9.18 Granulometry and morphological scale
In many applications, object size is itself a feature. A granulometry studies how the area of a shape changes under openings of increasing size. Let \(B_r\) be a structuring element of scale \(r\). Define
\[ G_A(r)=|A\circ B_r|. \]
As \(r\) increases, small structures disappear. The function \(G_A(r)\) is nonincreasing. A discrete size distribution can be defined by differences:
\[ \Delta G_A(r)=G_A(r)-G_A(r+1). \]
Large drops indicate that many structures of size approximately \(r\) were removed. This is useful for particle analysis, materials imaging, cell morphology, and remote sensing.
10.20 9.19 Statistical viewpoint: shapes as estimators
In statistical vision, the true object \(A^*\) is unknown. A model produces an estimated mask \(\widehat{A}\). Shape descriptors become statistics:
\[ T(\widehat{A})=\Psi(\widehat{A}). \]
Examples include area, perimeter, eccentricity, number of components, and compactness.
10.20.1 Bias from morphology
Morphological operations can introduce bias. If \(A\) is dilated by a disk of radius \(r\), its area generally increases. For sufficiently regular planar sets, the continuous Steiner formula gives
\[ |A\oplus B_r|=|A|+rP(A)+\pi r^2, \]
for convex \(A\). This formula shows that dilation changes area in a predictable way involving perimeter.
For erosion,
\[ |A\ominus B_r|\approx |A|-rP(A)+\pi r^2, \]
under regularity assumptions and for small \(r\). These formulas are useful when interpreting morphological preprocessing statistically.
10.20.2 Uncertainty in shape descriptors
Suppose a segmentation model outputs random masks \(A^{(1)},\ldots,A^{(M)}\), for example from test-time augmentation, Monte Carlo dropout, or posterior sampling. A shape descriptor \(\Psi\) then has an empirical distribution:
\[ \Psi(A^{(1)}),\ldots,\Psi(A^{(M)}). \]
We can estimate uncertainty using
\[ \widehat{\operatorname{Var}}[\Psi] =\frac{1}{M-1}\sum_{m=1}^M \left(\Psi(A^{(m)})-\overline{\Psi}\right)^2. \]
This is important when a downstream scientific conclusion depends on a shape measurement.
10.21 9.20 AI components: morphology in modern vision systems
Classical morphology remains important in AI-based computer vision.
10.21.1 1. Post-processing segmentation masks
A neural network often produces a probability map
\[ p_\theta(x)=P_\theta(Y_x=1\mid I). \]
A common pipeline is:
\[ I \longrightarrow p_\theta \longrightarrow \widehat{A}_\tau \longrightarrow \text{morphological cleanup} \longrightarrow \text{component filtering}. \]
For example:
- threshold the probability map;
- remove small connected components;
- close small holes;
- compute area, perimeter, and shape descriptors;
- flag masks with unusual topology.
10.21.2 2. Differentiable morphology
Grayscale dilation and erosion can be written using max and min operations. A differentiable approximation to maximum is the log-sum-exp function:
\[ \max_i a_i \approx \frac{1}{\alpha}\log\sum_i \exp(\alpha a_i), \]
where large \(\alpha\) gives a closer approximation. This permits differentiable morphology layers inside neural networks.
10.21.3 3. Max pooling as morphology
Max pooling is a local dilation:
\[ (\operatorname{MaxPool}_B f)(x)=\max_{b\in B} f(x+b). \]
This creates a direct bridge between morphology and CNNs.
10.21.4 4. Topological and shape-aware losses
For segmentation, pixelwise loss may not be enough. A mask can have high pixel accuracy but incorrect topology. Shape-aware training may include penalties for:
- incorrect number of connected components;
- holes that should not exist;
- disconnected vessels or roads;
- unrealistic area/perimeter ratios;
- deviation from expected shape priors.
10.21.5 5. Foundation-model masks still need shape analysis
Promptable segmentation systems can produce high-quality masks, but the outputs are still sets. For scientific and medical use, one must measure, validate, and summarize these sets. Morphology and shape descriptors remain essential.
10.22 9.21 Python example: binary morphology
The following code illustrates basic morphology using scipy.ndimage. The code is marked eval: false so that the book does not require SciPy during rendering.
10.23 9.22 Python example: connected components and shape features
import numpy as np
from scipy import ndimage as ndi
# Suppose mask is a binary segmentation result.
labeled, num_components = ndi.label(mask)
component_sizes = ndi.sum(mask, labeled, index=np.arange(1, num_components + 1))
print("number of connected components:", num_components)
print("component sizes:", component_sizes)
# Keep only components with at least 50 pixels.
keep_labels = np.where(component_sizes >= 50)[0] + 1
clean = np.isin(labeled, keep_labels)
# Simple centroid and covariance matrix of the largest component.
largest_label = int(np.argmax(component_sizes) + 1)
coords = np.column_stack(np.nonzero(labeled == largest_label))
centroid = coords.mean(axis=0)
centered = coords - centroid
cov = centered.T @ centered / coords.shape[0]
evals, evecs = np.linalg.eigh(cov)
print("centroid:", centroid)
print("covariance matrix:\n", cov)
print("principal variances:", evals[::-1])
print("principal directions:\n", evecs[:, ::-1])10.24 9.23 Python example: distance transform and skeleton
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage.morphology import medial_axis
# Distance from foreground pixels to background.
dist = ndi.distance_transform_edt(mask)
# Skeleton from the medial axis.
skeleton, dist_on_skeleton = medial_axis(mask, return_distance=True)
fig, axes = plt.subplots(1, 3, figsize=(10, 3))
axes[0].imshow(mask, cmap="gray")
axes[0].set_title("mask")
axes[1].imshow(dist, cmap="viridis")
axes[1].set_title("distance transform")
axes[2].imshow(skeleton, cmap="gray")
axes[2].set_title("skeleton")
for ax in axes:
ax.axis("off")
plt.tight_layout()
plt.show()10.25 9.24 Python example: grayscale morphology
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
rng = np.random.default_rng(7243)
n = 100
y, x = np.mgrid[-1:1:complex(n), -1:1:complex(n)]
image = np.exp(-((x + 0.25)**2 + (y + 0.1)**2) / 0.08)
image += 0.7 * np.exp(-((x - 0.35)**2 + (y - 0.25)**2) / 0.02)
image += 0.08 * rng.normal(size=(n, n))
size = 7
gray_dilation = ndi.maximum_filter(image, size=size)
gray_erosion = ndi.minimum_filter(image, size=size)
gray_gradient = gray_dilation - gray_erosion
fig, axes = plt.subplots(1, 4, figsize=(12, 3))
for ax, img, title in zip(
axes,
[image, gray_dilation, gray_erosion, gray_gradient],
["image", "dilation/max filter", "erosion/min filter", "morphological gradient"]
):
ax.imshow(img, cmap="gray")
ax.set_title(title)
ax.axis("off")
plt.tight_layout()
plt.show()10.26 9.25 TensorFlow example: morphology using pooling
The next example implements grayscale dilation and erosion using max pooling. This is useful because it connects morphology directly to neural-network layers.
import tensorflow as tf
def dilation2d(x, ksize=3):
"""Flat grayscale dilation for NHWC tensors."""
return tf.nn.max_pool2d(
x,
ksize=ksize,
strides=1,
padding="SAME"
)
def erosion2d(x, ksize=3):
"""Flat grayscale erosion using negative max pooling."""
return -tf.nn.max_pool2d(
-x,
ksize=ksize,
strides=1,
padding="SAME"
)
def opening2d(x, ksize=3):
return dilation2d(erosion2d(x, ksize), ksize)
def closing2d(x, ksize=3):
return erosion2d(dilation2d(x, ksize), ksize)
# Example tensor: batch size 1, height 64, width 64, one channel.
x = tf.random.uniform((1, 64, 64, 1))
y_open = opening2d(x, ksize=5)
y_close = closing2d(x, ksize=5)
print(y_open.shape, y_close.shape)10.27 9.26 AI-assisted study prompts
Use an AI assistant as a mathematical critic, not as a replacement for derivation.
10.27.1 Prompt 1: definitions
Explain dilation, erosion, opening, and closing using both set notation and indicator-function notation. Give one example where opening is useful and one example where closing is useful.
10.27.2 Prompt 2: proof checking
I claim that opening is idempotent. Write a proof using the definition \(A\circ B=(A\ominus B)\oplus B\). Identify any assumptions about the structuring element.
10.27.3 Prompt 3: segmentation post-processing
A neural network outputs a noisy probability map for a tumor. Design a mask post-processing pipeline using thresholding, opening, closing, connected components, and shape descriptors. Explain the statistical risk of each step.
10.27.4 Prompt 4: coding critique
Review my Python code for connected-component filtering. Check whether it accidentally removes true small objects and suggest a validation strategy.
10.27.5 Prompt 5: shape descriptors
Compare area, perimeter, compactness, eccentricity, and Euler characteristic as shape features. Which are invariant to translation, rotation, and scaling?
10.28 9.27 Common mistakes
- Confusing dilation with blurring. Dilation is not averaging. It expands sets or takes local maxima.
- Using too large a structuring element. This can remove real small structures.
- Ignoring connectivity choice. 4-connectivity and 8-connectivity can give different component counts.
- Treating post-processing as harmless. Morphology changes statistical estimators such as area and perimeter.
- Measuring topology after resizing without care. Interpolation and thresholding can create or destroy components and holes.
- Using raw perimeter counts across resolutions. Perimeter estimates depend strongly on pixel size and contour representation.
10.29 9.28 Exercises
10.29.1 Conceptual exercises
- Explain why dilation expands a foreground set but erosion shrinks it.
- Give an example where opening removes a false positive but also removes a true object.
- Give an example where closing fills a hole but incorrectly merges two nearby objects.
- Explain the difference between 4-connectivity and 8-connectivity.
- Explain why the skeleton of a shape is sensitive to boundary noise.
10.29.2 Mathematical exercises
- Prove that dilation is monotone: if \(A\subseteq C\), then \(A\oplus B\subseteq C\oplus B\).
- Prove the duality formula \[ (A\oplus B)^c=A^c\ominus \check{B}. \]
- Show that opening is anti-extensive: \(A\circ B\subseteq A\).
- Show that closing is extensive: \(A\subseteq A\bullet B\).
- Suppose \(B\) is symmetric and contains the origin. Prove that \(A\ominus B\subseteq A\subseteq A\oplus B\).
- For a disk-shaped set \(A\) and disk structuring element \(B_r\), describe geometrically \(A\oplus B_r\) and \(A\ominus B_r\).
- Let \(A\) consist of two disks whose centers are distance \(d\) apart. For a disk structuring element of radius \(r\), determine when dilation causes the two components to merge.
- Derive the centroid and covariance matrix of an ideal continuous ellipse centered at the origin.
10.29.3 Python exercises
- Generate a binary image with three components of different sizes. Use connected-component labeling to remove components below a size threshold.
- Compare opening with square and disk-like structuring elements.
- Compute the area, centroid, covariance matrix, and eccentricity of a binary mask.
- Add random salt-and-pepper noise to a binary image and compare median filtering, opening, and closing.
- Compute a distance transform and overlay the skeleton on the original image.
- Implement binary dilation from scratch using only NumPy loops. Then compare with
scipy.ndimage.binary_dilation. - Implement grayscale dilation using TensorFlow max pooling.
- Create a small experiment showing how estimated area changes after dilation with increasing radius.
10.29.4 AI and modeling exercises
- Take a segmentation model output from any image dataset. Apply thresholding, opening, closing, and connected-component filtering. Report how IoU and Dice change.
- Use test-time augmentation to obtain multiple predicted masks. Compute uncertainty intervals for area and eccentricity.
- Design a shape-aware validation rule for medical segmentation masks.
- Explain how morphology could be used to post-process masks produced by a promptable segmentation model.
- Compare a CNN max-pooling layer with grayscale dilation. What is the same? What is different?
- Propose a differentiable approximation to erosion and explain where it could be used in a neural network.
10.30 9.29 Chapter summary
This chapter treated shapes as mathematical objects. A binary image is a set, an indicator function, and possibly a random field. Morphological operations such as dilation, erosion, opening, and closing manipulate sets through local probes called structuring elements. These operations have clean algebraic properties: monotonicity, duality, translation equivariance, extensivity or anti-extensivity, and idempotence.
We then moved from morphology to shape analysis. Connected components turn masks into graphs. Distance transforms and skeletons describe the geometry of interiors. Moments, covariance matrices, compactness, eccentricity, and Fourier descriptors summarize shape numerically. Finally, we connected these classical tools to modern AI vision pipelines: segmentation post-processing, differentiable morphology, max pooling, shape-aware losses, and validation of foundation-model masks.
The main message is that computer vision does not end when a model produces a mask. The mask is a mathematical object. Its geometry, topology, uncertainty, and statistical meaning must still be analyzed.
10.31 References and further reading
Classical mathematical morphology was developed by Matheron and Serra (Matheron 1975; Serra 1982). A practical image-analysis treatment is given by Soille (Soille 2003). For general image processing and morphology operations, see Gonzalez and Woods (Gonzalez and Woods 2018). For connected components, segmentation, and shape analysis in a broad computer vision context, see Szeliski (Szeliski 2022).