Skip to content

Background Subtractor

btsimonh edited this page Nov 11, 2017 · 3 revisions

(these notes refer to background subtractor modifications pushed into master on 11 Nov 2017. To use them you may need to install directly from the github branch: "npm install peterbraden/node-opencv")

Accesses Opencv's implementation of MOG, MOG2 or GMG background subtraction algorithms.

The algorithm(s) available are dependent upon how OpenCV was compiled, and which version.

For 2.4, only MOG is avaiable.

For 3 MOG2 is availble by default. To get MOG or GMG, youe OpenCV must be compiled with opencv-contrib.

Basic creation:

var bg = new cv.BackgroundSubtractor();

This creates a backgroundsubtractor object. The default creates a MOG object if available, else a MOG2 if OpenCV3 without opencv-contrib.

Specific types may be created with:

var bgMOG = cv.BackgroundSubtractor.createMOG();

var bgMOG2 = cv.BackgroundSubtractor.createMOG2();

var bgGMG = cv.BackgroundSubtractor.createGMG();

Note: in Opencv2, ALL of these will return a MOG object.

In OpenCV3, if opencv-contrib was not part of the build trying to createMOG or createGMG will fail with an exception.

The background subtractor created will store information about the background, built as frames are sent in.

Passing frames in

Frames may be passed in Synchronously or Asynchronously through bg.apply

Synchronous:

var matout = bg.apply(matin);

Asynchronous:

bg.apply(matin, function(err, matout){ });

using the results

The output matout is a monochrome image where objects not considered to be background are represented by bright areas.

Typically, you could:

var contours = matout.findContours(cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);

to determine where these objects are.

Mog options:

Options for MOG can be accessed using

var history = bg.history();

bg.history(newhist);

var mixtures = bg.mixtures();

bg.mixtures(newmixtures);

var sigma = bg.noiseSigma();

bg.noiseSigma(newsigma);

var ratio = bg.backgroundRatio();

bg.backgroundRatio(newratio);

Note that MOG options these are largely untested. For more info, look at BackgroundSubtractor.cc

Memory

Note that when working with continuous video streams, you may find the notes in 'Memory Management' useful, especially if you have a constrained memory environment.