边缘缺陷检测

在这里插入图片描述

* This program shows how to calculate the pointwise
* distance of two contours to inspect the camera frame
* of a phone housing.
* 
* It uses the operator distance_contours_xld to calculate
* the distance between the inner and outer contour of the
* frame and the operator segment_contour_attrib_xld
* to extract the defects for visualization.
* 
* Set the thresholds for the allowed distances in pixels
MinWidth := 30
MaxWidth := 35
SmoothnessTolerance := .2
* 
* Initialize visualization
dev_update_off ()
dev_close_window ()
read_image (Image, 'plastic_parts/phone_camera_frame_01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
Colors := ['white','green','yellow','orange','red']
Legend[0] := 'Legend:'
Legend[1] := 'Extracted contours'
Legend[2] := 'Frame too narrow'
Legend[3] := 'Frame too wide'
Legend[4] := 'Contour defect'
* 
* Main loop:
* Inspect the camera frame
* 
NumImages := 2
for I := 1 to NumImages by 1
    read_image (Image, 'plastic_parts/phone_camera_frame_' + I$'02')
    * 
    * Extract frame edges
    * 
    * Select the frame region
    threshold (Image, Region, 100, 255)
    dilation_rectangle1 (Region, RegionDilation, 15, 15)
    reduce_domain (Image, RegionDilation, ImageReduced)
    * Extract edges in the reduced image domain
    edges_sub_pix (ImageReduced, Edges, 'canny', 0.7, 10, 60)
    union_adjacent_contours_xld (Edges, UnionContours, 7, 7, 'attr_keep')
    select_shape_xld (UnionContours, SelectedContours, 'contlength', 'and', 700, 99999)
    count_obj (SelectedContours, Number)
    * Select the inner and the outer contour
    *获取轮廓的长度
    length_xld (SelectedContours, Length)
    
    *tuple_sort_index按升序对元组的所有元素进行排序,并按元素值从小到大的顺序返回相应元素在原来的元祖中的索引,元素值最小的索引为0,
   
    tuple_sort_index (Length, Indices)
    
    *选择最里面的轮廓,selected_obj的Index是从1开始的,素组的索引是从0开始的,所以需要加1
    *Indices[1] = 0,轮廓长度最小,Index是从1开始的,所以Indices[1]得加1
    select_obj (SelectedContours, InnerContour, Indices[1]+1 )
    *选择最外面的轮廓
    select_obj (SelectedContours, OuterContour, Indices[3]+1 )
    * 先计算两个轮廓之间的距离
    * Calculate the distances between the inner and outer contour.
    * The distances are stored as an attribute to the output contour.
    distance_contours_xld (InnerContour, OuterContour, OuterContourWithWidth, 'point_to_segment')
    * Get the contour parts that lie outside of the tolerances
    *分割提取出距离小于指定最小距离的轮廓部分
    segment_contour_attrib_xld (OuterContourWithWidth, OuterContourPartToNarrow, 'distance', 'or', 0, MinWidth)
    length_xld (OuterContourPartToNarrow, Length1)
    *分割提取出距离大于指定距离的轮廓部分
    segment_contour_attrib_xld (OuterContourWithWidth, OuterContourPartToWide, 'distance', 'or', MaxWidth, 10000)
    length_xld (OuterContourPartToWide, Length2)
    
    * 检查轮廓本身的平滑度
    * Check, if the contours are sufficiently smooth by
    * comparing each contour with a smoothed version of itself
    smooth_contours_xld (OuterContour, OuterContourSmooth, 11)
    smooth_contours_xld (InnerContour, InnerContourSmooth, 11)
    distance_contours_xld (OuterContour, OuterContourSmooth, OuterContourWithDistance, 'point_to_segment')
    distance_contours_xld (InnerContour, InnerContourSmooth, InnerContourWithDistance, 'point_to_segment')
    * Get the contour parts that lie outside of the tolerances
    segment_contour_attrib_xld (OuterContourWithDistance, OuterContourDefect, 'distance', 'or', SmoothnessTolerance, 100)
    segment_contour_attrib_xld (InnerContourWithDistance, InnerContourDefect, 'distance', 'or', SmoothnessTolerance, 100)
    * 
    * Get pointwise distances for debugging
    get_contour_attrib_xld (OuterContourWithWidth, 'distance', FrameWidth)
    get_contour_attrib_xld (OuterContourWithDistance, 'distance', InnerDistances)
    get_contour_attrib_xld (InnerContourWithDistance, 'distance', OuterDistances)
    *
  
    * Display results
    dev_display (Image)
    disp_message (WindowHandle, 'Inspect frame of phone camera (image ' + I + '/' + NumImages + ')', 'window', 12, 12, 'black', 'true')
    count_obj (OuterContourPartToNarrow, NumTooNarrow)
    count_obj (OuterContourPartToWide, NumTooWide)
    count_obj (OuterContourDefect, NumInnerDefects)
    count_obj (InnerContourDefect, NumOuterDefects)
    if (NumTooNarrow + NumTooWide + NumInnerDefects + NumOuterDefects == 0)
        OK := 1
        disp_message (WindowHandle, 'Frame OK', 'window', 50, 12, 'forest green', 'true')
    else
        OK := 0
        disp_message (WindowHandle, 'Frame not OK', 'window', 50, 12, 'red', 'true')
    endif
    * 
    dev_set_line_width (1)
    dev_set_color (Colors[1])
    dev_display (OuterContour)
    dev_display (InnerContour)
    dev_set_line_width (5)
    dev_set_color (Colors[2])
    dev_display (OuterContourPartToNarrow)
    dev_set_color (Colors[3])
    dev_display (OuterContourPartToWide)
    dev_set_color (Colors[4])
    dev_display (OuterContourDefect)
    dev_set_color (Colors[4])
    dev_display (InnerContourDefect)
    disp_message (WindowHandle, Legend, 'window', 500, 12, Colors, 'false')
    if (I < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
<think>好的,我现在需要帮助用户了解如何在Vision Master中实现边缘缺陷检测。用户之前的问题是关于Vision Master的边缘检测功能,现在他们想要具体的实现方法或教程。根据用户提供的引用内容,特别是引用2和引用3,里面提到了直线和圆弧边缘缺陷检测的基础,以及一个OKNG计数方案的步骤。 首先,我需要整理引用中的相关信息。引用2提到Vision Master提供了直线和圆弧边缘缺陷检测模块,这些是基础模块,其他模块可能基于它们。引用3详细描述了OKNG计数方案的步骤,包括图像源添加、圆弧边缘检测的参数设置、条件检查、变量计算和界面设计等。这些步骤可以作为教程的基础。 接下来,我需要将这些信息结构化,分步骤说明实现方法。可能需要分为硬件准备、软件配置、检测流程、参数优化和案例应用几个部分。硬件部分需要确保相机和光源合适,软件部分要说明模块的添加和参数设置。检测流程需要详细说明每一步的操作,比如如何绘制ROI,设置边缘类型和极性,调整缺陷阈值等。参数优化部分需要指导用户如何根据实际图像调整参数,可能包括预处理和结果验证。案例应用可以引用用户提到的OKNG计数方案,说明如何结合其他工具进行统计和界面设计。 同时,需要注意用户提到的使用中文回答,并正确应用LaTeX格式。不过,在技术教程中,数学公式可能较少,但如果有参数计算,需要用$$...$$格式。例如,合格率的计算可能需要用公式,如合格率公式:$$ \text{合格率} = \frac{\text{OK数}}{\text{检测总数}} \times 100\% $$,这样符合独立公式的要求。 需要确保生成的回答结构清晰,步骤明确,参考用户提供的引用内容,并在适当的地方添加引用标识。例如,在提到圆弧边缘检测参数设置时,引用3中的步骤会被引用。此外,用户可能需要相关问题,如优化方法、常见问题处理、与其他检测方法的对比等,需要生成相关问题部分。 最后,检查是否符合所有系统级指令:行内公式用$...$,独立公式用$$...$$,正确的中文回答,生成相关问题,引用标识正确添加。确保没有使用Markdown格式,语言口语化,分步骤说明,同时保持专业性和准确性。</think>基于VisionMaster的边缘缺陷检测实现方法可分为以下步骤,结合直线/圆弧检测模块与深度学习技术实现高效缺陷识别: 一、硬件环境搭建 1. 使用200万像素以上工业相机,分辨率建议$1280 \times 1024$起 2. 配置同轴光源或条形光源,确保边缘对比度满足$SNR>30dB$ 3. 安装VisionMaster4.2以上版本,推荐搭配Intel i7处理器+16GB内存 二、软件配置流程 1. 创建新方案→添加"图像源"模块→设置触发模式(软触发/硬触发) 2. 添加"预处理"模块: - 高斯滤波:核尺寸$3 \times 3$,$\sigma=0.8$ - 直方图均衡化:增强边缘对比度 3. 添加检测模块(二选一): *直线边缘检测方案* ```python # 伪代码参数配置示例 roi_width = 50 # 检测区域宽度(pixel) edge_threshold = 30 # 边缘强度阈值 defect_sensitivity = 0.7 # 缺陷敏感度[0-1] ``` *圆弧边缘检测方案* ```python min_arc_angle = 30 # 最小圆弧角度(°) max_radius_deviation = 5 # 允许半径偏差(pixel) ``` 三、核心参数设置原则 1. 边缘极性选择: $$ \text{极性} = \begin{cases} \text{黑→白} & \text{深色背景浅色边缘}\\ \text{白→黑} & \text{浅色背景深色边缘} \end{cases} $$ [^2] 2. 缺陷判定阈值: - 距离阈值:建议初始值$10$像素,根据实际缺陷尺寸调整 - 面积阈值:通过$A = \pi r^2$计算允许的最大异常区域 四、深度学习增强方案 1. 传统算法+深度学习混合架构: ```mermaid graph TD A[原始图像] --> B(传统边缘检测) B --> C{缺陷置信度>90%?} C -->|是| D[直接输出结果] C -->|否| E[深度学习分类] E --> F[结果融合] ``` 2. 样本要求: - 正样本:$500+$张无缺陷图像 - 负样本:$1000+$张含各类缺陷图像 - 数据增强:旋转($\pm5°$)、平移($\pm3\%$)、噪声($\sigma=0.02$) 五、OK/NG计数实现(参考引用[3]) 1. 变量计算设置: $$ \text{合格率} = \frac{\text{OK数}}{\text{检测总数}} \times 100\% $$ 2. 界面设计要点: - 显示区域划分:图像区(≥800×600)、参数区、统计区 - 字体规范:报警信息用红色24pt,统计信息用绿色20pt 六、调试技巧 1. 边缘检测优化: - 使用$Canny$算子时,双阈值建议设为$(50, 150)$ - 对于模糊边缘,可应用$Sobel$增强:$$ G = \sqrt{G_x^2 + G_y^2} $$ 2. 常见问题处理: - 过检:增大高斯滤波$\sigma$值(建议步长0.1) - 漏检:减小"缺陷距离阈值"(建议步长1像素)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖子工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值