更快速地检测大图像

时间:2012-08-25 00:57:44

标签: c# image-processing

目前我使用它来查找在屏幕上的另一个图像中出现的位图,即窗口,但它是工具慢,我希望它花费1秒或更短的时间,我想将图像裁剪成一个部分,即在图像的点0,0处的80,60部分并在那里进行搜索,当它完成时它执行相同的裁剪但y + 60并继续直到它覆盖整个图像,假设它是800乘600像素

我目前使用的代码是:

        public bool findImage(Bitmap small, Bitmap large, out Point location)
    {
        //Loop through large images width
        for (int largeX = 0; largeX < large.Width; largeX++)
        {
            //And height
            for (int largeY = 0; largeY < large.Height; largeY++)
            {
                //Loop through the small width
                for (int smallX = 0; smallX < small.Width; smallX++)
                {
                    //And height
                    for (int smallY = 0; smallY < small.Height; smallY++)
                    {
                        //Get current pixels for both image
                        Color currentSmall = small.GetPixel(smallX, smallY);
                        Color currentLarge = large.GetPixel(largeX + smallX, largeY + smallY);
                        //If they dont match (i.e. the image is not there)

                        if (!colorsMatch(currentSmall, currentLarge))
                            //Goto the next pixel in the large image

                            goto nextLoop;
                    }
                }
                //If all the pixels match up, then return true and change Point location to the top left co-ordinates where it was found
                location = new Point(largeX, largeY);
                return true;
            //Go to next pixel on large image
            nextLoop:
                continue;
            }
        }
        //Return false if image is not found, and set an empty point
        location = Point.Empty;
        return false;
    }

1 个答案:

答案 0 :(得分:2)

由于您正在寻找完全匹配,因此您可以对大图片进行下采样,并对您的小图片进行下采样(但有几次,在不同的偏移处)。然后搜索下采样图片的精确匹配(可能会递归)。

你的大图片听起来 大,你可以通过Boyer-Moore搜索其中一条线来逃脱。