基于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)
最终的显示结果:
标签:基于,例程,dev,scaled,shape,model,模板,ModelID 来源: https://blog.csdn.net/BoomBiuBiu/article/details/122574827