diff --git a/src/types/opencv/BackgroundSubtractor.ts b/src/types/opencv/BackgroundSubtractor.ts index c8815ae..6df1a04 100644 --- a/src/types/opencv/BackgroundSubtractor.ts +++ b/src/types/opencv/BackgroundSubtractor.ts @@ -1,9 +1,15 @@ -import type { Algorithm, bool, double, InputArray, OutputArray } from "./_types"; +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 + * The class is only used to define the common interface for the whole family of background/foreground * segmentation algorithms. * * Source: @@ -17,20 +23,24 @@ export declare class BackgroundSubtractor extends Algorithm { * * @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. + * @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 + * 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; + 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 + * @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 index ebc9460..06496fc 100644 --- a/src/types/opencv/BackgroundSubtractorMOG2.ts +++ b/src/types/opencv/BackgroundSubtractorMOG2.ts @@ -3,7 +3,7 @@ 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] + * The class implements the Gaussian mixture model background subtraction described in [Zivkovic2004] * and [Zivkovic2006]. * * Source: @@ -12,11 +12,15 @@ import type { BackgroundSubtractor, bool, double, int } from "./_types"; 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 + * @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 + * @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 + public constructor( + history?: int, + varThreshold?: double, + detectShadows?: bool, + ); +} diff --git a/src/types/opencv/core_array.ts b/src/types/opencv/core_array.ts index 24f264a..7eb2765 100644 --- a/src/types/opencv/core_array.ts +++ b/src/types/opencv/core_array.ts @@ -808,8 +808,7 @@ export declare function dft( /** * The function [cv::divide] divides one array by another: `\\[\\texttt{dst(I) = - * saturate(src1(I)*scale/src2(I))}\\]` or a scalar by an array when there is no src1 : - * `\\[\\texttt{dst(I) = saturate(scale/src2(I))}\\]` + * saturate(src1(I)*scale/src2(I))}\\]` * * Different channels of multi-channel arrays are processed independently. * @@ -822,6 +821,9 @@ export declare function dft( * Saturation is not applied when the output array has the depth CV_32S. You may even get result of an * incorrect sign in the case of overflow. * + * Note: The scalar-by-array overload `divide(scale, src2, dst, dtype)` available in C++ OpenCV + * is not available in OpenCV.js. To achieve scalar division, use `cv.divide(cv.Mat.ones(...), src, dst, scale)`. + * * [multiply], [add], [subtract] * * @param src1 first input array. @@ -843,17 +845,6 @@ export declare function divide( dtype?: int, ): void; -/** - * This is an overloaded member function, provided for convenience. It differs from the above function - * only in what argument(s) it accepts. - */ -export declare function divide( - scale: double, - src2: InputArray, - dst: OutputArray, - dtype?: int, -): void; - /** * The function [cv::eigen] calculates just eigenvalues, or eigenvalues and eigenvectors of the * symmetric matrix src: diff --git a/test/divide.test.ts b/test/divide.test.ts new file mode 100644 index 0000000..e5c7f96 --- /dev/null +++ b/test/divide.test.ts @@ -0,0 +1,91 @@ +import { setupOpenCv, translateException } from "./cv"; + +beforeAll(setupOpenCv); + +describe("cv.divide overloading", () => { + it("should correctly call array-by-array divide", () => { + try { + // Test array-by-array division + const src1 = cv.Mat.ones(3, 3, cv.CV_32F); + const src2 = new cv.Mat(3, 3, cv.CV_32F); + src2.setTo(new cv.Scalar(2)); + const dst = new cv.Mat(); + + cv.divide(src1, src2, dst); + + // Result should be 1/2 = 0.5 for all elements + expect(dst.data32F[0]).toBeCloseTo(0.5, 5); + + src1.delete(); + src2.delete(); + dst.delete(); + } catch (err) { + throw translateException(err); + } + }); + + it("should correctly call array-by-array divide with scale parameter", () => { + try { + // Test array-by-array division with explicit scale + const src1 = cv.Mat.ones(3, 3, cv.CV_32F); + const src2 = new cv.Mat(3, 3, cv.CV_32F); + src2.setTo(new cv.Scalar(2)); + const dst = new cv.Mat(); + + // Result should be 1 * 10 / 2 = 5 for all elements + cv.divide(src1, src2, dst, 10); + + expect(dst.data32F[0]).toBeCloseTo(5, 5); + + src1.delete(); + src2.delete(); + dst.delete(); + } catch (err) { + throw translateException(err); + } + }); + + it("should achieve scalar-by-array division using Mat.ones with scale", () => { + try { + // To divide a scalar by an array: create a ones matrix and use scale parameter + // This is the workaround for the pattern from the issue + const srcStd = 0.5; + const sourceNormalized = new cv.Mat(3, 3, cv.CV_32F); + sourceNormalized.setTo(new cv.Scalar(2)); + + // To achieve: (1/srcStd) / sourceNormalized + // Use: ones / sourceNormalized with scale = (1/srcStd) + const ones = cv.Mat.ones(3, 3, cv.CV_32F); + cv.divide(ones, sourceNormalized, sourceNormalized, 1 / srcStd); + + // Result should be (1/0.5) * 1 / 2 = 2 / 2 = 1 for all elements + expect(sourceNormalized.data32F[0]).toBeCloseTo(1, 5); + + ones.delete(); + sourceNormalized.delete(); + } catch (err) { + throw translateException(err); + } + }); + + it("should not accept scalar as first parameter (TypeScript should catch this)", () => { + try { + const src = new cv.Mat(2, 2, cv.CV_32F); + src.setTo(new cv.Scalar(2)); + const dst = new cv.Mat(); + + // This should not compile in TypeScript (but we can't test that at runtime) + // If someone bypasses TypeScript, it will throw at runtime + expect(() => { + // @ts-expect-error - Testing that TypeScript prevents this + cv.divide(10, src, dst); + }).toThrow(); + + src.delete(); + dst.delete(); + } catch (err) { + throw translateException(err); + } + }); +}); +