思路:当模板匹配定位到目标后获取此模板的轮廓,因为轮廓是一个图元对象可以通过仿射变换将其映射到目标物上。
演示:
创建模板用图:
匹配用图:
halcon代码:
dev_set_line_width (6)
read_image (Image, 'D:/Halcon-WorkSpaces/Test/图像匹配,将模板轮廓映射到检测图像/test1.png')
threshold_sub_pix (Image, Border, 128)
*创建模板
create_shape_model_xld (Border, 'auto', rad(-180), rad(180), 'auto', 'auto', 'ignore_local_polarity', 5, ModelID)
*匹配模板
read_image (Image1, 'D:/Halcon-WorkSpaces/Test/图像匹配,将模板轮廓映射到检测图像/test2.png')
find_shape_model (Image1, ModelID, rad(-180), rad(180), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
*获取模板轮廓
get_shape_model_contours (ModelContours, ModelID, 1)
dev_clear_window ()
dev_display (Image1)
*使用仿射变换将模板映射到目标物
vector_angle_to_rigid (0, 0, 0, Row, Column, Angle, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2D)
效果:(红色的轮廓就是模板轮廓)