/// <summary> /// Performs Leptonica Otsu adaptive image thresholding using /// <see cref="Net.Sourceforge.Lept4j.Leptonica.PixOtsuAdaptiveThreshold(Tesseract.Pix, int, int, int, int, float, Com.Sun.Jna.Ptr.PointerByReference, Com.Sun.Jna.Ptr.PointerByReference) /// "/> /// method. /// </summary> /// <param name="pix"> /// /// <see cref="Tesseract.Pix"/> /// object to be processed /// </param> /// <returns> /// /// <see cref="Tesseract.Pix"/> /// object after thresholding /// </returns> internal static Pix OtsuImageThresholding(Pix pix) { if (pix != null) { Pix thresholdPix = null; if (pix.Depth == 8) { thresholdPix = pix.BinarizeOtsuAdaptiveThreshold(pix.Width, pix.Height, 0, 0, 0); if (thresholdPix != null && thresholdPix.Width > 0 && thresholdPix.Height > 0) { DestroyPix(pix); return(thresholdPix); } else { LogManager.GetLogger(typeof(TesseractOcrUtil)) .Info(MessageFormatUtil.Format(Tesseract4LogMessageConstant.CANNOT_BINARIZE_IMAGE, pix.Depth)); DestroyPix(thresholdPix); return(pix); } } else { LogManager.GetLogger(typeof(TesseractOcrUtil)) .Info(MessageFormatUtil.Format(Tesseract4LogMessageConstant.CANNOT_BINARIZE_IMAGE, pix.Depth)); return(pix); } } else { return(pix); } }
public override Pix Handle(Pix request) { request = request?.BinarizeOtsuAdaptiveThreshold(this.SizeX, this.SizeY, this.SmoothX, this.SmoothY, this.ScoreFraction); return(base.Handle(request)); }
public static void Capture(IntPtr handle, Point pos, Size size, ref OCRCapture ocrCapture, bool debug = false) { if (handle == IntPtr.Zero) { return; } Bitmap b = CaptureWindowArea(handle, pos, size); if (b == null) { return; } if (debug) { Directory.CreateDirectory(Application.StartupPath + @"\debug"); } if (debug) { b.Save(Application.StartupPath + @"\debug\00_input.png"); } using (Pix pixc = PixConverter.ToPix(b)) { b.Dispose(); using (Pix pixg = pixc.ConvertRGBToGray(0, 0, 0)) { if (debug) { pixg.Save(Application.StartupPath + @"\debug\01_grayscale.png"); } using (Pix pixs = pixg.ScaleGrayLI(Config.OCRScaleFactor, Config.OCRScaleFactor)) { if (debug) { pixs.Save(Application.StartupPath + @"\debug\02_scale.png"); } //pix = pix.UnsharpMaskingGray(5, 2.5f); //issues with light text on light bg using (Pix pixb = pixs.BinarizeOtsuAdaptiveThreshold(2000, 2000, 0, 0, 0.0f)) { if (debug) { pixb.Save(Application.StartupPath + @"\debug\03_binarize.png"); } float pixAvg = pixb.AverageOnLine(0, 0, pixb.Width - 1, 0, 1); pixAvg += pixb.AverageOnLine(0, pixb.Height - 1, pixb.Width - 1, pixb.Height - 1, 1); pixAvg += pixb.AverageOnLine(0, 0, 0, pixb.Height - 1, 1); pixAvg += pixb.AverageOnLine(pixb.Width - 1, 0, pixb.Width - 1, pixb.Height - 1, 1); pixAvg /= 4.0f; using (Pix pixi = pixAvg > 0.5f ? pixb.Invert() : pixb) { if (debug) { pixi.Save(Application.StartupPath + $@"\debug\04_invert_{pixAvg > 0.5f}.png"); } using (Pix pixn = pixi.SelectBySize(Config.OCRNoiseSize, Config.OCRNoiseSize, Config.OCRNoiseConnectivity, Config.OCRNoiseType, Config.OCRNoiseRelation)) { if (debug) { pixn.Save(Application.StartupPath + @"\debug\05_removenoise.png"); } //pixn.ClipToForeground(IntPtr.Zero); using (Pix pix = pixn.AddBorder(Config.OCRPadding, 0)) { if (debug) { pix.Save(Application.StartupPath + @"\debug\06_border.png"); } pix.XRes = 300; pix.YRes = 300; using (Page page = Engine.Process(pix, PageSegMode.SingleLine)) { ocrCapture.Text = page.GetText().Trim(); ocrCapture.Confidence = page.GetMeanConfidence(); ocrCapture.Iterations++; if (ocrCapture.Confidence >= Config.OCRMinimumConfidence) { if (decimal.TryParse(ocrCapture.Text, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out decimal cooldown)) { if (cooldown < Config.CooldownMaxPossible) { ocrCapture.Cooldown = cooldown; } } } } } } } } } } } }