MOG2 background subtraction
Learn a mixture of colour distributions at each pixel and flag observations that do not fit the background.
The object has moved away from its earlier location.
Previous object locations show why a temporal model needs multiple observations.
Compare the current sample with learned mixture components and their support.
The moving region is retained in this ideal example. Startup, shadows and camera motion complicate real masks.
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
Experiment at pixel level
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.
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
Select a pixel
Select a pixel
When to use it
Detect moving regions in a mostly stationary-camera sequence.
How it works
- 01Compare the current pixel with its learned mixture components.
- 02Update component weights and statistics.
- 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.
Related methods
Lucas-Kanade optical flow
Follow selected points by finding the small displacement that best aligns each local patch.
Farneback dense optical flow
Estimate a displacement vector at every pixel using local polynomial approximations.
DIS dense optical flow
Combine fast patch alignment with spatial propagation and dense refinement.
Dual TV-L1 optical flow
Estimate flow with a robust data term and a total-variation smoothness penalty.