Skip to content

Optical flow: where did each pixel go?

Optical flow gives every pixel a displacement between two frames. Learn how it is scored (endpoint error), the assumption underneath it, and three learned estimators: FlowNet, PWC-Net and RAFT.

Advanced ~15 min10 steps

What runs in CodeSplash

  • VID029FlowNet Runs as a notebook
  • VID030PWC-Net Explained here
  • VID031RAFT Needs a data type CodeSplash doesn't load yet

You will be able to

  • Define optical flow and the brightness-constancy assumption behind it
  • Compute endpoint error and say why it isn't pixel error
  • Explain warping, cost volumes and coarse-to-fine search in PWC-Net
  • Describe RAFT's all-pairs correlation and repeated updates, and what more iterations don't guarantee

Read first

01

A displacement for every pixel

Read

Optical flowOptical flowFor each pixel, how far it moved between two frames, as a horizontal and a vertical shift in pixels. assigns each pixel pp of one frame a shift up=(Δx,Δy)u_p = (\Delta x, \Delta y) in pixels: where that point appears in the next frame. Most estimators rest on brightness constancyBrightness constancyThe assumption that a point keeps its brightness as it moves, so matching brightness finds the motion., the idea that a point keeps its brightness as it moves:

I(x+Δx, y+Δy, t+1)≈I(x,y,t).I(x + \Delta x,\, y + \Delta y,\, t + 1) \approx I(x, y, t).

Flow is movement in the image, not speed in the world. A far car and a near bicycle can have the same flow. Shiny surfaces, shadows, occlusion and textureless walls all break the assumption.

02

Scoring a flow field

Read

The standard score is endpoint errorEndpoint errorThe straight-line distance between a predicted and a true flow vector, in pixels, averaged over pixels.: the straight-line distance between the predicted and the true shift at each pixel, averaged:

EPE=1N∑p∥u^p−up∥2.\mathrm{EPE} = \frac{1}{N} \sum_p \lVert \hat u_p - u_p \rVert_2 .

In the reference's example, three points truly move two pixels right. One prediction is exact; one moves only one pixel; one adds a pixel of vertical motion. The per-point errors are 0, 1 and 1 pixels, a mean of 0.667. That is a distance in pixels, not the squared difference of pixel brightness.

03

FlowNet: learn the mapping

Read
Reference · VID029 · Runs as a notebookFlowNetPredicts dense optical flow between two images with a convolutional encoder-decoder. FlowNet established a direct learned mapping from image pairs to per-pixel displacement, useful for motion measurement and as input to…

FlowNet feeds two frames to a convolutional encoder–decoder and trains it to output the flow field directly, with endpoint error at several output scales as the loss:

L=∑sλs∑p∥u^p(s)−up(s)∥2.L = \sum_s \lambda_s \sum_p \lVert \hat u^{(s)}_p - u^{(s)}_p \rVert_2 .

True flow is almost never measured in real video, so training sets are mostly synthetic: rendered scenes where every shift is known. That makes domain shift the first question for any flow model on your footage.

04

Warp, compare, refine

Read
Reference · VID030 · Explained herePWC-NetUses image pyramids, warping and a local cost volume to estimate optical flow from coarse to fine. These geometric operations encode useful motion structure and reduce the search problem at each resolution.

PWC-Net works coarse to fineCoarse-to-fine estimationEstimate motion on a small copy of the image, then upsample it and refine it at each larger scale.. At the smallest scale it estimates rough flow; at each larger scale it upsamples that flow, warpsWarpingMove the pixels of one frame along a flow field to predict another frame; the leftover difference shows the error. the second frame's features by it, and builds a small cost volumeCost volumeA table of how similar each feature in one frame is to features at nearby, or all, positions in the next frame.: how well each feature matches features a few pixels around the warped spot.

In the reference's toy, the coarse flow says two pixels; the local match scores for offsets of minus one, zero and plus one are 0.0, 0.4 and 1.0, so the refined shift is 3.

05

When the first guess is wrong

Predict

Predict first

At the coarsest level, PWC-Net's guess is off by much more than the small search window used at the next level. What happens?

The next level fixes itThe true match falls outside the search window and the error staysWarping removes the error
Reveal

The error stays. Each level only searches a few pixels around the warped guess. If the true match is outside that window, no level can find it. Large, fast motion is where coarse-to-fine methods fail, so test large displacements on purpose.

06

RAFT: all pairs, many small steps

Read
Reference · VID031 · Needs a data type CodeSplash doesn't load yetRAFTComputes all-pairs feature correlations and repeatedly updates a dense flow field using a recurrent refinement operator.

RAFT compares every feature in frame one with every feature in frame two, C(p,q)=f1(p)⊤f2(q)C(p, q) = f_1(p)^\top f_2(q), and pools that table at several scales. A recurrent unit then updates one full-resolution flow field again and again, each time looking up the correlations around the current estimate:

uk+1=uk+Δθ(uk,C,hk).u^{k+1} = u^k + \Delta_\theta(u^k, C, h^k).

The reference's toy replaces the learned update with a fixed half-step toward the best match. After three steps the flow is 0.875, 0.875 and −1.75: seven-eighths of the way. More iterations converge to the model's answer, not to the truth.

07

Where the errors are

Read

An average endpoint error hides where the mistakes sit. Report it separately for:

  • occluded and visible pixels: points that disappear have no true match;
  • small and large motions;
  • the resolution you actually run at.

A clip where the whole frame shifts together is easy. Objects moving independently, rotation, transparency and lighting changes are the real test.

DExplorable · Optical flow: Lucas–Kanade, warping and endpoint error

How can a computer tell how far each part of an image moved between two frames?

With this picture and motion, what will Lucas–Kanade find?

How sure?

Picture

Many small blobs (rich texture), diagonal stripes, or two broad smooth blobs.

1.5

How far the picture moves to the right between the frames.

−1.0

How far the picture moves down between the frames.

0.0

Turns the picture about its center, so the motion differs from place to place.

1.00

Scales the picture about its center: things move outward when it grows.

3

Lucas–Kanade assumes one motion inside a square window this many pixels around each point.

3

Each step warps the second frame by the current estimate and solves for the remaining motion.

0.01

Random noise added to the second frame.

Frame 1Frame 2 with motion (arrows drawn 2× long)0.210No warp0.017Warped by the estimate

Mean endpoint error 0.14 px. 4% of the windows see the pattern change in one direction only.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

08

What runs here

Read
EntryIn CodeSplash
VID029 · FlowNetRuns as a notebook (the reference program; no method)
VID030 · PWC-NetExplained here
VID031 · RAFTNeeds a data type CodeSplash doesn't load yet (video)
09

Check: optical flow

Checkpoint

Check yourself

What does an optical-flow field give you?

Show the answer

Answer: b

Flow is displacement in the image plane. Turning it into real-world speed needs depth and camera geometry.

Check yourself

A flow model's endpoint error is reported as a mean over pixels. What is it?

Show the answer

Answer: a

EPE compares displacement vectors. A warped-brightness error is a different, photometric quantity.

Check yourself

Why is domain shift a central worry for supervised flow models like FlowNet?

Show the answer

Answer: b

Rendered training data has perfect labels and a different look from your footage. Test on data like yours.

Check yourself

At each pyramid level, what does PWC-Net compare?

Show the answer

Answer: b

Warping by the upsampled flow, then a local cost volume, keeps each level's search small.

Check yourself

Which test is most likely to expose a coarse-to-fine flow model's weakness?

Show the answer

Answer: b

A wrong coarse guess can't be repaired by a small local search. Report errors by motion size.

Check yourself

Running RAFT's update for more iterations makes its flow converge. Does that make it physically correct?

Show the answer

Answer: b

Iteration refines the model's answer. Ambiguous regions stay ambiguous; report iterations, memory and errors by region.

Check yourself

What does RAFT's all-pairs correlation cost as images grow?

Show the answer

Answer: a

Every feature is compared with every other, so large images need pooling, cropping or a lot of memory.

Check yourself

A cloud passes over the scene between two frames. Which assumption does that break?

Show the answer

Answer: b

Brightness-based matching can mistake a lighting change for motion, or miss motion hidden by it.

10

Recap

Recap
  • Optical flow is per-pixel displacement in the image, resting on brightness constancy.
  • Endpoint error is a distance between flow vectors; report it by region and motion size.
  • FlowNet learns the mapping directly, mostly from synthetic data.
  • PWC-Net warps and searches locally, coarse to fine; RAFT correlates all pairs and refines many times.

Concepts in this lesson