AForge ExhaustiveTemplateMatching工作极其缓慢

问题描述:

我试图找到另一个使用AForge框架内一个图像的坐标:AForge ExhaustiveTemplateMatching工作极其缓慢

ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(); 
TemplateMatch[] matchings = tm.ProcessImage(new Bitmap("image.png"), new Bitmap(@"template.png")); 
int x_coordinate = matchings[0].Rectangle.X; 

ProcessImages大约需要2分钟完成。

图像的大小大约是1600x1000像素 模板的尺寸约为60×60像素

有谁知道如何加快这一进程?

至于除了其他的答案,我会说,你的情况:

图像的大小大约是1600x1000像素模板的尺寸约为60×60像素

这个框架是不是最合适的。你试图实现的是更多的搜索图像在其他图像,比比较两个不同分辨率的图像(如“搜索谷歌这个图像”可以使用)。

关于本这样

称为金字塔搜索。

确实,该算法的工作方式更快为更大图像。其实image-pyramid是基于template matching。如果我们把最流行的实现(我发现和使用):

private static bool IsSearchedImageFound(this Bitmap template, Bitmap image) 
    { 
     const Int32 divisor = 4; 
     const Int32 epsilon = 10; 

     ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.90f); 

     TemplateMatch[] tm = etm.ProcessImage(
      new ResizeNearestNeighbor(template.Width/divisor, template.Height/divisor).Apply(template), 
      new ResizeNearestNeighbor(image.Width/divisor, image.Height/divisor).Apply(image) 
      ); 

     if (tm.Length == 1) 
     { 
      Rectangle tempRect = tm[0].Rectangle; 

      if (Math.Abs(image.Width/divisor - tempRect.Width) < epsilon 
       && 
       Math.Abs(image.Height/divisor - tempRect.Height) < epsilon) 
      { 
       return true; 
      } 
     } 

     return false; 
    } 

应该关闭给你一个图片这一个:

page pyramid

作为底线 - 尝试用不同的方法。也许接近Sikuliintegration.Net。或者你可以试试accord .Net更新版本的AForge。

如果这是太多的工作,你可以尝试扩展你的屏幕截图功能,裁剪所需的页面元素(Selenium example)。

对于最近的CPU来说,2分钟看起来太多了,因为图像是您正在使用的模板大小。但是有几种方法可以加速这个过程。第一个是使用较小的规模。这被称为金字塔搜索。您可以尝试将图像和模板分成4份,以便您可以获得400x250的图像和15x15的模板,并匹配此较小的模板。这会跑得更快,但它也会不太准确。然后,您可以使用15x15模板中找到的有趣像素,并使用60x60模板在1600x1000图像中搜索相应的像素,而不是搜索整个图像。

取决于模板的细节,您可以尝试更低的比例(1/8)。

要知道的另一件事是一个更大的模板将运行得更快。这是违反直觉的,但有了更大的模板,您将有更少的像素进行比较。所以如果可能的话,尝试使用更大的模板。有时如果你的模板已经尽可能大,这种优化是不可能的。