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

套路:

(1)读取图片

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

(3)创建模板

create_scaled_shape_model()

(4)匹配模板

find_scaled_shape_model()

(5)清除模板

clear_shape_model()

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

基于Halcon学习的基于形状模板匹配【一】find_scaled_shape_model.hdev例程_第1张图片

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)

 基于Halcon学习的基于形状模板匹配【一】find_scaled_shape_model.hdev例程_第2张图片基于Halcon学习的基于形状模板匹配【一】find_scaled_shape_model.hdev例程_第3张图片

 最终的显示结果:

基于Halcon学习的基于形状模板匹配【一】find_scaled_shape_model.hdev例程_第4张图片

注: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

你可能感兴趣的:(Halcon学习笔记,#,模板匹配,图像处理,图像识别,算法)