TechStark / opencv-js

OpenCV JavaScript version for node.js or browser
Apache License 2.0
353 stars 31 forks source link

TypeError: cv.findCountours #43

Closed einvik closed 8 months ago

einvik commented 8 months ago

Hi, following code gives me an argument type error:

         // img is of type Mat
    const imgGray = new cv.Mat();
    cv.cvtColor(img, imgGray, cv.COLOR_RGBA2GRAY);

    const imgBlur = new cv.Mat();
    cv.GaussianBlur(imgGray, imgBlur, new cv.Size(5, 5), 0, 0, cv.BORDER_DEFAULT);

    const imgThresh = new cv.Mat();
    cv.threshold(imgBlur, imgThresh, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU);

    const contours = new cv.MatVector();
    const hierarchy = new cv.Mat();

    cv.findContours(imgThresh, contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE);

This is the error i am receving:

TS2769: No overload matches this call.
  Overload 1 of 2, '(image: Mat, contours: Mat, hierarchy: Mat, mode: number, method: number, offset?: Point): void', gave the following error.
    Argument of type 'MatVector' is not assignable to parameter of type 'Mat'.
      Type 'Vector<Mat>' is missing the following properties from type 'Mat': allocator, cols, data, data8S, and 67 more.
  Overload 2 of 2, '(image: Mat, contours: Mat, mode: number, method: number, offset?: Point): void', gave the following error.
    Argument of type 'MatVector' is not assignable to parameter of type 'Mat'.
    14 |        const hierarchy = new cv.Mat();
    15 |
  > 16 |        cv.findContours(imgThresh, contours, hierarchy, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE);
       |                                   ^^^^^^^^
    17 |        let maxArea = 0;
    18 |        let maxContourIndex = -1;
    19 |        // for (let i = 0; i < contours.size(); ++i) {

Found 1 error in 405 ms.

I've been reading the documentation for opencv js and we are informed that contours should be a MatVector, but types says otherwise. Any ideas of workaround? Local settings?

Using 4.8.0-release.9

zimmru commented 8 months ago

I'm running into the same issue with cv.merge(). This code works in version 4.8.0-release.4, but not in version 4.8.0-release.9:

const channels = new cv.MatVector();
cv.split(mat, channels);
cv.merge(channels, mat);

In 4.8.0-release.9, it gives the error:

error TS2345: Argument of type 'MatVector' is not assignable to parameter of type 'Mat'.
  Type 'Vector<Mat>' is missing the following properties from type 'Mat': allocator, cols, data, data8S, and 67 more.
ttt43ttt commented 8 months ago

Fixed in v4.8.0-release.10