zoukankan      html  css  js  c++  java
  • 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: 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);

    }

  • 相关阅读:
    汉诺塔
    给出一个字符串,要求插入最少的字符,使得原字符串为一个回文串
    最长回文子串
    回文数 第N个回文数
    屋子里有1到100号100盏关闭的灯
    无头结点的单链表删除一个中间结点
    单链表逆转
    编程之美2.21 加法
    在一个数组中找 差值最大的两个数 差值最小的两个数 推广到 点对
    斐波那契
  • 原文地址:https://www.cnblogs.com/xishi/p/3560861.html
Copyright © 2011-2022 走看看