views:

668

answers:

3

I have an image with letters in it, the letters are in two colors black and blue, I want to read the blue colored letters from the image.

Can anyone suggest me a method to do this in C#. Iam studying GDI+,but still didn't get any logic to develop this program..

I tried OCRing it, but the issue with common OCRs is that they dont recognize the color difference.

I only want to read the Blue characters....

Any guidance is highly appreciated.

A: 

Step 1 -- set all black pixels to white

Step 2 -- set all blue pixels to blue

Step 3 -- OCR it

?

Regards

Mark

High Performance Mark
+7  A: 

Try this one ;) But that's unsafe code.

void RedAndBlue()
{

    OpenFileDialog ofd;
    int imageHeight, imageWidth;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
     Image tmp = Image.FromFile(ofd.FileName);
     imageHeight = tmp.Height;
     imageWidth = tmp.Width;
    }
    else
    {
     // error
    }

    int[,] bluePixelArray = new int[imageWidth, imageHeight];
    int[,] redPixelArray = new int[imageWidth, imageHeight];
    Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height);
    Bitmap temp = new Bitmap(tmp);
    BitmapData bmpData = temp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int remain = bmpData.Stride - bmpData.Width * 3;
    unsafe
    {
     byte* ptr = (byte*)bmpData.Scan0;
     for (int j = 0; j < bmpData.Height; j++)
     {
      for (int i = 0; i < bmpData.Width; i++)
      {
       bluePixelArray[i, j] = ptr[0];
       redPixelArray[i, j] = ptr[2];
       ptr += 3;
      }
      ptr += remain;
     }
    }
    temp.UnlockBits(bmpData);
    temp.Dispose();
}
Ahmet Kakıcı
This is the right way to do bitmap pixel manipulation. The "unsafe" keyword is unfortunate in this case, since it's only really unsafe if you aren't good at math. "notaspathethicallyslowasgetpixelandsetpixel" would be a more appropriate keyword for this.
MusiGenesis
I have to warn him about 'unsafe' part because he needs to allow unsafe code before. And you are right about "notaspathethicallyslowasgetpixelandsetpixel" :)
Ahmet Kakıcı
+1 for this, but should you also call temp.Dispose() after the final UnlockBits operation?
Andy
@Andy i've copied this code from my own project and it already has dispose(); Now i'm adding it here too :) Thanks.
Ahmet Kakıcı
Thanks Ahmet, this code works like charm for me...Thanks a lot
Sumit Ghosh
You are welcome.
Ahmet Kakıcı
A: 

You could probably modify the palette to have only black and white on the image

Thomas Levesque