Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/types/opencv/BackgroundSubtractor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Algorithm, bool, double, InputArray, OutputArray } from "./_types";

/**
* Base class for background/foreground segmentation algorithms.
*
* The class is only used to define the common interface for the whole family of background/foreground
* segmentation algorithms.
*
* Source:
* [opencv2/video.hpp](https://github.com/opencv/opencv/tree/master/modules/video/include/opencv2/video/background_segm.hpp).
*/
export declare class BackgroundSubtractor extends Algorithm {
public constructor();

/**
* Computes a foreground mask.
*
* @param image Next video frame.
* @param fgmask The output foreground mask as an 8-bit binary image.
* @param learningRate The value between 0 and 1 that indicates how fast the background model is learnt.
* Negative parameter value makes the algorithm 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.
*/
public apply(image: InputArray, fgmask: OutputArray, learningRate?: double): void;

/**
* Computes a background image.
*
* @param backgroundImage The output background image.
*
* @note Sometimes the background image can be very blurry, as it contain the average background
* statistics.
*/
public getBackgroundImage(backgroundImage: OutputArray): void;
}
22 changes: 22 additions & 0 deletions src/types/opencv/BackgroundSubtractorMOG2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { BackgroundSubtractor, bool, double, int } from "./_types";

/**
* Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
*
* The class implements the Gaussian mixture model background subtraction described in [Zivkovic2004]
* and [Zivkovic2006].
*
* Source:
* [opencv2/video.hpp](https://github.com/opencv/opencv/tree/master/modules/video/include/opencv2/video/background_segm.hpp).
*/
export declare class BackgroundSubtractorMOG2 extends BackgroundSubtractor {
/**
* @param history Length of the history.
* @param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
* to decide whether a pixel is well described by the background model. This parameter does not
* affect the background update.
* @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
* speed a bit, so if you do not need this feature, set the parameter to false.
*/
public constructor(history?: int, varThreshold?: double, detectShadows?: bool);
}
2 changes: 2 additions & 0 deletions src/types/opencv/_types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from "./Affine3";
export * from "./Algorithm";
export * from "./AutoBuffer";
export * from "./BackgroundSubtractor";
export * from "./BackgroundSubtractorMOG2";
export * from "./BFMatcher";
export * from "./BOWTrainer";
export * from "./calib3d";
Expand Down
72 changes: 72 additions & 0 deletions test/BackgroundSubtractorMOG2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { setupOpenCv } from "./cv";

beforeAll(async () => {
await setupOpenCv();
});

describe("BackgroundSubtractorMOG2", () => {
it("should have correct TypeScript definitions for constructor", () => {
// Test constructor without parameters
const bs1 = new cv.BackgroundSubtractorMOG2();
expect(bs1).toBeDefined();
bs1.delete();

// Test constructor with history parameter
const bs2 = new cv.BackgroundSubtractorMOG2(500);
expect(bs2).toBeDefined();
bs2.delete();

// Test constructor with history and varThreshold
const bs3 = new cv.BackgroundSubtractorMOG2(500, 16);
expect(bs3).toBeDefined();
bs3.delete();

// Test constructor with all parameters
const bs4 = new cv.BackgroundSubtractorMOG2(500, 16, true);
expect(bs4).toBeDefined();
bs4.delete();
});

it("should have correct TypeScript definitions for inherited methods", () => {
const bs = new cv.BackgroundSubtractorMOG2();

// Test inherited methods from BackgroundSubtractor
expect(typeof bs.apply).toBe("function");
expect(typeof bs.getBackgroundImage).toBe("function");

// Test apply method with a real Mat
const testImage = new cv.Mat(100, 100, cv.CV_8UC3);
const fgMask = new cv.Mat();

// This should not throw TypeScript errors
bs.apply(testImage, fgMask);
bs.apply(testImage, fgMask, 0.1); // with learning rate

// Test getBackgroundImage method
const bgImage = new cv.Mat();
bs.getBackgroundImage(bgImage);

// Clean up
testImage.delete();
fgMask.delete();
bgImage.delete();
bs.delete();
});

it("should work in TypeScript usage scenarios from the issue", () => {
// This test verifies the original issue is resolved
// These should compile without TypeScript errors

// Test the main usage pattern mentioned in the issue
const backgroundSubtractor = new cv.BackgroundSubtractorMOG2();
expect(backgroundSubtractor).toBeDefined();

// Test with parameters
const backgroundSubtractorWithParams = new cv.BackgroundSubtractorMOG2(500, 16, true);
expect(backgroundSubtractorWithParams).toBeDefined();

// Clean up
backgroundSubtractor.delete();
backgroundSubtractorWithParams.delete();
});
});