Sicos1977 / TesseractOCR

A .net library to work with Google's Tesseract
169 stars 22 forks source link

Enhancement: expose pixLinearTRC and pixBlend? #36

Closed richardtallent-erm closed 1 year ago

richardtallent-erm commented 1 year ago

I've been using System.Drawing to pre-process images before sending them to TesseractOCR:

I'd love to be rid of System.Drawing for good, and I believe Leptonica supports these operations using the pixLinearTRC and pixBlend (with a "blend to black" option?), but I'm not seeing a way to call these through TesseractOCR.Pix.Image.

I could duplicate all the efforts you've gone through to import Leptonica here but then I'll have to potentially deal with conflicting versions, and Tesseract is my only use case for image processing like this.

Could you by chance expose these operations and their flags?

Sicos1977 commented 1 year ago

Do you know the signatures for these methods?

Sicos1977 commented 1 year ago

I added the blend method to the Image class

        #region Blend
        /// <summary>
        ///     Blends this image with the given <paramref name="imageToBlendWith"/>
        /// </summary>
        /// <param name="imageToBlendWith"><see cref="Image"/></param>
        /// <param name="x">origin [UL corner] of <paramref name="imageToBlendWith"/> relative to the origin of <see cref="Image"/> can be  is smaller 0</param>
        /// <param name="y">origin [UL corner] of <paramref name="imageToBlendWith"/> relative to the origin of <see cref="Image"/> can be  is smaller 0</param>
        /// <param name="fraction"></param>
        /// <returns></returns>
        public Image Blend(Image imageToBlendWith, int x, int y, float fraction)
        {
            if (imageToBlendWith == null) 
                throw new ArgumentNullException(nameof(imageToBlendWith));

            var result = LeptonicaApi.Native.pixBlend(_handle, imageToBlendWith.Handle, x, y, fraction);

            return result == IntPtr.Zero ? null : new Image(result);
        }
        #endregion
Sicos1977 commented 1 year ago

The other method I can't find in the Leptonica headers

https://github.com/DanBloomberg/leptonica/blob/6c68e008a1b98226d295ee4695f2124b2a968ef5/src/allheaders.h

richardtallent-erm commented 1 year ago

Ah! Looks like it's called pixContrastTRC (though the Leptonica docs call it pixEnhanceContrast)

https://github.com/DanBloomberg/leptonica/blob/6c68e008a1b98226d295ee4695f2124b2a968ef5/src/enhance.c#L402

Thanks!