Ask Your Question

how to Reduce false detection of template matching

asked 2015-11-27 11:19:24 -0500

amir_h gravatar image

updated 2015-11-27 12:19:31 -0500

pklab gravatar image

how to Reduce false detection of template matching ??

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    try {
        image = inputFrame.rgba();
        templ = Utils.loadResource(MainActivity.this, TEMPLATE, Highgui.CV_LOAD_IMAGE_COLOR);
    } catch (Exception e) {
        Log.d("eeeeeeeee", e.getMessage());
    Mat template = new Mat(templ.size(), CvType.CV_32F);
    Imgproc.cvtColor(templ, template, Imgproc.COLOR_BGR2RGBA);
    int match_method = Imgproc.TM_CCOEFF;

    // Create the result matrix
    int result_cols = image.cols() - template.cols() + 1;
    int result_rows = image.rows() - template.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

    // Do the Matching and Normalize
    Imgproc.matchTemplate(image, template, result, match_method);
    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;

        Point pt2 = new Point(matchLoc.x + template.cols(), matchLoc.y + template.rows());
        Scalar color = new Scalar(0, 255, 255, 0);
        Core.rectangle(image, matchLoc, pt2, color);

    return image;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-11-27 12:17:40 -0500

pklab gravatar image

updated 2015-11-30 02:07:58 -0500

Many times about. See this and this for more information.

Short answer:

  1. using TM_CCOEFF_NORMED result will be between 0..1 (1=perfect match)
  2. remove normalize(result, result, 0, ... otherwise you will see always a perfect match
  3. use a threshold on matching result to accept or reject the matching. You could start with 0.9

EDIT as requested...

matchTemplate result is an image map. result(x,y) is the result of matching formula calculated in (x,y)... is a score, a matching quality indicator for the point!

minMaxLoc is needed to get the value and the location for the highest score, so you have maxValue and maxLoc. But highest score means the highest of the scores you got for your image, it could be the least worst.

Using normalize(result, result, 0, 1,... you will lost information about the quality without counterpart. normalize is useless here!! (use it just for imshow!!!!)

Question 1) is your highest score good enough to accept the matching ? Answer: If it's too low means that matching quality is poor!?!? Question 2) how to define too low ?

Using non _NORMED version (like is TM_CCOEFF) you don't know the value for perfect match than you can't set numeric value for too low. You can use some empiric value valid just for a given image and a given template.

Using _NORMED version (like TM_CCOEFF_NORMED) you know that perfect match is 1.0. If your maxValue is 0.99 you had almost a perfect match. If it's 0.9 you had a good match, if 0.7 may be you had a mismatch !

at the end you can write:

Imgproc.matchTemplate(image, template, result, TM_CCOEFF_NORMED);
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
double minMatchQuality = 0.9 // with CV_TM_SQDIFF_NORMED you could use 0.1
if mmr.maxValue > minMatchQuality // with CV_TM_SQDIFF_NORMED use minValue < minMatchQuality
  accept the match
  rejecty the match
edit flag offensive delete link more


Thanks, Can you explain more about option 3 (use a threshold on matching result to accept or reject the matching. You could start with 0.9)

amir_h gravatar imageamir_h ( 2015-11-28 06:08:49 -0500 )edit

see EDIT in my answer

pklab gravatar imagepklab ( 2015-11-28 13:25:21 -0500 )edit

Question Tools

1 follower


Asked: 2015-11-27 11:19:24 -0500

Seen: 8,248 times

Last updated: Nov 30 '15