AForge ExhaustiveTemplateMatching的运行非常缓慢

问题描述 投票:0回答:2

我正在尝试使用AForge框架在一个图像中寻找另一个图像的坐标:

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像素模板的大小约为60x60像素

有人知道如何加快该过程吗?

c# .net image-processing aforge
2个回答
1
投票

除了其他答案,我想针对您的情况说:

图像的大小约为1600x1000像素模板的大小约为60x60像素

此框架不是最合适的。您要实现的目标是,在其他图像中搜索更多的图像,而不是比较具有不同分辨率的两个图像(例如可以使用“在Google中搜索此图像”)。

关于此事

称为金字塔搜索。

确实,该算法对bigger图像的工作方式更快。实际上,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;
    }

它应该给您接近这张照片:

“页面金字塔”

作为底线-尝试使用其他方法。用Sikuli可能更接近integration .Net。或者,您可以尝试使用accord .Net较新版本的AForge。

如果这项工作太多,您可以尝试通过裁剪所需的页面元素(Selenium example)来扩展屏幕快照功能。


0
投票

2分钟对于您使用的图像和模板大小的最新CPU来说似乎太多了。但是,有两种方法可以加快这一过程。第一个是使用较小的比例尺。这称为金字塔搜索。您可以尝试将图像和模板除以4,这样您将获得400x250的图像和15x15的模板,并匹配此较小的模板。这将运行得更快,但准确性也会降低。然后,您可以使用在15x15模板中找到的有趣像素,并使用60x60模板在1600x1000图像中搜索相应的像素,而不是在整个图像中搜索。

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

另一件事是,更大的模板将运行得更快。这是违反直觉的,但是使用更大的模板,您可以比较的像素就更少了。因此,如果可能,请尝试使用更大的模板。如果您的模板已经尽可能大,则有时无法进行优化。

© www.soinside.com 2019 - 2024. All rights reserved.