简体   繁体   English

使这个简单的 c# 图像搜索代码更快?

[英]Make this simple c# image searching code quicker?

I am scanning the screen for a button to click using the code below.我正在扫描屏幕以使用下面的代码单击按钮。

I pass in two bitmaps one is a picture of the button the other is a screenshot.我传入了两个位图,一个是按钮的图片,另一个是屏幕截图。

Is there anyway I can speed this basic method up?无论如何我可以加快这种基本方法的速度吗?

private PositionToClick IsInCapture(Bitmap searchFor, Bitmap searchIn)
{
    for (int x = 0; x < searchIn.Width; x++)
    {
        for (int y = 0; y < searchIn.Height; y++)
        {
            bool invalid = false;
            int k = x, l = y;
            for (int a = 0; a < searchFor.Width; a++)
            {
                l = y;
                for (int b = 0; b < searchFor.Height; b++)
                {
                    if (searchFor.GetPixel(a, b) != searchIn.GetPixel(k, l))
                    {
                        invalid = true;
                        break;
                    }
                    else
                        l++;
                }
                if (invalid)
                    break;
                else
                    k++;
            }

            if (!invalid)
                return new PositionToClick() { X = x, Y = y, found = true };
        }
    }

    return new PositionToClick();
}

You can convert the bitmaps to byte arrays first, and then compare those arrays.您可以先将位图转换为字节数组,然后再比较这些数组。 Also, you can restrict your search area by subtracting the width and height of the image you are searching from.此外,您可以通过减去您正在搜索的图像的宽度和高度来限制您的搜索区域。 I did not test the code below, it's just an illustration of what I mean.我没有测试下面的代码,这只是我的意思的说明。

private PositionToClick IsInCapture(Bitmap searchFor, Bitmap searchIn)
{
    var searchForArray = ImageToByte2(searchFor);
    var searchInArray = ImageToByte2(searchIn);
    for (int x = 0; x <= searchIn.Width - searchFor.Width; x++)
    {
        for (int y = 0; y <= searchIn.Height - searchFor.Height; y++)
        {
            bool invalid = false;
            int k = x, l = y;
            for (int a = 0; a < searchFor.Width; a++)
            {
                l = y;
                for (int b = 0; b < searchFor.Height; b++)
                {
                    var pixelFor = searchForArray[a * searchFor.Width + b];
                    var pixelIn = searchInArray[k + searchIn.Width + l];
                    if (pixelIn != pixelFor)
                    {
                        invalid = true;
                        break;
                    }
                    else
                        l++;
                }
                if (invalid)
                    break;
                else
                    k++;
            }

            if (!invalid)
                return new PositionToClick() { X = x, Y = y, found = true };
        }
    }
    return new PositionToClick();
}

public static byte[] ImageToByte2(Image img)
{
    using (var stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        return stream.ToArray();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM