Ask Your Question
1

Filtering false match from matchTemplate function

asked 2018-05-08 04:59:53 -0600

tuhdo gravatar image

updated 2018-08-28 11:44:52 -0600

How do I filter the false match returned from matchTemplate best match? Here are the sample images:

This is the sub image: SubImage

The sub-image is found correctly in this image: Correct Match

But not in this image: Bad Match

How do I check for a bad match (like the example above) and filter it out?

Example results:

  • Good match:

Good Match  Example

  • Bad match:

Bad Match Example

edit retag flag offensive close merge delete

Comments

maybe show, how you call it, too ?

berak gravatar imageberak ( 2018-05-08 05:08:16 -0600 )edit

@berak the same code as the official tutorial, with modification to image paths: https://docs.opencv.org/2.4/doc/tutor...

tuhdo gravatar imagetuhdo ( 2018-05-08 05:10:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-05-08 07:44:25 -0600

updated 2018-05-08 07:45:37 -0600

The problem with the tutorial is that it knows the object is going to be in the image, and it does not cope with the fact that there are possible no matches at all. There are two main issues in this tutorial code if you want to apply it for your case.

  1. It always normalizes the map using normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() ); which means the smallest match (very low score) will still be normalized towards the 1 value.
  2. it always looks for the best hit in your matching map

As you can see here:

/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );

Problem is that in your case, you will have to find a harsh threshold on the non-normalized match score map. If it does not meet the threshold, there will simply be no hit!

edit flag offensive delete link more

Comments

1

I solved the problem by using the NORMED variants and remove the normalize call, then use minVal or maxVal as similarity score. The good match returns > 0.99 and the bad match returns ~0.92, which is good enough for me since both differ only the text. You should update the example with this info and I can mark it as accepted.

tuhdo gravatar imagetuhdo ( 2018-05-08 13:33:59 -0600 )edit

Well we should not update the example, as it clearly shows that you take a part from an existing image and try to find it again using templates. However, feel free to add a second one with a more general case, for example find a logo in a promo video that tends to be gone from time to time.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-05-09 03:17:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-08 04:59:53 -0600

Seen: 430 times

Last updated: May 08 '18