Halcon例子难点解释

  • This example shows how to use the component-based matching in order

  • to locate all instances of a dip switch module that consists of several

  • components. The body of the module and each of the twelve switches

  • represent a separate component. The relative movements (relations) are

  • automatically trained by using twelve training images each of which shows

  • another switch turned on while the remaining switches are turned off. Thus,

  • the training images show all possible relative movements of the components,

  • and hence cover all possible relative movements in a run-time image.

  • After creating the component model all instances of the dip switch module are

  • found in a run-time image. Finally, the found instances are horizontally aligned

  • in order to decide whether the switches are turned on or off.
    dev_update_off ()
    dev_set_draw (‘margin’)
    read_image (ModelImage, ‘dip_switch/dip_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
    dev_display (ModelImage)
    *截取初始组件(不会变化)
    gen_rectangle2 (InitialComponents, 265, 138, -0.02, 23, 13)
    gen_rectangle2 (InitialComponent, 342, 286, -0.02, 168, 13)
    concat_obj (InitialComponents, InitialComponent, InitialComponents)
    for I := 0 to 11 by 1
    gen_rectangle2 (InitialComponent, 311 + I * 0.5, 128 + I * 28.2, 0, 10, 12)
    concat_obj (InitialComponents, InitialComponent, InitialComponents)
    endfor
    dev_set_colored (12)
    dev_display (ModelImage)
    dev_display (InitialComponents)
    dev_set_color (‘yellow’)
    disp_message (WindowHandle, ‘Model image and’, ‘image’, 20, 20, ‘yellow’, ‘false’)
    disp_message (WindowHandle, ‘input regions describing the initial components’, ‘image’, 40, 20, ‘yellow’, ‘false’)
    disp_message (WindowHandle, ‘Press mouse button to continue’, ‘window’, 450, 20, ‘yellow’, ‘false’)
    get_mbutton (WindowHandle, Row1, Column1, Button1)

  • Get the training images,获取所有情况的训练图像并组合到一起
    gen_empty_obj (TrainingImages)
    for I := 1 to 12 by 1
    read_image (TrainingImage, ‘dip_switch/dip_switch_training_’ + I$‘02’)
    concat_obj (TrainingImages, TrainingImage, TrainingImages)
    dev_display (TrainingImage)
    disp_message (WindowHandle, ‘Training image ’ + I + ’ of 12’, ‘image’, 20, 20, ‘yellow’, ‘false’)
    disp_message (WindowHandle, ‘Press mouse button to continue’, ‘window’, 450, 20, ‘yellow’, ‘false’)
    get_mbutton (WindowHandle, Row2, Column2, Button2)
    endfor

  • The component-based matching uses the contours of the components

  • as features for the matching. In order to make sure that only significant

  • features are involved in the matching the parameters ContrastLow,

  • ContrastHigh, and MinSize can be passed to the training. To inspect the

  • effect of different values for these three parameters, and hence to find

  • the optimum values for a certain application, the following two lines of

  • code can be used:

  • add_channels (InitialComponents, ModelImage, ModelImageInitComp)

  • gen_initial_components (ModelImageInitComp, InitialComponentEdges, 45, 45, 30, ‘connection’, [], [])

  • Extract the model components and train the relations

  • (This may take a few minutes!)
    dev_display (ModelImage)
    disp_message (WindowHandle, ‘Train the model components…’, ‘image’, 20, 20, ‘yellow’, ‘false’)
    disp_message (WindowHandle, ‘(This may take a few minutes)’, ‘image’, 40, 20, ‘yellow’, ‘false’)
    dev_set_check (’~give_error’)
    dev_error_var (Error, 1)
    read_training_components (‘train_dip_switch.ct’, ComponentTrainingID)
    dev_error_var (Error, 0)
    dev_set_check (‘give_error’)
    *判断是否已经存在训练文件,如果没有则重新训练一次

if (Error != H_MSG_TRUE)
train_model_components (ModelImage, InitialComponents, TrainingImages, ModelComponents, 45, 45, 30, 0.95, -1, -1, rad(20), ‘speed’, ‘rigidity’, 0.2, 0.5, ComponentTrainingID)
* Ignore writing errors as they are not critical for this example
dev_set_check (’~give_error’)
write_training_components (ComponentTrainingID, ‘train_dip_switch.ct’)
dev_set_check (‘give_error’)
endif
dev_set_color (‘yellow’)
dev_display (ModelImage)
disp_message (WindowHandle, ‘Result of the training:’, ‘image’, 20, 20, ‘yellow’, ‘false’)
disp_message (WindowHandle, ’ 1) Final model components’, ‘image’, 40, 20, ‘yellow’, ‘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, ‘yellow’, ‘false’)
get_mbutton (WindowHandle, Row3, Column3, Button3)
count_obj (ModelComponents, NumComp)
for I := 0 to NumComp - 1 by 1
dev_display (ModelImage)
disp_message (WindowHandle, ‘Result of the training:’, ‘image’, 20, 20, ‘yellow’, ‘false’)
disp_message (WindowHandle, ’ 1) Final model components’, ‘image’, 40, 20, ‘yellow’, ‘false’)
disp_message (WindowHandle, ’ 2) Relations with respect to component ’ + I, ‘image’, 60, 20, ‘yellow’, ‘false’)
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, ‘yellow’, ‘false’)
get_mbutton (WindowHandle, Row4, Column4, Button4)
endfor

  • Add small tolerances to the automatically derived relations in order
  • to receive a higher robustness.
    *稍微休整相对关系,加大一点公差
    modify_component_relations (ComponentTrainingID, ‘all’, ‘all’, 0, rad(4))
  • Create the component model based on the training result.
  • The minimum score for the model components should be set individually.
  • On the one hand the print on the module may be occluded, which requires
  • a low minimum score. On the other hand the switches must be distinguished
  • from from the background, which requires to set the corresponding minimum
  • score values to higher values.
    MinScoreComp := gen_tuple_const(13,0.8)
    MinScoreComp[0] := 0.4
    dev_display (ModelImage)
    disp_message (WindowHandle, ‘Create the component model…’, ‘image’, 20, 20, ‘yellow’, ‘false’)
  • Automatically determine the optimum number of pyramid levels for the
  • switches. However, for the print on the module 4 pyramid levels
  • instead of the automatically determined 3 levels are used to speed up
  • the search.
    NumLevelsComp := gen_tuple_const(13,0)
    NumLevelsComp[0] := 4
    *注意,最后用到的是 ComponentModelID,而不是ComponentTrainingID。
    create_trained_component_model (ComponentTrainingID, 0, rad(360), 10, MinScoreComp, NumLevelsComp, ‘auto’, ‘none’, ‘use_polarity’, ‘false’, ComponentModelID, RootRanking)
    get_component_model_tree (Tree, Relations, ComponentModelID, RootRanking, ‘model_image’, StartNode, EndNode, Row, Column, Phi, Length1, Length2, AngleStart, AngleExtent)
    dev_display (ModelImage)
    disp_message (WindowHandle, ‘Component model’, ‘image’, 20, 20, ‘yellow’, ‘false’)
    dev_set_colored (12)
    dev_display (Tree)
    dev_display (Relations)
    disp_message (WindowHandle, ‘Press mouse button to continue’, ‘window’, 450, 20, ‘yellow’, ‘false’)
    get_mbutton (WindowHandle, Row5, Column5, Button5)
    wait_seconds (0.5)
    clear_training_components (ComponentTrainingID)
  • Find the component model in a run-time image
    ImgNo := 1
    Button := 0
    while (Button != 1)
    read_image (SearchImage, ‘dip_switch/dip_switch_’ + ImgNo ′ 02 d ′ ) c o u n t s e c o n d s ( S e c o n d s 1 ) ∗ 参 数 解 释 , M o d e l S t a r t : 每 一 个 组 件 模 板 开 始 索 引 也 就 是 说 , R o w C o m p [ M o d e l S t a r t [ 0 ] ]   R o w C o m p [ M o d e l S t a r t [ 1 ] ] , C o l u m n C o m p [ M o d e l E n d [ 0 ] ]   C o l u m n C o m p [ M o d e l E n d [ 1 ] ] 是 第 一 组 组 件 的 中 心 坐 标 , 这 其 中 包 括 M o
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值