*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