Lucas–Kanade: local least squares, the structure tensor, and pyramids

Image analysis

About this lecture

Last unit: each pixel gives one constraint line in velocity space; one line is not enough (the aperture problem). Today, the classic local answer (Lucas and Kanade, 1981).

  • Assume the flow is constant in a small window; take the velocity closest to all the window’s lines.
  • Its 2\times2 matrix is the structure tensor of the edges day: flat, edge and corner tell us where flow can be measured.
  • The window size trade-off.
  • Large motion breaks the linearisation: warping and coarse-to-fine pyramids fix it.
  • Block matching for comparison, and tracking points as a pointer.

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

The idea: one velocity per window

Assume all pixels in a small window move together. Each pixel contributes a line; take the point that is closest to all lines (least squares).

  • Flat window: no lines, no answer.
  • Edge: all lines parallel; only the motion across the edge.
  • Corner or texture: lines in many directions cross at one point: the motion.

Recall: the structure tensor

From the structure tensor unit: collect the gradients (I_x, I_y) of a window; their spread is the 2\times2 matrix J = \begin{pmatrix}\overline{I_x^2} & \overline{I_x I_y}\\ \overline{I_x I_y} & \overline{I_y^2}\end{pmatrix} (averages over the window, weighted by a Gaussian of width ρ), drawn as an ellipse.

Its eigenvalues \mu_1 \ge \mu_2 \ge 0 classify the window:

\mu_1 \mu_2 window
small small flat
large small edge (one direction)
large large corner or texture

Scatter plot of the gradients in three windows: a flat window gives a dot at the origin; an edge gives points along one line and a thin ellipse; a texture gives a cloud in all directions and a round ellipse. Labels: flat mu1 0, mu2 0; edge mu1 201, mu2 0; texture mu1 212, mu2 140.

The Lucas–Kanade matrix is the structure tensor

The least-squares problem over the window has exactly this matrix J. So the edges day’s map already says where flow can be measured:

  • corner/texture (both eigenvalues large): reliable vector;
  • edge (\mu_2 small): only the normal component, the aperture problem again;
  • flat (both small): nothing.

Use \mu_2 as a confidence and mask the flow where it is small. Points with large \mu_2 are the Shi–Tomasi corners: “good features to track”.

Four panels of the micrograph moved by 1.5 pixels: frame 1; the confidence map mu2, dark along membranes and in flat areas; the Lucas-Kanade flow in the colour coding; and the same flow greyed out where mu2 is below 20.

Lucas–Kanade as pseudocode

Ix, Iy, It at every pixel
for every pixel (x, y):
    J = Σ over the window  w · ( Ix²   Ix·Iy )
                               ( Ix·Iy  Iy²  )
    b = Σ over the window  w · ( Ix·It, Iy·It )
    μ2 = smaller eigenvalue of J               # the confidence
    (u, v) = −J⁻¹ b   where μ2 is large enough; elsewhere no answer
  • With Gaussian weights w, each Σ is a blur of a product image: five blurs for the whole image.
  • Large motion: repeat with frame 2 warped back by the estimate, coarse to fine (later in this lecture).

The math: Lucas–Kanade

Minimise over \mathbf u = (u, v), with window weights w (a Gaussian of width ρ): E(\mathbf u) = \sum_{\mathbf x \in W} w(\mathbf x)\,\big(I_x u + I_y v + I_t\big)^2 . Setting \partial E/\partial u = \partial E/\partial v = 0 gives the normal equations \underbrace{\begin{pmatrix}\sum wI_x^2 & \sum wI_xI_y\\ \sum wI_xI_y & \sum wI_y^2\end{pmatrix}}_{J}\begin{pmatrix}u\\v\end{pmatrix} = -\underbrace{\begin{pmatrix}\sum wI_xI_t\\ \sum wI_yI_t\end{pmatrix}}_{\mathbf b} .

  • With Gaussian weights every sum is a blur of a product: J and \mathbf b for all pixels at once are five Gaussian blurs (I_x^2, I_xI_y, I_y^2, I_xI_t, I_yI_t), then a 2\times2 solve per pixel.
  • Solvable iff \mu_2 > 0. Noise in \mathbf b is amplified by up to 1/\mu_2 along the weak eigenvector: the condition number \mu_1/\mu_2 says how ill-posed the window is.
  • Where \mu_2 \approx 0, the minimum-norm solution keeps only the strong direction: the normal flow.

By hand: four pixels

pixel I_x I_y I_t
1, 2 1 0 −2
3, 4 0 1 −1

J = \begin{pmatrix}2 & 0\\0 & 2\end{pmatrix}, -\mathbf b = (4, 2), so (u, v) = (2, 1): the two vertical and two horizontal lines cross there.

If all four pixels were (1, 0, -2): J = \begin{pmatrix}4 & 0\\0 & 0\end{pmatrix}, singular. u = 2, v unknown: a vertical edge.

Two velocity-space plots. Top: a vertical line u = 2 and a horizontal line v = 1 cross at (2, 1). Bottom: only the vertical line u = 2; v is unknown along it.

Try it: Lucas–Kanade on a frame pair

Window size

The window radius ρ is the same trade-off as the integration scale on the edges day (structure tensor):

  • small window: few pixels, noisy estimate, more windows that see only an edge (aperture);
  • large window: averages noise away, but mixes different motions: at a motion boundary (a cell crawling over a still background) it reports a blend.

In the demo (noise 12): mean endpoint error 0.35 px at ρ = 1.5, 0.18 at ρ = 3, 0.09 at ρ = 6. At a motion boundary the error grows with ρ instead.

Mean endpoint error against the window radius rho. With noise 12 on the micrograph the error falls: 0.35 at rho 1.5, 0.18 at 3, 0.09 at 6. Within 6 pixels of a moving cell's border the error rises from 0.58 to 0.76.

Large motion fails

The linearisation I_t \approx -I_x u - I_y v holds only while the motion is small compared with the smoothness of the image: about a pixel at the scale of the gradients.

In the demo (micrograph, one solve): mean error 0.08 px for a 1 px shift, 0.36 px for 2 px, 2.4 px for 4 px and 7.45 px for 8 px: no better than guessing zero.

Two cures, usually used together:

  1. Iterate with warping: estimate, warp frame 2 back by the estimate (images unit: inverse mapping and bilinear interpolation), estimate the remaining small motion, add, repeat.
  2. Coarse to fine: on a Gaussian pyramid (scale space unit: blur, then keep every second pixel) an 8 px motion is 4 px at level 1, 2 px at level 2, 1 px at level 3.

Mean endpoint error against the shift of the micrograph. One solve: 0.08 at 1 pixel, 0.36 at 2, 2.4 at 4 and 7.45 at 8, close to the error of guessing zero. The pyramid with 4 levels and 3 iterations stays below 0.6 pixels up to 8.

Coarse to fine on a pyramid

  1. Estimate at the coarsest level, where the motion is about a pixel.
  2. Go one level finer: double the flow (pixels are half as large), warp frame 2 by it, estimate the small remainder, add.
  3. Repeat down to full resolution.

Price: coarse levels blur away small objects; a small cell moving differently from its surroundings may be lost before the fine levels can see it.

The math: iterative and pyramidal LK

Iterative LK is Gauss–Newton on the nonlinear least-squares problem \min_{\mathbf u}\;\sum_{\mathbf x \in W} w(\mathbf x)\,\big(I_2(\mathbf x + \mathbf u) - I_1(\mathbf x)\big)^2 . Linearise around the current estimate \mathbf u_k: with I_2^{(k)}(\mathbf x) = I_2(\mathbf x + \mathbf u_k) (frame 2 warped back) and I_t^{(k)} = I_2^{(k)} - I_1, J\,\Delta\mathbf u = -\mathbf b(\mathbf u_k),\qquad \mathbf u_{k+1} = \mathbf u_k + \Delta\mathbf u .

  • At pyramid level \ell the motion is \mathbf u / 2^\ell; initialise level \ell - 1 with 2\,\mathbf u_\ell (upsampled).
  • Each level only needs to find a correction of about a pixel, which the linearisation can.
  • For a quadratic image I(\mathbf x) = \tfrac12\mathbf x^\top A\mathbf x + \mathbf g^\top\mathbf x translated by \mathbf d, one step is exact (exercise).

Try it: the pyramid rescues large motion

Block matching for comparison

Another way to find local motion: for each window, try shifts and keep the best score (correlation, as in FFT registration, but per window). Particle image velocimetry (PIV) does exactly this with phase correlation.

block matching Lucas–Kanade
large motion fine (search range) needs pyramids
precision integer, unless a peak is fitted sub-pixel by construction
cost many shifts per window five blurs + a 2\times2 solve
needs texture or particles texture, smooth image

Pointer: tracking points

Run Lucas–Kanade only at the Shi–Tomasi corners (largest \mu_2) and follow them from frame to frame: feature tracking. Chaining the vectors over many frames gives tracks. For large motions, match keypoints instead (matching descriptors).

Following whole objects (cells, nuclei) over time, linking them between frames, handling divisions and lineages: the tracking day (day 26).

Summary

  • Lucas–Kanade: the flow is constant in a window; least squares over the window’s constraint lines.
  • Its matrix is the structure tensor J: the smaller eigenvalue \mu_2 is the confidence (flat: nothing; edge: normal flow only; corner/texture: reliable).
  • Window size: noise and aperture (small) against mixing motions (large).
  • Linearisation needs small motion: iterate with warping and go coarse to fine on a pyramid.
  • Block matching handles large motion but is integer and slow; LK is sub-pixel and fast.