了解openCV中的模板匹配

Mzk*_*Mzk 3 opencv

只想清除我的困惑.我测试了openCV模板匹配方法来匹配一些数字.首先,我有一个数字序列0 1 2 3 4 5 1 2 3 4 5(二值化之后,字符宽度可能不同).模板匹配如何与数字'1'匹配?可以;

  1. 滑过所有窗口,直到找到2个匹配(2输出),或
  2. 在匹配第一个'1'后停止,或
  3. 找到两个数字'1'之间的最高相关性并选择其中之一.

匹配数字'1'

编辑:附件是输出.它只匹配一个数字'1'而不是两个'1'.

[问]如何同时检测两个数字'1'?

Jea*_*ôté 5

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

当您执行MatchTemplate时,它将输出灰度图像.之后,您需要对其执行MinMax.然后,您可以检查您要查找的范围内是否有结果.在下面的例子中,使用EmguCV(C#中OpenCV的包装器),只有当它低于0.75时,我才会在最佳查找(minValues数组的索引0)周围绘制一个矩形(您可以根据需要调整此阈值).

这是代码:

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
}
Run Code Online (Sandbox Code Playgroud)

编辑

以下是输出示例:

输出示例