在另一个大图像中快速找到一个较小的图像

时间:2012-02-25 12:11:30

标签: c# image image-processing imaging aforge

无论如何要让这件事变得更快?现在,它就像sourceImage上的6秒,大小为1024x768,模板为50x50左右。这是使用AForge,如果有人知道其他更快更简单的方法请提交。 我要做的任务是在屏幕截图中找到一个较小的图像。最好快我的限制是1秒。我正在寻找的图像是一个红色矩形简单图像,截图更复杂。

System.Drawing.Bitmap sourceImage = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\1.jpg");
System.Drawing.Bitmap template = (Bitmap)Bitmap.FromFile(@"C:\SavedBMPs\2.jpg");
// create template matching algorithm's instance
// (set similarity threshold to 92.5%)

ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.921f);
// find all matchings with specified above similarity

TemplateMatch[] matchings = tm.ProcessImage(sourceImage, template);
// highlight found matchings

BitmapData data = sourceImage.LockBits(
    new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
    ImageLockMode.ReadWrite, sourceImage.PixelFormat);
foreach (TemplateMatch m in matchings)
{

        Drawing.Rectangle(data, m.Rectangle, Color.White);

    MessageBox.Show(m.Rectangle.Location.ToString());
    // do something else with matching
}
sourceImage.UnlockBits(data);

1 个答案:

答案 0 :(得分:2)

http://opencv.willowgarage.com/wiki/FastMatchTemplate - 在这里您可以找到有趣的想法,使用两个步骤加快模板匹配,首先尝试匹配下采样图像,当找到时匹配原始搜索区域较小的图像。

在matchTemplate函数中还有opencv模板匹配实现。此功能移植到GPU,可以显着加快速度。

请参阅以下

http://opencv.willowgarage.com/documentation/cpp/object_detection.html - matchTemplate函数。 http://opencv.willowgarage.com/wiki/OpenCV_GPU - 关于移植到GPU的OpenCV功能。

相关问题