justadudewhohacks/opencv4nodejs

remove outside of contour

NivGreenstein opened this issue · 0 comments

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

i want to mask it like so (and even crop it afterwards),
image

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();