Halcon中常用的模板匹配方法
1.NCC匹配
a.创建模板基本步骤:
读入图片——>选取ROI——>预处理(threshold、connection、select_shape)——>得到模板区域——>创建模板——>保存模板
read_image (Image, 'imgPath')
rotate_image (Image, ImageRotate, -90, 'constant')
gen_rectangle1 (ROI_0, 342.061, 103.048, 426.646, 428.794)
reduce_domain (ImageRotate, ROI_0, ImageReduced)
threshold (ImageReduced, Regions, 120, 255)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 100, 1800)
union1 (SelectedRegions, RegionUnion)
reduce_domain (ImageRotate, RegionUnion, ImageReduced1)
create_ncc_model (ImageReduced1, 'auto', rad(-10), rad(10), 'auto', 'use_polarity', ModelID)
write_ncc_model(ModelID,'modePath')
b.进行匹配
读入图片——>读入模板——>进行匹配——>显示匹配区域
read_image (Image, 'imgPath')
read_ncc_model ('modePath', ModelID)
find_ncc_model (Image, ModelID,rad(-10), rad(10), 0.1, 15, 0.5, 'true', 0, Row, Column, Angle, Score)
count := |Score|
*计算ROI区域的中心点坐标
area_center (ROI_0, Area, RowRef, ColumnRef)
*显示匹配的区域
for Index := 1 to count-1 by 1
vector_angle_to_rigid (RowRef, ColumnRef, 0, Row[Index], Column[Index], Angle[Index], HomMat2D)
affine_trans_region (ROI_0, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
dev_display (RegionAffineTrans)
dev_display (RegionAffineTrans)
endfor
2.形状匹配
与NCC匹配步骤类似。
*创建模板
create_shape_model (ModelImage, 'auto', rad(-30), rad(30), 'auto', ['point_reduction_high','no_pregeneration'], 'use_polarity', [69,121,42], 3, ModelId)
*保存模板
write_shape_model (ModelId,'modePath')
*读入模板
read_shape_model ('modePath'', ModelID)
*进行匹配
find_shape_model (Image, ModelId,rad(-30), rad(30), 0.5, 9, 0.5, 'least_squares', [5,1], 0.75, ModelRow, ModelColumn, ModelAngle, ModelScore)
版权声明:本文为baidu_24565387原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。