From dc97583fd5d91841ec56d056090170ff2fc7a14b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 5 Sep 2025 03:19:45 +0000 Subject: [PATCH 1/4] Initial plan From dcca7aeb73d36b394b236f67eefe8fb7a31f066d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 5 Sep 2025 03:30:22 +0000 Subject: [PATCH 2/4] Add TypeScript definitions for BackgroundSubtractor and BackgroundSubtractorMOG2 Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com> --- src/types/opencv/BackgroundSubtractor.ts | 36 ++++++++++ src/types/opencv/BackgroundSubtractorMOG2.ts | 22 ++++++ src/types/opencv/_types.ts | 2 + test/BackgroundSubtractorMOG2.test.ts | 72 ++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 src/types/opencv/BackgroundSubtractor.ts create mode 100644 src/types/opencv/BackgroundSubtractorMOG2.ts create mode 100644 test/BackgroundSubtractorMOG2.test.ts diff --git a/src/types/opencv/BackgroundSubtractor.ts b/src/types/opencv/BackgroundSubtractor.ts new file mode 100644 index 0000000..c8815ae --- /dev/null +++ b/src/types/opencv/BackgroundSubtractor.ts @@ -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; +} \ No newline at end of file diff --git a/src/types/opencv/BackgroundSubtractorMOG2.ts b/src/types/opencv/BackgroundSubtractorMOG2.ts new file mode 100644 index 0000000..ebc9460 --- /dev/null +++ b/src/types/opencv/BackgroundSubtractorMOG2.ts @@ -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); +} \ No newline at end of file diff --git a/src/types/opencv/_types.ts b/src/types/opencv/_types.ts index 4d7a0af..7442d74 100644 --- a/src/types/opencv/_types.ts +++ b/src/types/opencv/_types.ts @@ -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"; diff --git a/test/BackgroundSubtractorMOG2.test.ts b/test/BackgroundSubtractorMOG2.test.ts new file mode 100644 index 0000000..ede2159 --- /dev/null +++ b/test/BackgroundSubtractorMOG2.test.ts @@ -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(); + }); +}); \ No newline at end of file From 41fa812470756f237de07889654697d53b93bb64 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 5 Sep 2025 03:32:18 +0000 Subject: [PATCH 3/4] Final validation and cleanup for BackgroundSubtractorMOG2 TypeScript support Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com> --- test/typescript-validation.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/typescript-validation.ts diff --git a/test/typescript-validation.ts b/test/typescript-validation.ts new file mode 100644 index 0000000..4318fd4 --- /dev/null +++ b/test/typescript-validation.ts @@ -0,0 +1,30 @@ +// Final validation file to be compiled with the project context + +// This file demonstrates that the original issue is resolved +// These TypeScript constructs should now work without compilation errors: + +declare global { + var cv: { + BackgroundSubtractorMOG2: new (history?: number, varThreshold?: number, detectShadows?: boolean) => any; + BackgroundSubtractor: new () => any; + Mat: new (rows?: number, cols?: number, type?: number) => any; + CV_8UC3: number; + }; +} + +// ===== ORIGINAL ISSUE EXAMPLES ===== +// These examples from the issue should now work without TypeScript errors: + +// 1. Basic constructor usage - the main issue reported +const backgroundSubtractor = new cv.BackgroundSubtractorMOG2(); + +// 2. Constructor with parameters +const backgroundSubtractorWithHistory = new cv.BackgroundSubtractorMOG2(500); +const backgroundSubtractorWithVarThreshold = new cv.BackgroundSubtractorMOG2(500, 16); +const backgroundSubtractorFull = new cv.BackgroundSubtractorMOG2(500, 16, true); + +// 3. Method usage should work +const image = new cv.Mat(480, 640, cv.CV_8UC3); +const fgMask = new cv.Mat(); + +export {}; // Make this a module \ No newline at end of file From 175e544d20a341f9a230fe39d5075bef078f1e80 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 8 Oct 2025 04:40:38 +0000 Subject: [PATCH 4/4] Remove redundant typescript-validation.ts file Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com> --- test/typescript-validation.ts | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 test/typescript-validation.ts diff --git a/test/typescript-validation.ts b/test/typescript-validation.ts deleted file mode 100644 index 4318fd4..0000000 --- a/test/typescript-validation.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Final validation file to be compiled with the project context - -// This file demonstrates that the original issue is resolved -// These TypeScript constructs should now work without compilation errors: - -declare global { - var cv: { - BackgroundSubtractorMOG2: new (history?: number, varThreshold?: number, detectShadows?: boolean) => any; - BackgroundSubtractor: new () => any; - Mat: new (rows?: number, cols?: number, type?: number) => any; - CV_8UC3: number; - }; -} - -// ===== ORIGINAL ISSUE EXAMPLES ===== -// These examples from the issue should now work without TypeScript errors: - -// 1. Basic constructor usage - the main issue reported -const backgroundSubtractor = new cv.BackgroundSubtractorMOG2(); - -// 2. Constructor with parameters -const backgroundSubtractorWithHistory = new cv.BackgroundSubtractorMOG2(500); -const backgroundSubtractorWithVarThreshold = new cv.BackgroundSubtractorMOG2(500, 16); -const backgroundSubtractorFull = new cv.BackgroundSubtractorMOG2(500, 16, true); - -// 3. Method usage should work -const image = new cv.Mat(480, 640, cv.CV_8UC3); -const fgMask = new cv.Mat(); - -export {}; // Make this a module \ No newline at end of file