• 算法功能:边缘缺陷检测


    请添加图片描述

    *2. 算法思路:
    *      (1)初始化
    *      (2)使用canny滤波器边缘检测
    *      (3)缺陷检测
    *           --计算待检测边缘上的点到平滑后边缘的距离,超过一定阈值公差即为缺陷
    *      (4)结果显示
    
    *-----------------------初始化--------------------------
    
    dev_update_off ()
    *声明空对象,用于保存缺陷边缘
    gen_empty_obj(FlawEdgeObject)
    *读图
    read_image (Image1, 'C:/Users/Administrator/Desktop/1.bmp')
    
    *-----------------------边缘检测--------------------------
    
    *阈值分割+形态学处理,将包含边缘部分图像进行抠图
    binary_threshold (Image1, Region, 'smooth_histo', 'light', UsedThreshold)
    connection(Region, ConnectedRegions)   
    select_shape(ConnectedRegions, SelectedRegions, ['area','anisometry'], 'and', [10000,0.8], [99999,2]) 
    closing_circle(SelectedRegions, RegionClosing, 1)
    dilation_circle (RegionClosing, RegionDilation, 3.5)
    erosion_circle (RegionClosing, RegionErosion, 3.5)
    difference (RegionDilation, RegionErosion, RegionDifference)
    *抠图处理,得到包换边缘部分图像
    reduce_domain (Image1, RegionDifference, ImageReduced)
    *使用canny滤波器进行边缘检测
    edges_sub_pix (ImageReduced, Edges1, 'canny', 3, 20, 40)
    *边缘处理
    select_contours_xld (Edges1, SelectedContours, 'contour_length', 10, 2000, -0.5, 0.5)
    union_adjacent_contours_xld (SelectedContours, UnionContours, 10, 1, 'attr_keep')
    
    *-----------------------缺陷检测--------------------------
    *计算边缘个数,分别进行缺陷检测
    count_obj(UnionContours, Number)    
    
    for Index := 1 to Number by 1
        
          *-----算法核心:计算待检测边缘上的点到平滑后边缘的距离,超过一定阈值公差即为缺陷--------
          *选择待检测边缘
          select_obj(UnionContours, ObjectSelected, Index)
          *平滑边缘
          smooth_contours_xld (ObjectSelected, SmoothedContours, 89)
          *阈值公差
          disTH := 5
          *用于保存缺陷边缘X坐标
          flawPtsX :=[] 
          *用于保存缺陷边缘Y坐标
          flawPtsY :=[]
          *得到待检测边缘点坐标
          get_contour_xld(ObjectSelected, Row2, Col2)
          *待检测边缘上点到平滑边缘的距离
          tuple_length(Row2, Length)
          for Index1 := 0 to Length-1 by 1
              distance_pc(SmoothedContours, Row2[Index1], Col2[Index1], DistanceMin, DistanceMax)
              *根据公差判断是否合格
              if(DistanceMin > disTH)
                  flawPtsX :=[flawPtsX,Col2[Index1]]
                  flawPtsY :=[flawPtsY,Row2[Index1]]
              endif
          endfor      
          *将缺陷点生成区域,并进一步筛选
          gen_region_points(Region1, flawPtsY, flawPtsX)
          dilation_circle(Region1, RegionDilation, 5)
          connection(RegionDilation, ConnectedRegions1)
          select_shape(ConnectedRegions1, SelectedRegions1, 'area', 'and', 200, 99999)
          gen_empty_obj(EmptyObject)
          count_obj(SelectedRegions1, Number1)
          *再从筛选后的区域,选择不合格的边缘
          for Index1 := 1 to Number1 by 1
              select_obj(SelectedRegions1, ObjectSelected2, Index1)
              smallest_rectangle1(ObjectSelected2, Row11, Column11, Row21, Column2)
              clip_contours_xld(ObjectSelected, ClippedContours, Row11, Column11, Row21, Column2)
              concat_obj(EmptyObject, ClippedContours, EmptyObject)        
          endfor
          *连接相邻不合格边缘缺陷
          union_adjacent_contours_xld(EmptyObject, EmptyObject, 50, 1, 'attr_keep')
          
          *合并缺陷
          concat_obj(FlawEdgeObject, EmptyObject, FlawEdgeObject)
    endfor
          
    dev_display (Image1)
    dev_display (FlawEdgeObject)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    时间戳的拓展及应用实例
    Excel常用公式总结非常实用
    Linux之 USB驱动框架-USB鼠标驱动源码分析(5)
    行至青鸟 | 为学习保驾护航的“教学管理”
    unity烘焙过程与注意事项,希望你们少走弯路
    AWTK 表格视图的实现原理和用法
    算法分析与设计编程题 递归与分治策略
    【测试人生】游戏业务测试落地精准测试专项的一些思路
    java毕业设计乐儿乐社区生鲜团购系统源码+lw文档+mybatis+系统+mysql数据库+调试
    关于ES5内置函数Object的新方法--Object.create()
  • 原文地址:https://blog.csdn.net/zhouzongzong/article/details/126651382