曾梦垚

导航

Halcon案例学习之cbm_modules_simple

*cbm_modules_simple 程序说明:
*这个示例程序展示了如何使用基于组件的匹配来定位复合对象。
*在这种情况下,应该在一幅图像中找到几个模块,它们之间的关系,即相对运动,是用户所知道的。
*因此,创建组件模型只需要一个操作符:
* 1) create_component_model
*最后,使用
* 2) find_component_model

dev_update_off ()
dev_close_window ()
read_image (ModelImage, 'modules/modules_model')
dev_open_window_fit_image (ModelImage, 0, 0, -1, -1, WindowHandle)
dev_display (ModelImage)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
Message := '这个程序展示了如何使用基于组件的匹配来定位复合对象'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*定义组件的区域
*gen_rectangle2 —创建任何方向的矩形
gen_rectangle2 (ComponentRegions, 318, 109, -1.62, 34, 19)
gen_rectangle2 (Rectangle2, 342, 238, -1.63, 32, 17)
gen_rectangle2 (Rectangle3, 355, 505, 1.41, 25, 17)
gen_rectangle2 (Rectangle4, 247, 448, 0, 14, 8)
gen_rectangle2 (Rectangle5, 237, 537, -1.57, 13, 10)
*concat_obj —连接两个图标对象元组
concat_obj (ComponentRegions, Rectangle2, ComponentRegions)
concat_obj (ComponentRegions, Rectangle3, ComponentRegions)
concat_obj (ComponentRegions, Rectangle4, ComponentRegions)
concat_obj (ComponentRegions, Rectangle5, ComponentRegions)
dev_set_colored (12)
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display (ModelImage)
dev_display (ComponentRegions)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
disp_message (WindowHandle, '分量区域', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*通过显式指定关系来创建组件模型
*create_component_model —根据明确指定的组件和关系准备要匹配的组件模型
create_component_model (ModelImage, ComponentRegions, 20, 20, rad(25), 0, rad(360), 15, 40, 15, 10, 0.8, [4,3,3,3,3], 0, 'none', 'use_polarity', 'true', ComponentModelID, RootRanking)
*在运行时映像中查找组件模型
ImageName := 'modules/modules_'
for I := 1 to 12 by 1
read_image (SearchImage, ImageName + I$'.2d')
*find_component_model —查找图像中组件模型的最佳匹配
find_component_model (SearchImage, ComponentModelID, RootRanking, 0, rad(360), 0.5, 0, 0.5, 'stop_search', 'search_from_best', 'none', 0.8, 'interpolation', 0, 0.8, ModelStart, ModelEnd, Score, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp)
dev_display (SearchImage)
*显示找到的组件模型
for Match := 0 to |ModelStart| - 1 by 1
dev_set_line_width (1)
*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, '发现组件模型', 'window', 12, 12, 'black', 'true')
*如果程序在每个图像后停止,下面的线必须被激活
if (I < 12)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
*clear_component_model —释放组件模型的内存
clear_component_model (ComponentModelID)

posted on 2021-01-14 10:54  曾梦垚  阅读(311)  评论(0编辑  收藏  举报