You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何去除位图中的所有橙色色调以优化Tesseract OCR识别?

Hey there! Let's tackle that orange hue interference messing with your Tesseract OCR. Since you already know removing those oranges fixes the problem, we just need to translate that Photoshop magic into code. Here's how to do it in C#:

Step 1: Target All Orange Hues

Oranges vary in tone, but they all share a common trait: high red values, moderate green values, and very low blue values. We'll define a range of RGB values to catch all those orange pixels, then replace them with a neutral background color (white, assuming your document's intended background is plain white).

Simple Implementation (Good for Small Images)

This uses GetPixel/SetPixel which is straightforward, though slower for large images:

Bitmap modificar = new Bitmap("imagenamodificar.png");

// Adjust these thresholds based on your specific orange hues
int minRed = 200;
int maxRed = 255;
int minGreen = 80;
int maxGreen = 200;
int maxBlue = 60; // Oranges barely have blue in them

for (int y = 0; y < modificar.Height; y++)
{
    for (int x = 0; x < modificar.Width; x++)
    {
        Color pixelColor = modificar.GetPixel(x, y);
        
        // Check if the pixel falls within our orange range
        if (pixelColor.R >= minRed && pixelColor.R <= maxRed &&
            pixelColor.G >= minGreen && pixelColor.G <= maxGreen &&
            pixelColor.B <= maxBlue)
        {
            // Replace orange with white (swap to your actual background color if needed)
            modificar.SetPixel(x, y, Color.White);
        }
    }
}

// Now pass this cleaned bitmap to Tesseract for OCR
// Your Tesseract processing code goes here...

// Don't forget to clean up memory
modificar.Dispose();

Optimized Implementation (For Large Images)

For bigger scans, LockBits is way faster than GetPixel/SetPixel because it works directly with pixel data in memory:

Bitmap modificar = new Bitmap("imagenamodificar.png");
BitmapData bmpData = modificar.LockBits(
    new Rectangle(0, 0, modificar.Width, modificar.Height),
    ImageLockMode.ReadWrite,
    modificar.PixelFormat
);

IntPtr ptr = bmpData.Scan0;
int bytesPerPixel = Image.GetPixelFormatSize(modificar.PixelFormat) / 8;
int stride = bmpData.Stride;
byte[] pixels = new byte[stride * modificar.Height];

// Copy pixel data to a byte array for fast access
System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, pixels.Length);

// Orange hue thresholds (tweak these based on your scans!)
int minRed = 200;
int maxRed = 255;
int minGreen = 80;
int maxGreen = 200;
int maxBlue = 60;

for (int y = 0; y < modificar.Height; y++)
{
    for (int x = 0; x < modificar.Width; x++)
    {
        int index = y * stride + x * bytesPerPixel;
        
        // Note: For most formats, pixel order is B-G-R (adjust if your format is ARGB)
        byte blue = pixels[index];
        byte green = pixels[index + 1];
        byte red = pixels[index + 2];
        
        if (red >= minRed && red <= maxRed &&
            green >= minGreen && green <= maxGreen &&
            blue <= maxBlue)
        {
            // Set pixel to white
            pixels[index] = 255;     // Blue
            pixels[index + 1] = 255; // Green
            pixels[index + 2] = 255; // Red
            
            // If alpha channel exists, set to opaque
            if (bytesPerPixel == 4)
            {
                pixels[index + 3] = 255;
            }
        }
    }
}

// Copy modified data back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(pixels, 0, ptr, pixels.Length);
modificar.UnlockBits(bmpData);

// Proceed with Tesseract OCR
// ...

// Clean up
modificar.Dispose();

Pro Tips

  • Tweak the thresholds: Use a color picker (like Photoshop's) to sample your orange pixels, then adjust the minRed, maxGreen, etc., values to catch all your specific orange tones.
  • Match the background: If your document's background isn't white, replace Color.White with the correct background color (e.g., Color.LightGray) to keep contrast high for Tesseract.

内容的提问来源于stack exchange,提问作者Cristian Gerani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:24:37