Tessnet 2返回错误的结果

时间:2016-06-11 17:47:45

标签: c# tessnet2

我尝试在使用getwindow函数创建的屏幕截图返回的位图上运行tessnet,但结果很糟糕。我试图在一个保存在油漆中的bmp文件上运行。此图像与使用getwindow创建的图像相同,为此tessnet工作。 This is the image有什么想法吗?

    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);

    }

1 个答案:

答案 0 :(得分:0)

Tesseract期待白色背景上的黑色字体。反转,直方图均衡和灰度转换将有所帮助。

您的图片看起来只包含数字。您可以提示tesseract查找数字(仅限点)。但我不知道如何用Tessnet做到这一点。

Tesseract不喜欢不同大小的字体。如果结果仍然不好,则可能需要将图像分成两个不同的图像并单独进纸。