Skip to content

BackgroundSubtractorMOG2

Motion and trackingclassOpenCV 5.0.0
import { BackgroundSubtractorMOG2 } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

Native object: release it with using or delete(). Factories can return null; check before calling methods. Inherits BackgroundSubtractor.

Gaussian Mixture-based Background/Foreground Segmentation Algorithm.

The class implements the Gaussian mixture model background subtraction described in [Zivkovic2004] and [Zivkovic2006] .

Constructors and members

clone

Create another handle to the same native object. This retains the object without copying its pixels or algorithm state; dispose both handles separately.

clone(): this;
Returns

The this result.

getHistory

Returns the number of last frames that affect the background model

getHistory(): number;
Returns

The number result.

setHistory

Sets the number of last frames that affect the background model

setHistory(history: number): void;
history

history argument (number).

getNMixtures

Returns the number of gaussian components in the background model

getNMixtures(): number;
Returns

The number result.

setNMixtures

Sets the number of gaussian components in the background model.

The model needs to be reinitialized to reserve memory.
setNMixtures(nmixtures: number): void;
nmixtures

nmixtures argument (number).

getBackgroundRatio

needs reinitialization! Returns the "background ratio" parameter of the algorithm

If a foreground pixel keeps semi-constant value for about backgroundRatio\*history frames, it's
considered background and added to the model as a center of a new component. It corresponds to TB
parameter in the paper.
getBackgroundRatio(): number;
Returns

The number result.

setBackgroundRatio

Sets the "background ratio" parameter of the algorithm

setBackgroundRatio(ratio: number): void;
ratio

ratio argument (number).

getVarThreshold

Returns the variance threshold for the pixel-model match

The main threshold on the squared Mahalanobis distance to decide if the sample is well described by
the background model or not. Related to Cthr from the paper.
getVarThreshold(): number;
Returns

The number result.

setVarThreshold

Sets the variance threshold for the pixel-model match

setVarThreshold(varThreshold: number): void;
varThreshold

var threshold argument (number).

getVarThresholdGen

Returns the variance threshold for the pixel-model match used for new mixture component generation

Threshold for the squared Mahalanobis distance that helps decide when a sample is close to the
existing components (corresponds to Tg in the paper). If a pixel is not close to any component, it
is considered foreground or added as a new component. 3 sigma =\> Tg=3\*3=9 is default. A smaller Tg
value generates more components. A higher Tg value may result in a small number of components but
they can grow too large.
getVarThresholdGen(): number;
Returns

The number result.

setVarThresholdGen

Sets the variance threshold for the pixel-model match used for new mixture component generation

setVarThresholdGen(varThresholdGen: number): void;
varThresholdGen

var threshold gen argument (number).

getVarInit

Returns the initial variance of each gaussian component

getVarInit(): number;
Returns

The number result.

setVarInit

Sets the initial variance of each gaussian component

setVarInit(varInit: number): void;
varInit

var init argument (number).

getVarMin

Return the var min configured on this BackgroundSubtractorMOG2 object.

getVarMin(): number;
Returns

The number result.

setVarMin

Set the var min used by this BackgroundSubtractorMOG2 object.

setVarMin(varMin: number): void;
varMin

var min argument (number).

getVarMax

Return the var max configured on this BackgroundSubtractorMOG2 object.

getVarMax(): number;
Returns

The number result.

setVarMax

Set the var max used by this BackgroundSubtractorMOG2 object.

setVarMax(varMax: number): void;
varMax

var max argument (number).

getComplexityReductionThreshold

Returns the complexity reduction threshold

This parameter defines the number of samples needed to accept to prove the component exists. CT=0.05
is a default value for all the samples. By setting CT=0 you get an algorithm very similar to the
standard Stauffer&Grimson algorithm.
getComplexityReductionThreshold(): number;
Returns

The number result.

setComplexityReductionThreshold

Sets the complexity reduction threshold

setComplexityReductionThreshold(ct: number): void;
ct

ct argument (number).

getDetectShadows

Returns the shadow detection flag

If true, the algorithm detects shadows and marks them. See createBackgroundSubtractorMOG2 for
details.
getDetectShadows(): boolean;
Returns

The boolean result.

setDetectShadows

Enables or disables shadow detection

setDetectShadows(detectShadows: boolean): void;
detectShadows

detect shadows argument (boolean).

getShadowValue

Returns the shadow value

Shadow value is the value used to mark shadows in the foreground mask. Default value is 127. Value 0
in the mask always means background, 255 means foreground.
getShadowValue(): number;
Returns

The number result.

setShadowValue

Sets the shadow value

setShadowValue(value: number): void;
value

value argument (number).

getShadowThreshold

Returns the shadow threshold

A shadow is detected if pixel is a darker version of the background. The shadow threshold (Tau in
the paper) is a threshold defining how much darker the shadow can be. Tau= 0.5 means that if a pixel
is more than twice darker then it is not shadow. See Prati, Mikic, Trivedi and Cucchiara,

Detecting Moving Shadows...*, IEEE PAMI,2003.

getShadowThreshold(): number;
Returns

The number result.

setShadowThreshold

Sets the shadow threshold

setShadowThreshold(threshold: number): void;
threshold

threshold argument (number).

apply

Computes a foreground mask.

apply(image: Mat, fgmask: Mat, learningRate: number): void;
2 available overloads
apply(image: Mat, fgmask: Mat): void;
apply(image: Mat, fgmask: Mat, learningRate: number): void;
image

Next video frame. Floating point frame will be used without scaling and should be in range [0,255].

fgmask

Output destination, filled by the native operation. The output foreground mask as an 8-bit binary image.

learningRate

The value between 0 and 1 that indicates how fast the background model is learnt. Negative parameter value makes the algorithm to use some automatically chosen learning rate. 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame.

apply1

Computes a foreground mask and skips known foreground in evaluation.

apply1(image: Mat, knownForegroundMask: Mat, fgmask: Mat, learningRate: number): void;
2 available overloads
apply1(image: Mat, knownForegroundMask: Mat, fgmask: Mat): void;
apply1(image: Mat, knownForegroundMask: Mat, fgmask: Mat, learningRate: number): void;
image

Next video frame. Floating point frame will be used without scaling and should be in range [0,255].

knownForegroundMask

The mask for inputting already known foreground, allows model to ignore pixels.

fgmask

Output destination, filled by the native operation. The output foreground mask as an 8-bit binary image.

learningRate

The value between 0 and 1 that indicates how fast the background model is learnt. Negative parameter value makes the algorithm to use some automatically chosen learning rate. 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.