Skip to content

MOG2 background subtraction

Learn a mixture of colour distributions at each pixel and flag observations that do not fit the background.

Try it on your image ↓

VISUAL WALKTHROUGHMotion
REFERENCE INPUTCurrent frame

The object has moved away from its earlier location.

RESULTForeground mask

The moving region is retained in this ideal example. Startup, shadows and camera motion complicate real masks.

STEP 03 / 03

Classify the observation as background, foreground or a possible shadow.

Illustrative example. The stages explain the method; they are not a live OpenCV execution.

Try it on an image

YOUR IMAGE · REAL OPENCV

Experiment at pixel level

Open full lab ↗

Learn the first image as a static background, then classify the second image. This is a two-frame experiment, not a video history.

The engine loads on your first run. Your images stay in this browser.

Input448 × 320
OutputWaiting for a result

Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.

Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Hover to inspect. Click to pin a pixel.
Input
Select a pixel

Output
Select a pixel

Sample models and licenses

When to use it

Detect moving regions in a mostly stationary-camera sequence.

How it works

  1. 01Compare the current pixel with its learned mixture components.
  2. 02Update component weights and statistics.
  3. 03Classify the observation as background, foreground or a possible shadow.

pixel history → mixture model → foreground mask

What to tune

history and learningRate control adaptation; variance thresholds control classification; shadow detection adds a separate mask label.

Where it breaks down

Camera motion changes the meaning of each pixel. Long-stationary foreground can be absorbed into the background.

TypeScript API

Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.

All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.