Ask Your Question
0

matchTemplate determine match

asked 2016-12-02 17:37:14 -0600

coredump gravatar image

I am looking for a way to determine if a match was found, when calling this function:

Mat result;

matchTemplate(frame, template_img, result, CV_TM_SQDIFF_NORMED);

I have seen many examples of receiving a match drawing a square over the matched area and displaying it. I just want to know if it matched in a Boolean sense (Yes it matched, No it didn't)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-02 21:22:37 -0600

Tetragramm gravatar image

You have to use one of the _NORMED methods, and you have to set the threshold yourself. The _NORMED methods return a value between 0 and 1. You find the maximum value, and if it's over your threshold (or below, for SQDIFF, where 0 is a perfect match) then it was found.

Figuring out a good threshold is harder than it sounds. Usually trial and error. Good Luck.

edit flag offensive delete link more

Comments

Still a little confused ... So I use the normalize (...) function followed by the minMaxLoc (...) as shown below:

Mat result;
double minVal, maxVal;
Point  minLoc, maxLoc, matchLoc;
double thresholdMatch = 0;

matchTemplate(frame, template_img, result, CV_TM_SQDIFF_NORMED);

normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

std::cout << "minVal: " << minVal << endl;
std::cout << "minLoc: " << minLoc.x <<  " maxLoc: " << minLoc.y << endl;

The output looks like this:

// not found minLoc: 429 maxLoc: 512 minVal: 0 minLoc: 429 maxLoc: 512

// found minLoc: 58 maxLoc: 257 minVal: 0

The question is what should I match on MinLoc ?, assuming I don't know where the match is ?

coredump gravatar imagecoredump ( 2016-12-03 08:23:17 -0600 )edit

You do NOT use normalize. That destroys the information you're trying to read. You do have to use SQDIFF_NORMED, not just SQDIFF. The magnitude of minVal is the error. If minVal is 0, it's a perfect match. If it's 1, it's totally different.

The minLoc x,y values are just where it found the best match, and contain no information about how good it is. That's what minVal is for. You would use minLoc for drawing the square afterwards, like those examples you mentioned.

Tetragramm gravatar imageTetragramm ( 2016-12-03 10:59:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-12-02 17:37:14 -0600

Seen: 264 times

Last updated: Dec 02 '16