Halcon学习-算子/基于组件的匹配/find_text提取字符

1.calculate_lines_gauss_parameters
从要提取的线的最大宽度和对比度计算lines_gauss的参数Sigma、Low和High。
lines_gauss 检测线条及其宽度。

2.get_contour_attrib_xld XLD轮廓的返回点属性值。

3.points_foerstner 使用Forstner操作符检测感兴趣的点。
proj_match_points_ransac 通过寻找点之间的对应关系,计算两幅图像之间的投影变换矩阵。

4.gen_projective_mosaic 将多个图像组合成马赛克图像。
bundle_adjust_mosaic 执行图像镶嵌的束调整。
gen_bundle_adjusted_mosaic 将多个图像组合成马赛克图像。

5.dots_image 加强网点打印。

6.partition_dynamic 在垂直范围较小的位置水平地划分区域。可以用来分离不小心合并的字符。

7.do_ocr_single_class_mlp 使用OCR分类器对单个字符进行分类。

8.基于组件的匹配
train_model_components 为基于组件的匹配训练组件和关系。
get_training_components 返回特定图像中的初始组件或模型组件。

get_component_relations 返回包含在训练结果中的模型组件之间的关系。

9.find_text 提取字符:
read_ocr_class_mlp
create_text_model_reader
clear_ocr_class_mlp
set_text_model_param 设置查找字符所需要的参数,如’dark_on_light’‘min_char_height’'text_line_separators’等
find_text
get_text_object
get_text_result 识别结果

当create_text_model_reader选择的’mode’为自动时,可以由get_text_result识别结果。
当选择manual时, get_text_result (TextResult, ‘manual_num_lines’, NumLines) 获取字符个数,
然后由 get_text_object遍历每个字符,由do_ocr_word_mlp得到结果

modify_component_relations( : : ComponentTrainingID, ReferenceComponent, ToleranceComponent, PositionTolerance,AngleTolerance : )修改培训结果中的关系。
应该更改的关系的选择是通过分别设置ReferenceComponent和tolerance ecomponent来执行的。这意味着相对于组件ReferenceComponent,组件ToleranceComponent的相对运动被修改。

create_trained_component_model 准备一个基于训练过的组件进行匹配的组件模型。
get_component_model_tree 返回组件模型的搜索树。

clear_training_components

find_component_model
get_found_component_model

visualize_bin_switch_match 显示所找到的箱子开关的通用图形模型
clear_component_model

基于训练过的组件进行匹配的例程

   dev_update_off ()
    dev_set_draw ('margin')
    dev_set_line_width (2)
    read_image (ModelImage, 'bin_switch/bin_switch_model')
    get_image_size (ModelImage, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
    set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
    * Define the initial components
    gen_rectangle1 (Region1, 78, 196, 190, 359)
    gen_rectangle1 (Sub1, 150, 196, 190, 321)
    difference (Region1, Sub1, InitialComponents)
    gen_rectangle1 (Region2, 197, 204, 305, 339)
    gen_rectangle1 (Sub2, 205, 232, 285, 314)
    difference (Region2, Sub2, InitialComponent)
    concat_obj (InitialComponents, InitialComponent, InitialComponents)
    dev_set_colored (12)
    dev_display (ModelImage)
    dev_display (InitialComponents)
    disp_message (WindowHandle, 'Model image and', 'window', 20, 20, 'blue', 'false')
    disp_message (WindowHandle, 'input regions describing the initial components', 'window', 40, 20, 'blue', 'false')
    disp_message (WindowHandle, 'Press mouse button to continue', 'window', 450, 20, 'blue', 'false')
    get_mbutton (WindowHandle, Row1, Column1, Button1)
    * Get the training image
    read_image (TrainingImage, 'bin_switch/bin_switch_training_1')
    dev_display (TrainingImage)
    disp_message (WindowHandle, 'Training image', 'window', 20, 20, 'blue', 'false')
    disp_message (WindowHandle, 'Press mouse button to continue', 'window', 450, 20, 'blue', 'false')
    get_mbutton (WindowHandle, Row2, Column2, Button2)
    
dev_display (ModelImage)
disp_message (WindowHandle, 'Train the model components...', 'window', 20, 20, 'blue', 'false')
train_model_components (ModelImage, InitialComponents, TrainingImage, ModelComponents, 30, 30, 20, 0.7, -1, -1, rad(25), 'speed', 'rigidity', 0.2, 0.5, ComponentTrainingID)
dev_set_color ('blue')
dev_display (ModelImage)
disp_message (WindowHandle, 'Result of the training:', 'window', 20, 20, 'blue', 'false')
disp_message (WindowHandle, '  1) Final model components', 'window', 40, 20, 'blue', 'false')
dev_set_colored (12)
get_training_components (ModelComponents, ComponentTrainingID, 'model_components', 'model_image', 'false', RowRef, ColumnRef, AngleRef, ScoreRef)
dev_display (ModelComponents)
disp_message (WindowHandle, 'Press mouse button to continue', 'window', 450, 20, 'blue', 'false')
get_mbutton (WindowHandle, Row3, Column3, Button3)
count_obj (ModelComponents, NumComp)
dev_set_line_width (1)
for I := 0 to NumComp - 1 by 1
    dev_set_color ('blue')
    dev_display (ModelImage)
    set_tposition (WindowHandle, 20, 20)
    write_string (WindowHandle, 'Result of the training:')
    set_tposition (WindowHandle, 40, 20)
    write_string (WindowHandle, '  1) Final model components')
    set_tposition (WindowHandle, 60, 20)
    write_string (WindowHandle, '  2) Relations with respect to component ' + I)
    dev_set_colored (12)
    get_component_relations (Relations, ComponentTrainingID, I, 'model_image', Row, Column, Phi, Length1, Length2, AngleStart, AngleExtent)
    dev_display (Relations)
    disp_message (WindowHandle, 'Press mouse button to continue', 'window', 450, 20, 'blue', 'false')
    get_mbutton (WindowHandle, Row4, Column4, Button4)
endfor

modify_component_relations (ComponentTrainingID, 'all', 'all', 1, rad(1))
* Create the component model based on the training result.
dev_set_color ('blue')
dev_display (ModelImage)
disp_message (WindowHandle, 'Create the component model...', 'window', 20, 20, 'blue', 'false')
create_trained_component_model (ComponentTrainingID, 0, rad(360), 10, 0.7, 'auto', 'auto', 'none', 'use_polarity', 'false', ComponentModelID, RootRanking)
get_component_model_tree (Tree, Relations, ComponentModelID, 1, 'model_image', StartNode, EndNode, Row, Column, Phi, Length1, Length2, AngleStart, AngleExtent)
dev_display (ModelImage)
disp_message (WindowHandle, 'Component model', 'window', 20, 20, 'blue', 'false')
dev_set_colored (12)
dev_display (Tree)
dev_display (Relations)
disp_message (WindowHandle, 'Press mouse button to continue', 'window', 450, 20, 'blue', 'false')
get_mbutton (WindowHandle, Row5, Column5, Button5)
wait_seconds (0.5)
clear_training_components (ComponentTrainingID)
* Find the component model in a run-time image
dev_set_line_width (2)
Button := 0
ImgNo := 1
while (Button != 1)
    read_image (SearchImage, 'bin_switch/bin_switch_' + ImgNo)
    count_seconds (Seconds1)
    find_component_model (SearchImage, ComponentModelID, 1, 0, rad(360), 0, 0, 1, 'stop_search', 'prune_branch', 'none', 0.6, 'least_squares', 0, 0.85, ModelStart, ModelEnd, Score, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp)
    count_seconds (Seconds2)
    dev_display (SearchImage)
    NumFound := |ModelStart|
    Time := (Seconds2 - Seconds1) * 1000
    disp_message (WindowHandle, NumFound + ' object(s) found in ' + Time$'3.2f' + ' ms', 'window', 20, 20, 'blue', 'false')
    for Match := 0 to |ModelStart| - 1 by 1
        get_found_component_model (FoundComponents, ComponentModelID, ModelStart, ModelEnd, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp, Match, 'false', RowCompInst, ColumnCompInst, AngleCompInst, ScoreCompInst)
        dev_set_colored (12)
        dev_display (FoundComponents)
        visualize_bin_switch_match (AngleCompInst, Match, WindowHandle)
    endfor
    disp_message (WindowHandle, 'Press mouse button to stop', 'window', 450, 20, 'blue', 'false')
    ImgNo := ImgNo + 1
    if (ImgNo > 9)
        ImgNo := 1
    endif
    dev_error_var (Error, 1)
    dev_set_check ('~give_error')
    get_mposition (WindowHandle, R, C, Button)
    dev_error_var (Error, 0)
    dev_set_check ('give_error')
    if (Error != H_MSG_TRUE)
        Button := 0
    endif
*     stop ()
endwhile
clear_component_model (ComponentModelID)

find_text用法例程:

dev_update_off ()
dev_close_window ()
dev_open_window_fit_size (0, 0, 960, 768, 700, 700, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
read_ocr_class_mlp ('Industrial_Rej', OCRHandle)
disp_message (WindowHandle, 'This example shows how find_text can be used to \nfind and read text. Furthermore, the most \nimportant parameters are explained.', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
dev_clear_window ()
disp_message (WindowHandle, 'Essentially, the only thing that needs to be \ndone before calling find_text is to create \na text model with create_text_model_reader \nwith mode set to \'auto\'.\n \nThe resulting segmentation can then be obtained \nby calling the operators get_text_result and \nget_text_object.', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

create_text_model_reader ('auto', OCRHandle, TextModel)
clear_ocr_class_mlp (OCRHandle)
* 
read_image (Image, 'ocr/medication_package_02_right')
* 
find_text (Image, TextModel, TextResult)
get_text_object (TextLines, TextResult, 'all_lines')
* 
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, 'Find_text extracts all text within the input image,\nregardless of the character size. \nPer default, light and dark text is extracted.', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
clear_text_result (TextResult)
stop ()
* 
set_text_model_param (TextModel, 'polarity', 'dark_on_light')
find_text (Image, TextModel, TextResult)
get_text_object (TextLines, TextResult, 'all_lines')
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, 'It is possible to restrict the text segmentation \naccording to the polarity of the text. \n \nFor example, restricting the polarity to dark text \non a light background ignores all light text.', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
clear_text_result (TextResult)
* 
set_text_model_param (TextModel, 'polarity', 'dark_on_light')
set_text_model_param (TextModel, 'min_char_height', 20)
find_text (Image, TextModel, TextResult)
get_text_object (TextLines, TextResult, 'all_lines')
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, 'Furthermore, it is possible to restrict the segmentation \nto text attributes such as the character height, \nwidth or stroke width.\n \nFor example, setting the minimal characters height \nto 20px ignores all characters with a smaller height.', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
dev_display (TextLines)
stop ()
clear_text_result (TextResult)
* 
set_text_model_param (TextModel, 'polarity', 'dark_on_light')
set_text_model_param (TextModel, 'min_char_height', 20)
set_text_model_param (TextModel, 'text_line_separators', '/')
set_text_model_param (TextModel, 'text_line_structure', '2 4')
find_text (Image, TextModel, TextResult)
get_text_object (TextLines, TextResult, 'all_lines')
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
disp_message (WindowHandle, 'When searching for specific text structures, it can \nbe helpful to set the corresponding text_line_structure \nwith set_text_model_param. \n \nFor example, when searching for a date of the form MM/YYYY,\nit is possible to add \'/\' to the text_line_separators and \nset the text_line_structure to \'2 4\'.', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
dev_set_colored (12)
dev_display (Image)
dev_display (TextLines)
* 
smallest_rectangle1 (TextLines, Row1, Column1, Row2, Column2)
get_text_result (TextResult, 'class', SingleCharacters)
tuple_sum (SingleCharacters, TextLineCharacters)
dev_set_color ('dark green')
for CharacterIndex := 0 to |SingleCharacters| - 1 by 1
    set_tposition (WindowHandle, Row2[CharacterIndex] + 10, Column1[CharacterIndex])
    write_string (WindowHandle, SingleCharacters[CharacterIndex])
endfor
disp_message (WindowHandle, 'It is possible to directly retain the classification \nresults for each of the segmented characters \nwith the get_text_result operator. \n \nExtracted Text: ' + TextLineCharacters, 'window', 12, 12, 'black', 'true')
stop ()
clear_text_result (TextResult)
clear_text_model (TextModel)
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值