Flood filling with specified color starting from specified point.

The filter performs image's area filling (4 directional) starting from the specified point. It fills the area of the pointed color, but also fills other colors, which are similar to the pointed within specified tolerance. The area is filled using specified fill color.

The filter accepts 8 bpp grayscale images and 24 bpp color images for processing.

Sample usage:

// create filter PointedColorFloodFill filter = new PointedColorFloodFill( ); // configure the filter filter.Tolerance = Color.FromArgb( 150, 92, 92 ); filter.FillColor = Color.FromArgb( 255, 255, 255 ); filter.StartingPoint = new IntPoint( 150, 100 ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlacePartialFilter
        private Bitmap processImage(Bitmap img)
        {
            //Generate closing structural element
            short[,] structEl = new short[13, 13];

            for (int i = 0; i < 13; i++)
                for (int j = 0; j < 13; j++)
                    if ((i - 6) * (i - 6) + (j - 6) * (j - 6) < 64)
                        structEl[i, j] = 1;
                    else
                        structEl[i, j] = -1;

            //Initialize filters
            HSLFiltering borderFind = new HSLFiltering();
            Closing borderClose = new Closing(structEl);
            Invert invert = new Invert();
            Grayscale grayFilter = new Grayscale(0, 0, 1.0);
            Threshold bwFilter = new Threshold(1);

            PointedColorFloodFill blackout = new PointedColorFloodFill();
            blackout.Tolerance = Color.FromArgb(0, 0, 0);
            blackout.FillColor = Color.FromArgb(0, 0, 0);

            ExtractBiggestBlob getgame = new ExtractBiggestBlob();
            getgame.OriginalImage = new Bitmap(img);

            GrayscaleToRGB colorFilter = new GrayscaleToRGB();

            //Color determined with ColorProbe.
            borderFind.Hue        = new IntRange(190, 200);
            borderFind.Saturation = new Range(0.6f, 0.8f);
            borderFind.Luminance  = new Range(0.6f, 1.0f);

            borderFind.ApplyInPlace(img);
            borderClose.ApplyInPlace(img);
            img = grayFilter.Apply(img);
            bwFilter.ApplyInPlace(img);
            invert.ApplyInPlace(img);
            img = colorFilter.Apply(img);

            blackout.StartingPoint = new AForge.IntPoint(0, 0);
            blackout.ApplyInPlace(img);

            img = getgame.Apply(img);

            int tilesx = img.Width / 56;
            int tilesy = img.Height / 56;
            int offsetx = 56 * (int)(tilesx - img.Width / 56.0);
            int offsety = 56 * (int)(tilesy - img.Height / 56.0);

            if ((Math.Abs(offsetx) > 11) || (Math.Abs(offsety) > 11))
                    throw new GameNotFoundException();

            List<IntPoint> corners = new List<IntPoint>();
            Dictionary<IntPoint, Bitmap> tiles = new Dictionary<IntPoint, Bitmap>();
            SimpleQuadrilateralTransformation tileXtract = new SimpleQuadrilateralTransformation();

            for (int j = 0; j < tilesy; j++)
                for (int i = 0; i < tilesx; i++)
                {
                    corners.Add(new IntPoint(offsetx + i * 56,           offsety + j * 56          ));
                    corners.Add(new IntPoint(offsetx + i * 56,           offsety + (j + 1) * 56 - 1));
                    corners.Add(new IntPoint(offsetx + (i + 1) * 56 - 1, offsety + (j + 1) * 56 - 1));
                    corners.Add(new IntPoint(offsetx + (i + 1) * 56 - 1, offsety + j * 56          ));

                    tileXtract.SourceQuadrilateral = corners;

                    tiles.Add(new IntPoint(i, j), tileXtract.Apply(img));

                    corners.Clear();
                }

            img = (Bitmap)Properties.Resources.ResourceManager.GetObject("cb");

            /*Graphics g = Graphics.FromImage(img);
            Pen bluePen = new Pen(Color.Blue, 2);

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                if (edgePoints.Count > 1)
                {
                    List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                    g.DrawPolygon(bluePen, ToPointsArray(corners));
                }
            }

            bluePen.Dispose();
            g.Dispose();
            */

            return img;
        }
示例#2
0
 /// <summary>
 /// Flood fill in a BFS manner, so as not to overwhelm the stack
 /// </summary>
 /// <param name="image">the image we wish to fill on</param>
 /// <param name="xpixel">the x pixel to sample from</param>
 /// <param name="ypixel">the y pixel to sample from</param>
 /// <param name="threshold">the threshold of difference</param>
 /// <returns>the background which can be subtracted</returns>
 public static Bitmap FloodFill(Bitmap image, int xpixel, int ypixel, double threshold, Bgr myColor)
 {
     AForge.Imaging.Filters.PointedColorFloodFill filter = new PointedColorFloodFill();
     int thresh = (int)threshold;
     filter.Tolerance = Color.FromArgb(thresh, thresh, thresh);
     filter.FillColor = Color.Black;
     filter.StartingPoint = new IntPoint(xpixel, ypixel);
     Bitmap dotjpg = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb); //force the jpgs
     return filter.Apply(dotjpg);
 }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        /// <remarks></remarks>
        public TileOCR(string trainingPath)
        {
            classifier = new KNearestClassifier(1, Metric.EuclideanDistance, WeightMode.InverseDistance);
            training = LoadInstancesFromBitmaps(trainingPath);
            
            classifier.Train(training);

            results = new List<OCRResult>();

            grayscale = new Grayscale(0, 0.85, 0.15);
            invert = new Invert();
            resize = new ResizeNearestNeighbor(32, 32);
            floodFill = new PointedColorFloodFill(Color.Black);
            dilate = new BinaryDilatation3x3();
            blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinWidth = 4;
            blobCounter.MinHeight = 14;
            blobCounter.MaxWidth = 30;
            blobCounter.MaxHeight = 30;
            blobCounter.ObjectsOrder = ObjectsOrder.XY;
            threshold = new BradleyLocalThresholding();
            threshold.PixelBrightnessDifferenceLimit = 0;
            //Threshold.WindowSize = 20;
            threshold.WindowSize = 24;
        }