Halcon–基于组件的模板匹配
dev_close_window ()
*读取参考图像,这里读取的是单通道灰度图像
read_image (ModelImage, 'D:/USER/Desktop/design/dataset/bolts-0.jpg')
*设置显示图像、绘制线条等窗口参数
dev_open_window_fit_image (ModelImage, 0, 0, -1, -1, WindowHandle)
dev_display (ModelImage)
dev_set_draw ('margin')
dev_set_line_width(3)
stop ()
*定义各个组件, 选取各个组件的ROI区域
gen_rectangle1 (Rectangle1, 140, 71, 279, 168)
gen_rectangle1 (Rectangle2, 181, 281,285, 430)
gen_circle (Circle, 106, 256, 60)
*将所有组件放进一个名为ComponentRegions的Tuple中
concat_obj (Rectangle1, Rectangle2, ComponentRegions)
concat_obj (ComponentRegions, Circle, ComponentRegions)
*显示参考图像,以及选择的各个组件区域。核对区域选择是否理想
dev_display (ModelImage)
dev_display (ComponentRegions)
stop ()
*创建基于组件的模板,返回模板句柄ComponentModelID
create_component_model (ModelImage, ComponentRegions, 20, 20, rad(25), 0, rad(360), 15, 40, 15, 10, 0.8, 3, 0, 'none', 'use_polarity', 'true', ComponentModelID, RootRanking)
*读取测试图像,该测试图像相对于参考图像有一定的位移和旋转。
read_image (SearchImage, 'D:/USER/Desktop/design/dataset/bolts-1')
*在参考图像模板的基础上,进行基于基于组件的匹配
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 (4)
*获得每个组件的实例和位移旋转等参数
get_found_component_model (FoundComponents, ComponentModelID, ModelStart, ModelEnd, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp, Match, 'false', RowCompInst, ColumnCompInst, AngleCompInst, ScoreCompInst)
dev_display (FoundComponents)
endfor
stop ()
*匹配结束,释放模板资源
clear_component_model (ComponentModelID)