Example #1
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);
        }
Example #2
0
        public void bradley(ref Bitmap tmp)
        {
            BradleyLocalThresholding filter = new BradleyLocalThresholding();

            // filter.AssertCanApply(tmp.PixelFormat);
            filter.ApplyInPlace(tmp);
        }
Example #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);
        }
        private void process()
        {
            stopwatch.Reset();
            stopwatch.Start();
            plates = new List <string>();
            frame  = contrastCorrectionFilter.Apply(originalImage);
            frame  = grayscaleFilter.Apply(frame);

            BitmapData     frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, frame.PixelFormat);
            UnmanagedImage data      = new UnmanagedImage(frameData);

            bradleyLocalFilter.ApplyInPlace(data);
            fillHoles.ApplyInPlace(data);
            openingFilter.ApplyInPlace(data);

            blobCounter.ProcessImage(data);

            data.Dispose();
            frame.UnlockBits(frameData);

            Graphics g = Graphics.FromImage(originalImage);

            Blob[] blobs = blobCounter.GetObjectsInformation();
            foreach (Blob blob in blobs)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                List <IntPoint> corners    = null;

                // da li je četverougao?
                if (!shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {
                    continue;
                }

                if (FindNewCornersAndCheckAspectRatio(corners))
                {
                    SimpleQuadrilateralTransformation sqt = new SimpleQuadrilateralTransformation(corners, 300, 66);
                    Bitmap plate = sqt.Apply(originalImage);
                    plate = grayscaleFilter.Apply(plate);
                    otsuThresholdFilter.ApplyInPlace(plate);

                    if (!IsLicensePlate(plate))
                    {
                        continue;
                    }

                    String plateText;
                    if (FindText(plate, out plateText))
                    {
                        g.DrawPolygon(pen, ToPointsArray(corners));
                        frame = plate;
                        plates.Add(plateText);
                    }
                }
            }
            g.Dispose();
            stopwatch.Stop();
        }
Example #5
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);
        }
Example #6
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);
        }
Example #7
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);
 }
        public void ProcessImage(Bitmap input_image)
        {
            lock (balanceLock)
            {
                int       side     = Math.Min(input_image.Height, input_image.Width);
                Rectangle cropRect = new Rectangle(0, 0, side, side);                                                               // this is square that represents feed from camera
                g.DrawImage(input_image, new Rectangle(0, 0, input_image.Width, input_image.Height), cropRect, GraphicsUnit.Pixel); // place it on original bitmap

                // set new processed
                if (processed != null)
                {
                    processed.Dispose();
                }

                //  Конвертируем изображение в градации серого
                processed = grayFilter.Apply(AForge.Imaging.UnmanagedImage.FromManagedImage(original));
                //  Пороговый фильтр применяем. Величина порога берётся из настроек, и меняется на форме
                threshldFilter.PixelBrightnessDifferenceLimit = ThresholdValue;
                threshldFilter.ApplyInPlace(processed);
                InvertFilter.ApplyInPlace(processed);
                Blober.ProcessImage(processed);
                AForge.Imaging.Blob[] blobs = Blober.GetObjectsInformation();
                BlobCount = blobs.Length;

                if (blobs.Length > 0)
                {
                    var BiggestBlob = blobs[0];
                    Recongnised = true;
                    Blober.ExtractBlobsImage(processed, BiggestBlob, false);
                    processed = BiggestBlob.Image;
                    AForge.Point mc = BiggestBlob.CenterOfGravity;
                    AForge.Point ic = new AForge.Point((float)BiggestBlob.Image.Width / 2, (float)BiggestBlob.Image.Height / 2);
                    AngleRad = (ic.Y - mc.Y) / (ic.X - mc.X);
                    Angle    = (float)(Math.Atan(AngleRad) * 180 / Math.PI);
                }
                else
                {
                    // TODO make arrengaments for No blobs case
                    Recongnised = false;
                    Angle       = 0;
                    AngleRad    = -1;
                }

                if (number != null)
                {
                    number.Dispose();
                }
                number = processed.ToManagedImage();
            }
        }
Example #9
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;
     }
 }
Example #10
0
        /// <summary>
        /// Processes the specified image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <remarks></remarks>
        public void Process(Bitmap image)
        {
            //Graphics g = Graphics.FromImage(image);

            /*
             * float area = image.Width * image.Height;
             * float borderWidth = (float)Math.Max(3, Math.Round(0.25 * (area / 1024)));
             *
             * g.DrawRectangle(new Pen(Color.Black, borderWidth), 0, 0, image.Width - 1, image.Height - 1);
             * g.Dispose();
             */
            image = grayscale.Apply(image);

            //ContrastStretch filter = new ContrastStretch();
            //HistogramEqualization filter = new HistogramEqualization();
            //ContrastCorrection filter = new ContrastCorrection(0.5);
            //BrightnessCorrection filter = new BrightnessCorrection(-0.15);
            //GammaCorrection filter = new GammaCorrection(1.5);
            //filter.ApplyInPlace(image);

            threshold.ApplyInPlace(image);
            invert.ApplyInPlace(image);

            float sideAvg     = (image.Width + image.Height) / 2;
            float borderWidth = (float)Math.Max(2, Math.Round(0.02 * sideAvg));

            Bitmap   temp = new Bitmap(image.Width, image.Height);
            Graphics g    = Graphics.FromImage(temp);

            g.DrawImageUnscaled(image, 0, 0);
            g.DrawRectangle(new Pen(Color.White, borderWidth), 0, 0, image.Width - 1, image.Height - 1);
            g.Dispose();

            image = (new Threshold()).Apply(grayscale.Apply(temp));

            floodFill.StartingPoint = new IntPoint(0, 0);
            floodFill.ApplyInPlace(image);

            blobCounter.ProcessImage(image);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            //Results = new List<OCRResult>();
            Results.Clear();

            foreach (Blob b in blobs)
            {
                int w = b.Rectangle.Width;
                int h = b.Rectangle.Height;

                if (w > 16 && h > 16 && h < 24)
                {
                    w = 16;
                    h = 16;
                }
                else
                //{
                if (!(w >= 3 && w <= 5 && h >= 14 && h <= 17 && b.Fullness > 0.6f))
                {
                    //Bitmap test = image.Clone(new Rectangle(b.Rectangle.X, b.Rectangle.Y, w, h), PixelFormat.Format24bppRgb);
                    if (b.Area < 20 || b.Fullness < 0.2f || b.Fullness > 0.9f || h > 18)
                    {
                        continue;
                    }
                }
                //}

                //blobCounter.MinWidth = 3;
                //blobCounter.MinHeight = 10;

                //if (b.Area < 20 || b.Fullness > 0.9f) continue;

                Bitmap slice = image.Clone(new Rectangle(b.Rectangle.X, b.Rectangle.Y, w, h), PixelFormat.Format24bppRgb);
                slice = grayscale.Apply(slice);
                slice = resize.Apply(slice);

                dilate.ApplyInPlace(slice);

                if (Recognise)
                {
                    Result res = classifier.Classify(new Instance("?", BitmapFeatureExtract(slice)));
                    Results.Add(new OCRResult(b, slice, res.Class, res.Value.ToString()));
                }
                else
                {
                    Results.Add(new OCRResult(b, slice, "?", ""));
                }
            }
        }
Example #11
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);
            }
        }