PIXELBANKv9.1.0
Menu

Lucas-Kanade Optical Flow

You are given image derivatives for a window of pixels and need to compute the Lucas-Kanade optical flow.

Lucas-Kanade assumes constant flow within a local window, creating an overdetermined system:

(Ix1Iy1Ix2Iy2⋮⋮)(uv)=−(It1It2⋮)\begin{pmatrix} I_{x1} & I_{y1} \\ I_{x2} & I_{y2} \\ \vdots & \vdots \end{pmatrix} \begin{pmatrix} u \\ v \end{pmatrix} = -\begin{pmatrix} I_{t1} \\ I_{t2} \\ \vdots \end{pmatrix}

Or in matrix form: Av=bA\mathbf{v} = \mathbf{b} where v=(u,v)T\mathbf{v} = (u, v)^T

Solve using least squares (normal equations): ATAv=ATbA^T A \mathbf{v} = A^T \mathbf{b}

The 2×2 system ATAA^T A can be solved using Cramer's rule or matrix inversion.

Example:

Input:
window = [(1, 0, -1), (0, 1, -1), (1, 1, -2)]
Output:
(1.0, 1.0)
Reasoning:

Building the normal equations:

  • A = [[1, 0], b = [-(-1)] = [1] [0, 1], [-(-1)] [1] [1, 1]] [-(-2)] [2]

A^T A = [[1²+0²+1², 1×0+0×1+1×1], = [[2, 1], [0×1+1×0+1×1, 0²+1²+1²]] [1, 2]]

A^T b = [1×1 + 0×1 + 1×2] = [3] [0×1 + 1×1 + 1×2] [3]

Solving [2,1; 1,2] × [u,v]^T = [3,3]:

  • det = 2×2 - 1×1 = 3
  • u = (2×3 - 1×3) / 3 = 3/3 = 1.0
  • v = (2×3 - 1×3) / 3 = 3/3 = 1.0

Result: (1.0, 1.0)

Constraints:

  • window: list of (Ix, Iy, It) tuples for each pixel in window
  • Return flow (u, v) rounded to 4 decimal places
  • If the system is singular (det ≈ 0), return (0.0, 0.0)
solution.py

Test Results

0/0
Run code to see test results.
Lucas-Kanade Optical Flow - Hard | PixelBank