CV Lab 22 · Module 9 — Motion & Video

Background Subtraction

At every pixel: is this the boring unchanging background, or something new standing in front of it? Free-play with four real algorithms, watch the exact per-pixel formula live under your cursor, export working OpenCV code, then prove you understand it in three graded challenges.

Learn the background, flag the rest Watch the scene long enough and a model of "normal" emerges per pixel. Anything that stops matching that model gets cut out as foreground.
Foreground (real motion) Shadow (correctly rejected) Ghost / false detection
Ready.
Live Explanation
Loading…
Threshold τ
20
Learning rate α
0.08

Hover the video for a live pixel-math tooltip: I_t, B_t, |I_t−B_t|, and the exact foreground/background verdict.

Background subtraction's classic bug: a car that was parked (and got absorbed into the background) suddenly drives off — leaving a ghost mask where it used to sit.
👻 Dekha? Wahi khali jagah "naya" lag rahi hai isliye foreground ban gayi. Is ghost ko gayab karne ke liye Learning Rate (α) thoda badhao — taaki background model us khali jagah ko jaldi "normal" maan le.
📋 Live OpenCV codeFrame Differencing

        

The one formula behind everything here

|I_t − B_t| > τ

Every method in this lab answers the same question differently: what should B_t (the background estimate) be? Differencing uses the last frame. Running average blends slowly. MOG2 and KNN each keep a richer memory per pixel.

Why ghosting happens

If a static object sits still long enough, alpha slowly folds it into B_t as "the new normal." The instant it moves away, the true background behind it looks different from B_t — briefly flagged as foreground, even though nothing new actually arrived.

Why exported code matters

Every slider here maps directly onto a real OpenCV parameter — varThreshold, learningRate, history. Tuning it visually first, then reading the generated code, is exactly how these parameters get chosen in a real pipeline.

Predict, then verify

In Level 2 (Swaying Tree), you keep the algorithm on Frame Differencing and just lower the threshold to try to clean things up. What happens?