C#調用開源圖像識別類庫tessnet2


首先下載tessnet2_32.dll及相關語言包,將dll加入引用

 

使用方法很簡單

 

1.下載tesseract 的.net 類庫tessnet2_32.dll ,添加引用。   http://www.pixel-technology.com/freeware/tessnet2/

2.下載tesseract 相對應的語言包。 http://code.google.com/p/tesseract-ocr/downloads/list

3.調用tesseract 的方法進行識別。

  1.        private tessnet2.Tesseract ocr = new tessnet2.Tesseract();//聲明一個OCR類  
  2.   
  3.   
  4.             //程序開始的時候,初始化OCR  
  5.        ocr.SetVariable("tessedit_char_whitelist""0123456789."); //設置識別變量,當前只能識別數字。  
  6.         ocr.Init(@"D:\tessdata""eng"false); //應用當前語言包。注,Tessnet2是支持多國語的。語言包下載鏈接:http://code.google.com/p/tesseract-ocr/downloads/list  
  7.   
  8.   
  9. //下邊這個函數是將網絡上的圖片識別成字符串,傳入圖片的超鏈接,輸出字符串  
  10.   
  11.         public string Bmp2Str(string bmpurl)  
  12.   
  13.         {  
  14.             //http://www.newwhy.com/2010/0910/13708.html  
  15.             string s = "0";  
  16.             WebClient wc = new WebClient();  
  17.             try  
  18.             {  
  19.                 byte[] oimg = wc.DownloadData(bmpurl);//將要識別的圖像下載下來  
  20.                 MemoryStream ms = new MemoryStream(oimg);  
  21.                 Bitmap image = new Bitmap(ms);  
  22.   
  23.   
  24. //為了提高識別率,所以對圖片進行簡單的處理  
  25.                 image = BlackAndWhite(image, 0.8);//黑白處理,這個函數看下邊  
  26.                 image = new Bitmap(image, image.Width * 3, image.Height * 3);//放大三倍  
  27.                   
  28.                 Monitor.Enter(this);//因為是多線程,所以用到了Monitor。  
  29.                 System.Collections.Generic.List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);//執行識別操作  
  30.                 foreach (tessnet2.Word word in result) //遍歷識別結果。  
  31.                     s = s + word.Text;  
  32.                 Monitor.Exit(this);  
  33.                 if (s.Length > 2)  
  34.                     s = s.Substring(2, s.Length - 2);  
  35.   
  36.             }  
  37.             catch  
  38.             {  
  39.                 s = "0";  
  40.             }  
  41.             finally  
  42.             {  
  43.                 wc.Dispose();  
  44.             }  
  45.             return s;  
  46.   
  47.   
  48.             //Console.WriteLine("{0} : {1}", word.Confidence, word.Text);   
  49.         }  
  50.   
  51.   
  52.   
  53.   
  54.   
  55. //黑白處理的函數,網上查的。  
  56.   
  57.         public static Bitmap BlackAndWhite(Bitmap bitmap, Double hsb)  
  58.         {  
  59.             if (bitmap == null)  
  60.             {  
  61.                 return null;  
  62.             }  
  63.   
  64.   
  65.             int width = bitmap.Width;  
  66.             int height = bitmap.Height;  
  67.             try  
  68.             {  
  69.                 Bitmap bmpReturn = new Bitmap(width, height, PixelFormat.Format1bppIndexed);  
  70.                 BitmapData srcBits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);  
  71.                 BitmapData targetBits = bmpReturn.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);  
  72.   
  73.   
  74.                 unsafe  
  75.                 {  
  76.                     byte* pSrcBits = (byte*)srcBits.Scan0.ToPointer();  
  77.                     for (int h = 0; h < height; h++)  
  78.                     {  
  79.                         byte[] scan = new byte[(width + 7) / 8];  
  80.                         for (int w = 0; w < width; w++)  
  81.                         {  
  82.                             int r, g, b;  
  83.                             r = pSrcBits[2];  
  84.                             g = pSrcBits[1];  
  85.                             b = pSrcBits[0];  
  86.   
  87.   
  88.                             if (GetBrightness(r, g, b) >= hsb) scan[w / 8] |= (byte)(0x80 >> (w % 8));  
  89.                             pSrcBits += 3;  
  90.                         }  
  91.                         Marshal.Copy(scan, 0, (IntPtr)((int)targetBits.Scan0 + targetBits.Stride * h), scan.Length);  
  92.                         pSrcBits += srcBits.Stride - width * 3;  
  93.                     }  
  94.                     bmpReturn.UnlockBits(targetBits);  
  95.                     bitmap.UnlockBits(srcBits);  
  96.                     return bmpReturn;  
  97.                 }  
  98.             }  
  99.             catch  
  100.             {  
  101.                 return null;  
  102.             }  
  103.   
  104.   
  105.         }  

 

找到了
private static float GetBrightness(int r, int g, int b)
{
float fR = ((float)r) / 255f;
float fG = ((float)g) / 255f;
float fB = ((float)b) / 255f;
float fMax = fR;
float fMin = fR;

fMax = (fG > fMax) ? fG : fMax;
fMax = (fB > fMax) ? fB : fMax;

fMin = (fG < fMax) ? fG : fMax;
fMin = (fB < fMax) ? fB : fMax;

return ((fMax + fMin) / 2f);

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM