2016-06-11 4 views
0

getwindow関数で作成されたスクリーンショットから返されたビットマップ上でtessnetを実行しようとしましたが、結果が悪いです。私はペイントに保存されたbmpファイルを実行しようとしました。このイメージは、getwindowで作成されたイメージと同じもので、これはtessnetの作業です。 This is the imageTessnet 2が間違った結果を返す

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

    } 

答えて

0

Tesseractは、白い背景に黒いフォントが必要です。反転、ヒストグラムのイコライゼーション、グレースケールへの変換が役立ちます。

あなたの画像は数字だけで構成されているようです。あなたは数字を探すためにtesseractをヒントできます(そしてポイントのみ)。しかし、私はTessnetでそれをどうやって行うのか分かりません。

Tesseractでは、異なるサイズのフォントが好まれません。それでも結果が悪い場合は、2つの異なる画像で画像を分割して別々に送る必要があります。

+0

同じイメージでbmpファイルを実行しようとしましたが、動作します。 – ezegyfa

関連する問題