首页 > 解决方案 > 如何使用 Floodfill 加速 blob 像素列表搜索?

问题描述

我正在尝试从灰色黑白图像中删除小于 100 像素的连接像素,以在 ocr 中获得更好的结果。为了获得连接的像素,我使用了 floodfill 方法,它返回 blob,它是像素列表。如果 blob 大小大于 100 像素,则将 blob 添加到 blob 列表中。之后,清除整个事物并使用 blob 列表重新绘制像素。

该过程按预期工作,但处理小图像需要 4 分钟。我希望有更好的方法或方法使它更快。

我尝试使用 Aforge 的 BlobCounter bc = new BlobCounter();。它只能将整个图像作为一个单独的 blob 找到并且它不起作用。

private List<Blob1> getbloblist(Bitmap Bmp1, int thold)//thold=100
        {
            List<Blob1> bloblist = new List<Blob1>();
            List<System.Drawing.Point> allplist = new List<System.Drawing.Point>();

            for (int x1 = 0; x1 < Bmp1.Width; x1++)
            {
                for (int y1 = 0; y1 < Bmp1.Height; y1++)
                {
                    FF3result ff3result1 = new FF3result();                    
                    System.Drawing.Point cpoint1 = new System.Drawing.Point(x1, y1);

                    if(allplist.Contains(cpoint1))
                    {
                        continue;
                    }

                    Color chk1 = getcolorfast(cpoint1, Bmp1);//used lockbits
                    ff3result1 = FloodFillR(Bmp1, cpoint1, chk1, Color.Red, 100, allplist);
                    allplist = ff3result1.allplist;

                    if (ff3result1.Blob1.Size > thold)
                    {
                        bloblist.Add(ff3result1.Blob1);
                    }
                }
            }
            return bloblist;
        }

private FF3result FloodFillR(Bitmap bmp, System.Drawing.Point pt, Color targetColor, Color replacementColor, int sens, List<System.Drawing.Point> allplist)
        {
            FF3result ff3result1 = new FF3result();

            LockBitmap lockBitmap = new LockBitmap(bmp);
            lockBitmap.LockBits();

            Stack<System.Drawing.Point> pixels = new Stack<System.Drawing.Point>();

            pixels.Push(pt);

            while (pixels.Count > 0)
            {
                System.Drawing.Point a = pixels.Pop();
                if (a.X < lockBitmap.Width && a.X > 0 &&
                        a.Y < lockBitmap.Height && a.Y > 0)//make sure we stay within bounds
                {
                    pixel1 pixel1 = new pixel1();
                    pixel1.color1 = targetColor;
                    pixel1.x1 = a.X;
                    pixel1.y1 = a.Y;
                    System.Drawing.Point pt2 = new System.Drawing.Point(a.X, a.Y);
                    if (!allplist.Contains(pt2))
                    {
                        allplist.Add(pt2);
                        if (!ff3result1.Blob1.Blob.Contains(pixel1))
                        {                            
                            if (mypic.ColorMatch(sens, lockBitmap.GetPixel(a.X, a.Y), targetColor))//colormatch sense
                            {
                                //lockBitmap.SetPixel(a.X, a.Y, replacementColor);
                                ff3result1.Blob1.Blob.Add(pixel1);
                                ff3result1.Blob1.Size++;

                                pixels.Push(new System.Drawing.Point(a.X - 1, a.Y));
                                pixels.Push(new System.Drawing.Point(a.X + 1, a.Y));
                                pixels.Push(new System.Drawing.Point(a.X, a.Y - 1));
                                pixels.Push(new System.Drawing.Point(a.X, a.Y + 1));
                            }
                        }
                    }
                }
            }

            lockBitmap.UnlockBits();
            ff3result1.allplist = allplist;

            return ff3result1;
        }

有一个更好的方法吗?

标签: c#

解决方案


推荐阅读