Skip to content

BackgroundSubtractorKNN

Motion and trackingclassOpenCV 5.0.0
import { BackgroundSubtractorKNN } 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.

K-nearest neighbours - based Background/Foreground Segmentation Algorithm.

The class implements the K-nearest neighbours background subtraction described in [Zivkovic2006] . Very efficient if number of foreground pixels is low.

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).

getNSamples

Returns the number of data samples in the background model

getNSamples(): number;
Returns

The number result.

setNSamples

Sets the number of data samples in the background model.

The model needs to be reinitialized to reserve memory.
setNSamples(_nN: number): void;
_nN

n n argument (number).

getDist2Threshold

needs reinitialization! Returns the threshold on the squared distance between the pixel and the sample

The threshold on the squared distance between the pixel and the sample to decide whether a pixel is
close to a data sample.
getDist2Threshold(): number;
Returns

The number result.

setDist2Threshold

Sets the threshold on the squared distance

setDist2Threshold(_dist2Threshold: number): void;
_dist2Threshold

dist2 threshold argument (number).

getkNNSamples

Returns the number of neighbours, the k in the kNN.

K is the number of samples that need to be within dist2Threshold in order to decide that that
pixel is matching the kNN background model.
getkNNSamples(): number;
Returns

The number result.

setkNNSamples

Sets the k in the kNN. How many nearest neighbours need to match.

setkNNSamples(_nkNN: number): void;
_nkNN

nk nn argument (number).

getDetectShadows

Returns the shadow detection flag

If true, the algorithm detects shadows and marks them. See createBackgroundSubtractorKNN 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).

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.