模板就是一副已知的小图像,而模板匹配就是在一副大图像中搜寻目标,已知该图中有要找的目标,且该目标同模板有相同的尺寸、方向和图像元素,通过一定的算法可以在图中找到目标,确定其坐标位置。
①标准形状
②任意形状
③生成标准ROI
④通过XLD创建AOI
2、创建匹配模板
create_shape_model(Template , // 模板图像
NumLevels, // 图像金字塔
AngleStart, // 起始角度
AngleExtent, // 角度范围
AngleStep, // 角度步长
Optimization, // 优化算法
Metric, // 极性
Contrast, // 对比度
MinContrast, // 最小对比度
ModelID // 生成模板ID)
3、匹配模板
find_shape_model(Image, // 匹配图像
ModelID, //模板句柄
AngleStart, // 起始角度
AngleExtent, //角度范围
MinScore, // 最低得分输出的匹配的得分Score 大于该值
NumMatches, // 匹配数量
MaxOverlap, // 最大重叠
SubPixel, //计算精度的设置,五种模式,多选2,3
NumLevels, //搜索时金字塔的层数
Greediness : //贪婪度,搜索启发式,一般都设为0.9,越高速度快,容易 出现找不到的情况
Row,Column,Angle,Score) //输出匹配位置的行和列坐标、角度、得分。
dev_update_off ()
dev_close_window ()
read_image (Image, 'F:/培训/Halcon培训/Test/b7.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/4, Height/4, 'black', WindowHandle)
dev_display (Image)
gen_empty_obj(Models)
*模板数量数组,模板的下标从1开始
IndexS:=[]
*第n个模型的数量
IndexE:=[]
ModelIDs:=[]
*创建ROI
gen_rectangle1 (ROI_0, 1330.27, 953.165, 1611.85, 1414.88)
area_center (ROI_0, Area, Row, Column)
reduce_domain (Image, ROI_0, ImageReduced)
mean_image (ImageReduced, ImageMean, 5, 5)
*创建模板
create_shape_model (ImageReduced, 'auto', rad(0), rad(360), 'auto', 'auto', 'ignore_local_polarity', 'auto', 'auto', ModelID)
*获得模板
get_shape_model_contours (ModelContours, ModelID, 1)
count_obj (ModelContours, Number)
count_obj (Models, Number1)
concat_obj (Models, ModelContours, Models)
IndexS:=[IndexS,Number1+1]
IndexE:=[IndexE,Number+Number1]
ModelIDs:=[ModelIDs,ModelID]
dev_close_window ()
dev_open_window (0, 0, Width/4, Height/4, 'black', WindowHandle1)
*read_image (Image2, 'F:/培训/Halcon培训/Test/b17.png')
dev_display (Image)
gen_rectangle1 (ROI_1, 1302.41, 298.09, 1564.95, 650.634)
area_center (ROI_1, Area1, Row1, Column1)
reduce_domain (Image, ROI_1, ImageReduced1)
threshold (ImageReduced1, Regions1, 170, 255)
erosion_circle (Regions1, RegionErosion, 3.5)
connection (RegionErosion, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50000, 99999)
shape_trans (SelectedRegions, RegionTrans, 'convex')
dilation_circle (RegionTrans, RegionDilation, 4)
*获得xld
gen_contour_region_xld (RegionDilation, Contours, 'border')
*由于region创建模板效果差,创建xld模板
create_shape_model_xld (Contours, 'auto', rad(0), rad(360), 'auto', 'auto', 'ignore_local_polarity', 5, ModelID1)
get_shape_model_contours (ModelContours1, ModelID1, 1)
select_shape_xld (ModelContours1, SelectedXLD, 'contlength', 'and', 1000, 99999)
count_obj (SelectedXLD, Number2)
count_obj (Models, Number3)
concat_obj (Models, SelectedXLD, Models)
IndexS:=[IndexS,Number3+1]
IndexE:=[IndexE,Number2+Number3]
ModelIDs:=[ModelIDs,ModelID1]
for i := 1 to 25 by 1
read_image (Image1, 'F:/培训/Halcon培训/Test/b'+i+'.png')
dev_display (Image1)
*寻找模板
find_shape_models (Image1, ModelIDs, rad(0), rad(360), 0.8, 0, 0.5, 'least_squares', 0, 0.9, Row2, Column2, Angle, Score, Model)
Num:=|Score|
for j:= 0 to Num-1 by 1
copy_obj (Models, ModelSelected, IndexS[Model[j]], IndexE[Model[j]]-IndexS[Model[j]]+1)
*从点和角度计算刚性仿射变换
vector_angle_to_rigid (0, 0, 0, Row2[j], Column2[j], Angle[j], HomMat2D)
*对XLD等值线应用任意的二维仿射变换
affine_trans_contour_xld (ModelSelected, ContoursAffineTrans, HomMat2D)
endfor
endfor