通过C#提高图像质量

发布于 2025-01-17 18:46:56 字数 5314 浏览 3 评论 0原文

我正在尝试Tesseract OCR(目前使用4.1.1),然后潜入培训图书馆之前;我想确保我正在使用最优质的图像。

这是我发现和尝试过的各种线程和帖子的汇编。 我将发布我目前使用的内容可能会找到一些固定逻辑,并可以以正确的方式引导我。这里!

的编译

string pathToOriginal = 'whatever'
string pathToImage = 'whatever2'


    private Bitmap returnImage(int positionOfPriceX, int positionOfPriceY, int width, int height)
{
    ImageCodecInfo pngEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Png);
    Encoder myEncoder = Encoder.Quality;
    EncoderParameters myEncoderParameters = new EncoderParameters(1);
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;

    Size imageSize = new Size(width, height);
    Image image = CaptureScreen(positionOfPriceX, positionOfPriceY, 0, 0, imageSize);
    image.Save(pathToOriginal, pngEncoder, myEncoderParameters);
    image.Save(pathToImage, pngEncoder, myEncoderParameters);
    increaseImageQuality(pathToImage);
    Bitmap finalImage = (Bitmap)image;
    finalImage.SetResolution(300, 300);
    finalImage.Save(pathToImage,  pngEncoder, myEncoderParameters);
    invertImageColours(finalImage).Save(pathToImage, pngEncoder, myEncoderParameters);
    return finalImage;
}

使用屏幕上的捕获

    private Image CaptureScreen(int sourceX, int sourceY, int destX, int destY, Size regionSize)
    {
        Bitmap bmp = new Bitmap(regionSize.Width, regionSize.Height);
        bmp.SetResolution(300, 300);
        Graphics g = Graphics.FromImage(bmp);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        try
        {
            g.CopyFromScreen(sourceX, sourceY, destX, destY, regionSize);
        }catch (Exception ex)
        {
            bbeezUtils.createLogs(ex.StackTrace);
        }
        finally
        {
            bmp = resizeBitmap(bmp, regionSize.Width* resizeMultiplier, regionSize.Height*resizeMultiplier);

        }

        return bmp;
    }

的方法

 private Bitmap resizeBitmap(Image image, int width, int height)
        {
            var destRect = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        using (var graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }

        return destImage;
    }

private void increaseImageQuality(String imagePath)
{
    byte[] photoBytes = File.ReadAllBytes(imagePath);
    ISupportedImageFormat format = new PngFormat { Quality = 100 };

        using (MemoryStream inStream = new MemoryStream(photoBytes))
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                {
                    imageFactory.Load(inStream)
                                .Format(format)
                                .Brightness(10)
                                .Contrast(20)
                                .Quality(100)
                                .GaussianSharpen(1)
                                .Constrain(new Size(300,150))
                                .BitDepth(256)
                                .Save(outStream);
                                

                    FileStream fileStream = new FileStream(pathToImage, FileMode.Create); 
                    outStream.WriteTo(fileStream);
                    fileStream.Close();
                }
                // Do something with the stream.
                outStream.Close();
            }
        }
    }

这是

    private Image invertImageColours(Image imgSource)
    {
        Bitmap bmpDest = null;

        using (Bitmap bmpSource = new Bitmap(imgSource))
        {
            bmpDest = new Bitmap(bmpSource.Width, bmpSource.Height);

            for (int x = 0; x < bmpSource.Width; x++)
            {
                for (int y = 0; y < bmpSource.Height; y++)
                {

                    Color clrPixel = bmpSource.GetPixel(x, y);

                    clrPixel = Color.FromArgb(255 - clrPixel.R, 255 -
                       clrPixel.G, 255 - clrPixel.B);

                    bmpDest.SetPixel(x, y, clrPixel);
                }
            }
        }
        return bmpDest;
    }

正在 图像在文件夹中的外观

但是,封闭式的封闭方式非常消失:但是 我打开它时的图像看起来如何

您可以和我一样苛刻。 拜托,让我知道您会做些什么!

谢谢您提前的时间! :d

I am experimenting with Tesseract OCR (currently using 4.1.1) and before diving into training the library; I want to make sure that I am using the best quality image.

This is a compilation of a variety of threads and posts I found and tried.
I will post what I am currently using possibly the experts will locate some redudant logic and can steer me on the right way. Here it goes!

This is the compilation of methods I am using

string pathToOriginal = 'whatever'
string pathToImage = 'whatever2'


    private Bitmap returnImage(int positionOfPriceX, int positionOfPriceY, int width, int height)
{
    ImageCodecInfo pngEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Png);
    Encoder myEncoder = Encoder.Quality;
    EncoderParameters myEncoderParameters = new EncoderParameters(1);
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;

    Size imageSize = new Size(width, height);
    Image image = CaptureScreen(positionOfPriceX, positionOfPriceY, 0, 0, imageSize);
    image.Save(pathToOriginal, pngEncoder, myEncoderParameters);
    image.Save(pathToImage, pngEncoder, myEncoderParameters);
    increaseImageQuality(pathToImage);
    Bitmap finalImage = (Bitmap)image;
    finalImage.SetResolution(300, 300);
    finalImage.Save(pathToImage,  pngEncoder, myEncoderParameters);
    invertImageColours(finalImage).Save(pathToImage, pngEncoder, myEncoderParameters);
    return finalImage;
}

The capture from the screen refactored

    private Image CaptureScreen(int sourceX, int sourceY, int destX, int destY, Size regionSize)
    {
        Bitmap bmp = new Bitmap(regionSize.Width, regionSize.Height);
        bmp.SetResolution(300, 300);
        Graphics g = Graphics.FromImage(bmp);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        try
        {
            g.CopyFromScreen(sourceX, sourceY, destX, destY, regionSize);
        }catch (Exception ex)
        {
            bbeezUtils.createLogs(ex.StackTrace);
        }
        finally
        {
            bmp = resizeBitmap(bmp, regionSize.Width* resizeMultiplier, regionSize.Height*resizeMultiplier);

        }

        return bmp;
    }

An elaborate resize

 private Bitmap resizeBitmap(Image image, int width, int height)
        {
            var destRect = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        using (var graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }

        return destImage;
    }

Using Image Processor Library to increase quality

private void increaseImageQuality(String imagePath)
{
    byte[] photoBytes = File.ReadAllBytes(imagePath);
    ISupportedImageFormat format = new PngFormat { Quality = 100 };

        using (MemoryStream inStream = new MemoryStream(photoBytes))
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                {
                    imageFactory.Load(inStream)
                                .Format(format)
                                .Brightness(10)
                                .Contrast(20)
                                .Quality(100)
                                .GaussianSharpen(1)
                                .Constrain(new Size(300,150))
                                .BitDepth(256)
                                .Save(outStream);
                                

                    FileStream fileStream = new FileStream(pathToImage, FileMode.Create); 
                    outStream.WriteTo(fileStream);
                    fileStream.Close();
                }
                // Do something with the stream.
                outStream.Close();
            }
        }
    }

And invert image colors

    private Image invertImageColours(Image imgSource)
    {
        Bitmap bmpDest = null;

        using (Bitmap bmpSource = new Bitmap(imgSource))
        {
            bmpDest = new Bitmap(bmpSource.Width, bmpSource.Height);

            for (int x = 0; x < bmpSource.Width; x++)
            {
                for (int y = 0; y < bmpSource.Height; y++)
                {

                    Color clrPixel = bmpSource.GetPixel(x, y);

                    clrPixel = Color.FromArgb(255 - clrPixel.R, 255 -
                       clrPixel.G, 255 - clrPixel.B);

                    bmpDest.SetPixel(x, y, clrPixel);
                }
            }
        }
        return bmpDest;
    }

The result
How the images look in the folder

The close up is quite dissapointing though:
How the image looks when I open it

You can be as harsh as you want with me.
Please, let me know what would you do differently!

Thank you for your time in advance! :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文