0

I am trying to find coordinates of one image inside of another using AForge framework:

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

ProcessImages takes about 2 minutes to perform.

Image's size is about 1600x1000 pixels Template's size is about 60x60 pixels

Does anyone know how to speed up that process?

Dmitry Bakaev
  • 129
  • 2
  • 14

2 Answers2

1

As addition to the other answers, I would say that for your case:

Image's size is about 1600x1000 pixels Template's size is about 60x60 pixels

This framework is not the best fit. The thing you are trying to achieve is more search-image-in-other-image, than compare two images with different resolution (like "Search Google for this image" can be used).

About this so

called pyramid search.

it's true that the algorithm works way faster for bigger images. Actually the image-pyramid is based on template matching. If we take the most popular implementation (I found and used):

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;
    }

It should give you a picture close to this one:

page pyramid

As bottom line - try to use different approach. Maybe closer to Sikuli integration with .Net. Or you can try the accord .Net newer version of AForge.

If this is too much work, you can try to just extend your screenshot functionality with cropping of the page element that is required (Selenium example).

ekostadinov
  • 6,880
  • 3
  • 29
  • 47
0

2 minutes seems too much for a recent CPU with the image a template sizes you are using. But there are a couple of ways to speed up the process. The first one is by using a smaller scale. This is called pyramid search. You can try to divide the image and template by 4 so that you will have an image of 400x250 and a template of 15x15 and match this smaller template. This will run way faster but it will be also less accurate. You can then use the interesting pixels found with the 15x15 template and search the corresponding pixels in the 1600x1000 image using the 60x60 template instead of searching in the whole image.

Depending on the template details you may try at an even lower scale (1/8) instead.

Another thing to know is that a bigger template will run faster. This is counter-intuitive but with a bigger template you will have less pixel to compare. So if possible try to use a bigger template. Sometimes this optimization is not possible if your template is already as big as it can be.

rold2007
  • 1,297
  • 1
  • 12
  • 25