halcon模板匹配(五)find_shape_model_clutter
目录
- 一、find_shape_model_clutter例程目的
- 二、默认模板匹配的过程
- 三、定义杂波区域
- 四、设置模型的杂波区域
一、find_shape_model_clutter例程目的
如下图所示,这个例程是想找到左图所示区域内的目标,要求上下临近区域无目标。
默认参数匹配结果
二、默认模板匹配的过程
read_image (ImageModel, ImageFiles[0])
gen_circle (ROI, 753.869, 551.624, 28.4027)
reduce_domain (ImageModel, ROI, ImageReduced)
create_aniso_shape_model (ImageReduced, 'auto', rad(0), rad(0), 'auto', 0.95, 1.05, 'auto', 0.95, 1.05, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
MinScoreStandard := 0.83for Index := 0 to |ImageFiles| - 1 by 1read_image (Image, ImageFiles[Index])find_aniso_shape_model (Image, ModelID, rad(0), rad(0), 0.95, 1.05, 0.95, 1.05, MinScoreStandard, 0, 0.0, 'least_squares', [4, 3], 0.0, Row, Column, Angle, ScaleR, ScaleC, Score)dev_display_shape_matching_results (ModelID, 'green', Row, Column, Angle, ScaleR, ScaleC, 0)
endfor
三、定义杂波区域
find_aniso_shape_model (ImageModel, ModelID, rad(0), rad(0), 0.95, 1.05, 0.95, 1.05, MinScoreStandard, 0, 0.0, 'least_squares', 0, 0.0, Row, Column, Angle, ScaleR, ScaleC, Score)
MatchIndex := 0
get_hom_mat2d_from_matching_result (Row[MatchIndex], Column[MatchIndex], Angle[MatchIndex], ScaleR[MatchIndex], ScaleC[MatchIndex], HomMat2D)
get_shape_model_contours (ModelContours, ModelID, 1)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2D)
gen_circle (ROI_0, 700.655, 548.666, 21.6273)
gen_circle (ROI_1_0, 810.655, 550.611, 21.6273)
union2 (ROI_0, ROI_1_0, ClutterRegion)
四、设置模型的杂波区域
set_shape_model_clutter 用于为形状匹配模型定义干扰抑制区域(Clutter Region),通过指定模型轮廓周围的特定区域,抑制该区域内出现的干扰边缘对匹配结果的影响。其核心作用在于:
精准定位:排除重复结构或背景噪声导致的误匹配,提升模型在复杂场景下的鲁棒性。
特征强化:通过约束匹配区域,突出目标对象的唯一性特征,尤其在目标周围存在结构缺失的场景中效果显著(如BGA球栅阵列匹配)。
sobel_amp (ImageModel, EdgeAmplitude, 'sum_sqrt', 3)
get_shape_model_params (ModelID, NumLevels, AngleStart, AngleExtent, AngleStep, ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)
Contrast := -1
determine_shape_model_params (ImageReduced, NumLevels, AngleStart, AngleExtent, ScaleMin, ScaleMax, 'none', Metric, Contrast, MinContrast, 'contrast_hyst', ParameterName, ParameterValue)
ClutterContrast := (ParameterValue[0] + ParameterValue[1]) / 2
threshold (EdgeAmplitude, Region, ClutterContrast, 255)
set_shape_model_clutter (ClutterRegion, ModelID, HomMat2D, ClutterContrast, 'clutter_border_mode', 'clutter_border_average')
ClutterRegion:不应该有杂波出现的区域 ClutterContrast:计算杂波对比度
参数理解:
ClutterRegion: 定义干扰抑制区域的几何范围(如矩形、多边形) 需通过坐标或ROI工具指定,区域应覆盖模型附近需排除干扰的位置。
ClutterContrast:控制干扰边缘的对比度阈值 值越高,对低对比度干扰的抑制越强,需根据图像噪声水平调整。
HomMat2D:2D变换矩阵,用于动态调整Clutter区域的位置/方向 适用于模型存在旋转或缩放时,确保区域与目标同步变换14。
GenParamName/Value:扩展参数(如’clutter_size’、‘clutter_threshold’) 可细化干扰抑制的敏感度和范围(典型值:clutter_threshold=0.5)。