Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

[OpenCV.js] Template matching with mask

Hello,

I'm trying to use template matching in OpenCV.js as per the example here: https://docs.opencv.org/3.4/d8/dd1/tutorial_js_template_matching.html

I have this working.

I am now trying to template match with a mask. I am using the following line:

cv.matchTemplate(image_to_search, template, output, cv.TM_CCORR_NORMED , mask);

If I use an empty mask:

mask = new cv.Mat();

Then the function is working correctly (this is the way the example is working), but as soon as I try to pass my own mask it fails (Uncaught error 6593664 is the console error message I am getting)

Can someone please provide some working code on how to perfrom a template match in OpenCV.js with a mask?

My template is a loaded via:

cv.imread(canvas); //canvas is a HTML5 <canvas> element

and I am currently generating the mask via the following function:

function generate_mask(source){
    let output =    new cv.Mat();
    //This gives me a black where there is currently transparency
    cv.cvtColor(source, output, cv.COLOR_RGBA2GRAY, 0);

    cv.threshold(output, output, 1, 1, cv.THRESH_BINARY);

    return output;
}

Thanks

[OpenCV.js] Template matching with mask

Hello,

I'm trying to use template matching in OpenCV.js as per the example here: https://docs.opencv.org/3.4/d8/dd1/tutorial_js_template_matching.html

I have this working.

I am now trying to template match with a mask. I am using the following line:

cv.matchTemplate(image_to_search, template, output, cv.TM_CCORR_NORMED , mask);

If I use an empty mask:

mask = new cv.Mat();

Then the function is working correctly (this is the way the example is working), but as soon as I try to pass my own mask it fails (Uncaught error 6593664 is the console error message I am getting)

Can someone please provide some working code on how to perfrom a template match in OpenCV.js with a mask?

My template is a loaded via:

cv.imread(canvas); //canvas is a HTML5 <canvas> element

and I am currently generating the mask via the following function:

function generate_mask(source){
    let output =    new cv.Mat();
    //This gives me a black where there is currently transparency
    cv.cvtColor(source, output, cv.COLOR_RGBA2GRAY, 0);

    cv.threshold(output, output, 1, 1, 255, cv.THRESH_BINARY);

    return output;
}

Thanks