halcon OCR識別

訓練分類器 

       訓練分類器可分爲以下幾步:1、分割字符;2、調用append_ocr_trainf函數將字符加入到訓練集中;3、使用create_ocr_class_mlp函數創建分類器;4、調用trainf_ocr_class_mlp函數來訓練分類器;5、最後使用write_ocr_class_mlp函數來將分類器寫入到文件(.omc格式)中

分類器識別

     識別可分爲以下幾步:1、對需要識別的圖像進行基本的處理;2、使用read_ocr_class_mlp函數來獲取分類器;3、最後使用do_ocr_multi_class_mlp函數來進行識別。

dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
gen_empty_obj (EmptyObject)
read_image (Image, 'D:/_Study/train_ocr/test1.jpg ')
rgb1_to_gray (Image, GrayImage)
for Index := 1 to 4 by 1
 disp_message (WindowHandle, '請框選單個漢字區域,右鍵確認:','window', 12, 12, 'yellow', 'false')

 **畫個矩形
 draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
 **根據畫的矩形生成對應的矩形
 gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
 *裁出來
 reduce_domain (GrayImage, Rectangle, ImageReduced1)
 *閾值
 threshold (ImageReduced1, Region1, 128, 255)
 *開運算,我看這步省了也行
 opening_circle (Region1, RegionOpening, 1.5)
 *準備接收所有提取的字符區域
 concat_obj (EmptyObject, RegionOpening, EmptyObject)
endfor

words:=['測','試','文','字']

*排個序
sort_region (EmptyObject, SortedRegions1, 'character', 'true', 'row')

for Index1:=1 to 4 by 1
 select_obj (SortedRegions1, ObjectSelected1, Index1)
 *(1)將待訓練樣本添加到訓練集中
 append_ocr_trainf (ObjectSelected1, Image, words[Index1-1], 'D:/_Study/train_ocr/2.trf')
 read_ocr_trainf_names ('D:/_Study/train_ocr/2.trf', CharacterNames, CharacterCount)
endfor

*read_ocr_trainf_names ('D:/_Study/train_ocr/2.trf', CharacterNames, CharacterCount)
*(2)創建mlp分類器
create_ocr_class_mlp (8, 10, 'constant', 'default', words, 80, 'none', 10, 42, OCRHandle)
*(3)使用樣本訓練mlp分類器
trainf_ocr_class_mlp (OCRHandle, 'D:/_Study/train_ocr/2.trf', 200, 1, 0.01, Error, ErrorLog)
*(4)將OCR分類器寫入文件
write_ocr_class_mlp (OCRHandle, 'D:/_Study/train_ocr/2.omc')

*(5)使用分類器來進行識別,先導入另一張做測試的圖
read_image (Image1, 'D:/_Study/train_ocr/test2.jpg')
rgb1_to_gray (Image1, GrayImage1)
threshold (GrayImage1, Regions, 117, 255)

connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 50, 99999)
sort_region (SelectedRegions, SortedRegions, 'upper_left', 'true', 'column')

*(6)開始識別   a、獲取分類器;b、使用分類器分割字符
read_ocr_class_mlp ('D:/_Study/train_ocr/2.omc', OCRHandle1)
do_ocr_multi_class_mlp (SortedRegions, GrayImage1, OCRHandle1, Class, Confidence)
disp_message (WindowHandle, '識別結果:', 'image', 30, 50, 'white', 'false')

for i:=1 to 4 by 1
    disp_message (WindowHandle, Class[i-1], 'image', 30, 120+40*i, 'yellow', 'false')
endfor

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章