title: Success of template-matching with opencv
body: <p>How do I measure success of the opencv template-matching algorithm ?</p>
  
  <p>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 ?)</p>
  
  <p>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 ?</p>
  
  <p>Do I simply take the average of all minmax-locations or what would you suggest ?</p>
  
  <p><img src="https://i.stack.imgur.com/waxw1.png" alt="example of template-matching function in opencv"></p>
  
  <pre><code>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, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;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);
  </code></pre>