Halcon亚像素边缘缺陷检测案例

该文介绍了如何运用Halcon进行金属区域的边缘检测,通过阈值分割和亚像素边缘提取获取边缘信息,再利用fit_rectangle2_contour_xld拟合最小外接矩形。之后计算轮廓点到矩形的距离,以此判断是否存在缺陷,如顶点附近的误检问题。文章还展示了如何屏蔽角落附近的像素点,并检查是否存在断胶、溢胶等缺陷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、下面的案例是总结的Halcon边缘缺陷检测的一种情况。本案例是利用阈值分割获取金属区域,并利用boundary和edges_sub_pix获取到亚像素边缘。然后综合利用fit_rectangle2_contour_xld拟合出金属对应的放射矩形,最后利用dist_rectangle2_contour_points_xld计算金属轮廓上面的点到最小外接矩轮廓上面的点距离判断有无缺陷。触类旁通,依据此案例的原理可以推理应用在别的区域。例如胶路轨迹检测,仍然可以利用边缘XLD轮廓和边缘点距离判断胶路有无,断胶,溢胶,等等。
二、由于金属顶点角落附近打光比较暗,容易误检,所以需要屏蔽角点附近区域。案例计算金属轮廓上的点到最小外接矩的四个顶点距离,并获取最小值。后面判断缺陷时会屏蔽距离小于7的点,其实就是屏蔽了矩形顶点附近七个像素以内的所有像素点。屏蔽代码如下:
2.1 计算轮廓上所有点到最小外接矩4个顶点距离。
D1 := sqrt((Rows - RowC[0]) * (Rows - RowC[0]) + (Cols - ColC[0]) * (Cols - ColC[0]))
D2 := sqrt((Rows - RowC[1]) * (Rows - RowC[1]) + (Cols - ColC[1]) * (Cols - ColC[1]))
D3 := sqrt((Rows - RowC[2]) * (Rows - RowC[2]) + (Cols - ColC[2]) * (Cols - ColC[2]))
D4 := sqrt((Rows - RowC[3]) * (Rows - RowC[3]) + (Cols - ColC[3]) * (Cols - ColC[3]))
2.2 通过min2比较所有的数组,并返回最小值数组。
DistCorner := min2(min2(D1,D2),min2(D3,D4))
2.3 计算金属轮廓上面的点到最小外接矩轮廓上面的点距离
dist_rectangle2_contour_points_xld (RectangleEdge, 0, Row[I], Column[I], Phi[I], Length1[I], Length2[I], Dist)
2.4 通过if (DistCorner[J] > 7.0 and Dist[J] > 1.0),屏蔽矩形顶点附近七个像素以内的所有像素点,并且金属轮廓上面的点到最小外接矩轮廓上面的点距离大于1的判定NG。

  • RectangleOK := true
    
  • for J := 0 to |Dist| - 1 by 1
    
  •     if (DistCorner[J] > 7.0 and Dist[J] > 1.0)
    
  •         RectangleOK := false
    
  •         break
    
  •     endif
    
  • endfor
    
* This example program shows how fit_rectangle2_contour_xld can be used to
* detect manufacturing errors of punched holes in a metal part.  The errors
* show up as small protrusions of the metal into the hole.  They can be detected
* by fitting rectangles to the edges of the hole robustly (i.e., with outlier
* suppression) and the calculating the distances of the edges to the rectangle
* sides using dist_rectangle2_contour_points_xld.  Since the corners of the
* holes are slightly rounded, some extra processing must be performed to
* disregard the corners in the check for errors.
dev_update_off ()
read_image (Image, 'punched_holes')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
* Since the metal part is backlit, the processing speed can be increased
* significantly by constructing a ROI for the subpixel-precise edge extraction
* that is as small as possible.  This can easily be achieved by thresholding and
* morphology.
fast_threshold (Image, Region, 128, 255, 10)
boundary (Region, Border, 'inner')
dilation_rectangle1 (Border, EdgeROI, 7, 7)
reduce_domain (Image, EdgeROI, ImageReduced)
* Perform the edge extraction.
edges_sub_pix (ImageReduced, Edges, 'canny', 1.7, 40, 120)
* Remove edge fragments that are too short.
select_shape_xld (Edges, RectangleEdges, 'contlength', 'and', 500, 100000)
* Fit rectangles to the holes' edges using the outlier suppression of Tukey.
fit_rectangle2_contour_xld (RectangleEdges, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
* Create rectangles with the fitted parameters for visualization purposes.
gen_rectangle2_contour_xld (Rectangles, Row, Column, Phi, Length1, Length2)
dev_set_color ('yellow')
dev_display (Rectangles)
* Check whether the holes are OK.
count_obj (RectangleEdges, Number)
for I := 0 to Number - 1 by 1
    select_obj (RectangleEdges, RectangleEdge, I + 1)
    * Get the contour's coordinates.
    get_contour_xld (RectangleEdge, Rows, Cols)
    * Create a rectangle with the appropriate rectangle parameters.
    gen_rectangle2_contour_xld (Rect, Row[I], Column[I], Phi[I], Length1[I], Length2[I])
    * Get the coordinates of the rectangle's corners.
    get_contour_xld (Rect, RowC, ColC)
    * Calculate the distances of all the contour points to the four corners of the
    * rectangle.
    D1 := sqrt((Rows - RowC[0]) * (Rows - RowC[0]) + (Cols - ColC[0]) * (Cols - ColC[0]))
    D2 := sqrt((Rows - RowC[1]) * (Rows - RowC[1]) + (Cols - ColC[1]) * (Cols - ColC[1]))
    D3 := sqrt((Rows - RowC[2]) * (Rows - RowC[2]) + (Cols - ColC[2]) * (Cols - ColC[2]))
    D4 := sqrt((Rows - RowC[3]) * (Rows - RowC[3]) + (Cols - ColC[3]) * (Cols - ColC[3]))
    * The distance of the contour points to the corners of the rectangle is given
    * by the minimum of the four distances.  This distance is used to exclude
    * contour points that are too close to the corners from the check for errors.
    DistCorner := min2(min2(D1,D2),min2(D3,D4))
    * Calculate the distances of the contour points of the rectangle.
    dist_rectangle2_contour_points_xld (RectangleEdge, 0, Row[I], Column[I], Phi[I], Length1[I], Length2[I], Dist)
    * Check whether the hole is OK by examining the distances of the contour
    * points to the rectangle.  A hole is OK if all points that lie more than seven
    * pixels from the corners have a distance of less than one pixel to the fitted
    * rectangle.  To do so, we could use the following code:
*     RectangleOK := true
*     for J := 0 to |Dist| - 1 by 1
*         if (DistCorner[J] > 7.0 and Dist[J] > 1.0)
*             RectangleOK := false
*             break
*         endif
*     endfor
    * A much faster way to do this in HDevelop is to generate a mask that
    * contains 0 for all points that should not be taken into account and 1
    * otherwise.  To do so, we subtract the minimum distance of 7.0 from the
    * distances to the corners and take the maximum of 0.0 and the resulting
    * values.  This sets all the distances that are too close to the corners to 0.
    * To set all other values to 1, we can simply take the sign of the values.
    Mask := sgn(max2(DistCorner - 7.0,0.0))
    * We can now multiply the distances to the rectangle with the mask and
    * check whether the maximum distance is smaller than the maximum allowed
    * distance of 1.0.
    RectangleOK := max(Dist * Mask) <= 1.0
    * Display whether the hole is OK.
    if (RectangleOK)
        dev_set_color ('green')
        get_string_extents (WindowHandle, 'OK', Ascent, Descent, Width, Height)
        set_tposition (WindowHandle, Row[I] - Height / 2, Column[I] - Width / 2)
        write_string (WindowHandle, 'OK')
    else
        dev_set_color ('red')
        get_string_extents (WindowHandle, 'Not OK', Ascent, Descent, Width, Height)
        set_tposition (WindowHandle, Row[I] - Height / 2, Column[I] - Width / 2)
        write_string (WindowHandle, 'Not OK')
    endif
endfor

原图
在这里插入图片描述
结果图
在这里插入图片描述

<think>好的,我现在需要处理用户关于Halcon高精度缺陷检测的询问。首先,我得仔细分析用户的请求,确保完全理解他们的需求。用户提到“高精度缺陷检测的应用或实现方法”,具体想要了解实现方案、算法和示例。根据用户提供的引用内容,特别是引用1、2、3、4,我需要结合这些信息来构建回答。 首先,我应该回顾Halcon的基本功能。Halcon作为机器视觉库,在工业检测中应用广泛。用户可能对高精度检测的关键技术点感兴趣,比如图像预处理、特征提取、算法选择等。引用1中提到了图像预处理、特征提取和缺陷判断的流程,这可能是一个结构化的框架。接下来,引用3提到了深度学习在Halcon中的应用,尤其是CNN模型的使用,这可能是一个重要的点。引用2和4分别提到了C#集成和卡尺算法,这些可以作为具体实现案例。 用户需要具体的实现方案和算法示例,因此我需要分步骤说明。首先,系统架构方面,可能需要包括图像采集、预处理、检测算法、分类和后处理模块。每个部分都需要结合Halcon的功能,比如引用2中的C#和Halcon结合,可能需要说明如何调用Halcon的库。 在核心技术部分,高精度检测的关键在于图像增强和算法选择。引用1提到的预处理方法如高斯滤波、形态学操作,这些可以详细说明。同时,引用3的深度学习部分需要强调与传统方法的对比,比如使用CNN提高准确率。另外,引用4的卡尺算法可能用于边缘检测或特定几何形状的缺陷,这也是一个值得提及的点。 算法示例部分,用户可能需要代码片段。比如,使用Halcon的HDevelop语言展示表面检测或深度学习检测的代码。这里需要确保代码符合用户给出的格式要求,即独立公式用$$...$$,行内用$...$。不过用户提供的示例中有Python代码,但Halcon通常使用HDevelop或C++,可能需要调整。 另外,用户要求生成相关问题,我需要根据内容提出相关问题,比如性能优化、多类型缺陷处理、3D检测等,这些在引用中提到的参数优化、算法选择、深度学习应用等部分可以自然延伸出来。 需要注意的是,回答中引用的部分要正确标注引用标识,比如引用3提到的深度学习集成,引用4的卡尺算法,这些都要在相应位置添加[^3]、[^4]等。同时,回答结构要清晰,分步骤或模块,让用户容易理解。 可能还需要考虑用户的实际应用场景,比如工业零部件、电子电路板等,结合引用中的案例,说明不同场景下的具体实现方法。此外,参数优化和实时检测也是高精度检测中的挑战,需要提及Halcon提供的工具和方法。 最后,检查是否符合系统级指令:使用中文,LaTeX格式正确,引用标识正确,生成相关问题。确保没有遗漏任何用户的要求,比如示例代码是否用正确的格式,是否分点说明等。</think>以下为Halcon高精度缺陷检测的实现方案与技术解析,结合机器视觉与深度学习技术: ### 一、Halcon高精度检测系统架构 $$系统框架=图像采集 \rightarrow 预处理 \rightarrow 缺陷检测算法 \rightarrow 分类器 \rightarrow 后处理$$ 1. **图像采集模块**:采用500万像素工业相机+环形光源,通过$GigE Vision$协议传输图像 2. **预处理流程**: - 非均匀性校正:消除$光照不均$的影响[^1] - 亚像素边缘提取:精度可达$0.1μm$级 3. **检测算法组合**:传统算法+深度学习混合架构(如CNN分类器)[^3] ### 二、高精度检测核心技术 #### 1. 图像增强算法 ```hdev * Halcon表面缺陷检测示例 read_image (Image, 'metal_part') emphasize (Image, ImageEmphasize, 7, 7, 1.5) median_image (ImageEmphasize, ImageMedian, 'circle', 3, 'mirrored') dyn_threshold (ImageMedian, ImageEmphasize, RegionDynThresh, 15, 'light') connection (RegionDynThresh, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 99999) ``` #### 2. 混合检测算法 | 算法类型 | 适用场景 | 精度指标 | |----------------|---------------------------|----------------| | 卡尺算法 | 边缘缺陷检测 | ±0.2像素 | | 形态学分析 | 表面凹陷检测 | 0.5μm | | 深度学习分类 | 复杂纹理缺陷识别[^3] | 准确率99.8% | #### 3. 深度学习实现方案(基于Halcon DL) ```hdev * 深度学习缺陷检测流程 read_dl_model ('defect_classifier.hdl', DLModelHandle) create_dict (DLPreprocessParam) set_dict_tuple (DLPreprocessParam, 'image_size', [512,512]) apply_dl_model (DLModelHandle, ImagePreprocessed, DLPreprocessParam, DLResultHandle) get_dict_tuple (DLResultHandle, 'defect_probabilities', DefectProbs) ``` ### 三、精度提升关键技术 1. **亚像素处理**: $$e_{sub} = \frac{\partial^2 f}{\partial x^2} \cdot \Delta x^2 + \frac{\partial^2 f}{\partial y^2} \cdot \Delta y^2$$ 通过二阶导数插值实现$0.1$像素级缺陷定位 2. **多尺度分析**: 采用$Gauss金字塔$进行多分辨率检测,兼顾检测速度与精度 3. **3D检测扩展**: 结合条纹投影技术实现三维缺陷检测,高度测量精度达$1μm$[^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值