Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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#_.net_Algorithm_Search_Image Processing - Fatal编程技术网

C# 优化我的图像搜索例程

C# 优化我的图像搜索例程,c#,.net,algorithm,search,image-processing,C#,.net,Algorithm,Search,Image Processing,我的例行程序如下SpeedyBitmap类仅锁定每个位图以更快地访问像素数据: private static bool TestRange(int number, int lower, int upper) { return number >= lower && number <= upper; } public static List<Point> Search(Bitmap toSearch, Bitmap t

我的例行程序如下SpeedyBitmap类仅锁定每个位图以更快地访问像素数据:

    private static bool TestRange(int number, int lower, int upper) {
        return number >= lower && number <= upper;
    }

    public static List<Point> Search(Bitmap toSearch, Bitmap toFind, double percentMatch = 0.85, byte rTol = 2, byte gTol = 2, byte bTol = 2) {
        List<Point> points = new List<Point>();
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        int findArea = toFind.Width * toFind.Height;
        int allowedMismatches = findArea - (int)(findArea * percentMatch);
        int mismatches = 0;

        using (SpeedyBitmap speedySearch = new SpeedyBitmap(toSearch)) {
            using (SpeedyBitmap speedyFind = new SpeedyBitmap(toFind)) {
                for (int i = 0; i < speedySearch.Height - speedyFind.Height + 1; i++) {
                    for (int j = 0; j < speedySearch.Width - speedyFind.Width + 1; j++) {
                        for (int k = 0; k < speedyFind.Height; k++) {
                            for (int l = 0; l < speedyFind.Width; l++) {
                                Color searchColor = speedySearch[j + l, i + k];
                                Color findColor = speedyFind[l, k];

                                if (!TestRange(searchColor.R, findColor.R - rTol, findColor.R + rTol) ||
                                    !TestRange(searchColor.G, findColor.G - gTol, findColor.G + gTol) ||
                                    !TestRange(searchColor.B, findColor.B - bTol, findColor.B + bTol)) {
                                    mismatches++;

                                    if (mismatches > allowedMismatches) {
                                        mismatches = 0;

                                        goto notFound;
                                    }
                                }
                            }
                        }

                        points.Add(new Point(j, i));

                        continue;

                        notFound:
                        ;
                    }
                }
            }
        }

        Console.WriteLine(stopwatch.ElapsedMilliseconds);

        return points;
    }
搜索中等大小的图像1000x1000需要20秒以上。删除匹配百分比需要几百毫秒,所以我想我已经确定了主要瓶颈所在


我怎样才能让它跑得更快?也许我可以将二维数组展平为一维数组,然后对这两个数组应用某种序列检查,以便可能将toFind位图数据出现的最长序列与相应的公差和匹配百分比进行匹配。如果这是一个好的解决方案,我将如何开始实施它?

我真的不知道删除百分比匹配将如何使其运行更快,除非您正在搜索的内容不存在,并且您更快地获得不匹配。如果它存在,你仍然会执行所有匹配的百分比,或者不匹配的百分比。据我所知,你是在一个较大的图像中搜索较小的图像。当谈到20sec处理时,较小的是什么尺寸,较大的是什么尺寸,不清楚哪个是1000x1000。还有,结果是对还是错?