2014-09-05 151 views
1

我試圖運行tessnet,圖像只包含數字,但它總是給我「〜」作爲結果,我不明白爲什麼... 這裏是代碼:tessnet c#給出錯誤的結果

private void button1_Click(object sender, EventArgs e) 
     { 
      tessnet2.Tesseract ocr = new tessnet2.Tesseract(); 
      ocr.SetVariable("tessedit_char_whitelist", ""); 
      ocr.Init(@"C:\Users\Poox\Documents\Visual Studio 2013\Projects\testCaptcha\tessdata", "fra", false); 
      Bitmap imgBit = new Bitmap(getImage()); 
      //Changing the colors of the picutre, making it easier to read, number in black and a white background: 
      Color good = new Color(); 
      good = imgBit.GetPixel(44, 19);//the color of the numbers 
      int x = 0, y = 0, mx = 100, my = 42; 
      for (x = 0; x < mx; x++) 
      { 
       for (y = 0; y < my; y++) 
       { 
        if (imgBit.GetPixel(x, y).Equals(good)) 
        { 
         //a number 
         imgBit.SetPixel(x, y, Color.Black); 
        } 
        else { imgBit.SetPixel(x, y, Color.White); }//the background 
       } 
      } 
      imgBit.Save("image2.bmp"); 
      //OCR; 
      String captcha = ""; 
      List<tessnet2.Word> result = ocr.DoOCR(imgBit, Rectangle.Empty); 
      imgBit.Dispose(); 
      foreach (tessnet2.Word word in result) 
      { 
       captcha = captcha + "" + word.Text; 
      } MessageBox.Show(captcha); 
     } 

     public Image getImage() 
     { 
      Image img = null; 
      WebClient client = new WebClient(); 
      try 
      { 
       img = Image.FromStream(client.OpenRead("- Image Link - ")); 
      }catch { } 
      client.Dispose(); 
      client = null; 
      return img; 
     } 

我從鏈接中得到圖片,我使它更加「清晰」,並且比我將它傳遞給OCR,但就像我說的,它沒有讀取它的正確性,它給了我〜 ......總是! 下面是改變顏色後的圖片: Image

代碼有什麼問題?我該如何解決它?

+0

這可能是因爲你的圖像太小 - OCR通常需要解決的一個體面的水平(最OCRD文檔往往掃描,因此300DPI或更好)。您可能需要嘗試重新縮放圖像幾倍的尺寸(首先嚐試10倍),然後查看是否給出了任何結果。 – Charleh 2014-09-05 14:34:00

+0

謝謝你的回答,試了一下,它不起作用 – user52713 2014-09-05 14:37:58

+0

你試過[訓練](http://blog.cedric.ws/how-to-train-tesseract-301)tesseract與你的字體? – 2014-09-05 14:44:23

回答

0

所以這個問題與Charleh在評論中所說的有關。圖像太小。我把你的圖片,並調整到300x300 dpi和使用插值(重新採樣)。然後我將它轉換爲1BPP,因爲您的原始圖像是使用AutoBinarize的32BPP並將其保存。

您可以在這裏看到的最終圖像:

Processed image