2014-11-06 127 views
0

我并不总是有正方形/长方形图像,有时我也应该匹配圆形图像。例如下面是2张图片。 Ball是模板图像,第二个是Source应该搜索模板的Source。我可以将模板的背景透明化,但是这会产生错误,使得白色会降低比赛分数,因为您在源图像上看到的球体周围没有白色。这些只是2个示例图片。你有建议/解决方案吗?Java中圆形图像的OpenCV模板匹配

Template image to be searched

Source image

回答

0

如果你知道属于模板的像素,可以写你的匹配

总和的绝对差异试验(伪代码)

Mat I, T // image and template 
vector<Point> template_pixels 
Rect sliding_window 
vector<double> match_rates 

for all rows in image 
update sliding_window 
    for all cols in image 
    update sliding_window 
    Mat W = I(sliding_window) 
    sum = 0 
     for all rows in template 
       for all cols in template 
       if(template_pixels contains pixel i) 
        sum += abs(W(i) - T(i)) 
       end for 
     end for 
    match_rates.pushback(sum) 
    end for 
end for 

minMaxLoc(match_rates) 

和在图像行上使用多线程优化它

+0

我不知道python如果这是python我可能会试试:)我在berky写这个。它是非常简单但功能强大的语言 - 真棒,因为你已经知道了 - 只是很难适应。8) – baci 2014-11-07 01:00:31

+0

你是对的,Python有点不同。如果这是Java代码,我肯定会+1。 – karlphillip 2014-11-07 01:01:44

1

没关系,你仍然可以使用matchTemplate()并取得了优异的成绩:

enter image description here

You can find a decent tutorial on OpenCV's documentation。顺便说一句,这是在那里共享的演示的输出。

+0

模板匹配找到最佳结果,即使模板不在源中,它也会标记一个正方形。如果搜索到的图像真的在源中,那么得分就像0.9987 ...但在这种情况下,白人的得分将为0.7 ...我会让我们说1000个不同图像的比较,一些会有绿色的一些会有蓝色,一些灰色的背景。这将始终影响匹配分数。根据找到或未找到我的情况和具有随机阈值的分数图像设置阈值将是一个问题。我知道它会始终高于0.6 ...但仍然... – Anarkie 2014-11-07 13:24:00

2

我认为你也可以使用直方图反投影。在那里你可以使用任意形状的面具。用反投影图像旋转蒙版,您将在图像中出现对象的区域检测到峰值,如下面的图像(颜色映射和缩放)所示。

背投影:

enter image description here

卷积:

enter image description here

编辑:

这是在此基础上paper。我正在试验它,并希望在博客中发布。 这是用C++编写的。

// model histogram: this is the football template 
calcHist(&model32fc3, 1, (const int*)channels, modelMask, histModel, 3, (const int*)histSize, (const float**)ranges, true, false); 
// image histogram 
calcHist(&image32fc3, 1, (const int*)channels, Mat(), histImage, 3, (const int*)histSize, (const float**)ranges, true, false); 
// ratio histogram 
divide(histModel, histImage, histRatio); 
cv::min(histRatio, 1.0, histRatio); 
// backproject ratio histogram 
calcBackProject(&image32fc3, 1, (const int*)channels, histRatio, backprj, (const float**)ranges); 
// convolve the kernel with the backprojected image 
filter2D(backprj, conv, CV_32F, modelMask); 
+0

您能举出代码示例吗?或者是一个指向这个地方的教程的链接? – Anarkie 2014-11-07 13:26:22

+0

添加了代码。我仍在试验它。如果我有机会在未来的博客中发布此代码,我会更新代码或提供链接。 – dhanushka 2014-11-07 14:11:37

+0

我知道C++,但在Java中工作不是,你可以发布它在Java?或者在完全完成后我会自己尝试。 – Anarkie 2014-11-07 14:15:10