Mig*_*ler 2 opencv matching matchtemplate
我正在使用OpenCV 3.0.0将图像定位到另一个图像.先验函数matchTemplate是我需要使用的,但看到结果我不再确定了.
问题在于,根据输入图像,结果非常准确或完全不准确.
例1:
主图像

模板

结果

这里没有抱怨.在这种情况下匹配是完美的.但现在我替换我想要使用的图像和...
主图像

模板

结果

所以,根本不工作(图像右上角的结果矩形).任何方法(在此示例中为CORR NORMED)都会打印模板所在的矩形.所有结果都远非准确.
所以,我的问题是,matchTemplate的结果是否取决于主图像有多少种不同的颜色/形状?SURF或SIFT会帮助我吗?你们现在有什么功能可以帮助我将模板定位到另一个图像中吗?
先感谢您!
PS:我没有添加任何代码,因为我猜这不是那种问题,因为第一个例子效果很好.
您的问题可能是,模板匹配不是比例不变的,您的模板大小适合对象大小.
使用此输入和代码我得到输出:
输入图像:

输入模板:

代码:基本上来自opencv教程:http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
int main()
{
cv::Mat input = cv::imread("../inputData/TemplateMatch.jpg");
cv::Mat gray;
cv::cvtColor(input,gray,CV_BGR2GRAY);
cv::Mat templ = cv::imread("../inputData/Template2.jpg");
cv::Mat img = input;
cv::Mat result;
/// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create( result_cols, result_rows, CV_32FC1 );
int match_method = CV_TM_SQDIFF;
/// Do the Matching and Normalize
matchTemplate( img, templ, result, match_method );
normalize( result, result, 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;
minMaxLoc( result, &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
if( match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
/// Show me what you got
cv::rectangle( input, matchLoc, cv::Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), cv::Scalar::all(0), 2, 8, 0 );
cv::rectangle( result, matchLoc, cv::Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), cv::Scalar::all(0), 2, 8, 0 );
cv::imshow("input", input);
cv::imshow("template", templ);
cv::imwrite("../outputData/TemplateMatch.jpg", input);
cv::waitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:

| 归档时间: |
|
| 查看次数: |
6375 次 |
| 最近记录: |