简体   繁体   English

OpenCV匹配模板

[英]OpenCV match template

I am trying to match a logo (template) I have with some images. 我试图将我的徽标(模板)与一些图像相匹配。 My images are all colored in nature and the way I am doing it is using the cvMatchTemplate() from openCV and running the template (logo) over my source image. 我的图像都是自然色的,我这样做的方法是使用openCV中的cvMatchTemplate()并在源图像上运行模板(徽标)。 I scale the template to multiple levels to find the best match. 我将模板缩放到多个级别以找到最佳匹配。 Please note that I am just finding the digitally imprinted logos on the image and not the logos present in the scene. 请注意,我只是在图像上找到数字印记的徽标,而不是场景中存在的徽标。 Eg: Detecting sky sports from this image http://i56.tinypic.com/2v3j3wx.jpg (The image is purely for representation and clarity of my task and is not images I am working with 例如:从这张图片中检测天空运动http://i56.tinypic.com/2v3j3wx.jpg (该图像纯粹用于表示和清晰我的任务,而不是我正在使用的图像

My images are not of fixed resolution so I scale it up to a standard 800x600. 我的图像没有固定的分辨率,因此我将其缩放到标准的800x600。 Now when my source image resolution is very bad say 300x300, the results are very ordinary. 现在当我的源图像分辨率很差时说300x300,结果非常普通。 I am using the method=CV_TM_CCOEFF_NORMED in the template parameter and the scores even for accurate matches is quite low (as low as 0.4 on a scale of 0 to 1.0) making it difficult for me to confidently say if the logo is present or not. 我在模板参数中使用方法= CV_TM_CCOEFF_NORMED,即使是精确匹配的分数也很低(在0到1.0的范围内低至0.4),这使我很难自信地说明徽标是否存在。 I have two questions on this: 我有两个问题:

1 - In opencv template matching, how does it handle colored images. 1 - 在opencv模板匹配中,它如何处理彩色图像。 I tried to comprehend from the document and my inference was it computes the score for each channel separately and the best is taken. 我试图从文档中理解,我的推论是它分别计算每个频道的得分,并且最好采取。 If that is the case wont I would be better off taking all the three channels into account for better results 如果是这样的话我不会更好地考虑所有三个渠道以获得更好的结果

2 - Any alternative approach !! 2 - 任何替代方法!! :) :)

Let me know if any thing is not clear ! 如果有任何事情不清楚,请告诉我!

EDIT (Additional information): As discussed in the comments I am attaching my current matching technique which is scaled template matching. 编辑(附加信息):正如评论中所讨论的,我附加了我目前的匹配技术,即缩放模板匹配。 Please note the attached images are purely for test purposes and is not my actual set of images I am working with (cannot post as the images are proprieotry) Source Image 请注意附加的图像纯粹是出于测试目的而不是我正在使用的实际图像集(不能发布,因为图像是proprieotry)源图像 从youtube拍摄的屏幕截图 Logo Image 形象标识 来自维基百科 Output Image using Template matching 使用模板匹配输出图像 指示最佳匹配的红色块与最高分

Although template is matched the score obtained here is 0.59 for this best match. 尽管模板匹配,但此处获得的分数对于此最佳匹配为0.59。 Although relatively its a good score for a match, but still not good enough for me to tell for sure that the desired logo exists or not. 虽然相对来说它是一场比赛的好成绩,但仍然不够好,我无法确定所需的标志是否存在。 In my test images when the logo on screen is transparent, it would still detect the logo but with a poor score of 0.3-0.4. 在我的测试图像中,当屏幕上的徽标是透明的时,它仍会检测到徽标,但得分为0.3-0.4。 Can a better result be obtained using SURF/SIFT? 使用SURF / SIFT可以获得更好的结果吗?

EDIT (Attempt with SURF) I tried to run a SURF code already given as an example in the opencv offical documentation (minHessian = 2000). 编辑(尝试使用SURF)我试图在opencv官方文档(minHessian = 2000)中运行一个SURF代码作为示例。 link here here is the output. 这里链接是输出。 I am not sure how to interpret it (2,3 points seem to be within the expected boundary. Is this considered good? Any further suggestions? 我不确定如何解释它(2,3点似乎在预期的边界内。这被认为是好的吗?还有什么进一步的建议吗? 在此输入图像描述 Thanks 谢谢

Did you try using a Gaussian Blur on your Source Image before performing template matching? 在执行模板匹配之前,您是否尝试在源图像上使用高斯模糊? This may give you more accurate results as I think it's the quality of the source image that's giving a worse match 这可能会给你更准确的结果,因为我认为源图像的质量会给出更糟糕的匹配

Link to Gaussian Blur in OpenCV Docs : 链接到OpenCV Docs中的高斯模糊:

OpenCV Python Gaussian Blur OpenCV Python高斯模糊

Alternatively you could try a histogram comparison technique on the area template matching suggests, for an extra confirmation that the Template Match correlation returned, even if small, is the correct value : 或者,您可以尝试在区域模板匹配建议上使用直方图比较技术,以进一步确认模板匹配关联返回(即使很小)是正确的值:

Drawing Histograms 绘制直方图

Drawing the histograms is optional, it might be useful for your own application 绘制直方图是可选的,它可能对您自己的应用程序有用

Comparing the Histograms 比较直方图

^ This method calculates the histograms of your images (source and template) and the correlation between them... However, you don't want a Histogram of the entire source, just where your Template Matching thinks the best correlation is, or some other location in the image, so you want to get a Histogram of a Region of Interest (ROI) instead, see the following C++ code : ^此方法计算图像的直方图(源和模板)以及它们之间的相关性...但是,您不需要整个源的直方图,只需要模板匹配认为最佳相关性的位置,或其他一些在图像中的位置,所以你想要获得感兴趣区域(ROI)的直方图,请参阅以下C ++代码:

Mat OriginalImage = imread("source.jpg", 0);
Rect RegionOfInterest = Rect(150, 150, 250, 250);
Mat ROIImage = OriginalImage(RegionOfInterest);

This lets you calculate the histogram of a region of interest. 这使您可以计算感兴趣区域的直方图。 You should get a Histogram of your template and a histogram for the region where Template Matching thinks your template is in the source and compare them to confirm or refute the Template Matching output 您应该获得模板的直方图和模板匹配认为模板在源中的区域的直方图,并比较它们以确认或反驳模板匹配输出

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

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