在工业视觉检测领域,方形Mark点的精确定位是许多自动化项目的基础需求。这类标记通常用于PCB板定位、产品装配引导或机器人抓取位置校准。传统基于OpenCV的方法在处理复杂背景或低对比度图像时往往表现不佳,而Halcon凭借其强大的亚像素边缘检测和几何拟合算法,能够实现微米级的定位精度。
我最近在一个半导体封装设备视觉引导项目中,就遇到了需要精确定位方形Mark点的需求。目标Mark尺寸仅2mm×2mm,且存在反光、油污等干扰。经过多次试验,最终形成了这套稳定可靠的解决方案,定位重复精度达到±0.01mm,完全满足产线要求。
edges_sub_pix是Halcon中用于亚像素级边缘检测的核心算子,其Canny算法实现相比OpenCV有以下优势:
典型参数配置:
halcon复制edges_sub_pix(Image, Edges, 'canny', 5, 3, 5)
注意:过大的滤波器会导致边缘细节丢失,而过小的阈值会使噪声被误检为边缘。在光照不均场景下,建议先做直方图均衡化。
原始边缘检测结果往往存在断裂和毛刺,通过union_cotangential_contours_xld可实现智能连接:
halcon复制union_cotangential_contours_xld(Edges, UnionEdges, 20, rad(30), rad(30), 10, 10, 3, 'attr_keep')
关键参数解析:
实际应用中发现,对于反光严重的金属Mark,适当增大角度容差(如45°)可以提高连接成功率,但会降低定位精度,需要根据实际情况权衡。
通过面积筛选是最可靠的方案,但需要注意:
select_contours_xld先筛选封闭轮廓area_center_xld计算轮廓面积时,XLD轮廓必须闭合改进后的筛选逻辑:
halcon复制* 增加轮廓凸性检查,排除非矩形干扰
select_contours_xld(UnionEdges, SelectedContours, 'contour_features',
'convexity', 0.9, 1.0)
* 增加长宽比筛选,典型方形Mark的宽高比应在0.8-1.2之间
smallest_rectangle2_xld(SelectedContours, _, _, _, L1, L2)
tuple_div(L1, L2, AspectRatio)
tuple_find(abs(AspectRatio-1)<0.2, 1, ValidIndices)
Halcon的Metrology模型通过以下步骤实现亚像素级测量:
halcon复制create_metrology_model(MetrologyHandle)
set_metrology_model_image_size(MetrologyHandle, Width, Height)
add_metrology_object_generic(MetrologyHandle, 'rectangle2',
[Row, Col, Phi, L1, L2], 20, 5, 1, 10, [], [], Index)
关键参数经验值:
获取测量点后的二次拟合能进一步提高精度:
halcon复制fit_rectangle2_contour_xld(MeasuresContour, 'regression', -1, 0, 0, 3, 2,
PreciseRow, PreciseCol, PrecisePhi, PreciseLen1, PreciseLen2)
拟合方法选择:
实测发现:当Mark存在轻微形变时,'tukey'方法的重复性比'regression'提高约30%
| 现象 | 可能原因 | 解决方案 |
|---|---|---|
| 无法检测到边缘 | 阈值过高/光照不足 | 调整edges_sub_pix阈值,增加光源亮度 |
| 轮廓连接错误 | 距离容差过大 | 减小union_cotangential_contours_xld的MaxDist |
| 矩形拟合偏差大 | 边缘点包含噪声 | 改用'tukey'拟合方法,或增加平滑系数 |
| 中心坐标跳动 | 测量点数不足 | 增加set_metrology_object_param的num_measures值 |
gen_rectangle1限定处理区域par_start并行处理不同区域set_system('use_gpu', 'true'))实测数据:在i7-11800H处理器上,处理500万像素图像时:
以下是经过工程验证的增强版实现:
halcon复制* 初始化设置
dev_update_off()
dev_set_check('~give_error')
* 图像采集
read_image(Image, 'mark_sample.png')
get_image_size(Image, Width, Height)
* 创建ROI减少处理区域(可选)
gen_rectangle1(ROI, Height*0.2, Width*0.2, Height*0.8, Width*0.8)
reduce_domain(Image, ROI, ImageReduced)
* 多尺度边缘检测(适应不同尺寸Mark)
for Scale := 1 to 3 by 0.5
edges_sub_pix(ImageReduced, Edges, 'canny', Scale*3, 2, 5)
count_obj(Edges, NumEdges)
if (NumEdges > 10)
break
endif
endfor
* 轮廓增强处理
smooth_contours_xld(Edges, SmoothedEdges, 3)
union_adjacent_contours_xld(SmoothedEdges, UnionEdges, 15, 1, 'attr_keep')
* 几何特征筛选
select_contours_xld(UnionEdges, SelectedContours, 'contour_features',
['area', 'convexity'], [1000, 0.9], [100000, 1.0])
* 精确测量模型
create_metrology_model(MetrologyHandle)
set_metrology_model_image_size(MetrologyHandle, Width, Height)
* 对每个候选轮廓单独处理
count_obj(SelectedContours, NumContours)
for i := 1 to NumContours by 1
select_obj(SelectedContours, Contour, i)
smallest_rectangle2_xld(Contour, Row, Col, Phi, L1, L2)
* 动态调整测量区域大小
MeasureLength := max([L1*1.5, 20])
MeasureWidth := max([5, L2*0.3])
add_metrology_object_generic(MetrologyHandle, 'rectangle2',
[Row, Col, Phi, L1, L2],
MeasureLength, MeasureWidth, 1, 10, [], [], Index)
* 设置抗噪参数
set_metrology_object_param(MetrologyHandle, Index, 'min_score', 0.7)
set_metrology_object_param(MetrologyHandle, Index, 'num_measures', 50)
endfor
* 执行测量并获取结果
apply_metrology_model(Image, MetrologyHandle)
get_metrology_object_result(MetrologyHandle, 'all', 'all', 'result_type',
'all_param', RectParams)
* 结果可视化
dev_display(Image)
dev_set_color('green')
for i := 0 to |RectParams|-1 by 5
gen_rectangle2_contour_xld(Rect, RectParams[i], RectParams[i+1],
RectParams[i+2], RectParams[i+3], RectParams[i+4])
dev_display(Rect)
endfor
* 释放资源
clear_metrology_model(MetrologyHandle)
对于特殊场景的调整建议:
低对比度Mark:
emphasize增强边缘edges_color_sub_pix利用色彩信息edges_sub_pix的阈值参数变形Mark:
proj_match_points_ransac进行透视校正local_deformable_matching进行弹性匹配高速检测:
shape_based_matching快速粗定位在实际项目中,这套方案经过验证可以达到: