Computer Vision/Optical Flow
8 / 67

03/2024Computer Vision

Optical Flow

Tracking apparent motion through a video — Lucas-Kanade and its inverse-compositional refinements, derived from the brightness-constancy equation down to a 2×2 linear solve.

╌╌╌╌

Optical flow is the apparent motion of the scene across a video, whether the objects move or the camera does. This project builds three trackers for it from first principles: a translation-only Lucas-Kanade solver (LucasKanade), a six-parameter affine version (LucasKanadeAffine), and its inverse-compositional counterpart (InverseCompositionAffine, the Matthews-Baker formulation). All three sample the image and its gradients at subpixel locations with a RectBivariateSpline, and iterate up to a hundred times or until the parameter update drops below tolerance.

All three rest on one equation. Assume a pixel keeps its brightness as it moves; expanding to first order gives the brightness-constancy constraint

one equation in two unknowns per pixel — the aperture problem: motion along an edge is invisible. Lucas-Kanade resolves it by assuming the flow is constant over a small window and solving all of the window's constraints at once, in least squares:

The window around a tracked corner keeps its intensity pattern — brightness constancy — as it drifts across the frames; dashed squares mark where the patch sat before, and each arrow is one frame-to-frame flow . Lucas-Kanade recovers each displacement by solving the window's stacked gradient constraints in least squares, and tracking chains the solves.

The translation tracker reduces to a solve per window, and the matrix is exactly the Harris corner matrix, which is why corners are the pixels worth tracking: the system is well-conditioned precisely where the image has structure in two directions. The affine version widens the warp to six parameters, so each pixel's Jacobian is the block and the per-step update comes from the pseudo-inverse of the Hessian.

Algorithm:Lucas-Kanade(I,J,p)\textsc{Lucas-Kanade}(I, J, p) — iterative flow for one window
  1. 1
    input: frames I,JI, J, window center pp, initial flow w0w \gets 0
  2. 2
    repeat
  3. 3
    warp JJ by current flow ww around pp
  4. 4
    bb \gets residuals IJwarpedI - J_{warped} over the window
  5. 5
    solve (AA)Δ=Ab(A^\top A)\, \Delta = A^\top b for the update Δ\Delta
  6. 6
    ww+Δw \gets w + \Delta
  7. 7
    until Δ\lVert \Delta \rVert is below tolerance
  8. 8
    return ww

The classical affine loop re-evaluates the image gradient and rebuilds the Hessian on the warped frame every iteration. The inverse-compositional trick (Matthews-Baker) swaps template and image so the gradient, Jacobian, and Hessian depend only on the template: InverseCompositionAffine computes them once before the loop, and each iteration solves against the precomputed pseudo-inverse, then folds the incremental warp in by composing with its inverse, . The iteration reaches the same fixed point at a fraction of the cost. The write-up with derivations and results is available as a PDF report.

References

  1. Project repository
  2. Reference notes: Linear Algebra

╌╌ END ╌╌