C#调用开源图像识别类库tessnet2

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

}

转载于:https://www.cnblogs.com/watchfluture/p/4755997.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值