Blobs filtering by size.

The filter performs filtering of blobs by their size in the specified source image - all blobs, which are smaller or bigger then specified limits, are removed from the image.

The image processing filter treats all none black pixels as objects' pixels and all black pixel as background.

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

Sample usage:

// create filter BlobsFiltering filter = new BlobsFiltering( ); // configure filter filter.CoupledSizeFiltering = true; filter.MinWidth = 70; filter.MinHeight = 70; // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlaceFilter
Exemple #1
1
 private string reconhecerCaptcha(Image img)
 {
     Bitmap imagem = new Bitmap(img);
     imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
     Erosion erosion = new Erosion();
     Dilatation dilatation = new Dilatation();
     Invert inverter = new Invert();
     ColorFiltering cor = new ColorFiltering();
     cor.Blue = new AForge.IntRange(200, 255);
     cor.Red = new AForge.IntRange(200, 255);
     cor.Green = new AForge.IntRange(200, 255);
     Opening open = new Opening();
     BlobsFiltering bc = new BlobsFiltering();
     Closing close = new Closing();
     GaussianSharpen gs = new GaussianSharpen();
     ContrastCorrection cc = new ContrastCorrection();
     bc.MinHeight = 10;
     FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter);
     pictureBox.Image = seq.Apply(imagem);
     string reconhecido = OCR((Bitmap)pictureBox.Image);
     return reconhecido;
 }
Exemple #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(pictureBox2.Image);
            b = new BlobsFiltering(2, 2, b.Width, b.Height).Apply(b);
            b = new BlobsFiltering(1, 2, b.Width, b.Height).Apply(b);
            b = new BlobsFiltering(1, 1, b.Width, b.Height).Apply(b);
            //b = new BlobsFiltering(10,10, b.Width, b.Height).Apply(b);

            this.pictureBox2.Image = b;
        }
        /// <summary>
        /// Detects and recognizes cards from source image
        /// </summary>
        /// <param name="source">Source image to be scanned</param>
        /// <returns>Recognized Cards</returns>
        public CardCollection Recognize(Bitmap source)
        {
            CardCollection collection = new CardCollection();  //Collection that will hold cards
            Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image

            FiltersSequence seq = new FiltersSequence();
            seq.Add(Grayscale.CommonAlgorithms.BT709);  //First add  grayScaling filter
            seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
            temp = seq.Apply(source); // Apply filters on source image

            //Extract blobs from image whose size width and height larger than 150
            BlobCounter extractor = new BlobCounter();
            extractor.FilterBlobs = true;
            extractor.MinWidth = extractor.MinHeight = 150;
            extractor.MaxWidth = extractor.MaxHeight = 350;
            extractor.ProcessImage(temp);

            //Will be used transform(extract) cards on source image 
            QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation();

            //Will be used resize(scaling) cards 
            ResizeBilinear resizer = new ResizeBilinear(CardWidth, CardHeight);

            foreach (Blob blob in extractor.GetObjectsInformation())
            {
                //Get Edge points of card
                List<IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob); 
                //Calculate/Find corners of card on source image from edge points
                List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);

                quadTransformer.SourceQuadrilateral = corners; //Set corners for transforming card 
                quadTransformer.AutomaticSizeCalculaton = true;

                Bitmap cardImg = quadTransformer.Apply(source); //Extract(transform) card image

                if (cardImg.Width > cardImg.Height) //If card is positioned horizontally
                    cardImg.RotateFlip(RotateFlipType.Rotate90FlipNone); //Rotate
                cardImg =  resizer.Apply(cardImg); //Normalize card size

                Card card = new Card(cardImg, corners.ToArray()); //Create Card Object
                char color = ScanColor(card.GetTopRightPart()); //Scan color
                bool faceCard = IsFaceCard(cardImg); //Determine type of card(face or not)

                if (!faceCard)
                {
                    card.Suit = ScanSuit(cardImg, color); //Scan Suit of non-face card
                    card.Rank = ScanRank(cardImg); //Scan Rank of non-face card
                }
                else
                {
                    Bitmap topRight = card.GetTopRightPart();
                    seq.Clear();
                    seq.Add(Grayscale.CommonAlgorithms.BT709);
                    seq.Add(new BradleyLocalThresholding());
                    topRight = seq.Apply(topRight);
                    BlobsFiltering bFilter = new BlobsFiltering(5, 5, 150, 150);
                    bFilter.ApplyInPlace(topRight); //Filter blobs that can not be a suit
            
                    card.Suit = ScanFaceSuit(topRight, color); //Scan suit of face card
                    card.Rank = ScanFaceRank(topRight); //Scan rank of face card
                }
                collection.Add(card); //Add card to collection
            }
            return collection;
        }
Exemple #4
0
        private void InitFilters()
        {
            L_brownFilter = new ColorFiltering();
            D_brownFilter = new ColorFiltering();

            L_brownFilter.Red = new IntRange(125, 140);
            L_brownFilter.Green = new IntRange(95, 110);
            L_brownFilter.Blue = new IntRange(110, 130);

            D_brownFilter.Red = new IntRange(55, 85);
            D_brownFilter.Green = new IntRange(45, 75);
            D_brownFilter.Blue = new IntRange(45, 75);

            blobFilter = new BlobsFiltering();
            blobFilter.CoupledSizeFiltering = true;
            blobFilter.MinWidth = 70;
            blobFilter.MinHeight = 70;

            diffFilter = new Difference();
            diffFilter.OverlayImage = back;

            thresholdFilter = new Threshold(40);

            erosionFilter = new Erosion();

            edgeFilter = new Edges();

            openFilter = new Opening();

            pixelFilter = new Pixellate();

            morphFilter = new Morph();
            morphFilter.SourcePercent = 0.9;

            towardsFilter = new MoveTowards();
            towardsFilter.StepSize = 10;

            blobCounter = new BlobCounter();
            blobGrabber = new ExtractBiggestBlob();
        }
 protected override void UpdateFilter()
 {
     filter = new BlobsFiltering(MinWidth, MinHeight, MaxWidth, MaxHeight, CoupledFiltering);
 }
Exemple #6
0
        private void button2_Click(object sender, EventArgs e)
        {
            button2.Text = "处理中";
            switch (comboBox4.SelectedIndex)
            {
            case 0:
            {
                Bitmap      temp    = (Bitmap)pictureBox1.Image;
                OilPainting filter3 = new OilPainting(10);
                // apply the filter
                filter3.ApplyInPlace(temp);
                this.pictureBox2.Image = ResizeBitmap(temp);
                break;
            }

            case 1:
            {
                Bitmap temp = (Bitmap)pictureBox1.Image;
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();
                temp = edgeDetector.Apply(temp);
                temp = new Threshold((int)numericUpDown1.Value).Apply(temp);

                //FillHoles filter2 = new FillHoles();
                //filter2.MaxHoleHeight = MinHeight;
                //filter2.MaxHoleWidth = MaxWidth;
                //filter2.CoupledSizeFiltering = false;
                // apply the filter
                //temp = filter2.Apply(temp);
                //HorizontalRunLengthSmoothing hrls = new HorizontalRunLengthSmoothing(40);
                // apply the filter
                //hrls.ApplyInPlace(temp);

                /*AForge.Imaging.Filters.BlobsFiltering filter = new AForge.Imaging.Filters.BlobsFiltering();
                 * // 设置过滤条件(对象长、宽至少为70)
                 * filter.CoupledSizeFiltering = true;
                 * filter.MaxWidth = (int)numericUpDown3.Value;
                 * filter.MaxHeight = (int)numericUpDown4.Value;
                 * filter.MinWidth = (int)numericUpDown5.Value;
                 * filter.MinHeight = (int)numericUpDown6.Value;
                 * filter.ApplyInPlace(temp);*/



                BlobCounter blobCounter = new BlobCounter();

                blobCounter.MinHeight    = 32;
                blobCounter.MinWidth     = 32;
                blobCounter.FilterBlobs  = true;
                blobCounter.ObjectsOrder = ObjectsOrder.Size;

                // 4 - find all stand alone blobs
                blobCounter.ProcessImage(temp);
                Blob[]             blobs        = blobCounter.GetObjectsInformation();
                SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

                List <IntPoint> corners  = null;
                List <IntPoint> corners2 = null;
                for (int i = 0, n = blobs.Length; i < n; i++)
                {
                    List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                    // does it look like a quadrilateral ?
                    if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                    {
                        // get edge points on the left and on the right side
                        List <IntPoint> leftEdgePoints, rightEdgePoints;
                        blobCounter.GetBlobsLeftAndRightEdges(blobs[i],
                                                              out leftEdgePoints, out rightEdgePoints);
                        listBox1.DataSource = leftEdgePoints;
                        listBox2.DataSource = rightEdgePoints;
                    }
                }
                //listBox1.DataSource = corners;
                //listBox2.DataSource = corners2;
                this.pictureBox1.Image = temp;
                break;
            }

            case 2:
            {
                Bitmap bt2 = new Bitmap(@"D:\TCL条码\截图01.bmp");
                Bitmap bt1 = new Bitmap(@"D:\TCL条码\截图03.bmp");
                //Bitmap bt1 = new Bitmap(pictureBox2.Image);
                ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.80f);
                //基于一定的相似性阈值获得匹配块
                TemplateMatch[] matchings = tm.ProcessImage(bt1, bt2);
                BitmapData      data      = bt1.LockBits(
                    new Rectangle(0, 0, bt1.Width, bt1.Height),
                    ImageLockMode.ReadWrite, bt1.PixelFormat);
                foreach (TemplateMatch m in matchings)
                {
                    Drawing.Rectangle(data, m.Rectangle, Color.Red);
                }
                bt1.UnlockBits(data);
                pictureBox2.Image = bt1;
                break;
            }

            case 3:
            {
                Bitmap bt2 = new Bitmap(@"D:\TCL条码\Canny算法.png");
                AForge.Imaging.Filters.BlobsFiltering filter = new AForge.Imaging.Filters.BlobsFiltering();
                // 设置过滤条件(对象长、宽至少为70)
                filter.CoupledSizeFiltering = true;
                filter.MaxWidth             = (int)numericUpDown3.Value;
                filter.MaxHeight            = (int)numericUpDown4.Value;
                filter.MinWidth             = (int)numericUpDown5.Value;
                filter.MinHeight            = (int)numericUpDown6.Value;
                filter.ApplyInPlace(bt2);
                pictureBox1.Image = bt2;
                byte[] RESULT = BitmapToBytes(bt2);
                break;
            }

            case 4:
            {
                Bitmap temp = (Bitmap)pictureBox1.Image;
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector();
                filter.ApplyInPlace(temp);
                pictureBox2.Image = temp;
                break;
            }
            }

            button2.Text = "处理";
        }