1

目前我用它来查找屏幕上另一个图像中出现的位图,即一个窗口,但它的工具很慢,我希望它花费 1 秒或更短的时间,我正在考虑将图像裁剪成一个部分,即 80,60在图像的 0,0 点处的部分并在那里搜索,当它完成时,它会进行相同的裁剪,但 y+60 并继续,直到它覆盖了整个图像,假设它是 800 x 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;
    }
4

1 回答 1

2

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

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

于 2012-08-25T01:09:52.487 回答