曾梦垚

导航

halcon案例学习之cbm_label_simple

*cbm_label_simple 程序说明:
*这个示例程序展示了如何使用基于组件的匹配来定位复合对象。在这种情况下,应该在图像中找到一个标签,用户既不知道其中的组件,也不知道它们之间的关系。因此,创建组件模型需要三个操作符:
* 1) gen_initial_components
* 2) train_model_components
* 3) create_trained_component_model
*最后,使用
* 4) find_component_model

dev_update_off ()
dev_close_window ()
*获取所有搜索图像
list_image_files ('label', 'default', [], SearchImageFiles)
SearchImageFiles := regexp_select(SearchImageFiles,'(label_[0-9]+.png)|(label_training_)')
*读取并显示模型图像
read_image (Image, 'label/label_model')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
*设置显示设置
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (2)
dev_set_draw ('margin')
dev_set_color ('blue')
*定义模型区域
gen_rectangle1 (ModelRegion, 119, 106, 330, 537)
*reduce_domain —缩小图像的域。
reduce_domain (Image, ModelRegion, ModelImage)
dev_display (ModelRegion)
disp_message (WindowHandle, 'Model image and model region', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*获取训练图像
*gen_empty_obj —创建一个空的对象元组
gen_empty_obj (TrainingImages)
for Index := 1 to 5 by 1
read_image (TrainingImage, 'label/label_training_' + Index)
*concat_obj —连接两个图标对象元组
concat_obj (TrainingImages, TrainingImage, TrainingImages)
endfor
*自动提取初始组件
* ---------------------------------------------
*gen_initial_components —提取组件模型的初始组件
gen_initial_components (ModelImage, InitialComponents, 40, 40, 20, 'connection', [], [])
dev_set_line_width (1)
dev_set_colored (12)
dev_display (Image)
dev_display (InitialComponents)
disp_message (WindowHandle, 'Extracted initial components', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 提取模型组件并训练关系
* -----------------------------------------------------
* (这可能需要几分钟!)
*get_system —获取HALCON系统参数的当前值
get_system ('pregenerate_shape_models', SavePregenerateShapeModels)
*set_system —设置HALCON系统参数
*pregenerate_shape_models 形状模型 此参数确定使用create_shape_model或create_scaled_shape_model
*创建的形状模型是否已完全预先生成('true')。该参数主要用于在最少的代码更改下实现两种模式之间的切换。通常,仅需要插入或更改一行。
*值:“ true”或“ false” 默认值:'false'
set_system ('pregenerate_shape_models', 'true')
*train_model_components —为基于组件的匹配训练组件和关系
train_model_components (ModelImage, InitialComponents, TrainingImages, ModelComponents, 40, 40, 20, 0.85, -1, -1, rad(15), 'reliability', 'rigidity', 0.2, 0.5, ComponentTrainingID)
set_system ('pregenerate_shape_models', SavePregenerateShapeModels)
*训练结果可以写入文件,以便
*结果可以在以后的程序中从文件中读取
*执行和时间要求高的培训不需要再次执行。
* write_training_components (ComponentTrainingID, 'train_label.ct')
* read_training_components ('train_label.ct', ComponentTrainingID)
dev_display (Image)
dev_display (ModelComponents)
disp_message (WindowHandle, 'Extracted model components', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*为计算出的关系添加小公差值,以覆盖训练图像中未出现的组件变化。
*Modify_component_relations —修改训练结果中的关系
modify_component_relations (ComponentTrainingID, 'all', 'all', 15, rad(5))
* 根据训练结果创建组件模型
* -------------------------------------------------------
create_trained_component_model (ComponentTrainingID, -rad(30), rad(60), 10, 0.8, 'auto', 'auto', 'none', 'use_polarity', 'false', ComponentModelID, RootRanking)
clear_training_components (ComponentTrainingID)
*在运行时映像中查找组件模型
for Index := 0 to |SearchImageFiles| - 1 by 1
read_image (SearchImage, SearchImageFiles[Index])
*find_component_model —查找图像中组件模型的最佳匹配
find_component_model (SearchImage, ComponentModelID, RootRanking, -rad(30), rad(60), 0.5, 0, 0.5, 'stop_search', 'prune_branch', 'none', 0.6, 'least_squares', 4, 0.9, ModelStart, ModelEnd, Score, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp)
dev_display (SearchImage)
for Match := 0 to |ModelStart| - 1 by 1
dev_display (SearchImage)
*get_found_component_model —返回找到的组件模型实例的组件
get_found_component_model (FoundComponents, ComponentModelID, ModelStart, ModelEnd, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp, Match, 'false', RowCompInst, ColumnCompInst, AngleCompInst, ScoreCompInst)
dev_display (FoundComponents)
endfor
disp_message (WindowHandle, 'Found components in image ' + (Index + 1) + ' of 15', 'window', 12, 12, 'black', 'true')
* 要不停止地运行程序,请取消激活以下行
if (Index < |SearchImageFiles| - 1)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
*clear_component_model —释放组件模型的内存
clear_component_model (ComponentModelID)

posted on 2021-01-14 09:55  曾梦垚  阅读(331)  评论(0编辑  收藏  举报