Halcon形状模板匹配

在使用Halcon进行模板匹配的时候,我们使用find_shape_model、find_scaled_shape_model等算子找到模板后返回的是实例,得到的数据是模板中心的行列坐标、角度和缩放比例等数据,不是具体的区域,下面介绍怎么得到具体区域。

解决思路:
一:先获取模板的轮廓,此时获取的轮廓位置在(0,0)的位置;

二:求出模板到匹配实例的旋转矩阵;

三:利用仿射变换得到匹配实例的轮廓;

四:将轮廓转成区域;
————————————————
 

read_image (Image,'green-dot')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
*draw_circle (WindowHandle, Row, Column, Radius)
* gen_circle (Circle, 274.5, 280.5, 95)

*获取模板区域
* reduce_domain (Image, Circle, ImageReduced)

*创建模板
* create_scaled_shape_model (ImageReduced, 'auto', rad(-45), rad(90), 'auto', 0.9, 1.1, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)

*保存模板
* write_shape_model (ModelID, 'D:/Demo/Model.shm')

*读取模板
read_shape_model ('D:/Demo/Model.shm', ModelID1)

read_image (Image1, 'green-dots')
dev_display (Image1)

*获取初始模板轮廓
get_shape_model_contours (ModelContours, ModelID1, 1)

find_scaled_shape_model (Image1, ModelID1,rad(-45), rad(90),  0.9, 1.1, 0.5, 0, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Scale, Score)
for i:= 0 to |Score|-1 by 1

        *显示找到的模板轮廓
    *dev_display_shape_matching_results (ModelID1, 'red', Row1[i], Column1[i], Angle[i], 1, 1, 0)

  *求出初始模板到实例中间的旋转矩阵--注:初始模板的行列坐标和角度都是0
    vector_angle_to_rigid (0, 0, 0, Row1[i], Column1[i], Angle[i], HomMat2D)

*给旋转矩阵添加缩放信息
    hom_mat2d_scale (HomMat2D, Scale[i], Scale[i],  Row1[i], Column1[i], HomMat2DScale)

*通过初始模板和旋转矩阵运算得到模板实例的轮廓
    affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DScale)

*轮廓转区域
    gen_region_contour_xld (ContoursAffinTrans, Region, 'margin')
    
     if(Score[i] > 0.8)

*用十字显示中心点
        *disp_cross (WindowHandle, Row1[i], Column1[i], 20, Angle[i])
    endif
endfor

你可能感兴趣的:(Halcon,c#)