Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepping a small low resolution image for OCR in C#

Today I started using Tesseract to parse portions of my screen for numbers. I have had a decent amount of success with larger text (which results in a higher resolution image). Now I am trying to use Tesseract in a more practical sense and the image quality is too low. I have tried increasing the resolution and redrawing with anti-aliasing, but I am not sure if I am even doing these things right. Do you have any suggestions as to how I might be able to get Tesseract to recognize the "12" in my tiny image?

Image: img

static public void test()
{
    string readIn;
    TesseractEngine engine = new TesseractEngine(@".\tessdata","eng", EngineMode.Default);

    engine.SetVariable("tessedit_char_whitelist", "0123456789"); //only read as numbers

    Rectangle rect = new Rectangle(181, 107, 25, 25);

    Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);

    Graphics g = Graphics.FromImage(bmp);
    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
    g.InterpolationMode = InterpolationMode.High;
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.SmoothingMode = SmoothingMode.AntiAlias;

    g.DrawImage(bmp, rect.Width, rect.Height); //Do some anti-aliasing hopefully?
    bmp.SetResolution(300, 300) //Try increasing resolution??

    bmp.Save(@".\tmp.jpg");
    readIn =  engine.Process(PixConverter.ToPix(bmp)).GetText();
    Console.WriteLine("This is what was read: " + readIn); //Empty
}
like image 951
M. Santiago Avatar asked Dec 06 '25 07:12

M. Santiago


1 Answers

I suggest using image processing methodes to improve the accuracy of tesseract-ocr. I use OpenCV libraries in c++ for this.

So let's take your image and rescale it by +500%:

rescaled image

You can see the image is getting a bit pixely. In this case you want to smooth the edges by using a Gaussian filter. I used a Gaussian filter with a kernal size of 3x3:

smooth image

The last thing you need to do is segmentation of the digits by using a threshold:

segmented image

Running tesseract on the segmented image using the digit whitelist will result in "12".

Hope this helped. :)

like image 161
Janco de Vries Avatar answered Dec 09 '25 06:12

Janco de Vries