Computer Vision · Advanced level

Advanced level · Module 7: Video Analytics · Lesson 2 of 3

Background Subtraction and Line-Crossing Counts

Learners detect moving objects with background subtraction and count objects crossing a line with a detector and tracker, comparing counts with manual counts.

In this lesson you will

  • Detect motion with background subtraction
  • Count line crossings and measure count error

Key concepts

cv2.createBackgroundSubtractorMOG2foreground maskscounting linetrack directioncount error

The lesson

For a fixed camera, background subtraction (cv2.createBackgroundSubtractorMOG2) learns the static scene and marks moving pixels as foreground. It is cheap and needs no training, but shadows, swaying trees and lighting changes cause false motion.

For reliable counts by class, combine a detector with a tracker from Module 4: an object is counted when its track crosses a virtual line, and the direction of crossing tells which way it moved.

Evaluate by counting manually for several short periods and computing the error. Report counts with their measured error, not as exact numbers.

Step by step

  1. Apply background subtraction and clean the mask with morphology.
  2. Count moving blobs crossing a line.
  3. Repeat with detector-plus-tracker counting by class.
  4. Compare both with manual counts for three one-minute clips.

Practice activity

Build two counters and report their errors against manual counts.

Expected result: Working counters with measured errors and a recommendation.

Check your understanding

  1. What problems affect background subtraction?

    Show answer

    Shadows, moving vegetation and lighting changes.

  2. How is direction determined in line-crossing counts?

    Show answer

    From the direction in which a track crosses the line.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.