2

How do I measure success of the opencv template-matching algorithm ?

I understand that the minmaxLoc function can be used to find the location of the best match. But does it also give an indication on how good the match actually was ? (If yes, how would you find out ?)

Is there an even more appropriate function to measure the correlation between the found match (green rectangle) and the original template ? For example, what if the template-image is slightly rotated or translated compared to as it can be found in the matching-image ?

Do I simply take the average of all minmax-locations or what would you suggest ?

example of template-matching function in opencv

cv::Mat cv_in_image = [in_image CVMat];
cv::Mat cv_in_template = [in_template CVMat];
cv::Mat output;

// Do some OpenCV stuff with the image

/// Create the result matrix
int result_cols = in_image.size.width - in_template.size.width + 1;
int result_rows = in_image.size.height - in_template.size.height + 1;

output.create(result_rows, result_cols, CV_32FC1);

cv::matchTemplate(cv_in_image, cv_in_template, output, cv::TM_CCORR_NORMED);

cv::normalize(output, output, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());

/// Localizing the best match with minMaxLoc
double minVal; double maxVal;
cv::Point minLoc; cv::Point maxLoc;
cv::Point matchLoc;

cv::minMaxLoc(output, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());

/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
int match_method;
if(match_method  == cv::TM_SQDIFF || match_method == cv::TM_SQDIFF_NORMED) {
    matchLoc = minLoc;
    NSLog(@"Correlation minVal = %f", minVal);
    NSLog(@"(Correlation maxVal = %f)", maxVal);
}
else {
    matchLoc = maxLoc;
    NSLog(@"Correlation maxVal = %f", maxVal);
    NSLog(@"(Correlation minVal = %f)", minVal);
}

/// Show me what you got

cv::Rect rect1;
rect1.x = matchLoc.x;
rect1.y = matchLoc.y;
rect1.width = cv_in_template.cols;
rect1.height = cv_in_template.rows;

cv::rectangle(cv_in_image, rect1, cv::Scalar::all(0), 2, 8, 0);
iKK
  • 6,394
  • 10
  • 58
  • 131
  • even if this is a old post and has nothing to do with your question: output.create(result_rows, result_cols, CV_32FC1); This is useless, since the openCV will make this call itself. – Hafnernuss Jul 09 '15 at 05:23

1 Answers1

2

You can try to use some similarity metrics, like PSNR or SSIM.

Another link.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • Thank you very much for the hint! Could you please tell me what the "block_size" would have to be (in the SSIM code example of your link) ?? – iKK Jul 15 '14 at 22:46
  • It should depend on size of your template and detail level you want to control. It's some kind of averaging unit. Larger image - larger block size. Lower details - larger block size. I think resonable value is about 10-20 pixels, but it depends on your problem and need adjustments after experiments. – Andrey Smorodov Jul 16 '14 at 05:30
  • Perfect - thank you for your answer - I'll gave it a shot: My pictures are only 100x100 pixels, for now. With a block_size of 10 I get quite reasonable results. – iKK Jul 16 '14 at 08:28