C#调用开源图像识别类库tessnet2
2014-08-15 10:23
351 查看
转自:http://blog.csdn.net/agai001/article/details/7352815
本人在做比价蜘蛛的时候用到:京东商城的单品价格是图片形式,所以需要识别。
首先下载tessnet2_32.dll及相关语言包,将dll加入引用
[csharp] view
plaincopy
private tessnet2.Tesseract ocr = new tessnet2.Tesseract();//声明一个OCR类
//程序开始的时候,初始化OCR
ocr.SetVariable("tessedit_char_whitelist", "0123456789."); //设置识别变量,当前只能识别数字。
ocr.Init(@"D:\tessdata", "eng", false); //应用当前语言包。注,Tessnet2是支持多国语的。语言包下载链接:http://code.google.com/p/tesseract-ocr/downloads/list
//下边这个函数是将网络上的图片识别成字符串,传入图片的超链接,输出字符串
public string Bmp2Str(string bmpurl)
{
//http://www.newwhy.com/2010/0910/13708.html
string s = "0";
WebClient wc = new WebClient();
try
{
byte[] oimg = wc.DownloadData(bmpurl);//将要识别的图像下载下来
MemoryStream ms = new MemoryStream(oimg);
Bitmap image = new Bitmap(ms);
//为了提高识别率,所以对图片进行简单的处理
image = BlackAndWhite(image, 0.8);//黑白处理,这个函数看下边
image = new Bitmap(image, image.Width * 3, image.Height * 3);//放大三倍
Monitor.Enter(this);//因为是多线程,所以用到了Monitor。
System.Collections.Generic.List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);//执行识别操作
foreach (tessnet2.Word word in result) //遍历识别结果。
s = s + word.Text;
Monitor.Exit(this);
if (s.Length > 2)
s = s.Substring(2, s.Length - 2);
}
catch
{
s = "0";
}
finally
{
wc.Dispose();
}
return s;
//Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
}
//黑白处理的函数,网上查的。
public static Bitmap BlackAndWhite(Bitmap bitmap, Double hsb)
{
if (bitmap == null)
{
return null;
}
int width = bitmap.Width;
int height = bitmap.Height;
try
{
Bitmap bmpReturn = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
BitmapData srcBits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData targetBits = bmpReturn.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
unsafe
{
byte* pSrcBits = (byte*)srcBits.Scan0.ToPointer();
for (int h = 0; h < height; h++)
{
byte[] scan = new byte[(width + 7) / 8];
for (int w = 0; w < width; w++)
{
int r, g, b;
r = pSrcBits[2];
g = pSrcBits[1];
b = pSrcBits[0];
if (GetBrightness(r, g, b) >= hsb) scan[w / 8] |= (byte)(0x80 >> (w % 8));
pSrcBits += 3;
}
Marshal.Copy(scan, 0, (IntPtr)((int)targetBits.Scan0 + targetBits.Stride * h), scan.Length);
pSrcBits += srcBits.Stride - width * 3;
}
bmpReturn.UnlockBits(targetBits);
bitmap.UnlockBits(srcBits);
return bmpReturn;
}
}
catch
{
return null;
}
}
本人在做比价蜘蛛的时候用到:京东商城的单品价格是图片形式,所以需要识别。
首先下载tessnet2_32.dll及相关语言包,将dll加入引用
[csharp] view
plaincopy
private tessnet2.Tesseract ocr = new tessnet2.Tesseract();//声明一个OCR类
//程序开始的时候,初始化OCR
ocr.SetVariable("tessedit_char_whitelist", "0123456789."); //设置识别变量,当前只能识别数字。
ocr.Init(@"D:\tessdata", "eng", false); //应用当前语言包。注,Tessnet2是支持多国语的。语言包下载链接:http://code.google.com/p/tesseract-ocr/downloads/list
//下边这个函数是将网络上的图片识别成字符串,传入图片的超链接,输出字符串
public string Bmp2Str(string bmpurl)
{
//http://www.newwhy.com/2010/0910/13708.html
string s = "0";
WebClient wc = new WebClient();
try
{
byte[] oimg = wc.DownloadData(bmpurl);//将要识别的图像下载下来
MemoryStream ms = new MemoryStream(oimg);
Bitmap image = new Bitmap(ms);
//为了提高识别率,所以对图片进行简单的处理
image = BlackAndWhite(image, 0.8);//黑白处理,这个函数看下边
image = new Bitmap(image, image.Width * 3, image.Height * 3);//放大三倍
Monitor.Enter(this);//因为是多线程,所以用到了Monitor。
System.Collections.Generic.List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);//执行识别操作
foreach (tessnet2.Word word in result) //遍历识别结果。
s = s + word.Text;
Monitor.Exit(this);
if (s.Length > 2)
s = s.Substring(2, s.Length - 2);
}
catch
{
s = "0";
}
finally
{
wc.Dispose();
}
return s;
//Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
}
//黑白处理的函数,网上查的。
public static Bitmap BlackAndWhite(Bitmap bitmap, Double hsb)
{
if (bitmap == null)
{
return null;
}
int width = bitmap.Width;
int height = bitmap.Height;
try
{
Bitmap bmpReturn = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
BitmapData srcBits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData targetBits = bmpReturn.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
unsafe
{
byte* pSrcBits = (byte*)srcBits.Scan0.ToPointer();
for (int h = 0; h < height; h++)
{
byte[] scan = new byte[(width + 7) / 8];
for (int w = 0; w < width; w++)
{
int r, g, b;
r = pSrcBits[2];
g = pSrcBits[1];
b = pSrcBits[0];
if (GetBrightness(r, g, b) >= hsb) scan[w / 8] |= (byte)(0x80 >> (w % 8));
pSrcBits += 3;
}
Marshal.Copy(scan, 0, (IntPtr)((int)targetBits.Scan0 + targetBits.Stride * h), scan.Length);
pSrcBits += srcBits.Stride - width * 3;
}
bmpReturn.UnlockBits(targetBits);
bitmap.UnlockBits(srcBits);
return bmpReturn;
}
}
catch
{
return null;
}
}
相关文章推荐
- C#调用开源图像识别类库tessnet2
- C#WinForm-OCRTessnet图像识别
- C#调用DLL实现图像文字识别(OCR)
- 实现验证码图像文字的识别(C#调用DLL)
- C#调用DLL实现图像文字识别(OCR)
- 机器视觉开源处理库汇总-介绍n款计算机视觉库/人脸识别开源库/软件 -几种图像处理类库的比较-视觉相关网站
- c#调用c++程序(DLL方法,以及opencv,运行开源的人脸识别seetaface)
- 机器视觉开源处理库汇总-介绍n款计算机视觉库/人脸识别开源库/软件 -几种图像处理类库的比较-视觉相关网站
- 机器视觉开源处理库汇总-介绍n款计算机视觉库/人脸识别开源库/软件 -几种图像处理类库的比较-视觉相关网站
- 实现验证码图像文字的识别(C#调用DLL)
- 下面介绍一个开源的OCR引擎Tesseract2。值得庆幸的是虽然是开源的但是它的识别率较高,并不比其他引擎差劲。网上介绍Tessnet2也是当时时间排名第三的识别引擎,只是后来慢慢不维护了,目前是G
- 机器视觉开源处理库汇总-介绍n款计算机视觉库/人脸识别开源库/软件 -几种图像处理类库的比较-视觉相关网站
- 机器视觉开源处理库汇总-介绍n款计算机视觉库/人脸识别开源库/软件 -几种图像处理类库的比较-视觉相关网站
- C#调用DLL实现图像文字识别(OCR)
- C#调用DLL实现图像文字识别(OCR)
- 使用IDispatch::Invoke函数在C++中调用C#实现的托管类库方法
- 开源CxImage图像操作类库
- C++调用C#类库; 非托管C++字符串和托管字符串的转换
- 利用C#的反射机制动态调用DLL类库
- 【转】Delphi调用C#类库