与opencv模板匹配的成功

时间:2014-07-12 18:01:58

标签: opencv correlation matchtemplate

如何衡量opencv模板匹配算法的成功?

据我所知,minmaxLoc函数可用于查找最佳匹配的位置。但它是否也表明比赛实际上有多好? (如果是的话,你怎么知道?)

是否有更合适的功能来衡量找到的匹配(绿色矩形)和原始模板之间的相关性?例如,如果模板图像稍微旋转或平移,那么与匹配图像中的相比会怎么样?

我是否只考虑所有minmax位置的平均值或您会建议什么?

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);

1 个答案:

答案 0 :(得分:1)

您可以尝试使用某些相似性指标,例如PSNR or SSIM

Another link