Skip to content

Commit d484c8c

Browse files
Copilotttt43ttt
andauthored
Add TypeScript definitions for BackgroundSubtractorMOG2 and BackgroundSubtractor (#87)
* Initial plan * Add TypeScript definitions for BackgroundSubtractor and BackgroundSubtractorMOG2 Co-authored-by: ttt43ttt <[email protected]> * Final validation and cleanup for BackgroundSubtractorMOG2 TypeScript support Co-authored-by: ttt43ttt <[email protected]> * Remove redundant typescript-validation.ts file Co-authored-by: ttt43ttt <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: ttt43ttt <[email protected]>
1 parent 4ac46f5 commit d484c8c

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { Algorithm, bool, double, InputArray, OutputArray } from "./_types";
2+
3+
/**
4+
* Base class for background/foreground segmentation algorithms.
5+
*
6+
* The class is only used to define the common interface for the whole family of background/foreground
7+
* segmentation algorithms.
8+
*
9+
* Source:
10+
* [opencv2/video.hpp](https://github.com/opencv/opencv/tree/master/modules/video/include/opencv2/video/background_segm.hpp).
11+
*/
12+
export declare class BackgroundSubtractor extends Algorithm {
13+
public constructor();
14+
15+
/**
16+
* Computes a foreground mask.
17+
*
18+
* @param image Next video frame.
19+
* @param fgmask The output foreground mask as an 8-bit binary image.
20+
* @param learningRate The value between 0 and 1 that indicates how fast the background model is learnt.
21+
* Negative parameter value makes the algorithm use some automatically chosen learning rate.
22+
* 0 means that the background model is not updated at all, 1 means that the background model is
23+
* completely reinitialized from the last frame.
24+
*/
25+
public apply(image: InputArray, fgmask: OutputArray, learningRate?: double): void;
26+
27+
/**
28+
* Computes a background image.
29+
*
30+
* @param backgroundImage The output background image.
31+
*
32+
* @note Sometimes the background image can be very blurry, as it contain the average background
33+
* statistics.
34+
*/
35+
public getBackgroundImage(backgroundImage: OutputArray): void;
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { BackgroundSubtractor, bool, double, int } from "./_types";
2+
3+
/**
4+
* Gaussian Mixture-based Background/Foreground Segmentation Algorithm.
5+
*
6+
* The class implements the Gaussian mixture model background subtraction described in [Zivkovic2004]
7+
* and [Zivkovic2006].
8+
*
9+
* Source:
10+
* [opencv2/video.hpp](https://github.com/opencv/opencv/tree/master/modules/video/include/opencv2/video/background_segm.hpp).
11+
*/
12+
export declare class BackgroundSubtractorMOG2 extends BackgroundSubtractor {
13+
/**
14+
* @param history Length of the history.
15+
* @param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
16+
* to decide whether a pixel is well described by the background model. This parameter does not
17+
* affect the background update.
18+
* @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
19+
* speed a bit, so if you do not need this feature, set the parameter to false.
20+
*/
21+
public constructor(history?: int, varThreshold?: double, detectShadows?: bool);
22+
}

src/types/opencv/_types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export * from "./Affine3";
22
export * from "./Algorithm";
33
export * from "./AutoBuffer";
4+
export * from "./BackgroundSubtractor";
5+
export * from "./BackgroundSubtractorMOG2";
46
export * from "./BFMatcher";
57
export * from "./BOWTrainer";
68
export * from "./calib3d";
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { setupOpenCv } from "./cv";
2+
3+
beforeAll(async () => {
4+
await setupOpenCv();
5+
});
6+
7+
describe("BackgroundSubtractorMOG2", () => {
8+
it("should have correct TypeScript definitions for constructor", () => {
9+
// Test constructor without parameters
10+
const bs1 = new cv.BackgroundSubtractorMOG2();
11+
expect(bs1).toBeDefined();
12+
bs1.delete();
13+
14+
// Test constructor with history parameter
15+
const bs2 = new cv.BackgroundSubtractorMOG2(500);
16+
expect(bs2).toBeDefined();
17+
bs2.delete();
18+
19+
// Test constructor with history and varThreshold
20+
const bs3 = new cv.BackgroundSubtractorMOG2(500, 16);
21+
expect(bs3).toBeDefined();
22+
bs3.delete();
23+
24+
// Test constructor with all parameters
25+
const bs4 = new cv.BackgroundSubtractorMOG2(500, 16, true);
26+
expect(bs4).toBeDefined();
27+
bs4.delete();
28+
});
29+
30+
it("should have correct TypeScript definitions for inherited methods", () => {
31+
const bs = new cv.BackgroundSubtractorMOG2();
32+
33+
// Test inherited methods from BackgroundSubtractor
34+
expect(typeof bs.apply).toBe("function");
35+
expect(typeof bs.getBackgroundImage).toBe("function");
36+
37+
// Test apply method with a real Mat
38+
const testImage = new cv.Mat(100, 100, cv.CV_8UC3);
39+
const fgMask = new cv.Mat();
40+
41+
// This should not throw TypeScript errors
42+
bs.apply(testImage, fgMask);
43+
bs.apply(testImage, fgMask, 0.1); // with learning rate
44+
45+
// Test getBackgroundImage method
46+
const bgImage = new cv.Mat();
47+
bs.getBackgroundImage(bgImage);
48+
49+
// Clean up
50+
testImage.delete();
51+
fgMask.delete();
52+
bgImage.delete();
53+
bs.delete();
54+
});
55+
56+
it("should work in TypeScript usage scenarios from the issue", () => {
57+
// This test verifies the original issue is resolved
58+
// These should compile without TypeScript errors
59+
60+
// Test the main usage pattern mentioned in the issue
61+
const backgroundSubtractor = new cv.BackgroundSubtractorMOG2();
62+
expect(backgroundSubtractor).toBeDefined();
63+
64+
// Test with parameters
65+
const backgroundSubtractorWithParams = new cv.BackgroundSubtractorMOG2(500, 16, true);
66+
expect(backgroundSubtractorWithParams).toBeDefined();
67+
68+
// Clean up
69+
backgroundSubtractor.delete();
70+
backgroundSubtractorWithParams.delete();
71+
});
72+
});

0 commit comments

Comments
 (0)