halcon第十四讲:基于形状的模板匹配

基于形状的模板匹配(基于边缘方向梯度的模板匹配算法),对应halcon例程:方法—》模板匹配(基于形状)—》find objects using shape-based matching(with scaling and rotation)。将图像和模板在高层金字塔进行搜索,然后映射都低层,搜索速度快,模板有旋转有放缩的搜索。注意这里的目标有旋转的,也有放缩的。

原图和待匹配图分别如下:

从原图创建模板图像再生成模板轮廓,生成的轮廓默认在原点左上角,从原点开始搜索,移动轮廓位置看看生成的模板轮廓如下:

read_image (Image, '1.jpg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
draw_circle (WindowHandle, Row, Column, Radius)
gen_circle (Circle, Row, Column, Radius)
reduce_domain (Image, Circle, ImageReduced)
area_center (Circle, Area, Row1, Column1)

*【1】创建模板模型
*如果Metric= 'use_polarity',图片和模板必须要有相同的对比度。例如,如果模型是一个暗的目标在一个亮的背景上,那么仅仅那些比背景暗的目标可以被找到;
*如果Metric= 'ignore_global_polarity',图片和模板必须要有相同或相反的对比度
create_scaled_shape_model (ImageReduced, 5, rad(-45), rad(90), 'auto', 0.8, 1.1, 'auto', 'auto', 'ignore_global_polarity', 'auto', 'auto', ModelID)
*【2】生成模板轮廓
get_shape_model_contours (ModelContours, ModelID, 1)

*看看生成的轮廓
dev_set_color ('red')
dev_set_line_width (2)
vector_angle_to_rigid (0, 0, 0, Row1, Column1, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2D)
disp_continue_message (WindowHandle, 'black', 'true')
stop()

read_image (Image1, '2.jpg')
dev_display (Image1)

*【3】寻找模板,第七个参数为设置匹配的最小分数,第八个参数为设置匹配的个数,0表示有多少个识别多少个
find_scaled_shape_model (Image1, ModelID, rad(-45), rad(90), 0.8, 1.1, 0.5, 0, 0.5, 'least_squares', 5, 0.8, Row2, Column2, Angle, Scale, Score)

for I := 0 to |Score| - 1 by 1
    *生成一个单位矩阵
    hom_mat2d_identity (HomMat2DIdentity)
    *平移
    hom_mat2d_translate (HomMat2DIdentity, Row2[I], Column2[I], HomMat2DTranslate)
    *旋转
    hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row2[I], Column2[I], HomMat2DRotate)
    *放缩,得到最终的仿射变换矩阵
    hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row2[I], Column2[I], HomMat2DScale)
    *仿射变换
    affine_trans_contour_xld (ModelContours, ModelTrans, HomMat2DScale)
    dev_display (ModelTrans)
endfor

 运行结果如下:

猜你喜欢

转载自blog.csdn.net/qq_24946843/article/details/82181452