Scope:
I am trying to “Binarize” ( convert to only two colors ) the images that can be found here. The binarized output must follow this logic : Characters will be painted as a “Color X” and everything else will be painted as “Color Y”.
Ideally X is White and Y is black( but X and Y does not really matter).
Observations:
After some observations, i figured out that the characters are either “Yellow tones”, “White Tones” or “Black Tones”.
Using the code Bellow, i should be able to recognize and convert all those “tones” (Color channels thresholding) to one color, and whole rest, into another color.
Code Sample:
// My Own Web Requests Class. You can use C#
WebRequests wr = new WebRequests();
Native One
Bitmap bmp;
string url = "http://www.fazenda.rj.gov.br/projetoCPS/codigoImagem";
bmp = wr.GetBitmap(url);
bmp.Save(@"captcha.bmp");
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
Color pixel = bmp.GetPixel(i, j);
byte R, G, B;
// Fetching RGB Values
R = pixel.R;
G = pixel.G;
B = pixel.B;
// Converting Black tones
if ((R < Color.Black.R + 110)
&& (G < Color.Black.G + 110)
&& (B < Color.Black.B + 110))
{
bmp.SetPixel(i, j, Color.Orange);
continue;
}
// Converting Yellow tones
if ((R > Color.Yellow.R - 110)
&& (G > Color.Yellow.G - 110)
&& (B < Color.Yellow.B + 110))
{
bmp.SetPixel(i, j, Color.Orange);
continue;
}
// Converting White tones
if ((R > Color.White.R - 110)
&& (G > Color.White.G - 110)
&& (B > Color.White.B - 110))
{
bmp.SetPixel(i, j, Color.Orange);
continue;
}
bmp.SetPixel(i, j, Color.Orchid);
}
}
bmp.Save(@"FilteredCaptcha.bmp");
Problem:
After running this code, the saved output (Bitmap) actually have more than the two colors i defined in code (which are “Orange” for characters and “Orchid” for everything else, in this sample).
Once i “Zoom In” using Windows Explorer / Gimp / ImageProcessingLab , there are “tones” of orange and “tones” of orchid.
What am i missing here ? Why aren’t the outputs binarized if the only uses “two” colors, instead of “tones” to Set the pixel Colors ?
I uploaded some images to make your inspection job easier.
Input Bitmap:

Output Bitmap:

Change where you save it to:
Your code is clearly only setting two colours so the only possibility I see is that the source image is a JPG, and this format is being preseved when you save it.