0

I trying to run tessnet on a bitmap returned from a screenshot created with getwindow function but the result is bad. I tried to run on a bmp file saved in paint. This image is same as image created with getwindow and for this the tessnet work. This is the image Any idea?

    public const int SRCCOPY = 13369376;
    public const int WM_CLICK = 0x00F5;
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", EntryPoint = "GetDC")]
    internal extern static IntPtr GetDC(IntPtr hWnd);
    [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
    internal extern static IntPtr CreateCompatibleDC(IntPtr hdc);
    [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
    internal extern static IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
    [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
    internal extern static IntPtr DeleteDC(IntPtr hDc);
    [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
    internal extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
    [DllImport("gdi32.dll", EntryPoint = "BitBlt")]
    internal extern static bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp);
    [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
    internal extern static IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    internal extern static IntPtr DeleteObject(IntPtr hDc);
    [DllImport("user32.dll")]
    public static extern int SendMessage(
          int hWnd,      // handle to destination window
          uint Msg,       // message
          long wParam,  // first message parameter
          long lParam   // second message parameter
          );

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    public static Bitmap createBitmapFromWindow(string windowClass,string windowTitle,Point sarok1,Point sarok2)
    {
        IntPtr hWnd = FindWindow(windowClass, windowTitle);
        Bitmap bmp = null;
        IntPtr hdcFrom = GetDC(hWnd);
        IntPtr hdcTo = CreateCompatibleDC(hdcFrom);
        RECT windowSize;
        GetWindowRect(hWnd, out windowSize);
        int height = windowSize.Bottom;
        int width = windowSize.Right;
        IntPtr hBitmap = CreateCompatibleBitmap(hdcFrom, width, height);
        if (hBitmap != IntPtr.Zero)
        {
            // adjust and copy
            IntPtr hLocalBitmap = SelectObject(hdcTo, hBitmap);
            int posx, posy;
            if (sarok1.X > sarok2.X)
            {
                posx = sarok2.X;
            }
            else
            {
                posx = sarok1.X;
            }
            if (sarok1.Y > sarok2.Y)
            {
                posy = sarok2.Y;
            }
            else
            {
                posy = sarok1.Y;
            }
            BitBlt(hdcTo, 0, 0, Math.Abs(sarok1.X-sarok2.X), Math.Abs(sarok1.Y-sarok2.Y),
                hdcFrom, posx, posy, SRCCOPY);
            SelectObject(hdcTo, hLocalBitmap);


            //We delete the memory device context.
            DeleteDC(hdcTo);
            //We release the screen device context.
            ReleaseDC(hWnd, hdcFrom);
            //Image is created by Image bitmap handle and assigned to Bitmap variable.
            bmp = System.Drawing.Image.FromHbitmap(hBitmap);
            DeleteObject(hBitmap);
        }
        return bmp;
    }

    public static void main()
    {
        Bitmap b1 = new Bitmap(createBitmapFromWindow(null, "window title", new Point(557, 460), new Point(670, 500)));
        Bitmap b = b1.Clone(new Rectangle(new Point(0, 0), new Size(110, 29)),PixelFormat.Format24bppRgb);
        var ocr = new Tesseract();
        ocr.Init(@"path", "eng", false);
        b.SetResolution(300, 300);
        List<Word> l = ocr.DoOCR(b, Rectangle.Empty);

    }
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
ezegyfa
  • 31
  • 2

1 Answers1

0

Tesseract expects black font on white background. An invert, a histogram equalization and a conversion to grayscale will help.

Looks like your images will consists of numbers only. You can hint tesseract to look for a numbers (and the point only). But I don't know how to do that with Tessnet.

Tesseract doesn't like different sized fonts. If the result is still not good it might be necessary to split the image in two different images and feed them separately.

tobltobs
  • 2,782
  • 1
  • 27
  • 33