justadudewhohacks / justadudewhohacks/opencv4nodejs
Type error with mat.drawCountours()
- Dominant language
- C++
- Stars
- 5.1k
- Forks
- 826
- PR merge metrics
- No merged PRs in 30d
Description
(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.
Contributor guide
Research direction
Start by tracing the TypeScript declarations and runtime behavior for src.findContours and src.drawContours, using the Contour.getPoints() call shown in the issue. Compare the accepted contour and Point2 array shapes, then verify that the corrected call removes small contours without a TypeScript error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- computer-vision
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100