/// <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, "?", "")); } } }