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. //程序开始的时候,初始化OCR
  3. ocr.SetVariable("tessedit_char_whitelist", "0123456789."); //设置识别变量,当前只能识别数字。
  4. ocr.Init(@"D:\tessdata", "eng", false); //应用当前语言包。注,Tessnet2是支持多国语的。语言包下载链接:http://code.google.com/p/tesseract-ocr/downloads/list
  5. //下边这个函数是将网络上的图片识别成字符串,传入图片的超链接,输出字符串
  6. public string Bmp2Str(string bmpurl)
  7. {
  8. //http://www.newwhy.com/2010/0910/13708.html
  9. string s = "0";
  10. WebClient wc = new WebClient();
  11. try
  12. {
  13. byte[] oimg = wc.DownloadData(bmpurl);//将要识别的图像下载下来
  14. MemoryStream ms = new MemoryStream(oimg);
  15. Bitmap image = new Bitmap(ms);
  16. //为了提高识别率,所以对图片进行简单的处理
  17. image = BlackAndWhite(image, 0.8);//黑白处理,这个函数看下边
  18. image = new Bitmap(image, image.Width * 3, image.Height * 3);//放大三倍
  19. Monitor.Enter(this);//因为是多线程,所以用到了Monitor。
  20. System.Collections.Generic.List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);//执行识别操作
  21. foreach (tessnet2.Word word in result) //遍历识别结果。
  22. s = s + word.Text;
  23. Monitor.Exit(this);
  24. if (s.Length > 2)
  25. s = s.Substring(2, s.Length - 2);
  26. }
  27. catch
  28. {
  29. s = "0";
  30. }
  31. finally
  32. {
  33. wc.Dispose();
  34. }
  35. return s;
  36. //Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
  37. }
  38. //黑白处理的函数,网上查的。
  39. public static Bitmap BlackAndWhite(Bitmap bitmap, Double hsb)
  40. {
  41. if (bitmap == null)
  42. {
  43. return null;
  44. }
  45. int width = bitmap.Width;
  46. int height = bitmap.Height;
  47. try
  48. {
  49. Bitmap bmpReturn = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
  50. BitmapData srcBits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  51. BitmapData targetBits = bmpReturn.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
  52. unsafe
  53. {
  54. byte* pSrcBits = (byte*)srcBits.Scan0.ToPointer();
  55. for (int h = 0; h < height; h++)
  56. {
  57. byte[] scan = new byte[(width + 7) / 8];
  58. for (int w = 0; w < width; w++)
  59. {
  60. int r, g, b;
  61. r = pSrcBits[2];
  62. g = pSrcBits[1];
  63. b = pSrcBits[0];
  64. if (GetBrightness(r, g, b) >= hsb) scan[w / 8] |= (byte)(0x80 >> (w % 8));
  65. pSrcBits += 3;
  66. }
  67. Marshal.Copy(scan, 0, (IntPtr)((int)targetBits.Scan0 + targetBits.Stride * h), scan.Length);
  68. pSrcBits += srcBits.Stride - width * 3;
  69. }
  70. bmpReturn.UnlockBits(targetBits);
  71. bitmap.UnlockBits(srcBits);
  72. return bmpReturn;
  73. }
  74. }
  75. catch
  76. {
  77. return null;
  78. }
  79. }

找到了

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

}