Imgproc.matchTemplate() API returns void. IS there any way to know that a match has been found?

asked 2016-01-11 01:26:02 -0600

A rectangle is drawn irrespective of the template found or not. How can I know taht a valid match for the template has been found?

edit retag flag offensive close merge delete

Comments

Take a look at the matchTemplate tutorial, it does exactly what you need.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-11 03:02:13 -0600 )edit

Hi Steven, I am trying to match a template in camera frame, but even before I bring the template in camera frame , a rectangle is displayed.However, once the template is in camera frame,, then the rectangle is drawn only around the template. So I wanted to draw the rectangle only when template is found. Any suggestion will be of great help.

Priyanka Agarwal gravatar imagePriyanka Agarwal ( 2016-01-11 03:08:57 -0600 )edit

Without code, impossible for us to help you further ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-11 07:35:33 -0600 )edit

I managed drawing the rectangle only when my image is in camera preview, but that is very hard to detect. As well as when I rotate this image nothing happens, is this the correct algorithm to detect an image. The frames per second is 1, which makes it very slow. How can I fix these issues?

Priyanka Agarwal gravatar imagePriyanka Agarwal ( 2016-01-12 06:04:13 -0600 )edit

Below is the code: Imgproc.matchTemplate(image, template, result, match_method); Imgproc.threshold(result, result,0.8, 1.0, Imgproc.THRESH_TOZERO);

    //Core.normalize(result, result, 0, 255, Core.NORM_MINMAX, -1, new Mat());
    // Localizing the best match with minMaxLoc
    Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
    Point matchLoc;
    if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
        matchLoc = mmr.minLoc;
    } else {
        matchLoc = mmr.maxLoc;
    }

    //Rendering the rectangle around the template
    double tolerance = 0.80;
    Point pt2 = new Point(matchLoc.x + template.cols(), matchLoc.y + template.rows());
    Scalar color = new Scalar(0, 255, 0, 255);
    if (mmr.maxVal >= tolerance)
        Imgproc.rectangle(image, matchLoc, pt2, color);
Priyanka Agarwal gravatar imagePriyanka Agarwal ( 2016-01-12 06:04:48 -0600 )edit