您的位置:首页 > 其它

halcon学习笔记之(一)label_word_process_mlp.hdev

2013-05-27 08:29 246 查看
* This example shows how OCR results can be improved by restricting the
* allowed results using a regular expression or a lexicon. Note, that the
* classification results are artificially distorted for demonstration purposes.
dev_update_pc ('off')
dev_update_window ('off')
dev_update_var ('off')
dev_close_window ()
*图片的完整路径由环境变量决定,软件安装时,自动会添加该环境变量。
read_image (Image, 'label/label_01.png')
get_image_size (Image, Width, Height)
*新建一个背景为黑色的窗口
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
* Load pretrained font for character classification
*可能是破解软件,Industrial.omc文件的路径虽没有环境变量,但仍然可以到%安装路径%OCR中找到
read_ocr_class_mlp ('Industrial', OCRHandle)
* Create a lexicon of 3 allowed words
*创建OCR用的词典lexicon或者正则表达式
create_lexicon ('label', ['BEST','BEFORE','END'], LexiconHandle)
for i := 1 to 9 by 1
* Read image
* +运算符连接字符串
read_image (Image, 'label/label_0' + i + '.png')
* Find the label
threshold (Image, Region, 128, 230)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, LabelRaw, 'width', 'and', 350, 450)
*半径为5的圆形结构元素进行开运算
opening_circle (LabelRaw, LabelOpen, 5)
*根据区域的形状特性(区域的凸壳,外接椭圆,...等等)进行变换
shape_trans (LabelOpen, Label, 'rectangle2')
* Perform rough deskew (of course, the text lines are not even parallel)
text_line_orientation (Label, Image, 25, -0.523599, 0.523599, OrientationAngle)
*创建单位阵
hom_mat2d_identity (HomMat2DIdentity)
*创建旋转变换矩阵
hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, 0, 0, Deskew)
*根据旋转变换矩阵,对图像进行旋转,
affine_trans_image (Image, ImageDeskew, Deskew, 'constant', 'false')
*根据旋转变换矩阵,对区域进行旋转,
affine_trans_region (Label, LabelDeskew, Deskew, 'false')
smallest_rectangle1 (LabelDeskew, LabelTop, LabelLeft, LabelBottom, LabelRight)
*提取ROI对应的图像区域,提取结果仍然与原图像尺寸相等
reduce_domain (ImageDeskew, LabelDeskew, ImageOCR)
* Extract character regions
var_threshold (ImageOCR, Foreground, 40, 40, 0.8, 10, 'dark')
*对区域进行连通域分析
connection (Foreground, Blobs)
*partition_dynamic起什么作用?
partition_dynamic (Blobs, Split, 20, 40)
select_shape (Split, Characters, ['width','height'], 'and', [10,20], [30,50])
select_shape (Characters, CharactersWords, ['row'], 'and', 0, LabelTop + 80)
select_shape (Characters, CharactersDate, ['row'], 'and', LabelTop + 80, 600)
* Artificially distort segmentation results for correction demonstration
move_region (CharactersWords, CharactersWords, 0, 3)
move_region (CharactersDate, CharactersDate, -2, 0)
* Process Word Text
*对连通区域按照某个特性进行升序或者降序排列
sort_region (CharactersWords, SortedWords, 'character', 'true', 'row')
*求各个连通区域的面积和中心
area_center (SortedWords, Area, Row, Column)
*|Column|求数组长度, :=赋值运算符, Column[|Column|]对Column数组中第|Column|
*个元素(下标从0开始)赋值
Column[|Column|] := 9999
gen_empty_obj (Word)
Text := ''
OriginalText := ''
*Column[|Column|] := 9999,导致Column数组的长度增加1,所以下面|Column| - 1
for j := 1 to |Column| - 1 by 1
*通过下标(下标从1开始)从object tuple中选取某个object
select_obj (SortedWords, Character, j)
concat_obj (Word, Character, Word)
* Check character spacing to determine end of word
if (j = |Column| or (Column[j] - Column[j-1]) > 30)
* Classify the word without restriction (for comparison)
*正则表达式'.*'表示重复0到任意次(*)的任意字符(.)
do_ocr_word_mlp (Word, ImageOCR, OCRHandle, '.*', 1, 5, Class, Confidence, WordText, WordScore)
OriginalText := OriginalText + ' ' + WordText
* Classify the word using a lexicon
*使用预先定义的lexicon词典
do_ocr_word_mlp (Word, ImageOCR, OCRHandle, '<label>', 1, 5, Class, Confidence, WordText, WordScore)
Text := Text + ' ' + WordText
gen_empty_obj (Word)
endif
endfor
* Process Date Text
sort_region (CharactersDate, SortedDate, 'character', 'true', 'row')
* Classify the data string without restriction (for comparison)
do_ocr_word_mlp (SortedDate, ImageOCR, OCRHandle, '.*', 5, 5, Class, Confidence, OriginalDateText, DateScore)
* Classify the data string using a regular expression
*使用正则表达式进行匹配
do_ocr_word_mlp (SortedDate, ImageOCR, OCRHandle, '^([0-2][0-9]|30|31)/(0[0-9]|11|12)/0[0-5]$', 5, 5, Class, Confidence, DateText, DateScore)
* Visualization
dev_clear_window ()
dev_display (ImageOCR)
dev_set_colored (6)
*     dev_display (Characters)
dev_display (CharactersWords)
dev_display (CharactersDate)
dev_set_color ('red')
disp_message (WindowHandle, 'Distorted: ' + OriginalText + ' ' + OriginalDateText, 'image', 10, 10, 'red', 'false')
disp_message (WindowHandle, 'Corrected:' + Text + ' ' + DateText, 'image', 40, 10, 'green', 'false')
if (i < 9)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
endif
stop ()
endfor
clear_lexicon (LexiconHandle)
clear_ocr_class_mlp (OCRHandle)



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  halcon label_word_pr