基于组件的匹配是基于形状的匹配的扩展。像基于形状的匹配一样基于组件的匹配提取轮廓,并将其形状与先前创建的形状进行匹配模特。但是与基于形状的匹配相比,组件模型由几个组件组成,这些组件可以改变它们的关系,即它们可以相对于彼此移动和旋转。可能的关系必须是在创建模型时确定或指定。然后,实际的匹配返回找到了模型实例。
请注意,与基于形状的匹配相比,基于组件的匹配没有缩放大小是可能的。
定位可以相对移动的组件的任务比过程稍微复杂一点需要进行基于形状的匹配。
例如,代替单个ROI,几个ROI(包含初始的组件)必须被选择或提取。此外,这些关系,即必须确定模型组件。
流程:
例如:下面的例子来判断开关状态
1.提取初始组件特征:
- read_image (ModelImage, 'bin_switch/bin_switch_model')
- 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_display (ModelImage)
- dev_display (InitialComponents)
复制代码
(左)带有两个初始组件的参考图像(开关为“开”);(右)训练图像(开关是关)。
2.训练组件之间可能存在的关系(必须确定这种关系)
- read_image (TrainingImage, 'bin_switch/bin_switch_training_1')
- train_model_components (ModelImage, InitialComponents, TrainingImage, \
- ModelComponents, 30, 30, 20, 0.7, -1, -1, rad(25), \
- 'speed', 'rigidity', 0.2, 0.5, ComponentTrainingID)
复制代码
3.创建组件模型
如果训练的结果令人满意,就可以创建组件模型。为了让模型不那么严格,之前使用modify_component_model将创建小公差添加到获得的关系中。
- modify_component_relations (ComponentTrainingID, 'all', 'all', 1, rad(1))
- create_trained_component_model (ComponentTrainingID, 0, rad(360), 10, 0.7, \
- 'auto', 'auto', 'none', 'use_polarity', \
- 'false', ComponentModelID, RootRanking)
复制代码
4.找到组件模型并导出对应关系
- read_image (SearchImage, 'bin_switch/bin_switch_' + ImgNo)
- 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)
- dev_display (SearchImage)
- 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_display (FoundComponents)
- visualize_bin_switch_match (AngleCompInst, Match, WindowHandle)
- endfor
- Figure
复制代码
本地图像位置:(代码中使用基于%HALCONIMAGES%的相对路径,可直接打开图像)
- %HALCONIMAGES%\bin_switch
- (打开cmd命令行,输入echo %HALCONIMAGES%\bin_switch 复制路径即可)
复制代码
图片链接:https://pan.baidu.com/s/11uUkSuGDWJ_VLeKZPGtTiQ
提取码:ie3f
|