justadudewhohacks / opencv4nodejs

Nodejs bindings to OpenCV 3 and OpenCV 4
MIT License
4.97k stars 827 forks source link

Type error with mat.drawCountours() #876

Open arthurwolf opened 10 months ago

arthurwolf commented 10 months ago

(not related to building)

I'm trying to use src.drawCountours.

My code:

    // Remove islands.
    public remove_islands(island_size: number): void {

        // Assume mat is your binary image
        const src = this.as_matrix;

        // Write the image to disk.
        cv.imwrite('/tmp/ex-cv-src.png', src);

        // Find all contours in the binary image
        const contours = src.findContours(cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE);

        // Iterate over the contours
        for (let i = 0; i < contours.length; i++) {

            console.log({i, area: contours[i].area});

            // If the contour area is 2000 pixels or less
            if (contours[i].area < island_size) {

                // Log the countour.
                console.log(contours[i]);

                // Get the points as an array of Point2.
                const points = contours[i].getPoints();

                console.log({points});

                // Draw the contour with white color and thickness -1 (fills the contour)
                src.drawContours([points], -1, new cv.Vec3(255, 255, 255));

            }
        }

        // Set the mask to the matrix.
        this.from_matrix(src);

    }

The first parameter is an array of Countour objects.

But if I do pass an array of Countours, I get this error about the first parameter needing to be an array of arrays of Point2.

So I use Countour.getPoints() to get the array of Point2 for that Countour, and pass an array with that in it (array of array of Point2, as requested), but then Typescript is unhappy, because the declaration wants an array of Countours :

Type 'Point2[]' is missing the following properties from type 'Contour': numPoints, area, isConvex, hierarchy, and 15 more.ts(2740)

What's going on here? How do I fix this?

If I go passed the typescript error with ts-ignore, the code executes, but no islands/contours are removed by drawContours

Thanks a lot in advance.