您的位置:首页 > 其它

Halcon实战记录之三《模板制作--两点定位》

2017-07-19 11:09 357 查看
目的:计算被检测物体的位置和角度偏移

内容:

*创建模板
read_image (Image1, 'D:/Program Files/MVTec/HALCON-12.0/examples/images/bottles/bottle_crate_01.png')
dev_set_draw ('margin')
*创建第一个模板
gen_rectangle1 (mrect1, 83.3297, 63.4186, 142.725, 144.029)
reduce_domain (Image1, mrect1, Model1)
create_shape_model (Model1, 'auto', -3.14, 6.29, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID1)
*创建第二个模板
gen_rectangle1 (mrect2, 381.057, 79.7036, 438.948, 144.029)
reduce_domain (Image1, mrect2, Model2)
create_shape_model (Model2, 'auto', -3.14, 6.29, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID2)

find_shape_model (Model1, ModelID1, -3.14, 6.29, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Score1)
find_shape_model (Model2, ModelID2, -3.14, 6.29, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle2, Score2)
dev_display (Image1)
gen_cross_contour_xld (Cross1, Row1, Column1, 20, Angle1)
gen_cross_contour_xld (Cross2, Row2, Column2, 20, Angle2)
dev_display (Cross1)
dev_display (Cross1)

gen_rectangle2 (TEST_ROI, 100.246, 416.396, rad(-0), 40.7125, 41.3)
dev_display (TEST_ROI)

*匹配
read_image (Image2, 'D:/Program Files/MVTec/HALCON-12.0/examples/images/bottles/bottle_crate_04.png')
reduce_domain (Image2, mrect1, ImageFor1)
reduce_domain (Image2, mrect2, ImageFor2)

find_shape_model (ImageFor1, ModelID1, -3.14, 6.29, 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowFor1, ColumnFor1, AngleFor1, ScoreFor1)
gen_cross_contour_xld (CrossFor1, RowFor1, ColumnFor1, 20, AngleFor1)
dev_display (CrossFor1)

find_shape_model (ImageFor2, ModelID2, -3.14, 6.29, 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowFor2, ColumnFor2, AngleFor2, ScoreFor2)
gen_cross_contour_xld (CrossFor2, RowFor2, ColumnFor2, 20, AngleFor2)
dev_display (CrossFor2)

angle_ll (Row1, Column1, Row2, Column2, RowFor1, ColumnFor1, RowFor2, ColumnFor2, Angle)
vector_angle_to_rigid (Row1, Column1, 0, RowFor1,  ColumnFor1, Angle, HomMat2D)
affine_trans_region (TEST_ROI, RegionAffineTrans, HomMat2D, 'nearest_neighbor')


1、其中Angle为匹配图和模板的相对旋转角度,可以以此获取匹配图中感兴趣ROI。

2、可以通过模板匹配的方法获取位置偏移量。

模板图:



匹配图:



备注:

1、保存模板的算子:
write_shape_model( : : ModelID, FileName : )


2、加载模板的算子:
read_shape_model( : : FileName : ModelID)


3、匹配后的score需要作出判断,避免某些情况下匹配失败导致数据不对,程序奔溃。

4、在创建模板的时候,可以自己先对该区域做阈值处理,获取更明晰的轮廓,再创建模板。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息