如何使用OpenCV检测和匹配标记(模板匹配) [英] How to detect and match a marker using OpenCV (Template Matching)

查看:472
本文介绍了如何使用OpenCV检测和匹配标记(模板匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个在特定区域中保存标记的图像。我尝试使用模板匹配来做到这一点,模板匹配是在opencv中定义为cvMatchTemplate的方法。

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.

我正在使用网络摄像头来检测它们,目前程序正在检测标记,因为我提供了与模板相同的标记。

I am using a web cam to detect them, currently the program is detecting the marker, because I provided the same marker as template.

但我无法找到一种方法来检查它是最佳匹配还是略微匹配。因为在cvMatchTemplate中它不仅检测到最佳匹配,而且还不断检测略微匹配的区域。

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.

任何人都可以告诉我一种方法。或者如果我的问题还有其他方法,请告诉我!

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!

这里是我的图像卡的链接
http://imageshack.us/photo/my-images/266/piggycard.jpg/
(我想检测并检查它是否已配对)

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)

这里是代码

// template_mching_test_2.cpp:定义控制台应用程序的入口点。
//

// template_mching_test_2.cpp : Defines the entry point for the console application. //

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

}

我作为模板提供了相同的标记原始图像。从minMaxLoc我拿最大值来检查最佳匹配。但是当一个位置的图像标记时,它会继续给我相同的值,当图像标记不在框架中并且在之前与标记匹配的位置稍微匹配时.minMaxloc给我们的坐标(位置)标记或匹配百分比。或者还有其他方法。
感谢您的考虑。

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. 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. Thank you for your consideration.

推荐答案

有一个关于模板匹配主题的OpenCV教程

使用 matchTemplate 是一个良好的开端,它将为您提供包含与您的匹配指标相关的数字的图片(指标有多种选择,其中一些为更好的匹配提供高数字,一些更低)。

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

要随后选择最佳匹配,您还需要使用 minMaxLoc 可以找到最小值和最小值。此矩阵的最大值。

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.

这篇关于如何使用OpenCV检测和匹配标记(模板匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆