Ejemplo n.º 1
0
        public static Pix OtsuAdaptiveThreshold(Pix pix, int sx, int sy, int smoothx, int smoothy, float fraction)
        {
            if (pix.Depth != 8)
            {
                if (pix.Depth == 1)
                {
                    pix = Convert.Convert1To8(pix);
                }
                else
                {
                    pix = Convert.ConvertTo8(pix);
                }
            }
            (sx >= 16).VerifyCondition("sx must be at least 16 or greater.");
            (sy >= 16).VerifyCondition("sy must be at least 16 or greater.");
            IntPtr ppxith, ppixd;
            int    result = LeptonicaNativeApi.Native.pixOtsuAdaptiveThreshold(pix.Reference, sx, sy, smoothx, smoothy, fraction, out ppxith, out ppixd);

            if (ppxith != IntPtr.Zero)
            {
                LeptonicaNativeApi.Native.pixDestroy(ref ppxith);
            }
            if (result == 1)
            {
                throw new NullReferenceException("failed to binarize.");
            }
            return(Pix.Create(ppixd));
        }
Ejemplo n.º 2
0
        public static Pix SauvolaTiled(Pix pix, int wsize, float factor, int nx, int ny)
        {
            if (pix.Depth != 8)
            {
                if (pix.Depth == 1)
                {
                    pix = Convert.Convert1To8(pix);
                }
                else
                {
                    pix = Convert.ConvertTo8(pix);
                }
            }
            (wsize >= 2).VerifyCondition("wsize must be greater then 2");
            int max = Math.Min((pix.Width - 3) / 2, (pix.Height - 3) / 2);

            (wsize < max).VerifyCondition("wsize must be lower than value {0}", max);
            (factor >= 0).VerifyCondition("factor must be greater than 0");
            IntPtr a, b;
            int    result = LeptonicaNativeApi.Native.pixSauvolaBinarizeTiled(pix.Reference, wsize, factor, nx, ny, out a, out b);

            if (a != IntPtr.Zero)
            {
                LeptonicaNativeApi.Native.pixDestroy(ref a);
            }
            if (result == 1)
            {
                throw new NullReferenceException("failed to binarize.");
            }
            return(Pix.Create(b));
        }
Ejemplo n.º 3
0
        public static Pix pixConvert1To8(this Pix pixd, Pix pixs, byte val0, byte val1)
        {
            if (null == pixs)
            {
                throw new ArgumentNullException("pixs cannot be null.");
            }

            if (pixs.Depth != 1)
            {
                throw new ArgumentException("pixs not 1 bpp.");
            }

            if (null != pixd)
            {
                if (pixs.Width != pixd.Width || pixs.Height != pixd.Height)
                {
                    throw new ArgumentException("pix sizes unequal.");
                }
                if (pixd.Depth != 8)
                {
                    throw new ArgumentException("pixd not 8 bpp.");
                }
            }
            else
            {
                pixd = Pix.Create(pixs.Width, pixs.Height, 8);
            }
            Native.DllImports.pixCopyResolution((HandleRef)pixd, (HandleRef)pixs);
            var pointer = Native.DllImports.pixConvert1To8((HandleRef)pixd, (HandleRef)pixs, val0, val1);

            return(new Pix(pointer));
        }
Ejemplo n.º 4
0
        public static Pix Pivot(Pix pix, float angle, RotationMethod method = RotationMethod.AreaMap, RotationFill fillColor = RotationFill.White, int?w = null, int?h = null)
        {
            w = w.IsNullOrEmpty() ? pix.Width : w;
            h = h.IsNullOrEmpty() ? pix.Height : h;
            if (Math.Abs(angle) < TinyAngle)
            {
                return(pix.Clone());
            }
            IntPtr p;

            double rotations = 2 * angle / Math.PI;

            if (Math.Abs(rotations - Math.Floor(rotations)) < TinyAngle)
            {
                p = LeptonicaNativeApi.Native.pixRotateOrth(pix.Reference, (int)rotations);
            }
            else
            {
                p = LeptonicaNativeApi.Native.pixRotate(pix.Reference, angle, method, fillColor, w.Value, h.Value);
            }
            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to rotate");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 5
0
        public static Pix ClipToRectangle(Pix pix, Box box)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixClipRectangle(pix.Reference, box.Reference, IntPtr.Zero);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to crop");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 6
0
        public static Pix UnsharpMasking(Pix pix)
        {
            IntPtr p = LeptonicaNativeApi.Native.pixUnsharpMasking(pix.Reference, DefaultHalfWidth, DefaultFraction);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to unsharpMasking");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 7
0
        public static Pix Translate(Pix pix, int horizontalShift, int verticalShift, ColorOptions options)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixTranslate(IntPtr.Zero, pix.Reference, horizontalShift, verticalShift, options);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to translate");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 8
0
        public static Pix Flake(Pix pix, float x, float y)
        {
            IntPtr p = LeptonicaNativeApi.Native.pixScale(pix.Reference, x, y);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to scale.");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 9
0
        public static Pix Convert1To8(Pix pix, int whiteColor, int blackColor)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixConvert1To8(IntPtr.Zero, pix.Reference, whiteColor, blackColor);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to Convert1To8");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 10
0
        public static Pix ConvertTo1(Pix pix, int threshold)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixConvertTo1(pix.Reference, threshold);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to ConvertTo1");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 11
0
        public static Pix ConvertRGBToLuminance(Pix pix)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixConvertRGBToLuminance(pix.Reference);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to ConvertRGBToLuminance");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 12
0
        public static Pix CleanBackgroundToWhite(Pix pix, float gamma, int blackColor, int whiteColor)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixCleanBackgroundToWhite(pix.Reference, IntPtr.Zero, IntPtr.Zero, gamma, blackColor, whiteColor);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to CleanBackgroundToWhite");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 13
0
        public static Pix Blockconvolve(Pix pix, int sx, int sy)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixBlockconv(pix.Reference, sx, sy);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to Blockconvolve");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 14
0
        public static Pix PixTophat(this Pix pix, int w = 17, int h = 17, L_TOPHAT options = L_TOPHAT.BLACK)
        {
            if (pix == null)
            {
                throw new ArgumentNullException("pix is null");
            }
            IntPtr pixPtr = LeptonicaNativeApi.Native.pixTophat(pix.Reference, h, w, options);

            return(Pix.Create(pixPtr));
        }
Ejemplo n.º 15
0
        public static Pix CloseGray(Pix pix, int sx, int sy)
        {
            IntPtr ptr = LeptonicaNativeApi.Native.pixCloseGray(pix.Reference, sx, sy);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to CloseGray");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 16
0
        /*pix extensions*/
        public static Pix PixBackgroundNormSimple(this Pix pix)
        {
            if (pix == null)
            {
                throw new ArgumentNullException("pix is null");
            }
            IntPtr pixPtr = LeptonicaNativeApi.Native.pixBackgroundNormSimple(pix.Reference, IntPtr.Zero, IntPtr.Zero);

            return(Pix.Create(pixPtr));
        }
Ejemplo n.º 17
0
        public static Pix PixConvertRGBToGray(this Pix pix, float r = 0.5f, float g = 0.3f, float b = 0.2f)
        {
            if (pix == null)
            {
                throw new ArgumentNullException("pix is null");
            }
            IntPtr pixPtr = LeptonicaNativeApi.Native.pixConvertRGBToGray(pix.Reference, r, g, b);

            return(Pix.Create(pixPtr));
        }
Ejemplo n.º 18
0
        public static Pix PixThresholdToBinary(this Pix pix, int whiteColor = 35)
        {
            if (pix == null)
            {
                throw new ArgumentNullException("pix is null");
            }
            IntPtr pixPtr = LeptonicaNativeApi.Native.pixThresholdToBinary(pix.Reference, whiteColor);

            return(Pix.Create(pixPtr));
        }
Ejemplo n.º 19
0
        public static Pix ConvertTo8(Pix pix, int flag)
        {
            IntPtr p = LeptonicaNativeApi.Native.pixConvertTo8(pix.Reference, flag);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to convert 8pp");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 20
0
        public static Pix PixFindSkewAndDeskew(this Pix pix, int windowSize = 2)
        {
            if (pix == null)
            {
                throw new ArgumentNullException("pix is null");
            }
            float  angle, confidence;
            IntPtr pixPtr = LeptonicaNativeApi.Native.pixFindSkewAndDeskew(pix.Reference, windowSize, out angle, out confidence);

            return(Pix.Create(pixPtr).PixRotate(angle, confidence));
        }
Ejemplo n.º 21
0
        public static Pix DewrapSinglePage(Pix pix, DewrapThresh thresh, DewrapThresholding adaptive, DewrapOrientation orinetation, DewrapOptions options)
        {
            IntPtr ptr;
            int    result = LeptonicaNativeApi.Native.dewarpSinglePage(pix.Reference, thresh, adaptive, orinetation, out ptr, IntPtr.Zero, options);

            if (result == 1)
            {
                throw new NullReferenceException("failed to dewrap");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 22
0
        public static Pix FindSkewAndDeskew(Pix pix, int redSearch)
        {
            float  pangle, pconf;
            IntPtr ptr = LeptonicaNativeApi.Native.pixFindSkewAndDeskew(pix.Reference, redSearch, out pangle, out pconf);

            if (ptr == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to findSkewAndDeskew");
            }
            return(Pix.Create(ptr));
        }
Ejemplo n.º 23
0
        public static Pix UnsharpMasking(Pix pix, int halfwidth, float fract)
        {
            (fract > 0.2f).VerifyCondition("fraction must be greater than 0.2");
            (fract < 0.7f).VerifyCondition("fraction must be lower than 0.7");
            IntPtr p = LeptonicaNativeApi.Native.pixUnsharpMasking(pix.Reference, halfwidth, fract);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to unsharpMasking");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 24
0
 public static Pix PixRotate(this Pix pix, float angle, float confidence, RotationMethod method = RotationMethod.Shear, RotationFill fill = RotationFill.White)
 {
     if (pix == null)
     {
         throw new ArgumentNullException("pix is null");
     }
     if (IsRotationConfident(confidence))
     {
         IntPtr pixPtr = LeptonicaNativeApi.Native.pixRotate(pix.Reference, angle.DegreeToRadians(), method, fill, pix.Width, pix.Height);
         return(Pix.Create(pixPtr));
     }
     return(pix);
 }
Ejemplo n.º 25
0
        public static Pix SobelEdgeFilter(Pix pix, EdgeDirection orientflag = EdgeDirection.All)
        {
            if (pix.Depth != 8)
            {
                pix = Convert.ConvertTo8(pix);
            }
            IntPtr p = LeptonicaNativeApi.Native.pixSobelEdgeFilter(pix.Reference, orientflag);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to edge.");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 26
0
        public static Pix ConvertRGBToGray(Pix pix, float rwt, float gwt, float bwt)
        {
            (pix.Depth == 32).VerifyCondition("image must be 32pp.");
            (rwt >= 0).VerifyCondition("red weight must be greater than 0");
            (gwt >= 0).VerifyCondition("green weight must be greater than 0");
            (bwt >= 0).VerifyCondition("blue weight must be greater than 0");

            IntPtr p = LeptonicaNativeApi.Native.pixConvertRGBToGray(pix.Reference, rwt, gwt, bwt);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to convert gray scale.");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 27
0
        public static Pix Deskew(Pix pix, Sweep sweep, int redSearch, int threshold, out Scew scew)
        {
            float  angle, confidence;
            IntPtr p = LeptonicaNativeApi.Native.pixDeskewGeneral(pix.Reference, sweep.Reduction, sweep.Range, sweep.Delta, redSearch, threshold, out angle, out confidence);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to deskew.");
            }
            scew = new Scew()
            {
                Angle      = angle,
                Confidence = confidence
            };
            return(Pix.Create(p));
        }
Ejemplo n.º 28
0
        /*
         * public static Pix ContrastNorm(Pix pix) {
         *  return ContrastNorm(pix, DefaultTileWidth, DefaultTileHeight, DefaultDifference, DefaultSmoothX, DefaultSmoothY);
         * }
         *
         * public static Pix ContrastNorm(Pix pix, int sx, int sy, int diff, int smoothx, int smoothy) {
         *  if(pix.Depth != 8) {
         *      if(pix.Depth == 1) {
         *          pix = Convert.Convert1To8(pix);
         *      } else {
         *          pix = Convert.ConvertTo8(pix);
         *      }
         *  }
         *  IntPtr p = LeptonicaNativeApi.Native.pixContrastNorm(IntPtr.Zero, pix.Reference, sx, sy, diff, smoothx, smoothy);
         *  if(p == IntPtr.Zero) {
         *      throw new NullReferenceException("failed to contrastNorm");
         *  }
         *  return Pix.Create(p);
         * }*/

        public static Pix NormalizeIllumination(Pix pix, int reduction, int size, int backgroundColor, int sx, int sy)
        {
            IntPtr pixr, pixg, pixb;

            LeptonicaNativeApi.Native.pixBackgroundNormRGBArraysMorph(pix.Reference, IntPtr.Zero, reduction, size, backgroundColor, out pixr, out pixg, out pixb);
            if (pixr != IntPtr.Zero && pixg != IntPtr.Zero && pixb != IntPtr.Zero)
            {
                IntPtr ptr = LeptonicaNativeApi.Native.pixApplyInvBackgroundRGBMap(pix.Reference, pixr, pixg, pixb, sx, sy);
                if (ptr == IntPtr.Zero)
                {
                    throw new NullReferenceException("failed to normalize illumunation");
                }
                LeptonicaNativeApi.Native.pixDestroy(ref pixr);
                LeptonicaNativeApi.Native.pixDestroy(ref pixg);
                LeptonicaNativeApi.Native.pixDestroy(ref pixb);
                return(Pix.Create(ptr));
            }
            throw new NullReferenceException("failed to normalize illumunation");
        }
Ejemplo n.º 29
0
        public static Pix BackgroundNormMorph(Pix pix, int reduction, int size, int backgroundColor)
        {
            if (pix.Depth != 8)
            {
                if (pix.Depth == 1)
                {
                    pix = Convert.Convert1To8(pix);
                }
                else
                {
                    pix = Convert.ConvertTo8(pix);
                }
            }
            IntPtr p = LeptonicaNativeApi.Native.pixBackgroundNormMorph(pix.Reference, IntPtr.Zero, reduction, size, backgroundColor);

            if (p == IntPtr.Zero)
            {
                throw new NullReferenceException("failed to backgroundNormMorph");
            }
            return(Pix.Create(p));
        }
Ejemplo n.º 30
0
        // Unpacking conversion from 1 bpp to 2, 4, 8, 16 and 32 bpp
        public static Pix pixUnpackBinary(this Pix pixs, int depth, int invert)
        {
            if (null == pixs)
            {
                throw new ArgumentNullException("pixs cannot be null.");
            }

            if (pixs.Depth != 1)
            {
                throw new ArgumentException("pixs not 1 bpp.");
            }

            if (depth != 2 && depth != 4 && depth != 8 && depth != 16 && depth != 32)
            {
                throw new ArgumentException("depth not 2, 4, 8, 16, or 32 bpp.");
            }

            Pix pixd = Pix.Create(pixs.Width, pixs.Height, depth);

            if (pixs.Depth == 2)
            {
                if (invert == 0)
                {
                    pixConvert1To2(pixd, pixs, 0, 3);
                }
                else
                {
                    pixConvert1To2(pixd, pixs, 3, 0);
                }
            }
            else if (depth == 4)
            {
                if (invert == 0)
                {
                    pixConvert1To4(pixd, pixs, 0, 15);
                }
                else  /* invert bits */
                {
                    pixConvert1To4(pixd, pixs, 15, 0);
                }
            }
            else if (depth == 8)
            {
                if (invert == 0)
                {
                    pixConvert1To8(pixd, pixs, 0, 255);
                }
                else  /* invert bits */
                {
                    pixConvert1To8(pixd, pixs, 255, 0);
                }
            }
            else if (depth == 16)
            {
                if (invert == 0)
                {
                    pixConvert1To16(pixd, pixs, 0, 0xffff);
                }
                else  /* invert bits */
                {
                    pixConvert1To16(pixd, pixs, 0xffff, 0);
                }
            }
            else
            {
                if (invert == 0)
                {
                    pixConvert1To32(pixd, pixs, 0, 0xffffffff);
                }
                else
                {
                    pixConvert1To32(pixd, pixs, 0xffffffff, 0);
                }
            }

            return(pixd);
        }