基于Halcon学习的基于形状模板匹配【一】find_scaled_shape_model.hdev例程

套路:

(1)读取图片

(2)Blob分析/直接ROI抠图

(3)创建模板

create_scaled_shape_model()

(4)匹配模板

find_scaled_shape_model()

(5)清除模板

clear_shape_model()

 *此示例程序显示如何查找缩放和旋转的形状模型。

dev_update_pc ('off')
dev_update_window ('off')
dev_update_var ('off')

*读取图像
read_image (Image, 'green-dot')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_color ('red')
dev_display (Image)

*Blob分析
threshold (Image, Region, 0, 128)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10000, 20000)
fill_up (SelectedRegions, RegionFillUp)
dilation_circle (RegionFillUp, RegionDilation, 5.5)

*抠图
reduce_domain (Image, RegionDilation, ImageReduced)

*创建模板
create_scaled_shape_model (ImageReduced, 5, rad(-45), rad(90), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)

*得到图像的轮廓,是两个轮廓合在一起
get_shape_model_contours (Model, ModelID, 1)
*获取模板的行列坐标
area_center (RegionFillUp, Area, RowRef, ColumnRef)

*创建模板之后,行、列、角度都会回到原点【注:此处与相关性匹配不一样】
*进行仿射变换
vector_angle_to_rigid (0, 0, 0, RowRef, ColumnRef, 0, HomMat2D)
affine_trans_contour_xld (Model, ModelTrans, HomMat2D)
dev_display (Image)
dev_display (ModelTrans)
read_image (ImageSearch, 'green-dots')
dev_display (ImageSearch)

*scaled --缩放,即使是图像变大变小都可找到
*开始匹配模板
find_scaled_shape_model (ImageSearch, ModelID, rad(-45), rad(90), 0.8, 1.0, 0.5, 0, 0.5, 'least_squares', 5, 0.8, Row, Column, Angle, Scale, Score)

for I := 0 to |Score| - 1 by 1
    *单位矩阵
    hom_mat2d_identity (HomMat2DIdentity)
    *平移矩阵
    hom_mat2d_translate (HomMat2DIdentity, Row[I], Column[I], HomMat2DTranslate)
    *旋转矩阵
    hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row[I], Column[I], HomMat2DRotate)
    *缩放矩阵
    hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
    affine_trans_contour_xld (Model, ModelTrans, HomMat2DScale)
    dev_display (ModelTrans)
endfor
*清除模型
clear_shape_model (ModelID)

 创建模板之后,模板会行列坐标0 0 的位置,故需进行仿射变化

*创建模板
create_scaled_shape_model (ImageReduced, 5, rad(-45), rad(90), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)

*得到图像的轮廓,是两个轮廓合在一起
get_shape_model_contours (Model, ModelID, 1)
*获取模板的行列坐标
area_center (RegionFillUp, Area, RowRef, ColumnRef)

*创建模板之后,行、列、角度都会回到原点【注:此处与相关性匹配不一样】
*进行仿射变换
vector_angle_to_rigid (0, 0, 0, RowRef, ColumnRef, 0, HomMat2D)
affine_trans_contour_xld (Model, ModelTrans, HomMat2D)

 

 最终的显示结果:

注:for循环这一部分可以用另外的算子代替

for I := 0 to |Score| - 1 by 1
    *单位矩阵
    hom_mat2d_identity (HomMat2DIdentity)
    *平移矩阵
    hom_mat2d_translate (HomMat2DIdentity, Row[I], Column[I], HomMat2DTranslate)
    *旋转矩阵
    hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row[I], Column[I], HomMat2DRotate)
    
    *缩放矩阵
    hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
    affine_trans_contour_xld (Model, ModelTrans, HomMat2DScale)
    dev_display (ModelTrans)
endfor

----------------------------------------------------------
可以用下列算子进行代替【本质还是仿射变换】

for I := 0 to |Score| - 1 by 1

    vector_angle_to_rigid (0, 0, 0, Row[I], Column[I], Angle[I], HomMat2D1)

    *缩放矩阵
    hom_mat2d_scale (HomMat2D1, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
    affine_trans_contour_xld (Model, ModelTrans, HomMat2DScale)
    dev_display (ModelTrans)
endfor

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值