0
0
Computer-visionConceptBeginner · 3 min read

Optical Flow in OpenCV: What It Is and How It Works

In computer vision, optical flow is a technique to estimate the motion of objects between two video frames. OpenCV provides functions to calculate optical flow, helping track movement by analyzing pixel changes over time.
⚙️

How It Works

Imagine watching a video and trying to follow how a ball moves from one frame to the next. Optical flow works like your eyes tracking that ball by looking at how pixels shift between two images taken moments apart.

OpenCV calculates this movement by comparing small patches of pixels in the first frame to patches in the second frame. It finds the direction and speed each patch moves, creating a map of motion vectors. This helps computers understand how objects or the camera itself is moving.

💻

Example

This example uses OpenCV's Lucas-Kanade method to track points moving between two frames of a video or camera feed.

python
import cv2
import numpy as np

cap = cv2.VideoCapture(0)  # Open webcam

# Take first frame and find corners to track
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)

# Detect good features to track
p0 = cv2.goodFeaturesToTrack(old_gray, maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)

# Parameters for Lucas-Kanade optical flow
lk_params = dict(winSize=(15, 15), maxLevel=2,
                 criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

while True:
    ret, frame = cap.read()
    if not ret:
        break
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Calculate optical flow
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

    # Select good points
    if p1 is not None:
        good_new = p1[st == 1]
        good_old = p0[st == 1]

        # Draw tracks
        for i, (new, old) in enumerate(zip(good_new, good_old)):
            a, b = new.ravel()
            c, d = old.ravel()
            frame = cv2.line(frame, (int(c), int(d)), (int(a), int(b)), (0, 255, 0), 2)
            frame = cv2.circle(frame, (int(a), int(b)), 5, (0, 0, 255), -1)

    cv2.imshow('Optical Flow Tracking', frame)

    # Update previous frame and points
    old_gray = frame_gray.copy()
    p0 = good_new.reshape(-1, 1, 2)

    if cv2.waitKey(30) & 0xFF == 27:  # Press ESC to exit
        break

cap.release()
cv2.destroyAllWindows()
Output
A window opens showing live video from the webcam with green lines and red dots tracking moving points.
🎯

When to Use

Use optical flow when you want to track motion in videos or live camera feeds without detecting objects explicitly. It helps in applications like:

  • Tracking moving objects in surveillance cameras
  • Estimating vehicle speed or direction in traffic monitoring
  • Stabilizing shaky videos by understanding camera movement
  • Gesture recognition by following hand movements

It is especially useful when you want to understand motion patterns quickly and efficiently.

Key Points

  • Optical flow estimates motion by comparing pixel changes between frames.
  • OpenCV offers easy-to-use functions like calcOpticalFlowPyrLK for tracking points.
  • It works best with small movements and good lighting conditions.
  • Useful for motion tracking, video stabilization, and gesture recognition.

Key Takeaways

Optical flow tracks motion by analyzing pixel shifts between video frames.
OpenCV's Lucas-Kanade method is a popular way to compute optical flow for point tracking.
It is useful for applications like object tracking, video stabilization, and gesture recognition.
Good lighting and small frame-to-frame movement improve optical flow accuracy.
Optical flow provides motion information without needing to detect objects explicitly.