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 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.
- 1input: frames , window center , initial flow
- 2repeat
- 3warp by current flow around
- 4residuals over the window
- 5solve for the update
- 6
- 7until is below tolerance
- 8return
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
- Project repository
- Reference notes: Linear Algebra
╌╌ END ╌╌