0

我试图在从使用 getwindow 函数创建的屏幕截图返回的位图上运行 tessnet,但结果很糟糕。我试图在一个保存在paint中的bmp文件上运行。此图像与使用 getwindow 创建的图像相同,为此 tessnet 工作。这是图像任何想法?

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

    }
4

1 回答 1

0

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

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

Tesseract 不喜欢不同大小的字体。如果结果仍然不好,则可能需要将图像拆分为两个不同的图像并分别提供它们。

于 2016-06-11T17:56:01.783 回答