Scope:
Hi everyone, i am trying to convert this captcha to a “black and white” (binarized) image, where the characters are white and the whole rest (background,lines,random pictures) are black.
The link to the captcha can be found here. Refreshing will give you another captcha.
Reason:
I know most of people think it is wrong to mess with captchas so here i am defending myself. This will be used for knowledge / self challenge only. No other use is planned for these images.
Problem:
After studying these images for a while, i figured out that a good aproach would be to replace the colors : “White and Yellow” to “Color.Black”, and every other color should be replaced to “Color.White.”
After this, i would just “Invert” the colors, leading me to the output i want.
Code Sample:
In this code, i am trying to replace the color “Black” of every image for a SkyBlue Pixel.
WebRequests wr = new WebRequests(); // My class to handle WebRequests
Bitmap bmp;
string url = "http://www.fazenda.rj.gov.br/projetoCPS/codigoImagem";
bmp = wr.GetBitmap (url);
for (int i = 1; i < bmp.Height ; i++)
{
for (int j = 1 ; j < bmp.Width ; j++)
{
if (bmp.GetPixel(j,i).Equals(Color.Black))
{
bmp.SetPixel(j,i, Color.SkyBlue);
}
}
}
This code do not work at all, i’m not sure why, but no pixel is getting replaced in this example.
Question:
How can i make this work ? What am i missing here ?
Also, the ideal scenario for me would be to “reduce” the color pallete of this image to “basic” colors, that would make my job here much easier.
I already tried the AForge Framework, which i am using to reduce colors, but it is not working at all, the result is not the one i expected.
What can i do here in order to binarize this image correctly ?
Thanks in advance,
Marcello Lins.
The reason why your algorithm does not work is that you are looking for an exact match. But the black in the image isn’t really black due to JPEG compression artifacts; in three CAPTCHAS I’ve loaded there was not even a single black (0, 0, 0) pixel. The closest value was (0, 0, 4) which looks black, but isn’t.
An approximate approach would be:
At the end of this process, the background is all gray, all nongray pixels left are characters.
Some useful functions: