OCR引擎
OCR(Optical Character Recognition)是指使用掃描儀或數碼相機對文本資料進行掃描成圖像文件,然后對圖像文件進行分析處理,自動識別獲取文字信息及版面信息的軟件。
OCR引擎核心技術模塊主要是由下面幾個部分組成:
圖像輸入:讀取不同圖像格式文件的算法。
圖像預處理:主要包括圖像二進制化,噪聲去除,傾斜較正等算法
版面分析:將文檔圖片分段落,分行的算法就叫版面分析算法
字符切割:字符切割算法主要處理因字符粘連、斷筆造成字符難以簡單切割的問題。
字符特征提取:對字符圖像提取多維的特征用於后面的特征匹配模式識別算法。
字符識別:將當前字符提取的特征向量與特征模板庫進行模板粗分類和模板細匹配,識別出字符的算法。
版面恢復:識別原文檔的排版,按原排版格式將識別結果輸出到word或pdf等格式文檔,叫做版面恢復算法。
后處理校正: 根據特定的語言上下文的關系,對識別結果進行較正的算法。
其中,對於.Net來說三種比較主流和成熟的識別方式:
方式一、Asprise OCR實現。其中需要使用的3個dll是AspriseOCR.dll、DevIL.dll、ILU.dll。其數字識別率比較高,
示例代碼:
[DllImport("AspriseOCR.dll")] static extern string craboOCR(string file, int type); private void GetVeryfyCode() { if(File.Exists(_imgPath))//ok { try { this.picbVeryfyCode.Image=System.Drawing.Bitmap.FromFile(_imgPath); _veryfyCode=craboOCR(_imgPath,-1); //將返回string,並以"\r\n"結尾!! _veryfyCode=_veryfyCode.Substring(0,4); this.txtVeryfyCode.Text=_veryfyCode; } catch(Exception e) { this.lblResult.Text+=e.Message; } } }
方式二、Microsoft Office Document Imaging(Office 2007) 組件實現。
方式三、Tesseract引擎,其.NET版本地址為:http://www.pixel-technology.com/freeware/tessnet2/。其中在使用前要對該引擎進行安裝,安裝成功后可以對其Dos命令行進行封裝,
調用命令形式如下:
private void UseOCR(string v_strTesseractPath, string v_strSourceImgPath, string v_strOutputPath, string v_strLangPath) { using (Process process = new System.Diagnostics.Process()) { process.StartInfo.FileName = v_strTesseractPath; process.StartInfo.Arguments = v_strSourceImgPath + " " + v_strOutputPath + " -l " + v_strLangPath; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); process.WaitForExit(); } }