justadudewhohacks / justadudewhohacks/opencv4nodejs

remove outside of contour

Open
#832 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
5.1k
Forks
826
PR merge metrics
No merged PRs in 30d

Description

Hey,
I'm trying to write ANPR project.
i managed to draw the contours around the license plate but now i want to mask it.
![image](https://user-images.githubusercontent.com/88280771/138559248-3b0eaa8c-9f4d-4b0b-8647-c21f78d036fc.png)

i want to mask it like so (and even crop it afterwards),
![image](https://user-images.githubusercontent.com/88280771/138559260-db01e5fe-1b11-4abf-8a91-e69a6ba52388.png)

this is my code by now:

```
import cv from 'opencv4nodejs';

// read image, grayscale
const img = cv.imread('./images/img2.jpg');
const grayImg = img.bgrToGray();

const bfilter = grayImg.bilateralFilter(11, 17, 17);
const edged = bfilter.canny(30, 200);

const getContour = (handMask) => {
const mode = cv.RETR_TREE;
const method = cv.CHAIN_APPROX_SIMPLE;
const contours = handMask.findContours(mode, method);

return contours
.sort((c0, c1) => c1.area - c0.area)
.slice(0, 10)
.find((contour) => {
const x = contour.approxPolyDP(10, true);
return x.length === 4;
});
};
let edgeContour = getContour(edged);

let mask = new cv.Mat(grayImg.rows, grayImg.cols, 0, 0);

let x = img.drawContours([edgeContour.getPoints()], 0, new cv.Vec3(0, 255, 0), {
thickness: 5,
});

x = img.bitwiseAnd(img);
cv.imshow('drawContours', x);

cv.waitKey();

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.