[OpenCV.js] Template matching with mask

asked 2020-01-11 09:27:58 -0500

Mr_John gravatar image

updated 2020-01-12 07:04:50 -0500

Hello,

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

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, 255, cv.THRESH_BINARY);

    return output;
}

Thanks

edit retag flag offensive close merge delete

Comments

I wonder if the issue could be related to what is being masked. According to the docs, https://docs.opencv.org/4.2.0/df/dfb/..., the mask is applied to the template that is being searched for rather than the image that is being searched in. So the mask would be applied to template rather than image_to_search. This means mask must be same type and size as template. Does this help?

Chris gravatar imageChris ( 2020-01-16 08:48:33 -0500 )edit

@Chris, that is how I am using it at the moment - and I'm getting the issue in the post

Mr_John gravatar imageMr_John ( 2020-01-16 12:41:45 -0500 )edit