Optical Flow
Module: Computer Vision | Difficulty: Intermediate
Optical Flow Constraints
Brightness Constancy
First-order Taylor expansion gives the optical flow constraint:
Lucas-Kanade Method
Assume constant flow in a local window :
where
Horn-Schunck Regularization
End-Point Error (EPE)
import cv2
import numpy as np
def lucas_kanade_flow(prev_gray, curr_gray, features):
lk_params = dict(
winSize=(15, 15), maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS \| cv2.TERM_CRITERIA_COUNT, 10, 0.03)
)
next_pts, status, err = cv2.calcOpticalFlowPyrLK(
prev_gray, curr_gray, features, None, **lk_params
)
return next_pts, status, err
def dense_flow(prev_gray, curr_gray):
flow = cv2.calcOpticalFlowFarneback(
prev_gray, curr_gray, None,
pyr_scale=0.5, levels=3, winsize=15,
iterations=3, poly_n=5, poly_sigma=1.2, flags=0
)
return flow
Key Takeaways
- Optical flow estimates per-pixel motion between frames
- Lucas-Kanade is sparse and fast; Horn-Schunck is dense and smooth
- Deep flow networks (FlowNet, RAFT) achieve superior accuracy