Graph-cut segmentation

Image analysis

About this lecture

Two units ago: segmentation = minimise E(L) = \sum_p D_p(L_p) + \lambda \sum_{(p,q)} w_{pq}[L_p \ne L_q]. Last unit: minimum cuts by max-flow. Today we connect them (Boykov & Jolly, 2001).

  • Build a graph from an image so that cut cost = energy.
  • One max-flow gives the exact best segmentation.
  • Contrast-sensitive weights: boundaries are cheap at image edges.
  • Seeds: the user marks some object and background pixels.
  • What goes wrong: shrinking, blocky boundaries, a bad intensity model.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

The graph

One node per pixel, plus s (= object) and t (= background). A pixel on the source side gets L_p = 1.

  • t-links (to the terminals) carry the data term:
    • s \to p with capacity D_p(0): cut when p ends up on the background side;
    • p \to t with capacity D_p(1): cut when p ends up on the object side.
  • n-links between neighbours carry the smoothness term: p \to q and q \to p, each with capacity \lambda w_{pq}.

Every pixel loses exactly one of its two t-links in any cut: the one that matches its label’s cost.

A 3 \times 3 image as a graph. Node grey = pixel value. Bright pixels have strong links to s, dark ones to t; similar neighbours have strong n-links.

Cut cost = energy

Take any s–t cut and read off L_p = 1 for p \in S, L_p = 0 for p \in T.

  • Pixel p \in S: its edge p \to t crosses from S to T and costs D_p(1); s \to p stays inside S.
  • Pixel p \in T: s \to p crosses and costs D_p(0).
  • Neighbours with L_p = L_q: no n-link crosses. With L_p = 1, L_q = 0: only p \to q crosses, cost \lambda w_{pq}.

c(S, T) = \sum_p D_p(L_p) + \lambda \sum_{(p,q)} w_{pq}[L_p \ne L_q] = E(L).

Every labelling is a cut and every cut a labelling, at the same cost: minimum cut = minimum energy.

Graph-cut segmentation as pseudocode

nodes: one per pixel, plus s (object) and t (background)
for every pixel p:
    edge s → p with capacity D_p(0);   edge p → t with capacity D_p(1)
    for every neighbour q:   edge p → q with capacity λ · w_pq
flow = maximum flow from s to t                   # = the smallest energy
for every pixel p:
    L_p = 1 if p can still be reached from s, else 0
  • One maximum flow gives the exact minimum: no starting guess, no local minima.
  • Seeds and contrast weights only change capacities (later in this lecture).

A 1 \times 3 image by hand

I = (3, 1, 3), \mu_0 = 0, \mu_1 = 3, D_p(\ell) = (I_p - \mu_\ell)^2, w_{pq} = 1, \lambda = 1:

pixel 1 2 3
s \to p: D_p(0) 9 1 9
p \to t: D_p(1) 0 4 0

n-links 1 \leftrightarrow 2 and 2 \leftrightarrow 3, capacity \lambda = 1 each.

  • Cut \{s, 1, 3\} (L = 101): 0 + 1 + 0 (t-links) + 1 + 1 (n-links) = 3.
  • Cut \{s, 1, 2, 3\} (L = 111): 0 + 4 + 0 = 4.
  • With \lambda = 2: 101 costs 1 + 4 = 5, 111 still 4: the dark pixel is filled.

The graph for λ = 1 with its minimum cut (red, dashed). Blue nodes: source side, labelled object.

The math: which energies can be cut?

  • Capacities must be \ge 0. If D_p(0) or D_p(1) is negative, add the same constant to both: every labelling changes by the same amount.
  • The pairwise term must be submodular (Kolmogorov & Zabih, 2004): V(0,1) + V(1,0) \ge V(0,0) + V(1,1). The boundary term \lambda w_{pq}[L_p \ne L_q] is \lambda w_{pq} + \lambda w_{pq} \ge 0 + 0: fine.
  • Then the minimum is exact, global, in polynomial time.
  • Not covered: more than two labels. The Potts energy with 3 or more labels is NP-hard; α-expansion solves a sequence of two-label cuts and gets within a factor 2.

Contrast-sensitive weights

Make boundaries cheap where the image changes:

w_{pq} = \exp\!\left(-\frac{(I_p - I_q)^2}{2\sigma^2}\right)

  • Equal neighbours: w_{pq} = 1, a full boundary cost \lambda.
  • Very different neighbours: w_{pq} \approx 0, cutting there is almost free.
  • \sigma sets what counts as “very different”: about the size of the intensity jump at a real edge, larger than the noise.
  • \sigma = \infty gives w_{pq} = 1: the boundary length alone.

The 1\times 3 example with \sigma = 1: |I_1 - I_2| = 2, so w = e^{-2} = 0.135. Now 101 costs 1 + 2\lambda \cdot 0.135: thresholding wins up to \lambda = 11.1.

Right: for each pixel the smaller of its weights to the right and down neighbour. Dark = cheap to cut: along the nuclear envelope and the texture. Micrograph: Robert M. Hunt, CC BY 3.0, via Wikimedia Commons.

Try it: the graph of a small image

Seeds

The user paints a few object and background pixels (Boykov & Jolly, 2001):

  • Hard constraints: an object seed gets s \to p with capacity “\infty” (larger than any possible cut), so it can never be on the background side. Background seeds likewise with p \to t.
  • Intensity model from the seeds: fit a Gaussian, or take a histogram, of the seed pixels of each label; D_p(\ell) = -\log P(I_p \mid \ell).
  • Interactive: look at the result, add a seed where it is wrong, cut again. A new max-flow takes milliseconds at this size.

Thresholding with the same model (\lambda = 0) is the baseline to beat. A third way to use the same seeds, the random walker, and all three side by side (watershed as a spanning forest): the graph-based segmentation day.

The demo’s “nucleus” preset: one object stroke (red), five background strokes (blue), histogram model from the seed pixels, σ = 25. Right: the same model without smoothness. Micrograph: Robert M. Hunt, CC BY 3.0, via Wikimedia Commons.

Try it: interactive graph cut

Try it: graph cut on a micrograph

Failure modes

  • Shrinking bias: the boundary term grows with length. A thin structure has a long boundary for its area: its data gain \propto area, its boundary cost \propto length. A strip of width k survives only if its data gain per pixel exceeds about 2\lambda/k.
  • Metrication: with 4 neighbours a boundary costs its length in the “city-block” metric: a diagonal boundary costs \sqrt2 = 1.41 per unit length, a horizontal one 1. The cut prefers axis-aligned, blocky shapes. With 8 neighbours and weights \tfrac12 (sides) and \tfrac{1}{2\sqrt2} (diagonals), both cost \tfrac12 + \tfrac{1}{\sqrt2} = 1.21 per unit length.
  • Overlapping intensities: if object and background have the same grey levels, no data term can separate them; only seeds and image edges can.

The demo presets “thin arm lost”, “blocky” (4 and 8 neighbours) and “overlapping intensities” (background seeds only on white areas).

Beyond: GrabCut and more labels

  • GrabCut (Rother, Kolmogorov & Blake, 2004): colour images, the user draws only a box. Alternate: fit colour models (mixtures of Gaussians) to the current labelling, cut again, until it settles.
  • More than two labels (several tissues, several cells): α-expansion repeats binary cuts, “label \alpha or keep your label”, over all labels.
  • The same graph construction appears in stereo matching, image stitching and denoising: any energy with unary and submodular pairwise terms.

Summary

  • Graph: pixels + s (object) + t (background); t-links = data term, n-links = \lambda w_{pq}.
  • Cut cost = energy, so one max-flow gives the exact minimum of a submodular binary energy.
  • Contrast weights w_{pq} = e^{-(I_p - I_q)^2/2\sigma^2} pull the boundary onto image edges.
  • Seeds: hard constraints by infinite t-links, and the intensity model.
  • Watch for shrinking bias, metrication (use 8 neighbours) and overlapping intensity models.