예제 #1
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;
        }
 private void InitializeFilters()
 {
     grayscaleFilter               = new Grayscale(0.299, 0.587, 0.114);
     bradleyLocalFilter            = new BradleyLocalThresholding();
     bradleyLocalFilter.WindowSize = 9;
     bradleyLocalFilter.PixelBrightnessDifferenceLimit = 0.01f;
     plateBlobsFiltering     = new BlobsFiltering(10, 20, 80, 66);
     blobCounter             = new BlobCounter();
     blobCounter.FilterBlobs = true;
     blobCounter.MinWidth    = 50;
     blobCounter.MinHeight   = 10;
     blobCounter.MaxWidth    = 520;
     blobCounter.MaxHeight   = 110;
     plateBlobCounter        = new BlobCounter();
     shapeChecker            = new SimpleShapeChecker();
     otsuThresholdFilter     = new OtsuThreshold();
     fillHoles = new FillHoles();
     fillHoles.MaxHoleWidth  = 100;
     fillHoles.MaxHoleHeight = 40;
     pen                             = new Pen(Color.GreenYellow, 4);
     openingFilter                   = new Opening();
     contrastCorrectionFilter        = new ContrastCorrection(80);
     colorFiltering                  = new ColorFiltering();
     colorFiltering.Red              = new IntRange(150, 255);
     colorFiltering.Green            = new IntRange(150, 255);
     colorFiltering.Blue             = new IntRange(150, 255);
     colorFiltering.FillOutsideRange = true;
     invert                          = new Invert();
 }
예제 #3
0
        private Image <Gray, Byte> prepareImg2(System.Drawing.Image img, int threshold, bool invertImg)
        {
            Image <Gray, Byte> GrayImg = new Image <Gray, byte>((Bitmap)img);


            EmgImgProcssing = new ImageProcessing_Emgu();


            // create the filter
            BradleyLocalThresholding filter2 = new BradleyLocalThresholding();

            // apply the filter
            filter2.ApplyInPlace(GrayImg.Bitmap);

            GrayImg = GrayImg.Erode(1).Dilate(5).Erode(1);

            // create filter
            Invert filter3 = new Invert();

            // apply the filter
            filter3.ApplyInPlace(GrayImg.Bitmap);


            return(GrayImg);
        }
예제 #4
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);
        }
예제 #5
0
        public void bradley(ref Bitmap tmp)
        {
            BradleyLocalThresholding filter = new BradleyLocalThresholding();

            // filter.AssertCanApply(tmp.PixelFormat);
            filter.ApplyInPlace(tmp);
        }
예제 #6
0
        // adds BradleyLocalThreshold filter to the picture
        private Bitmap BradleyLocalThreshold(Bitmap bitmap)
        {
            Image <Gray, byte>       inputImage = new Image <Gray, byte>(bitmap);
            BradleyLocalThresholding bradleyLocalThresholding = new BradleyLocalThresholding();
            Bitmap filtered = new Bitmap(bradleyLocalThresholding.Apply(inputImage.Bitmap));

            return(filtered);
        }
예제 #7
0
        private static Bitmap PreProcess(Bitmap bmp)
        {
            Grayscale gfilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Invert    ifilter = new Invert();
            BradleyLocalThresholding thfilter = new BradleyLocalThresholding();

            bmp = gfilter.Apply(bmp);
            thfilter.ApplyInPlace(bmp);
            ifilter.ApplyInPlace(bmp);
            return(bmp);
        }
예제 #8
0
        // source
        // http://stackoverflow.com/questions/35980717/why-doesnt-aforge-net-blobcounter-getobjectsinformation-detect-objects
        private Bitmap PreProcess(Bitmap bmp)
        {
            //Those are AForge filters "using Aforge.Imaging.Filters;"
            Grayscale gfilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Invert    ifilter = new Invert();
            BradleyLocalThresholding thfilter = new BradleyLocalThresholding();

            bmp = gfilter.Apply(bmp);
            thfilter.ApplyInPlace(bmp);
            ifilter.ApplyInPlace(bmp);
            return(bmp);
        }
예제 #9
0
        public mAdjustBradley(float differenceLimit)
        {
            DifferenceLimit = differenceLimit;

            BitmapType = mFilter.BitmapTypes.None;

            Effect = new BradleyLocalThresholding();

            Effect.PixelBrightnessDifferenceLimit = DifferenceLimit;

            filter = Effect;
        }
        public override Bitmap ApplyFilter(List <Bitmap> bitmaps)
        {
            BradleyLocalThresholding filter = new BradleyLocalThresholding();

            if (bitmaps[0].PixelFormat != PixelFormat.Format16bppGrayScale)
            {
                BlackWhiteFilter filterGray = new BlackWhiteFilter(0.33f, 0.33f, 0.33f);
                bitmaps[0] = filterGray.ApplyFilter(bitmaps);
                return(filter.Apply(bitmaps[0]));
            }
            return(bitmaps[0]);
        }
예제 #11
0
 /// <summary>
 /// 设置图片为黑白色
 /// </summary>
 /// <param name="img"></param>
 /// <returns></returns>
 public static System.Drawing.Image BlackWhitePhoto(Bitmap map)
 {
     if (map.PixelFormat != PixelFormat.Format8bppIndexed)
     {
         var       bitmap = ReplaceBackgroundColor(map, Color.White);
         Grayscale g      = new Grayscale(0.2125, 0.7154, 0.0721);
         bitmap = g.Apply(bitmap);
         BradleyLocalThresholding filter = new BradleyLocalThresholding();
         filter.ApplyInPlace(bitmap);
         return(bitmap);
     }
     return(map);
 }
예제 #12
0
        public Bitmap Filter(Bitmap input_image)
        {
            Grayscale gray_filter = new Grayscale(0.2125, 0.7154, 0.0721);
            BradleyLocalThresholding threshold_filter = new BradleyLocalThresholding();

            threshold_filter.PixelBrightnessDifferenceLimit = 0.5f;
            ResizeBicubic scale_small_filter = new ResizeBicubic(28, 28);
            Crop          crop_filter        = new Crop(new Rectangle(51, 51, 199, 199));


            BlobCounter blober = new BlobCounter();

            blober.FilterBlobs  = true;
            blober.ObjectsOrder = ObjectsOrder.Size;

            Bitmap image = gray_filter.Apply(input_image);

            image = threshold_filter.Apply(image);

            image = crop_filter.Apply(image);
            //blober.ProcessImage(image);

            //Blob[] blobs = blober.GetObjectsInformation();
            //if (blobs.Length > 0)
            //{
            //    var bigger = blobs[0];
            //    UnmanagedImage img = UnmanagedImage.FromManagedImage(image);
            //    blober.ExtractBlobsImage(img, bigger, false);
            //    Accord.Point mc = bigger.CenterOfGravity;
            //    Accord.Point ic = new Accord.Point((float)bigger.Image.Width / 2, (float)bigger.Image.Height / 2);


            //    float AngleRad = (ic.Y - mc.Y) / (ic.X - mc.X);
            //    float Angle = (float)(AngleRad * 180 / Math.PI);

            //    image = img.ToManagedImage();



            //    RotateBicubic rot_filter = new RotateBicubic(Angle);
            //    image = rot_filter.Apply(image);
            //}


            image = scale_small_filter.Apply(image);



            return(image);
        }
예제 #13
0
 public Camera()
 {
     grayFilter     = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
     scaleFilter    = new AForge.Imaging.Filters.ResizeBilinear(300, 300);
     threshldFilter = new AForge.Imaging.Filters.BradleyLocalThresholding();
     InvertFilter   = new AForge.Imaging.Filters.Invert();
     Blober         = new AForge.Imaging.BlobCounter();
     original       = new Bitmap(300, 300);
     g = Graphics.FromImage(original);
     Blober.FilterBlobs  = true;
     Blober.MinWidth     = 5;
     Blober.MinHeight    = 5;
     Blober.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size;
 }
예제 #14
0
 public static System.Drawing.Image BlackWhitePhoto(System.Drawing.Image images)
 {
     try
     {
         Bitmap map    = new Bitmap(images);
         Bitmap bitmap = new Bitmap(images.Width, images.Height);
         if (bitmap.PixelFormat != PixelFormat.Format8bppIndexed)
         {
             bitmap = Grayscale.CommonAlgorithms.BT709.Apply(map);
             //Grayscale g = new Grayscale(0.2125, 0.7154, 0.0721);
             //bitmap = g.Apply(map);
         }
         BradleyLocalThresholding filter = new BradleyLocalThresholding();
         filter.ApplyInPlace(bitmap);
         return(bitmap);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
예제 #15
0
 private void brandlyToolStripMenuItem_Click(object sender, EventArgs e)
 {
     islem          = new GrayscaleBT709().Apply(kaynak);
     islem          = new BradleyLocalThresholding().Apply(islem);
     islemBox.Image = islem;
 }
예제 #16
0
        static void Main(string[] args)
        {
            Console.Title = "Basic OCR.exe";
            string filename, path, dir, language;

            //take full path from user
            Console.WriteLine("Enter path:");
            //Separating filename and directory
            path     = Console.ReadLine();
            dir      = Path.GetDirectoryName(path);
            filename = Path.GetFileNameWithoutExtension(path);
            //Enter language for Tesseract engine
            Console.WriteLine("Enter language(hin-Hindi,eng-English,ori-Oriya):");
            language = Console.ReadLine();
            //Create Bitmap object
            Bitmap img2 = Accord.Imaging.Image.FromFile(path);

            //Image is first converted to Grayscale as Thresholding doesn't support coloured images as input
            Grayscale gray   = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap    result = gray.Apply(img2);

            // This blackens the whole area in shades:  Threshold thres = new Threshold(100);

            /*
             * Applying Bradley Thresholding which brightens up the area in shades hence a uniformly bright
             * image is obtained.
             */
            Console.WriteLine("Applying Bradley Local Thresholding...");
            BradleyLocalThresholding thres = new BradleyLocalThresholding();

            thres.WindowSize = 80;
            thres.PixelBrightnessDifferenceLimit = 0.1F;
            thres.ApplyInPlace(result);

            //Save the binarized image
            result.Save(dir + "\\" + filename + "_1thres.bmp");

            //Sharpening
            Console.WriteLine("Sharpening...");
            Sharpen sharp = new Sharpen();

            // apply the filter
            sharp.ApplyInPlace(result);
            //result.Save(dir + "\\" + filename + "_2sharp.bmp");

            //Bilateral Smoothing
            // create filter
            Console.WriteLine("Applying Bilateral smoothing...");
            BilateralSmoothing smooth = new BilateralSmoothing();

            smooth.KernelSize    = 7;
            smooth.SpatialFactor = 10;
            smooth.ColorFactor   = 60;
            smooth.ColorPower    = 0.5;
            // apply the filter
            smooth.ApplyInPlace(result);

            //Save cleaned image
            //result.Save(dir + "\\" + filename + "_3smooth.bmp");

            //Document skew, line detection
            DocumentSkewChecker skew = new DocumentSkewChecker();
            double angle             = skew.GetSkewAngle(result);

            Console.WriteLine("Skewing and rotating...");
            RotateBilinear rot = new RotateBilinear(-angle);

            rot.FillColor = Color.White;
            result        = rot.Apply(result);

            result.Save(dir + "\\" + filename + "_4rot.bmp");

            try
            {
                using (var engine = new TesseractEngine(@".\tessdata", language, EngineMode.Default))
                {
                    using (var img = result)
                    {
                        using (var page = engine.Process(img))
                        {
                            var text = page.GetText();
                            //Console.WriteLine("Text(get text): \r\n{0}", text);

                            StreamWriter sw;
                            string       Filename = dir + "\\" + filename + "_text.doc";
                            sw = File.CreateText(Filename);
                            Console.WriteLine("Generating file...");
                            string FileData = text;
                            sw.WriteLine(FileData);
                            sw.Close();

                            //We make a list of type Rectangle. It stores data of all the bounding boxes
                            List <Rectangle> boxes = page.GetSegmentedRegions(PageIteratorLevel.Symbol);


                            /*
                             * Graphics gives the following error in case of result/img:
                             * A Graphics object cannot be created from an image that has an indexed pixel format. ...
                             * ...System.Exception: A Graphics object cannot be created from an image that has an indexed pixel format.
                             *
                             * I am using the following solution for now:
                             */
                            Bitmap rez = new Bitmap(result);


                            using (Graphics g = Graphics.FromImage(rez))
                            {
                                Pen p = new Pen(Brushes.Red, 1.0F);
                                foreach (Rectangle r in boxes)
                                {
                                    //Console.WriteLine(r);
                                    g.DrawRectangle(p, r);
                                }

                                g.DrawImage(rez, 0, 0);
                            }

                            //Saving the image with bounding boxes
                            Console.WriteLine("Generating image with bounding boxes...");
                            rez.Save(dir + "\\" + filename + "_5seg.bmp");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.ToString());
                Console.WriteLine(e.Message);
                Console.WriteLine(e.ToString());
            }
            finally
            {
                Console.WriteLine("\n\nPress ENTER/RETURN to exit");
                Console.ReadKey(true);
            }
        }
예제 #17
0
        private void button2_Click(object sender, EventArgs e)
        {
            Orignal = ConvertTo24bpp(CDC.Apply(Orignal, 0.6));


            if (Template.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                Template = Grayscale.CommonAlgorithms.BT709.Apply(Template);
            }
            if (Orignal.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                Orignal = Grayscale.CommonAlgorithms.BT709.Apply(Orignal);
            }

            BradleyLocalThresholding FilterBrad = new BradleyLocalThresholding();

            FilterBrad.WindowSize = 100;
            FilterBrad.PixelBrightnessDifferenceLimit = 0.2f;
            Template = FilterBrad.Apply(Template);
            Orignal  = FilterBrad.Apply(Orignal);
            //TemplatImage.Save( @"D:\Temp.jpg" );
            //SourceImage.Save( @"D:\src.jpg" );
            if (Template.PixelFormat != PixelFormat.Format24bppRgb)
            {
                GrayscaleToRGB FilterRGB = new GrayscaleToRGB();
                Template = FilterRGB.Apply(Template);
            }
            if (Orignal.PixelFormat != PixelFormat.Format24bppRgb)
            {
                GrayscaleToRGB FilterRGB = new GrayscaleToRGB();
                Orignal = FilterRGB.Apply(Orignal);
            }

            int divisor                    = int.Parse(textBox1.Text);
            int CellsizeLength             = int.Parse(textBox2.Text);
            ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.1f);

            if (Orignal.PixelFormat != PixelFormat.Format24bppRgb)
            {
                GrayscaleToRGB FilterRGB = new GrayscaleToRGB();
                Orignal = FilterRGB.Apply(Orignal);
            }

            ResizeNearestNeighbor Resize_filter2 = new ResizeNearestNeighbor(Orignal.Width / divisor, Orignal.Height / divisor);
            Bitmap Resize_Org_Image = Resize_filter2.Apply(AForge.Imaging.Image.Clone(Orignal));



            ResizeNearestNeighbor Resize_filter3 = new ResizeNearestNeighbor(Template.Width / divisor, Template.Height / divisor);
            Bitmap Resize_Template = Resize_filter3.Apply(AForge.Imaging.Image.Clone(Template));


            TemplateMatch[] tm = etm.ProcessImage(Resize_Org_Image, Resize_Template);

            if (tm.Length > 0)
            {
                List <IntPoint> cornersRect = new List <IntPoint>
                {
                    new IntPoint(tm[0].Rectangle.X * divisor - CellsizeLength, tm[0].Rectangle.Y * divisor - CellsizeLength),
                    new IntPoint((tm[0].Rectangle.X * divisor) + (tm[0].Rectangle.Width * divisor) + CellsizeLength, tm[0].Rectangle.Y * divisor - CellsizeLength),
                    new IntPoint((tm[0].Rectangle.X * divisor) + (tm[0].Rectangle.Width * divisor) + CellsizeLength, (tm[0].Rectangle.Y * divisor) + (tm[0].Rectangle.Height * divisor) + CellsizeLength),
                    new IntPoint(tm[0].Rectangle.X * divisor - CellsizeLength, (tm[0].Rectangle.Y * divisor) + (tm[0].Rectangle.Height * divisor) + CellsizeLength)
                };
                SimpleQuadrilateralTransformation squadtran = new SimpleQuadrilateralTransformation(cornersRect, Orignal.Width + CellsizeLength * 2, Orignal.Height + CellsizeLength * 2)
                {
                    AutomaticSizeCalculaton = true
                };
                Bitmap ExhaustiveTemplate24bit = squadtran.Apply(AForge.Imaging.Image.Clone(Orignal));
                ExhaustiveTemplate24bit = new ResizeNearestNeighbor(Template.Width, Template.Height).Apply(ExhaustiveTemplate24bit);
                pictureBox3.Image       = ExhaustiveTemplate24bit;
            }
            GC.Collect();
        }
예제 #18
0
        private void Repair(ImageEditor repair, ImageProcessingArgs args)
        {
            // Increment the internal Page Index for the current ImageConversionOptions object
            args.Options.PageIndex++;

            // Build the filename part based on the PageIndex (will be persisted across calls so long as the same Options are used)
            var filePart = String.Format("{0:000}.tif", args.Options.PageIndex);

            // Create the Page object
            var page = new PageInfo()
            {
                FileName     = Path.Combine(args.Options.SaveToPath, filePart),
                RelativePath = Path.Combine(args.Options.RelativePath, filePart),
                Width        = repair.Image.Width,
                Height       = repair.Image.Height
            };

            if (!repair.Image.IsBitonal())
            {
                // Not already bitonal, so process as necessary
                switch (args.Options.BitDepth)
                {
                case 1:
                    if (args.Options.BinarisationAlgorithm == BinarisationAlgorithm.Default || args.Options.BinarisationAlgorithm == BinarisationAlgorithm.ClearImage)
                    {
                        repair.AdvancedBinarize(args.Options.Resolution);
                    }
                    else
                    {
                        // Source image must be grayscaled first
                        repair.ToGrayscale();

                        // Using an unmanaged image we will operate directly on the source images memory
                        using (UnmanagedImage image = new UnmanagedImage(repair.Image.Buffer, repair.Width, repair.Height, repair.Image.LineBytes, PixelFormat.Format8bppIndexed))
                        {
                            IInPlaceFilter filter = null;

                            if (args.Options.BinarisationAlgorithm == BinarisationAlgorithm.OtsuThreshold)
                            {
                                filter = new OtsuThreshold();
                            }
                            else if (args.Options.BinarisationAlgorithm == BinarisationAlgorithm.FloydSteinbergDither)
                            {
                                filter = new FloydSteinbergDithering();
                            }
                            else
                            {
                                filter = new BradleyLocalThresholding();
                            }

                            filter.ApplyInPlace(image);
                        }

                        // Image is still in 8 bit format (but is bitonal) so we need to convert to a 1 bit image
                        repair.ToBitonal();
                    }

                    break;

                case 8:
                    repair.ToGrayscale();
                    break;
                }

                // Set the Resolution
                repair.Image.HorzDpi = args.Options.Resolution;
                repair.Image.VertDpi = args.Options.Resolution;
            }
            else
            {
                // Is already bitonal, so quite possibly a fax
                repair.FaxStandardToFine();

                // Remove the Fax Header if exists
                if (args.Options.RemoveFaxHeader)
                {
                    repair.FaxRemoveHeader();
                }
            }

            // Perform Auto Deskew
            if (args.Options.AutoDeskew.GetValueOrDefault())
            {
                page.Skew = repair.AutoDeskew();
            }

            // Perform Auto Rotate
            if (args.Options.AutoRotate.GetValueOrDefault())
            {
                page.Rotation = repair.AutoRotate();
            }

            // Check if the image is blank and store with the Page object
            page.IsBlank = repair.IsBlankImage();

            // Setup the image save options
            if (repair.Image.IsBitonal())
            {
                repair.Image.pComprBitonal = EComprBitonal.citbCCITTFAX4;
            }
            else
            {
                repair.Image.pComprColor = EComprColor.citcJPEG;
                repair.Image.JpegQuality = 85;
            }

            // Save the image
            repair.Image.SaveAs(page.FileName, EFileFormat.ciEXT);

            // Add the page to the collection
            args.Pages.Add(page);
        }