Following the slope of the error
We look for the displacement that moves a window of pixels in frame onto the same content in frame , by minimizing the sum of squared differences
A full search evaluates at every shift on a grid, which is expensive and only as accurate as the grid. Lucas and Kanade instead start from an initial guess , often zero or the motion of the previous frame, and improve it step by step using the image gradient. Shifts between pixels are no problem, since can be interpolated anywhere.
Linearizing the image
Close to the current estimate , the shifted image is approximated by its first-order Taylor expansion, with the image gradient :
with the current errors . This is linear in the unknown update . For , is the temporal derivative , and setting each term to zero gives the optical flow constraint : one equation per pixel for two unknowns. A single pixel can only tell the motion along its gradient, which is why Lucas–Kanade assumes the same motion for the whole window.
A 2 × 2 least-squares problem
Stacking the gradients of all window pixels as the rows of a matrix and the negated errors as a vector turns the approximation into , a least-squares problem like line fitting:
Its minimum solves the normal equations
is the second moment matrix of the autocorrelation surface and the Harris detector, and the dashed ellipse on the error surface is the contour of the linearized error through the current estimate. Its center is where the linear model expects the minimum, at .
Iterating
Because the image is not linear, the model's minimum is only an approximation. Lucas–Kanade moves there, or a fraction of the way, linearizes again and repeats,
until the update is smaller than a tolerance, here px. This is the Gauss–Newton method. Near the solution, full steps () make the distance to the truth drop steeply on the logarithmic plot. A smaller is more cautious and shrinks the distance by roughly a constant factor per step, while overshoots and zigzags across the minimum, and too large a step never settles.
The gradient has to be interpolated and rebuilt in every step. Since at the solution, the gradient is a good substitute near it, and it can be computed once, together with and its inverse.
When it fails
- Aperture problem: on a straight edge all gradients point the same way, has one eigenvalue near zero, and only the motion across the edge is determined. In a flat region both eigenvalues vanish. The windows that track well are the ones a corner detector would pick.
- Large motion: the linearization only holds over roughly the size of the image structures. From a start farther away, the steps can point the wrong way and end in a different local minimum. Smoothing both frames widens the basin of convergence at the cost of detail, which is the idea behind estimating the motion coarse to fine on an image pyramid.
- Changed appearance: the method assumes that a point keeps its brightness (brightness constancy). Noise, lighting changes and occlusion leave errors that no shift can explain.
Try this
- Press Play from u₀ with the defaults: after 7 steps the estimate is within 0.02 px of the true shift (2.6, −1.8), far below the grid of a full search. The small rest comes from interpolation: was made by resampling , which the smoothing does not undo exactly. With , the estimate ends at (2.600, −1.800).
- Click on the error surface at about (−4, 4): the path follows the slope down to the minimum. Start in a corner, e.g. (−7, −7) or (7, 7): the steps run off to a wrong minimum outside the surface shown. Raise the pre-smoothing to 3 and try the same starts again.
- Set the step size to 0.3: every step covers only part of the way, and the distance falls along a nearly straight line on the log plot. At 1.3 the path zigzags across the minimum and needs about 25 steps; at 1.5 it no longer settles.
- Set the window radius to 4 and select a window on a straight edge, such as the right wall of the house below its window: , the dashed ellipse becomes long and thin, and the estimate stops somewhere along the dark valley. Only the motion across the edge is right. Click the flat sky: is singular and there is no step at all.
- Set to 0 and start at (−4, 4): without smoothing, the surface has flat stretches with tiny gradients, and the estimate crawls along and runs out of iterations.
- Switch the gradient to : from it converges as before. From (−4, 4) it gets lost, since only describes near the solution.
- Add noise : the error image stays grainy and cannot reach zero, but the estimate still ends within a few hundredths of a pixel, since the window averages the noise over all its pixels.