Tesseract 的 github地址:https://github.com/tesseract-ocr/tesseract
使用 Tesseract 主要是开源,识别效率还不错,貌似是微软在维护。
主要注意以下几点:PS楼主踩的坑
1.识别前需要做图片截取,只保留需要识别的部分
2.识别前要做图片相关的处理,比如图片二值化、文字色调反转等
3.图片放大,Tesseract对DPI300*300 以上的 图片识别效果较好
4.图片识别文字
主要代码:
1.图片截取,先截取所需系统的图片,直接识别图片可省略此步
///hWnd 为窗体要截图的句柄
public Bitmap GetWindowCapture(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
RECT windowRect = new RECT();
GetWindowRect(hWnd, ref windowRect);
int width = (int)(windowRect.Right - windowRect.Left);
int height = (int)(windowRect.Bottom - windowRect.Top);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
/// <summary>
/// 从大图中截取一部分图片
/// </summary>
/// <param name="fromImagePath">来源图片地址</param>
/// <param name="offsetX">从偏移X坐标位置开始截取</param>
/// <param name="offsetY">从偏移Y坐标位置开始截取</param>
/// <param name="toImagePath">保存图片地址</param>
/// <param name="width">保存图片的宽度</param>
/// <param name="height">保存图片的高度</param>
/// <returns></returns>
public void CaptureImage(string fromImagePath, int offsetX, int offsetY, string toImagePath, int width, int height)
{
//原图片文件
Image fromImage = Image.FromFile(fromImagePath);
//创建新图位图
Bitmap bitmap = new Bitmap(width, height);
//创建作图区域
Graphics graphic = Graphics.FromImage(bitmap);
//截取原图相应区域写入作图区
graphic.DrawImage(fromImage, 0, 0, new Rectangle(offsetX, offsetY, width, height), GraphicsUnit.Pixel);
//从作图区生成新图
Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
//保存图片
saveImage.Save(toImagePath, ImageFormat.Png);
//释放资源
saveImage.Dispose();
graphic.Dispose();
bitmap.Dispose();
}
2.图片处理 用的OpenCvSharp +代码处理(有点大材小用了)
//图片二值化
public static Bitmap Run(string path)
{
using (var src = Cv2.ImRead(path, ImreadModes.Grayscale))
// using (var niblack = new Mat())
using (var bernsen = new Mat())
{
int kernelSize = 51;
// Binarizer.Niblack(src, niblack, kernelSize, -0.2);
Binarizer.Bernsen(src, bernsen, kernelSize, 50, 200);
return bernsen.ToBitmap();
}
}
//我要识别的文字 二值化之后 黑底白字 做一次转换 =》黑字白底
private static Bitmap TransLate(Bitmap bitMap)
{
for (int i = 0; i < bitMap.Width; i++)
{
for (int j = 0; j < bitMap.Height; j++)
{
Color origalColor = bitMap.GetPixel(i, j);
Color newColor = Color.FromArgb(255 - origalColor.R, 255 - origalColor.G, 255 - origalColor.B);
bitMap.SetPixel(i, j, newColor);
}
}
return bitMap;
}
.3. 图片放大
/// <summary>
/// 整体放大 没有锯齿 识别效率更高
/// </summary>
/// <param name="bit"></param>
/// <param name="beishu"></param>
/// <returns></returns>
public static Bitmap getnew(Image bit, double beishu)//beishu参数为放大的倍数。放大缩小都可以,0.8即为缩小至原来的0.8倍
{
Bitmap destBitmap = new Bitmap(Convert.ToInt32(bit.Width * beishu), Convert.ToInt32(bit.Height * beishu));
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(Color.Transparent);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(bit, new Rectangle(0, 0, destBitmap.Width, destBitmap.Height), 0, 0, bit.Width, bit.Height, GraphicsUnit.Pixel);
g.Dispose();
return destBitmap;
}
4.识别文字
tesseract 34W.png result -l chi_sim
识别率还是可以的,如果识别效果不佳也可以自己训练字体库我这边整理了下bat文件 执行前转相应的tif文件
echo Generate myfontlab.normal.exp0.tif...
rename 1.tif myfontlab.normal.exp0.tif
echo Generate makebox...
tesseract myfontlab.normal.exp0.tif myfontlab.normal.exp0 -l chi_sim batch.nochop makebox
--jTessBoxEditor 做位置调整
调整完成后执行下边bat命令
set filename=myfontlab.normal.exp0
:: 生成字符集文件
echo Generate unicharset...
unicharset_extractor %filename%.box
:: 生成训练文件
echo Run Tesseract for Training...
::tesseract %filename%.tif %filename% nobatch -l chi_sim box.train
tesseract %filename%.tif %filename% nobatch box.train
:: 生成font_properties
echo Generate font_properties...
echo normal 0 0 0 0 0 >font_properties
:: 生成聚集字符特征文件
echo Generate Character...
::mftraining -F font_properties -U unicharset -O unicharset %filename%.tr
shapeclustering -F font_properties -U unicharset %filename%.tr
mftraining -F font_properties -U unicharset -O unicharset %filename%.tr
:: 生成字符正常化特征文件
echo Clustering...
cntraining %filename%.tr
:: 重新命名
echo Rename Files...
rename normproto normal.normproto
rename inttemp normal.inttemp
rename pffmtable normal.pffmtable
rename shapetable normal.shapetable
rename unicharset normal.unicharset
:: 合并训练文件
echo Create normal...
combine_tessdata normal.
pause
多张图片合并也是需要有相关bat 有需要找我要吧,简单使用OCR到此结束了