HALCON示例程序bottle.hdev、bottlet.hdev瓶体字符OCR的训练和检测
HALCON示例程序bottle.hdev、bottlet.hdev瓶體字符OCR的訓練和檢測
示例程序源碼(加注釋)
1、先介紹bottlet.hdev(訓練OCR識別文件)
*定義一個字符串變量FontName ,內容是bottle
FontName := ‘bottle’
*第一步分割字符,以便單獨對字體進行訓練
Step 1: Segmentation
*窗口顯示更新關閉
dev_update_window (‘off’)
*讀入圖片
read_image (Bottle, ‘bottle2’)
*得到讀入圖片大小
get_image_size (Bottle, Width, Height)
*關閉顯示窗口
dev_close_window ()
*根據剛剛獲得的圖片大小創建圖片大小二倍的窗口
dev_open_window (0, 0, 2 * Width, 2 * Height, ‘black’, WindowID)
*設置字體顯示格式
set_display_font (WindowID, 27, ‘mono’, ‘true’, ‘false’)
*對讀入圖片進行閾值分割
threshold (Bottle, RawSegmentation, 0, 95)
*對區域面積在1-5之間的進行孔洞填充,目的就是為了消除噪聲。
fill_up_shape (RawSegmentation, RemovedNoise, ‘area’, 1, 5)
*使用半徑位2.5的圓形元素進行開運算
opening_circle (RemovedNoise, ThickStructures, 2.5)
*進行孔洞填充
fill_up (ThickStructures, Solid)
*使用寬7高1的矩形元素對區域進行開運算
opening_rectangle1 (Solid, Cut, 1, 7)
*分割連通域
connection (Cut, ConnectedPatterns)
*取區域ConnectedPatterns與區域ThickStructures的交集,將字符的原本形態恢復,之前對字符也進行了填
*充是怕進行開運算的時候把字符也給干掉
intersection (ConnectedPatterns, ThickStructures, NumberCandidates)
*使用面積對區域進行篩選,篩選出面積在300-9999的區域
select_shape (NumberCandidates, Numbers, ‘area’, ‘and’, 300, 9999)
*對區域進行排序,按照列,從第一列到最后一列,按照升序進行排列。這個算子之前介紹過了哈
sort_region (Numbers, FinalNumbers, ‘first_point’, ‘true’, ‘column’)
*顯示,下面的算子之前的都進行了介紹,這里就不進行細說了。
dev_display (Bottle)
dev_set_color (‘green’)
dev_set_line_width (2)
dev_set_shape (‘rectangle1’)
dev_set_draw (‘margin’)
dev_display (FinalNumbers)
*第二步進行訓練前相關準備,排序與定義分割出的區域代表字符。
Step2: Training file generation
*根據上文分割出的字符區域從左到右的順序,將他們的命名存入TrainingNames 數組變量中,其實就是你的
*字符是啥你就在這個訓練數組里寫入什么,讓halcon知道你剛剛分割出的字符的含義。
TrainingNames := [‘0’,‘1’,‘0’,‘8’,‘9’,‘4’]
*定義訓練出來的文件的路徑與文件名。這里強調一下哈,這里沒有路徑是因為它會把訓練文件存入halcon的
*默認路徑中,我們想把訓練文件存在我們想要的地方就得這么寫:TrainingFileName := C:/ + ‘.trf’。
TrainingFileName := FontName + ‘.trf’
*對區域進行排序,按照列,從第一列到最后一列,按照升序進行排列。和上邊一樣
sort_region (FinalNumbers, SortedRegions, ‘first_point’, ‘true’, ‘column’)
*分別求取每一個字符的最小外接矩形
shape_trans (SortedRegions, RegionTrans, ‘rectangle1’)
*求取每個區域的面積與中心坐標
area_center (RegionTrans, Area, Row, Column)
*mean這個算子:返回數組中元素的平均值。這里MeanRow 就是字符平均行位置
MeanRow := mean(Row)
*halcon的異常處理,程序運行正常的話程序會繼續運行,如果出錯顯示錯誤內容彈窗。在這里為了檢查求取
*數組均值是否完成,咱們設想一下,如果咱們提取字符沒提取出來,area_center 求取的Row為空,那么進
*行均值計算的時候除以0,程序就會崩潰。
dev_set_check (’~give_error’)
*刪除文件TrainingFileName
delete_file (TrainingFileName)
*這里再進行異常檢查,如果這個文件正在打開或者被使用,是刪除不了的,所以還要進行異常檢查
dev_set_check (‘give_error’)
*下邊一段代碼是把剛剛咱么在TrainingNames 的數字按照分割出字符的位置進行一個顯示,為了什么呢,就
*是為了讓我們直觀的看到我們存入TrainingNames 的字符與咱們分割出的是不是一一對應,如果不對應則會
*導致訓練出的文件是錯的。
for i := 0 to |TrainingNames| - 1 by 1
select_obj (SortedRegions, CharaterRegions, i + 1)
append_ocr_trainf (CharaterRegions, Bottle, TrainingNames[i], TrainingFileName)
disp_message (WindowID, TrainingNames[i], ‘image’, MeanRow - 40, Column[i] - 6, ‘yellow’, ‘false’)
endfor
*第三部訓練
Step3: Training
*sort按照升序對數組元素進行排列;tuple_uniq - 丟棄元組中連續相同元素。那么我們最開始的數組:
*[‘0’,‘1’,‘0’,‘8’,‘9’,‘4’]先進行升序排列得到[‘0’,‘0’,‘1’,‘4’,‘8’,‘9’]之后剔除相同元素得到[‘0’,‘1’,‘4’,‘8’,‘9’]
*最終CharNames =[‘0’,‘1’,‘4’,‘8’,‘9’]
CharNames := uniq(sort(TrainingNames))
*create_ocr_class_mlp - 使用多層感知器創建OCR分類器。
*函數原型:create_ocr_class_mlp(:: WidthCharacter,HeightCharacter,Interpolation,Features,Characters,NumHidden,Preprocessing,NumComponents,RandSeed:OCRHandle)
*WidthCharacter:字符寬度;HeightCharacter:字符高度;Interpolation:插值;Features:方式;
*Characters:字符;NumHidden:字符數量;Preprocessing:預處理;NumComponents:組件數量;
*RandSeed:隨機數數量;OCRHandle:OCR訓練句柄。
create_ocr_class_mlp (8, 10, ‘constant’, ‘default’, CharNames, 5, ‘none’, 10, 42, OCRHandle)
*trainf_ocr_class_mlp - 訓練OCR分類器。
*函數原型:trainf_ocr_class_mlp(:: OCRHandle,TrainingFile,MaxIterations,WeightTolerance,ErrorTolerance:Error,ErrorLog)
*OCRHandle:OCR識別句柄;TrainingFile:訓練文件名與路徑;MaxIterations:最大迭代次數;
*WeightTolerance:錯誤公差;Error:錯誤;ErrorLog:錯誤日志。
trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)
*將訓練好的ocr文件寫入指定位置。其實就是把訓練文件保存下來。
write_ocr_class_mlp (OCRHandle, FontName)
*清除OCR訓練句柄。
clear_ocr_class_mlp (OCRHandle)
2、介紹bottlet.hdev(OCR識別)
*定義一個字符串變量FontName ,內容是Industrial_0-9_NoRej
FontName := ‘Industrial_0-9_NoRej’
*第一步:分割字符
Step 1: Segmentation
*顯示讀入圖片操作之前介紹過了,這里就不介紹了
dev_update_window (‘off’)
read_image (Bottle, ‘bottle2’)
get_image_size (Bottle, Width, Height)
dev_close_window ()
dev_open_window (0, 0, 2 * Width, 2 * Height, ‘black’, WindowID)
set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
dev_display (Bottle)
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
- 創建讀入ocr訓練文件
create_text_model_reader (‘auto’, FontName, TextModel) - 設置文本模型的參數最小字符寬度為5
set_text_model_param (TextModel, ‘min_stroke_width’, 5) - 文本行的結構
set_text_model_param (TextModel, ‘text_line_structure’, ‘2 2 2’) - 在圖像中查找文本
find_text (Bottle, TextModel, TextResultID) - 顯示字符分割結果
get_text_object (Characters, TextResultID, ‘all_lines’)
dev_display (Bottle)
dev_display (Characters)
stop () - 顯示識別的字符結果
get_text_result (TextResultID, ‘class’, Classes)
area_center (Characters, Area, Row, Column)
for Index := 0 to |Classes| - 1 by 1
disp_message (WindowID, Classes[Index], ‘image’, 80, Column[Index] - 3, ‘green’, ‘false’)
endfor - 釋放內存
- 清理結果句柄
clear_text_result (TextResultID)
*清除OCR識別句柄
clear_text_model (TextModel)
處理思路
這個就是一個簡單的OCR字符從分割到訓練文本到識別的一個步驟。希望能幫助到大家。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。
總結
以上是生活随笔為你收集整理的HALCON示例程序bottle.hdev、bottlet.hdev瓶体字符OCR的训练和检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《科技之巅2》序——机器智能数据智能:工
- 下一篇: 2013Esri全球用户大会QA之Web