AI Compass
Compass

Video and motion

From frames to sequences: frame differencing, optical flow and object tracking, and why counting over time is the real difficulty.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

A video is a stack of images. What is new is the question of connection: is the person on the left in frame two the same as in frame one? Until that is answered, nothing can be counted.

What it is good for

  • Counting people at an entrance without identifying anyone.
  • Measuring queue waiting times.
  • Checking movement on a conveyor.
  • Alerting on standstill or unusual motion.

Detecting motion without a model

import cv2

cap = cv2.VideoCapture("hall.mp4")
# MOG2 learns the background continuously and flags what stands out.
bg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=40,
                                        detectShadows=True)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))

while True:
    ok, frame = cap.read()
    if not ok:
        break
    mask = bg.apply(frame)
    mask[mask == 127] = 0                        # discard shadows
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
    moving = [c for c in contours if cv2.contourArea(c) > 1500]
    if moving:
        pass                                     # raise an event
cap.release()

This needs no GPU and no training data. For "is something moving here?" it is the right approach.

Alternating detection and tracking

StrategyComputeAccuracy
Detect every frame100 %Highest
Every fifth frame, track in between~25 %Nearly identical on calm scenes
Detect only on motionVery lowScene-dependent

Optical flow

Brightness constancy assumption

I(x, y, t) = I(x + u, y + v, t + 1) ⇒ Iₓ·u + I_y·v + I_t = 0

A point keeps its brightness as it moves, which yields an equation linking motion and gradient.

I(x,y,t)
the brightness at a point at time t
u, v
the displacement sought in x and y

One equation, two unknowns. This is the aperture problem: from a single point only the motion component perpendicular to the edge can be recovered. Lucas-Kanade resolves it by assuming the equation is constant over a small neighbourhood and solving the overdetermined system by least squares.

Evaluating tracking

The standard metric is MOTA, and it is stricter than it looks:

Multiple object tracking accuracy

MOTA = 1 − (FN + FP + IDSW) / GT

All three error types are summed and measured against the total number of true objects; the value can go negative.

FN
missed objects across all frames
FP
false alarms
IDSW
identity switches: a track gets a new number
GT
number of true objects across all frames

One identity switch per person per minute sounds harmless but doubles the counted headcount at a one-minute dwell time. For counting applications IDF1 is therefore the more informative metric, because it judges identity consistency across the whole track.

Data protection for counting applications

A plain count with no image storage can be lawful if processing happens in the device and only numbers leave it. Three conditions decide:

  • Images do not leave the camera and are not stored.
  • No re-identifiable features are retained beyond the session.
  • Recognising the same person on a different day is technically excluded.

As soon as recognition across days is possible, this is biometric processing and the assessment changes entirely. See Face recognition.

Related courses and sources

PaperFreeEN

You Only Look Once

Detection in a single pass instead of proposals and checks. The reason real-time object detection became possible.

For real-time object detection; explains why it became possible at all.

Was this page helpful?
Video and motion