private IPix Preprocess(IPix pix) { IPix result = null; foreach (var preprocessor in _preprocessors) { result = preprocessor.Run(pix); } return(result); }
public IPix PrepareOneBitPerPixel(IPix pix) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } var pointer = Leptonica5Filters.pixPrepare1bpp(pix.HandleRef.Handle, IntPtr.Zero, 0, 0).GetPointerOrThrow(); return(new Pix(pointer)); }
public IPix OrientationCorrect(IPix pix, float minUpConfidence = 4f, float minRatio = 2.5f) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } var pointer = Leptonica5Filters.pixOrientCorrect(pix.HandleRef.Handle, minUpConfidence, minRatio, out _, out _, out _, 0).GetPointerOrThrow(); return(new Pix(pointer)); }
public IBox FindPageForeground(IPix pix, int threshold = 128, int minDistance = 50, int eraseDistance = 70) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } var pointer = Leptonica5Filters.pixFindPageForeground(pix.HandleRef.Handle, threshold, minDistance, eraseDistance, 0, IntPtr.Zero).GetPointerOrThrow(); return(new Box(pointer)); }
public IPix ClipRectangle(IPix pix, IBox box) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } var pointer = Leptonica5Filters.pixClipRectangle(pix.HandleRef.Handle, box.HandleRef.Handle, IntPtr.Zero).GetPointerOrThrow(); return(new Pix(pointer)); }
public IPix DeskewBoth(IPix pix, DeskewReductionFactor searchReduction) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } var pointer = Leptonica5Filters.pixDeskewBoth(pix.HandleRef.Handle, (int)searchReduction).GetPointerOrThrow(); return(new Pix(pointer)); }
public void AddPix(IPix pix) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } if (Leptonica5Pix.pixaAddPix(HandleRef.Handle, pix.HandleRef.Handle, 1) != 0) { throw new InvalidOperationException("Leptonica failed."); } }
public override IPix Run(IPix image) { if (image == null) { throw new ArgumentNullException(nameof(image)); } using var deskewedPix = leptonicaInterop.DeskewBoth(image, ReductionFactor); using var oneBitPix = leptonicaInterop.PrepareOneBitPerPixel(deskewedPix); using var correctedPix = leptonicaInterop.OrientationCorrect(oneBitPix, MinUpConfidence, MinRatio); using var foregroundBox = leptonicaInterop.FindPageForeground(correctedPix, Threshold, MinDistance, EraseDistance); return(leptonicaInterop.ClipRectangle(correctedPix, foregroundBox)); }
public override IPix Run(IPix image) { if (image == null) { throw new ArgumentNullException(nameof(image)); } return(image .ChainOutput(x => leptonicaInterop.DeskewBoth(x, ReductionFactor), disposePreviousPix: false) .ChainOutput(x => leptonicaInterop.PrepareOneBitPerPixel(x)) .ChainOutput(x => leptonicaInterop.OrientationCorrect(x, MinUpConfidence, MinRatio)) .ChainOutput(x => { using var box = leptonicaInterop.FindPageForeground(x, Threshold, MinDistance, EraseDistance); return leptonicaInterop.ClipRectangle(x, box); })); }
public static IPix ChainOutput(this IPix pix, Func <IPix, IPix> invocation, bool disposePreviousPix = true) { if (pix == null) { throw new ArgumentNullException(nameof(pix)); } if (invocation == null) { throw new ArgumentNullException(nameof(invocation)); } if (!disposePreviousPix) { return(invocation(pix)); } using (pix) return(invocation(pix)); }
/// <summary> /// Processes the specific image. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="region">The image region to process.</param> /// <returns>A result iterator</returns> public ResultIterator Process(IPix image, Rect region) { if (image == null) throw new ArgumentNullException("image"); if (region.X1 < 0 || region.Y1 < 0 || region.X2 > image.Width || region.Y2 > image.Height) throw new ArgumentException("The image region to be processed must be within the image bounds.", "region"); if (processCount > 0) throw new InvalidOperationException("Only one image can be processed at once. Please make sure you dispose of the result iterator once your finished with it."); processCount++; Interop.TessApi.BaseApiSetImage(handle, image.Handle); Interop.TessApi.BaseApiSetRectangle(handle, region.X1, region.Y1, region.Width, region.Height); if (Interop.TessApi.BaseApiRecognize(handle, IntPtr.Zero) != 0) { throw new InvalidOperationException("failed to process document."); } var iteratorHandle = Interop.TessApi.BaseApiGetIterator(handle); var iterator = new ResultIterator(iteratorHandle); iterator.Disposed += OnIteratorDisposed; return iterator; }
public virtual Func <Tr> PixStack(PixStack ps, Func <Tr>[] subArray) { return(() => { var typedOther = Other as PixStack; if (typedOther == null) { throw new ArgumentException(); } var length = subArray.Length; if (length != typedOther.PixArray.Length) { throw new ArgumentException(); } var result = ProductOp.PixStack(ps, typedOther, subArray.ProductArray(typedOther.PixArray, length, (fp0, p1) => { Other = p1; return fp0(); })); Other = typedOther; return result; }); }
public ITesseractResult Process(IPix image) { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (Interlocked.CompareExchange(ref _processCount, 1, 0) != 0) { throw new InvalidOperationException("Only one image can be processed at once. Please make sure you dispose of the page once your finished with it."); } try { const PageSegmentMode pageSegMode = PageSegmentMode.Auto; Tesseract4.TessBaseAPISetPageSegMode(_handle, (int)pageSegMode); Tesseract4.TessBaseAPISetImage2(_handle, image.HandleRef); if (Tesseract4.TessBaseAPIRecognize(_handle, out var rec) != 0) { throw new TesseractException("Recognition of image failed."); } using var text = Text.Create(() => Tesseract4.TessBaseAPIGetUTF8Text(_handle)); using var hocrText = Text.Create(() => Tesseract4.TessBaseAPIGetHOCRText(_handle, 0)); return(new TesseractResult { Text = text.ToString(), HocrText = $"{Tags.XhtmlBeginTag}{hocrText}{Tags.XhtmlEndTag}", Confidence = null }); } finally { Tesseract4.TessBaseAPIClear(_handle); Interlocked.Decrement(ref _processCount); } }
public PixSaveOp(IPix other, string pathPrefix) : base(new PixIntSumProductOp(), other) { PathPrefix = pathPrefix; }
public static IPix WithRemovedMipMaps(this IPix pix) { return(pix.Op(new PixRemoveMipMaps())); }
public static IPix WithLoadedImages(this IPix pix, string pathPrefix = null) { return(pix.Op(new PixLoadOp(pathPrefix == null ? "" : pathPrefix))); }
public static IPix WithMipMaps( this IPix pix, PixImageMipMap.MipMapOptions options = null) { return(pix.Op(new PixCreateMipMaps(options == null ? PixImageMipMap.MipMapOptions.Default : options))); }
public ResultIterator Process(IPix image) { return Process(image, new Rect(0, 0, image.Width, image.Height)); }
public static PixImage[] GetPixImageArray(this IPix pix, int level = 0) { return(pix.Op(new PixGetPixImageArray(level))()); }
public static IPix GetDescriptors(this IPix pix) { return(pix.Op(new PixGetInfo())); }
public static IPix ToPixImage <T>(this IPix pix) { return(pix.Op(new PixToPixImage <T>())); }
public PixInnerProductOp(IPixProductOp <Tr> productOp, IPix other) { ProductOp = productOp; Other = other; }
public abstract IPix Run(IPix image);
/// <summary> /// </summary> /// <returns>The number of saved PixImages.</returns> public static int SaveImages(this IPix pix, IPix names, string pathPrefix = null) { return(pix.Op(new PixSaveOp(names, pathPrefix == null ? "" : pathPrefix))()); }