即将接手一个halcon的项目,这段时间就在家学习了halcon相关例程,自己也做了一番尝试,把运行成功的代码放上来与大家分享,本人图像小白,如有表述不准确之处希望各位大佬提出来,我们一起讨论、共同进步。
该例程是基于灰度的模板匹配,适用于光照条件比较稳定的情况,模板图片和测试图片大家自行替换。
注意:两张图片的光照情况/灰度值需尽量接近。
*模板匹配基于灰度 2020/3/18
*前期准备
dev_update_off ()
dev_close_window ()
*读入图像
read_image (Image2, 'C:/硬币4.JPG')
dev_open_window_fit_image (Image2, 0, 0, -1, -1, WindowHandle)
dev_display (Image2)
dev_set_color ('red')
dev_set_line_width (2)
dev_set_draw ('margin')
stop()
*提取模板
gen_circle (ROI_0, 1776.94, 1494.7, 460.994)
reduce_domain (Image2, ROI_0, ImageReduced)
*创建模型
create_template_rot (ImageReduced, 4, -0.39, 0.79, 0.0982, 'sort', 'original', TemplateID)
*识别
read_image (Image1, 'C:/硬币5.JPG')
dev_open_window_fit_image (Image1, 0, 0, -1, -1, WindowHandle1)
dev_display (Image1)
count_seconds (T1)
best_match_rot_mg (Image1, TemplateID, -0.39, 0.79, 40, 'false', 3, Row, Column, Angle, Error)
count_seconds (T2)
Time := 1000 * (T2 - T1)
*显示的半径需与模板半径接近
disp_circle (WindowHandle1, Row, Column,440)
disp_message (WindowHandle1, '成功识别', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle1, '圆心坐标分别为:'+Row+','+Column, 'window', 42, 12, 'black', 'true')
disp_message (WindowHandle1, '耗时'+Time+'ms', 'window', 72, 12, 'black', 'true')
*清除句柄
clear_template (TemplateID)
识别结果
模板图片和测试图片如下