Adaptive thresholding using the internal image.

The image processing routine implements local thresholding technique described by Derek Bradley and Gerhard Roth in the "Adaptive Thresholding Using the Integral Image" paper.

The brief idea of the algorithm is that every image's pixel is set to black if its brightness is t percent lower (see PixelBrightnessDifferenceLimit) than the average brightness of surrounding pixels in the window of the specified size (see WindowSize), othwerwise it is set to white.

Sample usage:

// create the filter BradleyLocalThresholding filter = new BradleyLocalThresholding( ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Наследование: BaseInPlaceFilter
Пример #1
1
        //Adaptively threshold a Bitmap (using Bradley Local Thresholding)
        public static Bitmap AdaptiveThreshold(Bitmap img)
        {
            Bitmap greyImg;

            //If the image is 8bpp
            if (img.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                greyImg = img;
            }
            else //Otherwise the image needs converting to greyscale before any further processing
            {
                greyImg = Grayscale.CommonAlgorithms.BT709.Apply(img);
            }

            //Threshold the image
            BradleyLocalThresholding bradleyLocalThreshold = new BradleyLocalThresholding();
            Bitmap bradleyLocalImg = bradleyLocalThreshold.Apply(greyImg);

            //Clean up
            if(greyImg != img) //If we had to greyscale the input, dispose the greyscale Bitmap
            {
                greyImg.Dispose();
            }

            return bradleyLocalImg;
        }
Пример #2
0
        private void CorrectLevels()
        {
            ImageStatistics ims = new ImageStatistics(_recogimg);

            Histogram gr = ims.Gray;
            double median = gr.Median;
            double mean = gr.Mean;
            double stdev = gr.StdDev;
            //30 170 10

            //for (int i = 30; i < 170; i += 10)
            //{
            //    this.CorrectLevel(i, (int)mean);
            //    ims = new ImageStatistics(_recogimg);
            //    gr = ims.Gray;
            //    stdev = gr.StdDev;
            //    mean = gr.Mean;
            //    if (stdev >= 65 & mean >= 220) { break; }
            //}

            BradleyLocalThresholding filter = new BradleyLocalThresholding();
            // apply the filter
            filter.ApplyInPlace(_recogimg);
        }
Пример #3
0
 // On Filters->Grayscale item
 private void grayscaleFiltersItem_Click( object sender, System.EventArgs e )
 {
     ApplyFilter( Grayscale.CommonAlgorithms.BT709 );
     BradleyLocalThresholding filter = new BradleyLocalThresholding();
     // apply the filter
     filter.ApplyInPlace(filteredImage);
     grayscaleFiltersItem.Checked = true;
 }
Пример #4
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;
        }
Пример #5
0
        private void videoNewFrame(object sender, NewFrameEventArgs args)
        {
            Bitmap temp = args.Frame.Clone() as Bitmap;

            switch (comboBox3.SelectedIndex)
            {
            case 0:

                break;

            case 1:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                break;

            case 2:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                temp = new Threshold((int)numericUpDown1.Value).Apply(temp);
                break;

            case 3:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector();
                filter.ApplyInPlace(temp);
                break;

            case 4:
                for (int i = 0; i < (int)numericUpDown2.Value; i++)
                {
                    temp = new Dilatation().Apply(temp);
                }
                break;

            case 5:
                for (int i = 0; i < (int)numericUpDown2.Value; i++)
                {
                    temp = new Erosion().Apply(temp);
                }
                break;

            case 6:
                temp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp);
                AForge.Imaging.Filters.BradleyLocalThresholding filter1 = new AForge.Imaging.Filters.BradleyLocalThresholding();
                filter1.ApplyInPlace(temp);
                break;
            }

            switch (comboBox2.SelectedIndex)
            {
            case 0:
            {
                BarcodeReader reader = new BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                Result result = reader.Decode(temp);
                if (result != null)
                {
                    if (wait == false)
                    {
                        MessageBox.Show(result.ToString());
                        wait = true;
                    }
                }
                else
                {
                    wait = false;
                }
                break;
            }

            case 1:
            {
                /*Bitmap pImg = MakeGrayscale3((Bitmap)temp);
                 * using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
                 * {
                 *  scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0);
                 *  scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1);
                 *  scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1);
                 *
                 *  List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
                 *  symbols = scanner.Scan((System.Drawing.Image)pImg);
                 *  if (symbols != null && symbols.Count > 0)
                 *  {
                 *      string result = string.Empty;
                 *      symbols.ForEach(s => result += s.Data);
                 *      if (wait == false)
                 *      {
                 *          MessageBox.Show(result);
                 *          wait = true;
                 *      }
                 *  }
                 *  else
                 *      wait = false;
                 * }*/
                break;
            }

            case 2:
            {
                break;
            }

            case 3:
                break;
            }
            this.pictureBox1.Image = ResizeBitmap(temp);
        }