Exemplo n.º 1
0
        private IPix Preprocess(IPix pix)
        {
            IPix result = null;

            foreach (var preprocessor in _preprocessors)
            {
                result = preprocessor.Run(pix);
            }
            return(result);
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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));
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 9
0
        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);
            }));
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
        /// <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;
        }
Exemplo n.º 12
0
 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;
     });
 }
Exemplo n.º 13
0
        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);
            }
        }
Exemplo n.º 14
0
 public PixSaveOp(IPix other, string pathPrefix)
     : base(new PixIntSumProductOp(), other)
 {
     PathPrefix = pathPrefix;
 }
Exemplo n.º 15
0
 public static IPix WithRemovedMipMaps(this IPix pix)
 {
     return(pix.Op(new PixRemoveMipMaps()));
 }
Exemplo n.º 16
0
 public static IPix WithLoadedImages(this IPix pix, string pathPrefix = null)
 {
     return(pix.Op(new PixLoadOp(pathPrefix == null ? "" : pathPrefix)));
 }
Exemplo n.º 17
0
 public static IPix WithMipMaps(
     this IPix pix, PixImageMipMap.MipMapOptions options = null)
 {
     return(pix.Op(new PixCreateMipMaps(options == null
                     ? PixImageMipMap.MipMapOptions.Default : options)));
 }
Exemplo n.º 18
0
 public ResultIterator Process(IPix image)
 {
     return Process(image, new Rect(0, 0, image.Width, image.Height));
 }
Exemplo n.º 19
0
 public static PixImage[] GetPixImageArray(this IPix pix, int level = 0)
 {
     return(pix.Op(new PixGetPixImageArray(level))());
 }
Exemplo n.º 20
0
 public static IPix GetDescriptors(this IPix pix)
 {
     return(pix.Op(new PixGetInfo()));
 }
Exemplo n.º 21
0
 public static IPix ToPixImage <T>(this IPix pix)
 {
     return(pix.Op(new PixToPixImage <T>()));
 }
Exemplo n.º 22
0
 public PixInnerProductOp(IPixProductOp <Tr> productOp, IPix other)
 {
     ProductOp = productOp;
     Other     = other;
 }
Exemplo n.º 23
0
 public abstract IPix Run(IPix image);
Exemplo n.º 24
0
 /// <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))());
 }