简体   繁体   English

如何使用OpenCV检测和匹配标记(模板匹配)

[英]How to detect and match a marker using OpenCV (Template Matching)

I am using an image which holds a marker in a specific area. 我正在使用一个在特定区域中保存标记的图像。 I tried to do it using Template matching which is the method defined in opencv as cvMatchTemplate. 我尝试使用模板匹配来做到这一点,这是opencv中定义为cvMatchTemplate的方法。

I am using a web cam to detect them, currently the program is detecting the marker, because I provided the same marker as template. 我正在使用网络摄像头来检测它们,目前程序正在检测标记,因为我提供了与模板相同的标记。

But I cannot find a way to check whether it is the best match or just slightly matched. 但我无法找到一种方法来检查它是最佳匹配还是略微匹配。 Because in cvMatchTemplate it is not only detecting the best match, it also keeps detecting the areas which are slightly matching. 因为在cvMatchTemplate中它不仅检测到最佳匹配,而且还不断检测略微匹配的区域。

Can any one please tell me a way to do this. 任何人都可以告诉我一个方法来做到这一点。 Or if there is any other way for my problem, please let me know! 或者如果我的问题还有其他办法,请告诉我!

here is the link for my image card http://imageshack.us/photo/my-images/266/piggycard.jpg/ (I want to detect and check whether its mached) 这是我的图像卡的链接http://imageshack.us/photo/my-images/266/piggycard.jpg/ (我想检测并检查它是否有效)

here is the code 这是代码

// template_mching_test_2.cpp : Defines the entry point for the console application. // template_mching_test_2.cpp:定义控制台应用程序的入口点。 // //

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main()
{   
IplImage* imgOriginal = cvLoadImage("D:\\4Yr\\Research\\SRS\\Animations\\Piggycard.jpg", 0);
IplImage* imgTemplate = cvLoadImage("D:\\4Yr\\Research\\MakingOf\\Sample Imageas\\PiggyMarkerStart.jpg", 0);

CvCapture *cap = cvCaptureFromCAM(0);

if(!cap)
return -1;

cvNamedWindow("result");

IplImage* imgOriginal;
IplImage* imgOriginal2;
IplImage* imgResult;

while(true)
{
imgOriginal = cvQueryFrame(cap);//cvCreateImage(cvSize(imgOriginal->width-imgTemplate->width+1, imgOriginal->height-imgTemplate->height+1), IPL_DEPTH_32F, 1);
imgOriginal2 = cvCreateImage(cvSize(imgOriginal->width,imgOriginal->height),imgOriginal->depth,1);
imgResult = cvCreateImage(cvSize(imgOriginal->width-imgTemplate->width + 1,imgOriginal->height-imgTemplate->height+1),IPL_DEPTH_32F,1);

cvZero(imgResult);
cvZero(imgOriginal2);

cvCvtColor(imgOriginal,imgOriginal2,CV_BGR2GRAY);
cvMatchTemplate(imgOriginal2, imgTemplate, imgResult,CV_TM_CCORR_NORMED);

double min_val=0, max_val=0;
CvPoint min_loc, max_loc;
cvMinMaxLoc(imgResult, &min_val, &max_val, &min_loc, &max_loc);

cvRectangle(imgOriginal, max_loc, cvPoint(max_loc.x+imgTemplate->width,  max_loc.y+imgTemplate->height), cvScalar(0), 1);
printf("%f \n", max_val);

cvShowImage("result", imgOriginal);

cvWaitKey(10);

cvReleaseImage(&imgOriginal2);
cvReleaseImage(&imgResult);
}

cvDestroyAllWindows();
cvReleaseCapture(&cap);

return 0;

}

and as the template I provided the same marker which cropped from the original image. 并且作为模板,我提供了从原始图像裁剪的相同标记。 From minMaxLoc i took the max value to check the best match. 从minMaxLoc我拿最大值来检查最佳匹配。 but it is keep giving me the same values when the image marker in a position, And when the image marker is not in the frame and slightly matching at a place which previous matched with the marker.Does minMaxloc giving us the coordinates(position) of the marker or matching percentage.Or is there any other way for this. 但是当一个位置的图像标记时,它会继续给我相同的值,当图像标记不在框架中并且在之前与标记匹配的位置稍微匹配时.minMaxloc给我们的坐标(位置)标记或匹配百分比。或者还有其他方法。 Thank you for your consideration. 谢谢您的考虑。

There is an OpenCV tutorial on the subject of Template Matching . 有关模板匹配主题OpenCV教程

Using matchTemplate is a good start, it will provide you with an image containing numbers relating to your matching metric (there is a range of choices for the metric, some of which provide high numbers for better matches, some lower). 使用matchTemplate是一个好的开始,它将为您提供一个包含与您的匹配度量相关的数字的图像(指标有一系列选择,其中一些为更好的匹配提供高数字,一些更低)。

To subsequently pick out the best match, you will also need to use the function minMaxLoc which can locate the minimum & maximum values from this matrix. 要随后选出最佳匹配,您还需要使用函数minMaxLoc ,它可以从此矩阵中找到最小值和最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM