Lucas–Kanade step by step

Instead of trying every shift, Lucas–Kanade starts from a guess and asks the image gradients which way to go. Each step solves a small least-squares problem and moves the window closer to the true motion, down to a fraction of a pixel. Step through the iterations, change the step size and the starting point, and see where the method converges and where it fails.

Frame I0I_0 click to select the window
Frame I1I_1 estimates u0,u1,…\mathbf{u}_0, \mathbf{u}_1, \dots true u∗\mathbf{u}^* (×) click to start there
Error surface E(u)E(\mathbf{u}), ±8\pm 8 px linearized error at uk\mathbf{u}_k true u∗\mathbf{u}^* (×) click to start there
Window at step kk gray error = 0
I0(xi)I_0(\mathbf{x}_i)
I1(xi+uk)I_1(\mathbf{x}_i + \mathbf{u}_k)
error eie_i
Convergence over the iterations kk distance to the truth ∣uk−u∗∣|\mathbf{u}_k - \mathbf{u}^*| step length ∣α Δuk∣|\alpha\, \Delta\mathbf{u}_k| in pixels

Following the slope of the error

We look for the displacement u=(u,v)\mathbf{u} = (u, v) that moves a window of pixels xi\mathbf{x}_i in frame I0I_0 onto the same content in frame I1I_1, by minimizing the sum of squared differences

E(u)=∑i[I1(xi+u)−I0(xi)]2=∑iei2.E(\mathbf{u}) = \sum_i \left[ I_1(\mathbf{x}_i + \mathbf{u}) - I_0(\mathbf{x}_i) \right]^2 = \sum_i e_i^2 .

A full search evaluates EE 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 u0\mathbf{u}_0, 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 I1I_1 can be interpolated anywhere.

Linearizing the image

Close to the current estimate u\mathbf{u}, the shifted image is approximated by its first-order Taylor expansion, with the image gradient J1=∇I1=(∂I1/∂x,∂I1/∂y)\mathbf{J}_1 = \nabla I_1 = (\partial I_1 / \partial x, \partial I_1 / \partial y):

E(u+Δu)=∑i[I1(xi+u+Δu)−I0(xi)]2≈∑i[J1(xi+u) Δu+ei]2E(\mathbf{u} + \Delta\mathbf{u}) = \sum_i \left[ I_1(\mathbf{x}_i + \mathbf{u} + \Delta\mathbf{u}) - I_0(\mathbf{x}_i) \right]^2 \approx \sum_i \left[ \mathbf{J}_1(\mathbf{x}_i + \mathbf{u})\, \Delta\mathbf{u} + e_i \right]^2

with the current errors ei=I1(xi+u)−I0(xi)e_i = I_1(\mathbf{x}_i + \mathbf{u}) - I_0(\mathbf{x}_i). This is linear in the unknown update Δu\Delta\mathbf{u}. For u=0\mathbf{u} = 0, eie_i is the temporal derivative ItI_t, and setting each term to zero gives the optical flow constraint Ixu+Iyv+It=0I_x u + I_y v + I_t = 0: 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 NN window pixels as the rows of a matrix AA and the negated errors as a vector b\mathbf{b} turns the approximation into ∥A Δu−b∥2\| A\, \Delta\mathbf{u} - \mathbf{b} \|^2, a least-squares problem like line fitting:

A=[Ix(x1+u)Iy(x1+u)⋮⋮Ix(xN+u)Iy(xN+u)],b=−[e1⋮eN]A = \begin{bmatrix} I_x(\mathbf{x}_1 + \mathbf{u}) & I_y(\mathbf{x}_1 + \mathbf{u}) \\ \vdots & \vdots \\ I_x(\mathbf{x}_N + \mathbf{u}) & I_y(\mathbf{x}_N + \mathbf{u}) \end{bmatrix}, \qquad \mathbf{b} = - \begin{bmatrix} e_1 \\ \vdots \\ e_N \end{bmatrix}

Its minimum solves the normal equations

A⊤A Δu=A⊤b,[∑Ix2∑IxIy∑IxIy∑Iy2][ΔuΔv]=−[∑Ixei∑Iyei].A^\top A\, \Delta\mathbf{u} = A^\top \mathbf{b}, \qquad \begin{bmatrix} \sum I_x^2 & \sum I_x I_y \\ \sum I_x I_y & \sum I_y^2 \end{bmatrix} \begin{bmatrix} \Delta u \\ \Delta v \end{bmatrix} = - \begin{bmatrix} \sum I_x e_i \\ \sum I_y e_i \end{bmatrix} .

A⊤AA^\top A is the second moment matrix MM 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 u+Δu\mathbf{u} + \Delta\mathbf{u}.

Iterating

Because the image is not linear, the model's minimum is only an approximation. Lucas–Kanade moves there, or a fraction α\alpha of the way, linearizes again and repeats,

uk+1=uk+α Δuk,\mathbf{u}_{k+1} = \mathbf{u}_k + \alpha\, \Delta\mathbf{u}_k ,

until the update is smaller than a tolerance, here 0.0010.001 px. This is the Gauss–Newton method. Near the solution, full steps (α=1\alpha = 1) make the distance to the truth drop steeply on the logarithmic plot. A smaller α\alpha is more cautious and shrinks the distance by roughly a constant factor per step, while α>1\alpha > 1 overshoots and zigzags across the minimum, and too large a step never settles.

The gradient J1(xi+u)\mathbf{J}_1(\mathbf{x}_i + \mathbf{u}) has to be interpolated and A⊤AA^\top A rebuilt in every step. Since I1(xi+u∗)=I0(xi)I_1(\mathbf{x}_i + \mathbf{u}^*) = I_0(\mathbf{x}_i) at the solution, the gradient J0(xi)=∇I0(xi)\mathbf{J}_0(\mathbf{x}_i) = \nabla I_0(\mathbf{x}_i) is a good substitute near it, and it can be computed once, together with A⊤AA^\top A and its inverse.

When it fails

Try this