Segmentation
Normalized Cuts
A graph-partitioning method for image segmentation that measures a cut relative to the total connection of each side, and approximates the best partition with the second eigenvector of a generalized eigenvalue problem; the basis of spectral clustering in vision.
advanced
Normalized cuts segment an image by treating its pixels as the nodes of a weighted graph and splitting the graph where the connections between the parts are weak relative to the connections inside them. Shi and Malik (2000) defined the normalized cut criterion, noted that minimizing it exactly is NP-complete, and approximated it by solving a generalized eigenvalue problem. The same relaxation, with several eigenvectors and k-means, became normalized spectral clustering. The image segmentation hub places the method among the other classical ones.
Problem
Partition an image’s pixels, without labels or user input, into groups that are coherent in brightness, color, texture or motion. Shi and Malik framed this as perceptual grouping: capture the global impression of the scene first, then refine it hierarchically.
Inputs and Outputs
- Input: an image, a feature vector for each pixel (intensity, color, filter responses), and an affinity function with its parameters.
- Output: a two-way partition, applied recursively to give a hierarchy of segments, or a -way label map; later methods also use the eigenvectors as features.
Intuition
Build a graph whose nodes are pixels and whose edge weights say how likely two pixels are to belong together. The obvious way to split it is the minimum cut: remove the set of edges with the smallest total weight. Wu and Leahy (1993), as Shi and Malik describe their work, clustered data and segmented images by recursively finding minimum cuts. Shi and Malik pointed out that, as Wu and Leahy had also noticed, the minimum cut favors cutting off small sets of isolated nodes: the cut grows with the number of edges it crosses, so a single pixel with few or weak connections is cheap to separate.
The normalized cut measures the cut as a fraction of each side’s total connection to the graph. Cutting off one pixel then costs 100 percent of its connections, while a cut between two large, well-connected regions costs a small fraction of either side’s.
Algorithm
The recursive two-way algorithm of Shi and Malik (2000):
- Build a weighted graph with one node per pixel, connecting pixels within a radius , and form the affinity matrix and the diagonal degree matrix , .
- Solve for the eigenvectors with the smallest eigenvalues.
- Take the eigenvector with the second smallest eigenvalue and threshold it to split the graph in two, choosing the splitting point that minimizes the normalized cut.
- Check whether the split is stable and its normalized cut is below a preset maximum.
- If so, recursively partition each part.
Mathematical Formulation
Affinities. For brightness images Shi and Malik used the product of a feature-similarity term and a spatial-proximity term, set to zero beyond a radius :
where is the pixel’s position. This is the same pair of range and spatial Gaussians that weighs neighbors in the bilateral filter.
The criterion. For a partition of into and , with and ,
Defining the normalized association , and using , one gets : minimizing the dissimilarity between groups and maximizing the similarity within them are the same problem. The paper’s appendix gives a proof, due to Papadimitriou, that minimizing Ncut exactly is NP-complete even for graphs on regular grids, by reduction from the PARTITION problem.
Relaxation. Let indicate membership in , let and , and set , so that . Shi and Malik showed that
(The paper writes ; scaling does not change the quotient.) This is a Rayleigh quotient. Letting take real values, it is minimized by an eigenvector of the generalized system
equivalently of with . The smallest eigenvalue is 0, with ; the eigenvectors are -orthogonal, so the second smallest, , automatically satisfies and is the real-valued minimizer. It exactly minimizes under that constraint, pushing strongly connected pixels toward similar values, but need not take two values, hence the discretization step.
Spectral graph theory. is the graph Laplacian and Chung’s normalized Laplacian. Shi and Malik also related Ncut to the conductance of a random walk on the pixels. Replacing by the identity in the denominators gives the average cut, relaxed by the Fiedler vector of ; the paper argues that it lacks Ncut’s link to within-group association, and reports that Ncut segmented better.
Parameters
- sets how quickly affinity falls with feature difference. The paper says was typically set to 10–20 percent of the range of the feature distance; for an brightness image with intensities in it used .
- and set the spatial reach; the same example used and pixels. A larger captures longer-range grouping but makes denser.
- Splitting point. The split can be at 0, at the median, or at the Ncut-minimizing value; Shi and Malik searched evenly spaced candidates.
- Stopping and stability. Recursion stops when the best Ncut exceeds a threshold (0.04 and 0.08 in the paper’s examples), which thus controls the number of segments. Eigenvectors that vary smoothly rather than in steps are skipped: the paper histograms the eigenvector values and rejects it when the ratio of the smallest to the largest bin count exceeds 0.06.
Complexity
A full eigendecomposition costs for pixels. Because is sparse and only a few low-precision eigenvectors are needed, the paper uses the Lanczos method, which runs in for sparse matrix–vector products of each. The authors observed typically below , which by this count puts the solve at roughly . Randomly removing up to 90 percent of the connections in large neighborhoods did not change the eigenvectors, they report.
Implementation
The NumPy and SciPy code builds a noisy disk image and its sparse affinity matrix with the paper’s formula and parameters (, , ). It solves the generalized problem through the symmetric matrix , whose largest eigenvalues are , and splits the second eigenvector at the best of 30 evenly spaced thresholds. It then compares minimum cuts and segments a three-region image by k-means on three generalized eigenvectors.
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import eigsh
from scipy.sparse.csgraph import maximum_flow, breadth_first_order
from scipy.cluster.vq import kmeans2
from scipy.optimize import linear_sum_assignment
H = W = 30
yy, xx = np.mgrid[0:H, 0:W]
idx = np.arange(H * W).reshape(H, W)
def affinity(img, sigma_i=0.1, sigma_x=4.0, r=5):
"""Sparse W with Shi and Malik's weights: intensity term times distance term, zero beyond r."""
f = img.ravel()
rows, cols, vals = [], [], []
for dy in range(-r + 1, r):
for dx in range(-r + 1, r):
d2 = dy * dy + dx * dx
if d2 == 0 or d2 >= r * r:
continue
a = idx[max(0, -dy):H - max(0, dy), max(0, -dx):W - max(0, dx)].ravel()
b = idx[max(0, dy):H + min(0, dy), max(0, dx):W + min(0, dx)].ravel() # a shifted by (dy, dx)
rows.append(a); cols.append(b)
vals.append(np.exp(-(f[a] - f[b])**2 / sigma_i**2) * np.exp(-d2 / sigma_x**2))
n = H * W
return sp.csr_matrix((np.concatenate(vals), (np.concatenate(rows), np.concatenate(cols))), shape=(n, n))
def smallest_eigvecs(Wm, k):
"""First k solutions of (D - W) y = lambda D y, via the symmetric matrix D^-1/2 W D^-1/2."""
d = np.asarray(Wm.sum(axis=1)).ravel()
Dm = sp.diags(1 / np.sqrt(d))
mu, Z = eigsh(Dm @ Wm @ Dm, k=k, which="LA") # largest mu = 1 - lambda
order = np.argsort(-mu)
return 1 - mu[order], Z[:, order] / np.sqrt(d)[:, None], d
def cut(Wm, m):
return Wm[m][:, ~m].sum()
def ncut(Wm, d, m):
c = cut(Wm, m)
return c / d[m].sum() + c / d[~m].sum()
# 1. Two regions: a disk (0.65) on a background (0.35), Gaussian noise 0.05.
rng = np.random.default_rng(0)
gt = (yy - 15)**2 + (xx - 15)**2 <= 8**2
img = np.where(gt, 0.65, 0.35) + rng.normal(0, 0.05, (H, W))
Wm = affinity(img)
lam, Y, d = smallest_eigvecs(Wm, 2)
y = Y[:, 1] # second smallest generalized eigenvector
cands = np.linspace(y.min(), y.max(), 32)[1:-1] # 30 evenly spaced splitting points
scores = [ncut(Wm, d, y > t) for t in cands]
m = y > cands[int(np.argmin(scores))]
g = gt.ravel()
if (m & g).sum() < (~m & g).sum():
m = ~m # the eigenvector's sign is arbitrary
print(f"lambda_1 = {abs(lam[0]):.4f}, lambda_2 = {lam[1]:.4f}")
print(f"Ncut split: Ncut {min(scores):.4f}, IoU with the disk {(m & g).sum() / (m | g).sum():.3f}")
print(f"true partition: cut {cut(Wm, g):.2f}, Ncut {ncut(Wm, d, g):.4f}")
# 2. Minimum cut: no partition can cut less than the weakest pixel's degree.
i = int(np.argmin(d))
single = np.zeros(H * W, bool); single[i] = True
print(f"pixel {tuple(int(v) for v in np.unravel_index(i, (H, W)))} alone: cut {d[i]:.2f}, Ncut {ncut(Wm, d, single):.4f}")
# Minimum s-t cut between a pixel inside the disk and one in the background
# (max-flow needs integer capacities, so weights are scaled by 1e4).
s, t = idx[15, 15], idx[0, 0] # disk center, image corner
cap = sp.csr_matrix((np.round(Wm.data * 1e4).astype(np.int32), Wm.indices, Wm.indptr), shape=Wm.shape)
res = maximum_flow(cap, s, t)
resid = cap - res.flow # residual capacities
resid.data[resid.data < 0] = 0; resid.eliminate_zeros()
side = np.zeros(H * W, bool); side[breadth_first_order(resid, s, return_predecessors=False)] = True
print(f"min s-t cut: value {res.flow_value / 1e4:.2f}, source side {side.sum()} px, "
f"IoU with the disk {(side & g).sum() / (side | g).sum():.3f}")
# 3. Three regions, k-way: k-means on the first three generalized eigenvectors.
lab = np.zeros((H, W), int)
lab[(yy - 10)**2 + (xx - 10)**2 <= 6**2] = 1 # disk
lab[16:27, 14:27] = 2 # square
img3 = np.choose(lab, [0.2, 0.5, 0.8]) + rng.normal(0, 0.05, (H, W))
W3 = affinity(img3)
lam3, Y3, d3 = smallest_eigvecs(W3, 3)
print("three regions: lambda", np.round(np.abs(lam3), 4))
_, km = kmeans2(Y3, 3, minit="++", seed=1)
conf = np.array([[((km == a) & (lab.ravel() == b)).sum() / ((km == a) | (lab.ravel() == b)).sum()
for b in range(3)] for a in range(3)])
r_, c_ = linear_sum_assignment(-conf) # match clusters to regions
print("per-region IoU", np.round(conf[r_, c_][np.argsort(c_)], 3))
Output:
lambda_1 = 0.0000, lambda_2 = 0.0035
Ncut split: Ncut 0.0041, IoU with the disk 1.000
true partition: cut 12.94, Ncut 0.0041
pixel (15, 28) alone: cut 1.35, Ncut 1.0001
min s-t cut: value 10.33, source side 899 px, IoU with the disk 0.219
three regions: lambda [0. 0.0001 0.0055]
per-region IoU [0.998 0.991 1. ]
The thresholded eigenvector recovers the disk exactly (IoU 1.000, Ncut 0.0041). Since the relaxation minimizes the same quotient over a larger set, is a lower bound on the Ncut of any two-way partition. The minimum cut prefers something else. Cutting out the disk costs 12.94, but the background pixel at row 15, column 28, which noise pushed to 0.155, has a total connection of only 1.35, so the global minimum cut cannot be the disk boundary; isolating that pixel has Ncut 1.0001, about 240 times the disk’s. The – cut fares no better: the corner pixel has few neighbors within , so isolating it (10.33) is cheaper than cutting around the disk.
With three regions the generalized eigenvalues are 0, 0.0001 and 0.0055, and k-means on the three eigenvectors separates background, disk and square with per-region IoU of at least 0.991. Over noise seeds 0–9, the two-way IoU was 0.995–1.000 and the three-region IoUs at least 0.991; the weakest pixel always cut less than the disk boundary (by factors of 1.9 to 9.6), and the – cut isolated the corner in 8 of 10 runs.
Properties and Behavior
- No bias toward tiny segments. Isolating a single pixel costs an Ncut above 1, as the example shows.
- Global but approximate. The eigenvector depends on the whole graph, but the relaxation carries no general quality guarantee. Von Luxburg (2007) stresses this and reports Guattery and Miller’s “cockroach” graphs, on which unnormalized spectral clustering (the relaxation of RatioCut, see Variants) returns a RatioCut worse than the optimum by a factor proportional to the number of vertices.
- Higher eigenvectors. Later eigenvectors can subdivide the parts further, but the paper found them unreliable, because approximation error accumulates under the orthogonality constraint, and recommends recomputing on each part.
Limitations
- Affinity design. Results depend on the features and on , and ; the paper notes that combining conflicting cues, and weighting every region of a textured image well, is not trivial.
- Bias toward balanced partitions. Shi and Malik describe Ncut, like the average cut, as seeking a “balanced partition”. By our reasoning from the criterion, the normalization which removes the small-set bias also rewards splits with comparable total connection on each side, so a small but distinct object can lose to a split through a large, weakly varying region.
- Cost. Large images remain slow even with sparse Lanczos solvers, and the sparsity that makes them feasible limits affinities to short range.
Variants
- Simultaneous -way cuts. Shi and Malik also described clustering pixels by k-means on several eigenvectors into an oversegmentation, then greedily merging segments to minimize a -way Ncut, , or recursively cutting the condensed graph of segments; the paper’s results all use the recursive two-way version.
- Spectral clustering. Ng, Jordan and Weiss (2001) stacked the largest eigenvectors of , normalized each row to unit length, and ran k-means on the rows, with Gaussian affinities whose , in their experiments, was chosen as the value giving the tightest clusters. Von Luxburg (2007) shows that relaxing Ncut leads to the eigenvectors of the random-walk Laplacian , which are Shi and Malik’s generalized eigenvectors, while relaxing RatioCut, which measures each side by its number of vertices, leads to the unnormalized Laplacian . She advocates over both and .
- Contour and texture cues. Malik, Belongie, Leung and Shi (2001) combined intervening-contour and texton affinities, gated by local texturedness, oversegmented by k-means on eigenvectors 2–12, and finished with recursive two-way cuts on the graph of segments.
- Multiscale normalized cuts. Cour, Bénézit and Shi (2005) decomposed the graph into sparse affinity matrices at several scales linked by cross-scale constraints, capturing long-range connections with running time linear in the number of pixels.
- Spectral boundaries in gPb. Arbeláez et al. (2011) observed that k-means on the eigenvectors breaks up large uniform regions. Instead they take oriented derivatives of 16 generalized eigenvectors, weighted by , as a “spectral” boundary signal, sPb, combined with local cues in the gPb contour detector behind the gPb-owt-ucm hierarchy (see edge detection). In their boundary benchmark on BSDS500 (Table 1), multiscale normalized cuts reached an ODS F-measure of 0.64 against 0.73 for gPb-owt-ucm.
Graph cuts take the opposite route: they keep the minimum – cut, which can be computed exactly, and, for interactive segmentation, constrain it with user-supplied seeds and an energy with region and boundary terms.
Related
- Normalized Cuts and Image Segmentation
The 2000 IEEE TPAMI paper by Shi and Malik, first presented at CVPR 1997, that cast image segmentation as partitioning a weighted pixel graph, defined the normalized cut criterion, and approximated it with a generalized eigenvector, bringing spectral graph partitioning into computer vision.
- Graph Cuts
Energy minimization by minimum s–t cut, which labels the pixels of an image optimally for binary energies with submodular pairwise terms and approximately, through expansion and swap moves, for many labels.
- Superpixels
An oversegmentation of an image into a few hundred compact, roughly uniform regions that follow image boundaries, used in place of pixels to make later processing cheaper.
- Edge Detection
Locating the pixels where image intensity changes sharply, from derivative filters and the Canny detector to learned boundary detectors evaluated against human annotations.
- Bilateral Filter
An edge-preserving smoothing filter that averages each pixel with neighbors that are both nearby and similar in value, so noise and texture are removed while strong edges stay sharp.
- Least Squares
The method of fitting a model to more measurements than unknowns by minimizing the sum of squared residuals, the workhorse of estimation in computer vision.
References
- Shi, J. & Malik, J. (2000). Normalized Cuts and Image Segmentation. IEEE Transactions on Pattern Analysis and Machine Intelligence, 22(8), 888–905.
- Wu, Z. & Leahy, R. (1993). An Optimal Graph Theoretic Approach to Data Clustering: Theory and Its Application to Image Segmentation. IEEE Transactions on Pattern Analysis and Machine Intelligence, 15(11), 1101–1113.
- Ng, A. Y., Jordan, M. I. & Weiss, Y. (2001). On Spectral Clustering: Analysis and an Algorithm. Advances in Neural Information Processing Systems 14 (NIPS).
- von Luxburg, U. (2007). A Tutorial on Spectral Clustering. Statistics and Computing, 17(4), 395–416.
- Malik, J., Belongie, S., Leung, T. & Shi, J. (2001). Contour and Texture Analysis for Image Segmentation. International Journal of Computer Vision, 43(1), 7–27.
- Cour, T., Bénézit, F. & Shi, J. (2005). Spectral Segmentation with Multiscale Graph Decomposition. IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR), 2, 1124–1131.
- Arbeláez, P., Maire, M., Fowlkes, C. & Malik, J. (2011). Contour Detection and Hierarchical Image Segmentation. IEEE Transactions on Pattern Analysis and Machine Intelligence, 33(5), 898–916.