實例圖片
大體步驟:1.讀取圖片
2.圖像預處理(閾值分割,提取標簽部分,縮小處理區域)
3.將標簽區域的最小外接矩形,從原圖中剪切
4.圖像旋轉,將文字擺正
5.水平分割
6.局部閾值分割,提取出ROI
7.創建詞典(之所以不直接使用字符模板直接識別,是因為,直接識別的話,字母O會被識別為數字0 等等異常識別,使用字典會比較准確識別)
8.讀取識別模板
9.使用字典進行識別
關鍵點:字典 局部閾值分割 水平分割
注意點:halcon字符識別自帶模板是基於白底黑字 ,輸入的圖像要進行轉換 保證是二值化圖像 並且是白底黑字,
如果是黑底白字,使用算子 invert_image 進行圖像翻轉。
dev_close_window()
read_image (Image, 'label/label_01.png')
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
*dev_open_window (0, 0, 512, 512, 'black', WindowHandle1)
dev_set_window (WindowHandle)
dev_display (Image)
*固定閾值分割
threshold (Image, Region, 128, 230)
*區域連通
connection (Region, ConnectedRegions)
*區域篩選
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 47000, 59000)
*開運算
opening_circle (SelectedRegions, RegionOpening, 4)
*回去最小外接矩形
shape_trans (RegionOpening, RegionTrans, 'rectangle2')
*水平分割
text_line_orientation (RegionTrans, Image, 25, -0.523599, 0.523599, OrientationAngle)
*創建矩陣但也
hom_mat2d_identity (HomMat2DIdentity)
*求變換矩陣關系
hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, 0, 0, Deskew)
*原圖變化 旋轉
affine_trans_image (Image, ImageAffineTrans, Deskew, 'constant', 'false')
*區域旋轉
affine_trans_region (RegionTrans, RegionAffineTrans, Deskew, 'nearest_neighbor')
*外接矩形參數
smallest_rectangle1 (RegionAffineTrans, Row1, Column1, Row2, Column2)
*圖像截取
reduce_domain (ImageAffineTrans, RegionAffineTrans, ocrimage)
*通過局部平均值和標准偏差進行閾值圖像。試用場合不均勻的照明或噪聲的圖像
*第一個輸入參數:輸入圖像
*第二個輸出參數:分割后的區域
*第三個輸人參數:均值和標准差的掩碼寬度
*第四個輸入參數:均值和標准差的掩碼高度
*第五個輸入參數:標准差因子
*第六個輸入參數:最小灰度值和均值之差
*第七個輸入參數:提取區域的類型,是亮的區域,或暗的區域,或相似區域,或不相似區域
var_threshold (ocrimage, Region1,40, 40, 0.8, 10, 'dark')
connection (Region1, ConnectedRegions1)
*水平分割
partition_dynamic (ConnectedRegions1, Split, 21, 40)
*選擇文字整體
select_shape (Split, Characters, ['width','height'], 'and', [10,20], [30,50])
*選擇單詞部分
select_shape (Characters, word_image, 'row', 'and', 0,Row1+80)
*選擇日期部分
select_shape (Characters, date_image, 'row', 'and', Row1+80, 500)
*排序
sort_region (word_image, SortedRegions, 'character', 'true', 'row')
*獲取區域參數 面積 行 列
area_center (SortedRegions, Area, Row, Column)
Column[|Column|]:=888
gen_empty_obj (word)
text:=''
text_word_all:=''
text_date_all:=''
*創建詞典
create_lexicon ('label', ['BEFORE','BEST','END'], LexiconHandle)
*讀取模型
read_ocr_class_mlp ('Industrial_NoRej', OCRHandle)
for i := 1 to |Column|-1 by 1
select_obj (word_image, ObjectSelected, i)
concat_obj (word, ObjectSelected, word)
if (i==|Column| or (Column[i]-Column[i-1])>30)
*開始識別
*第一個參數 要識別的圖
*第二個參數 原圖
*第六個參數表示誤差范圍,既和字典中單詞對比 長度誤差范圍
do_ocr_word_mlp (word, ocrimage, OCRHandle, '<label>', 3, 2, Class, Confidence, text_1, Score)
text:=text+' '+text_1
gen_empty_obj (word)
endif
endfor
text_word_all:=text
*下面開始識別日期部分
*排序
sort_region (date_image, SortedRegions1, 'character', 'true', 'row')
*使用正則表達式識別
*^ 開始 ()子表達式 [] 標記一個中括號表達式的開始 | 指明兩項之間的一個選擇
t:='^([0-2][0-9]|30|31)/(0[1-9]|10|11|12)/0[0-5]$'
do_ocr_word_mlp (SortedRegions1, ocrimage, OCRHandle, t, 10, 5, Class1, Confidence1, Word, Score1)
text_date_all:=Word
*清除窗體
dev_clear_window ()
dev_display (ocrimage)
*設置多顏色顯示
dev_set_colored (6)
dev_display (word_image)
dev_display (date_image)
*設置字體
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
*在窗體中顯示文字
disp_message (WindowHandle,'字母部分:'+ text_word_all, 'image', 10, 10, 'red', 'true')
disp_message (WindowHandle,'日期部分:'+ text_date_all, 'image', 40, 40, 'green', 'true')
運行結果