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

摘要:
10月。初始化(@“D://www.newwhy.com/2010/0910/13708。html字符串s=“0”//下载要识别的图像MemoryStream ms=新MemoryStream(oimg);

首先下载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: essdata""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);

}

免责声明:文章转载自《C#调用开源图像识别类库tessnet2》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Redis的并发竞争问题,你用哪些方案来解决?ArcMap运行时出现Runtime Error错误的解决方案下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

趋势买卖--主图叠加

1、趋势买卖(主图叠加)   一、公式编写 1、第一种 N:=20; M:=32;P1:=80;P2:=100; VAR1:=(C+H+O+L)/4;卖出:XMA(VAR1,N)*(1+P1/1000),COLORGREEN,LINETHICK2;买入:XMA(VAR1,M)*(1-P2/1000),COLORMAGENTA,LINETHICK2;幅度:1...

Qt入门之常用Qt标准对话框之QMessageBox

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://devbean.blog.51cto.com/448512/217694 好久没有更新博客,主要是公司里面还在验收一些东西,所以没有及时更新。而且也在写一个基于Qt的画图程序,基本上类似于PS的东西,主要用到的是Qt Graphics...

SV中的数据类型

Verilog-1995中规定的数据类型有:变量(reg), 线网(wire), 32位有符号数(integer), 64位无符号数(time), 浮点数(real)。 SV扩展了reg类型为logic,除了reg类型的功能外,可以用在连续赋值,门单元和模块所驱动。但是不能用在双向总线建模,不能有多点驱动。 其他数据类型:无符号双状态  bit,     ...

链表面试题

1、逆置链表 假设链表现在是 4->3->2->1->NULL逆置后的链表是 1->2->3->4->NULL步骤:第一步:先把4用临时指针tmp保存起来,cur指向下一个节点,即cur指向3 第二步:令tmp指向newNode,4是第一个节点,则4的next为NULL,即令tmp->next =ne...

上传图片,预览并保存成blob类型 和 base64

场景: 获取到一个file类型的图片,如果直接在html中预览?这里就是利用html5的新特性,将图片转换为Base64的形式显示出来。有两种方法: 方法一:利用URL.createObjectURL() <!DOCTYPE html><html>  <head>     <title>base</ti...

我的Python之路:浏览器模拟

一、浏览器模拟——Header属性    有的时候,我们无法爬取一些网页,也就是说会出现403错误,这是因为这些网页为了防止有人恶意去采集其信息所以进行了一些反爬虫的设置。   为了可以获取这些数据我们使用一些两种方法: 1、使用 build opener() 由于urlopen()不支持HTTP的高级运用所以我们要修改头报。可以使用urllib.requ...