OpenCV (emgu) 没有检测到好的形式
OpenCV (emgu) don't detect the good form
我正在尝试学习 OCR 处理。我决定创建一个 .net 项目,它使用 EmguCv 包装器来使用 OpenCV 库。
我写了一小段代码(大部分行来自here):
public static Image<Bgr, byte> FindImage(string Imgtemplate, string Imgsource, float coeff)
{
Image<Bgr, byte> source = new Image<Bgr, byte>(Imgsource); // Image B
Image<Bgr, byte> template = new Image<Bgr, byte>(Imgtemplate); // Image A
Image<Bgr, byte> imageToShow = source.Copy();
using (Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
//double minValues = 0, maxValues = 0;
Point[] minLocations, maxLocations;
//Point minLocations = new Point(), maxLocations = new Point();
//CvInvoke.Normalize(result, result, 0, 1, Emgu.CV.CvEnum.NormType.MinMax, Emgu.CV.CvEnum.DepthType.Default, new Mat());
//CvInvoke.MinMaxLoc(result, ref minValues, ref maxValues, ref minLocations, ref maxLocations, new Mat());
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
// You can try different values of the threshold. I guess somewhere between 0.75 and 0.95 would be good.
if (maxValues[0] > coeff)
{
// This is a match. Do something with it, for example draw a rectangle around it.
Rectangle match = new Rectangle(maxLocations[0], template.Size);
//Rectangle match = new Rectangle(maxLocations, template.Size);
imageToShow.Draw(match, new Bgr(Color.Green), 1);
}
}
// Show imageToShow in an ImageBox (here assumed to be called imageBox1)
//imageBox1.Image = imageToShow;
return imageToShow;
}
源图像是:
模板图像是:
两张图片都是jpg。该方法的结果(100% 匹配)是:
如您所见,结果并不理想:/。然而,当我 运行 我的方法与 OpenCV 网站上给出的例子我有一个很好的结果(67% 匹配):
有人知道我的问题吗?
对于 OpenCV 示例结果,有什么方法可以提高匹配百分比吗?哪个 option/parameter 最好?
问题已解决。
模板需要从源图像中提取。
我正在尝试学习 OCR 处理。我决定创建一个 .net 项目,它使用 EmguCv 包装器来使用 OpenCV 库。
我写了一小段代码(大部分行来自here):
public static Image<Bgr, byte> FindImage(string Imgtemplate, string Imgsource, float coeff)
{
Image<Bgr, byte> source = new Image<Bgr, byte>(Imgsource); // Image B
Image<Bgr, byte> template = new Image<Bgr, byte>(Imgtemplate); // Image A
Image<Bgr, byte> imageToShow = source.Copy();
using (Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
//double minValues = 0, maxValues = 0;
Point[] minLocations, maxLocations;
//Point minLocations = new Point(), maxLocations = new Point();
//CvInvoke.Normalize(result, result, 0, 1, Emgu.CV.CvEnum.NormType.MinMax, Emgu.CV.CvEnum.DepthType.Default, new Mat());
//CvInvoke.MinMaxLoc(result, ref minValues, ref maxValues, ref minLocations, ref maxLocations, new Mat());
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
// You can try different values of the threshold. I guess somewhere between 0.75 and 0.95 would be good.
if (maxValues[0] > coeff)
{
// This is a match. Do something with it, for example draw a rectangle around it.
Rectangle match = new Rectangle(maxLocations[0], template.Size);
//Rectangle match = new Rectangle(maxLocations, template.Size);
imageToShow.Draw(match, new Bgr(Color.Green), 1);
}
}
// Show imageToShow in an ImageBox (here assumed to be called imageBox1)
//imageBox1.Image = imageToShow;
return imageToShow;
}
源图像是:
模板图像是:
两张图片都是jpg。该方法的结果(100% 匹配)是:
如您所见,结果并不理想:/。然而,当我 运行 我的方法与 OpenCV 网站上给出的例子我有一个很好的结果(67% 匹配):
有人知道我的问题吗? 对于 OpenCV 示例结果,有什么方法可以提高匹配百分比吗?哪个 option/parameter 最好?
问题已解决。
模板需要从源图像中提取。