Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让这个简单的c#图像搜索代码更快?_C#_Image Processing - Fatal编程技术网

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

让这个简单的c#图像搜索代码更快?,c#,image-processing,C#,Image Processing,我正在扫描屏幕上的一个按钮,使用下面的代码单击 我传入两个位图,一个是按钮的图片,另一个是屏幕截图 有没有什么方法可以加快这个基本方法的速度 private PositionToClick IsInCapture(Bitmap searchFor, Bitmap searchIn) { for (int x = 0; x < searchIn.Width; x++) { for (int y = 0; y < searchIn.Height; y++)

我正在扫描屏幕上的一个按钮,使用下面的代码单击

我传入两个位图,一个是按钮的图片,另一个是屏幕截图

有没有什么方法可以加快这个基本方法的速度

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();
}
private position点击IsInCapture(位图搜索,位图搜索)
{
对于(int x=0;x
您可以先将位图转换为字节数组,然后比较这些数组。 此外,您还可以通过减去正在搜索的图像的宽度和高度来限制搜索区域。 我没有测试下面的代码,它只是我的意思的一个例子

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();
    }
}
private position点击IsInCapture(位图搜索,位图搜索)
{
var searchForArray=ImageToByte2(searchFor);
var searchInArray=ImageToByte2(searchIn);

对于(x x=0;x看起来更好,避免像这样的GETEng.而是查找如何复制视频内存块并比较它们。尝试使用描述的方法;用标准位图扫描每一个可能的X/Y偏移的主题位图的裁剪。我强烈建议您考虑启发式方法-PrHAP。让我们先看看按钮的左边缘,然后再看看其他功能。如果有人偶然发现,这正是我所需要的。非常快。