2013-01-14 67 views
3

只是想清除我的困惑。我测试了openCV模板匹配方法来匹配一些数字。首先我有这个数字序列0 1 2 3 4 5 1 2 3 4 5(在二进制化之后,字符宽度可能不同)。模板匹配如何工作以匹配数字“1”?可以;了解openCV中的模板匹配

  1. 滑动通过所有的窗口,直到它发现2个匹配(2输出),或
  2. 停止后它匹配第一“1”,或
  3. 找到两个数之间的相关性最高“1”并选择其中一个。

Matching Number '1'

编辑:由于附着是输出。它只匹配一个数字'1'而不是两个'1'。

[问]我怎样才能同时检测两个数字'1'?

+0

你应该用Google多一点.. .http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matchi ng/template_matching.html –

+0

是的,我在阅读之前将其发布在此处。但是,当我试着对我的例子只选择一个数字来匹配。 – Mzk

+0

您正在使用哪种匹配方法?相关性?尝试CV_TM_SQDIFF –

回答

5

我知道这是一个老问题,但这里是一个答案。

当你做MatchTemplate时,它会输出一个灰度图像。之后,你需要在上面做一个MinMax。然后,您可以检查您正在查找的范围内是否有结果。在下面的例子中,使用EmguCV(C#中的OpenCV封装),我只在最低查找(minValues数组的索引0)周围画一个矩形,当它低于0.75时(您可以根据需要调整此阈值)。

下面是代码:

Image<Gray, float> result = new Image<Gray, float>(new System.Drawing.Size(nWidth, nHeight)); 
result = image.CurrentImage.MatchTemplate(_imageTemplate.CurrentImage, Emgu.CV.CvEnum.TM_TYPE.CV_TM_SQDIFF_NORMED); 


double[] minValues; 
double[] maxValues; 
System.Drawing.Point[] minLocations; 
System.Drawing.Point[] maxLocations; 

result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations); 
if (minValues[0] < 0.75) 
{ 
    Rectangle rect = new Rectangle(new Point(minLocations[0].X, minLocations[0].Y), 
     new Size(_imageTemplate.CurrentImage.Width, _imageTemplate.CurrentImage.Height)); 
    image.CurrentImage.Draw(rect, new Bgr(0,0,255), 1); 
} 
else 
{ 
    //Nothing has been found 
} 

编辑

下面是输出的一个例子:

Example of output

+0

你可以粘贴你做的一些图像吗? – Mzk

+0

我将在下周编辑我的答案,当我将代码与我 –

+0

感谢首先@让。=) – Mzk