Delete all tones of a bitmap color c #

2

Hi, I'm trying to delete all the orange tones of an image saved in a bitmap, I need to do OCR on the image with tesseract and the orange color of the scanned document seems to hinder the process producing errors in the text, I tried removing the orange color with photoshop, doing the OCR and works perfectly, the main problem is that the pixels are not all of the same color, they are orange but in different shades

Bitmap modificar = new Bitmap("imagenamodificar.png");
            for (int ycount2 = 0; ycount2 < modificar.Height; ycount2++)
            {
                for (int xcount2 = 0; xcount2 < modificar.Width; xcount2++)
                {
                    if (modificar.GetPixel(xcount2, ycount2) == Color.Orange)
                    {
                        modificar.SetPixel(xcount2, ycount2, Color.White);
                    }
                }
            }

This code does absolutely nothing, the image remains identical.

Then it occurred to me to compare with the pixel (0,0) since it is always the color I want to eliminate.

Bitmap modificar = new Bitmap("imagenamodificar.png");
            for (int ycount2 = 0; ycount2 < modificar.Height; ycount2++)
            {
                for (int xcount2 = 1; xcount2 < modificar.Width; xcount2++)
                {
                    if (modificar.GetPixel(xcount2, ycount2) == modificar.GetPixel(0,0))
                    {
                        modificar.SetPixel(xcount2, ycount2, Color.White);
                    }
                }
            }

But the problem is that it only eliminates a small part, orange pixels remain because, as I mentioned before, not all orange tones are the same, can someone think of something?

    
asked by Karla Urbina 31.03.2018 в 05:06
source

1 answer

0

I think your problem is in this condition

if (modificar.GetPixel(xcount2, ycount2) == modificar.GetPixel(0,0))

You are comparing with a specific color, you should compare with a range, you should compare with the orange tones.

You could do something like this:

Color colorNaranja = Color.FromArgb(255, 100, 0); //NARANJA
int nNaranja = colorNaranja.ToArgb();
Color colorNaranjaClaro = Color.FromArgb(255, 180, 0); //NARANJA CLARO
int nNaranjaClaro = colorNaranjaClaro.ToArgb();

Bitmap modificar = new Bitmap("imagenamodificar.png");
            for (int ycount2 = 0; ycount2 < modificar.Height; ycount2++)
            {
                for (int xcount2 = 1; xcount2 < modificar.Width; xcount2++)
                {
                   int n= modificar.GetPixel(xcount2, ycount2).ToArgb();
                    if ((n>nNaranja) && (n<nNaranjaClaro)) 
                    {
                        modificar.SetPixel(xcount2, ycount2, Color.White);
                    }
                }
            }

I hope it serves you.

    
answered by 31.03.2018 в 11:51