项目需求:要求圆环外圈毛刺的凸出高度不得超过20像素,如超过判为NG。

 

算法分析:按照一般的思路,应该是先将外圈拟合成一个圆;然后将这个工件阈值化,提取工件区域;然后用该区域减去之前拟合的圆,剩下的区域就是毛刺部分了。那么什么样特征的毛刺算超标的呢?很显然,如果仅仅用'area'、'inner_radius'等参数去筛选,至少从原理上来看,是不完全契合项目需求的。

 

具体完整算法如下:

 1 read_image (Image, 'C:/Users/02/Desktop/Ring.png')
 2 dev_set_draw ('margin')
 3 threshold (Image, Region, 0, 253)
 4 fill_up_shape (Region, RegionFillUp, 'area', 1, 20000)
 5 fill_up (RegionFillUp, RegionFillUp1)
 6 difference (RegionFillUp1, RegionFillUp, CircleInner)
 7 smallest_circle (CircleInner, Row, Column, Radius)
 8 find_circle (Image, PartCircleXLD, Regions, Cross, Circle, Row, Column, Radius + 100, 0, 360, 40, 100, 20, 1, 40, 'negative', 'first', 'inner', 10, 'circle', RowCenter, ColCenter, Radius1)
 9 gen_region_contour_xld (Circle, RegionCircle, 'filled')
10 
11 *稍微膨胀一下,便于后续处理
12 dilation_circle (RegionCircle, RegionCircle, 1.5)
13 difference (RegionFillUp, RegionCircle,RegionDifference)
14 opening_circle (RegionDifference, RegionDifference, 3.5)
15 connection (RegionDifference, ConnectedRegions)
16 select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 9999999)
17 count_obj (SelectedRegions, Number)
18 
19 *后面精确测量一下多出的毛刺是否超标
20 gen_empty_obj (RegionDefects)
21 if (Number >0)
22     for i := 1 to Number by 1
23         select_obj (SelectedRegions, ObjectSelected, i)
24         distance_pr (ObjectSelected, RowCenter, ColCenter, DistanceMin, DistanceMax)
25         *如果毛刺凸出高度超过20像素,则判断超标
26         if (DistanceMax - Radius1 > 20)
27             concat_obj (RegionDefects, ObjectSelected, RegionDefects)
28         endif
29     endfor   
30 endif
31 
32 dev_display (Image)
33 dev_display (RegionDefects)
34 disp_cross (3600, RowCenter, ColCenter, 20, 0.45)

 

 

与程序中的抓圆函数find_circle相关的函数,可以从这里下载:

https://files.cnblogs.com/files/xh6300/find_line_find_circle.zip

 

posted on 2019-08-01 11:40  xh6300  阅读(1722)  评论(8编辑  收藏  举报