Skip to main content
The Video Analysis module provides algorithms for motion analysis, object tracking, and background/foreground segmentation in video streams.

Overview

From opencv2/video.hpp:47-52:
This module contains algorithms for motion analysis, object tracking, and background subtraction. It enables applications such as motion detection, object following, and foreground extraction from video sequences.

Optical Flow

Dense and sparse motion estimation between frames

Object Tracking

Track objects using MeanShift and CamShift

Background Subtraction

Separate foreground from background

Motion Analysis

Analyze motion patterns in video

Module Components

From opencv2/video.hpp, the module includes:
  • opencv2/video/tracking.hpp - Optical flow and tracking
  • opencv2/video/background_segm.hpp - Background subtraction

Optical Flow

Optical flow estimates the motion of pixels between consecutive frames.

Lucas-Kanade Sparse Optical Flow

From tracking.hpp:134-186, tracks sparse feature points:

Dense Optical Flow (Farneback)

From tracking.hpp:188-200, computes flow for every pixel:

Optical Flow Flags

From tracking.hpp:59-62:

Object Tracking

MeanShift Tracking

From tracking.hpp:88-107, finds object center:

CamShift Tracking

From tracking.hpp:64-86, adaptive tracking with rotation:

Background Subtraction

From background_segm.hpp:55-97, separate foreground from background:

BackgroundSubtractor Base Class

MOG2 Background Subtractor

From background_segm.hpp:100-150, Gaussian Mixture Model:

KNN Background Subtractor

Extracting Foreground Objects

Kalman Filter

Predict and track object positions:

Practical Examples

Motion Detection System

People Counter

Best Practices

Optical Flow:
  • Use sparse flow (Lucas-Kanade) when you need specific point tracking
  • Use dense flow (Farneback) for motion field visualization
  • Redetect features periodically to maintain good tracks
Background Subtraction:
  • MOG2 is generally more robust than KNN
  • Adjust learning rate based on scene dynamics
  • Use morphological operations to clean up masks
  • Filter detections by minimum area to reduce noise
Performance:

Source Reference

Key headers:
  • ~/workspace/source/modules/video/include/opencv2/video/tracking.hpp
  • ~/workspace/source/modules/video/include/opencv2/video/background_segm.hpp
Examples:
  • samples/cpp/lkdemo.cpp - Lucas-Kanade optical flow
  • samples/cpp/camshiftdemo.cpp - CamShift tracking
  • samples/cpp/bgfg_segm.cpp - Background subtraction