Ask Your Question
0

[OpenCV.js] Template matching with mask [closed]

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

Mr_John gravatar image

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

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 reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Mr_John
close date 2020-03-22 09:52:36.127866

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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-03-22 09:51:49 -0600

Mr_John gravatar image

Found a solution using the following code:

function(){
    var method  =   cv.TM_CCORR_NORMED;

    var source      =   cv.imread('source', 1);
    var template    =   cv.imread('template',  1);
    var mask        =   cv.imread('mask',  1);

    let result_cols =   source.cols - template.cols + 1;
    let result_rows =   source.rows - template.rows + 1;

    var result      =   new cv.Mat(result_rows, result_rows, cv.CV_32FC1);

    cv.matchTemplate(source, template, result, method, mask);

    cv.normalize(result, result, 0, 1, cv.NORM_MINMAX, -1, new cv.Mat() );

    let minMaxLoc   =   cv.minMaxLoc(result);

    let matchLoc;

    if(method == cv.TM_SQDIFF || method == cv.TM_SQDIFF_NORMED){
        matchLoc    =   minMaxLoc.minLoc;
    }else{
        matchLoc    =   minMaxLoc.maxLoc;
    }

    let canvas  =   document.createElement("canvas");

    var output  =   new cv.Mat();
    source.copyTo(output);

    let point1 = new cv.Point(matchLoc.x, matchLoc.y);
    let point2 = new cv.Point(matchLoc.x + template.cols, matchLoc.y + template.rows);

    let rectangleColor = new cv.Scalar(255, 0, 0);
    cv.rectangle(output, point1, point2, rectangleColor, 2, cv.LINE_AA, 0);

    cv.imshow(canvas, output);
    document.body.appendChild(canvas);
}

CV.TM_SQDIFF is still causing issues, but this seems to work well enough.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-01-11 09:27:58 -0600

Seen: 5,821 times

Last updated: Mar 22 '20