v9.1.1
Menu

Horn-Schunck Global Optical Flow

mediumPROOptical FlowCVCV: Motion Estimation

Implement Horn-Schunck optical flow with smoothness constraint to estimate the motion of pixels between two consecutive frames. This method is used to calculate the optical flow, which is the apparent motion of pixels in an image.

The Horn-Schunck method assumes that the brightness constancy of the scene is maintained between frames, which can be expressed as Ixu+Iyv+It=0I_x u + I_y v + I_t = 0, where IxI_x and IyI_y are the spatial gradients of the image intensity, ItI_t is the temporal gradient, and uu and vv are the horizontal and vertical components of the optical flow. To handle the aperture problem, a smoothness constraint weighted by α2\alpha^2 (where α\alpha is the alpha argument) is added to the energy function.

Here are the steps to calculate the optical flow:

  1. Compute the spatial and temporal gradients of the image intensity.
  2. Initialize the optical flow components uu and vv.
  3. Iterate to minimize the energy function.
E=∫∫(Ixu+Iyv+It)2+α2(∣∇u∣2+∣∇v∣2)dxdyE = \int\int (I_x u + I_y v + I_t)^2 + \alpha^2(|\nabla u|^2 + |\nabla v|^2) dx dy

This technique is widely used in video analysis and object tracking applications.

Example

Input
Two frames
Output
Dense optical flow
Reasoning
  • Inputs: img1, img2 are consecutive H×WH \times W grayscale frames; alpha weights smoothness, n_iter counts sweeps. The test uses two all-zero 16×1616 \times 16 frames, α=1\alpha = 1, 100 sweeps.
  • Gradients: IxI_x is the horizontal (left-to-right) brightness change and IyI_y the vertical (top-to-bottom) change in the first frame; It=I2−I1I_t = I_2 - I_1 is the change between frames. Here all three are 00.
  • Math: each pixel's (u,v)(u, v) trades the brightness-constancy error (Ixu+Iyv+It)2(I_x u + I_y v + I_t)^2 against α2\alpha^2 times its deviation from the neighbours' average. At zero flow both terms vanish, so u=v=0u = v = 0 after every sweep.
  • Output: dense optical flow, an H×W×2H \times W \times 2 array (channel 0 is uu, channel 1 is vv); the test prints (16, 16, 2).
  • Notice: flow is measurable only where there is texture. On a horizontal ramp (Iy=0I_y = 0, constant IxI_x) the flow settles at u=−It/Ixu = -I_t / I_x, v=0v = 0, and flat regions borrow flow from textured neighbours.

Constraints

  • Input frames: 2D numpy arrays (grayscale) with shape (H, W) and dtype float32
  • Pixel values: in range [0, 255] and assumed to be normalized to [0, 1] for computation
  • Output optical flow: 2D numpy array with shape (H, W, 2) and dtype float32, representing horizontal and vertical flow components
  • Smoothness parameter: α is a positive scalar, assumed to be provided as an input or set to a default value of 0.001
  • Iteration limit: maximum number of iterations to minimize the energy function, assumed to be set to a default value of 1000 or provided as an input

Learn this in

More from CV: Motion Estimation

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.