halcon工件圆孔检测
下面展示一些 码片
。
* This program gets the deviation between
* circular contours and their best fitting circles
*
dev_update_var ('off') //开启或关闭程序运行过程中变量窗口的更新。
dev_update_off () //关闭窗口更新
*
* step: acquire image and initialize the visualization
* 获取图片
*
read_image (Image, 'pumpe') //获取图
get_image_size (Image, Width, Height) //得到图片尺寸
dev_close_window () //关闭窗口
dev_open_window (0, 0, Width, Height, 'light gray', WindowID)//打开一个尺寸和图片一样的窗口
dev_set_part (0, 0, Height - 1, Width - 1)//
dev_set_line_width (1)
dev_set_color ('red')
dev_set_draw ('margin')
dev_display (Image)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')//显示那个按F5继续
stop ()
*
* * step: create ROI
*
fast_threshold (Image, Region, 0, 70, 150)
dev_set_colored (3) //设置3个输出颜色
connection (Region, ConnectedRegions)//计算连通区域
select_shape (ConnectedRegions, SelectedRegions, ['outer_radius','anisometry','area'], 'and', [5,1,100], [50,1.8,99999]) //区域提取,很牛逼的一步
shape_trans (SelectedRegions, RegionTrans, 'outer_circle')
dilation_circle (RegionTrans, RegionDilation, 5.5)
union1 (RegionDilation, RegionUnion) //取并集
reduce_domain (Image, RegionUnion, ImageReduced)//取新图像
dev_clear_window ()
dev_display (ImageReduced)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: create and select relevant contours
*
threshold_sub_pix (ImageReduced, Border, 80)
select_shape_xld (Border, SelectedXLD, ['contlength','outer_radius'], 'and', [70,15], [99999,99999])
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 4, 2, 2)
select_shape_xld (ContoursSplit, SelectedXLD3, ['outer_radius','contlength'], 'and', [15,30], [45,99999])
union_cocircular_contours_xld (SelectedXLD3, UnionContours2, 0.5, 0.1, 0.2, 2, 10, 10, 'true', 1)//合并圆的轮廓
sort_contours_xld (UnionContours2, SortedContours, 'upper_left', 'true', 'column')
dev_clear_window ()
dev_set_color ('white')
dev_display (ImageReduced)
dev_display (SortedContours)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* step: fit circles into the contours and get the
* average deviation between contour and circle
*
count_obj (SortedContours, NumSegments)//取轮廓的个数
dev_display (Image)
dev_display (SortedContours)//在原图中扣出圆
NumCircles := 0//计数,先设置0
disp_message (WindowID, 'Circle radius and average distance', 'window', 10, 10, 'white', 'false')
disp_message (WindowID, 'between circle and contour:', 'window', 30, 10, 'white', 'false')
for i := 1 to NumSegments by 1
select_obj (SortedContours, SingleSegment, i)
NumCircles := NumCircles + 1
fit_circle_contour_xld (SingleSegment, 'atukey', -1, 2, 0, 5, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1)
dev_display (ContCircle)
dist_ellipse_contour_xld (SingleSegment, 'algebraic', -1, 0, Row, Column, 0, Radius, Radius, MinDist, MaxDist, AvgDist, SigmaDist)
disp_message (WindowID, NumCircles, 'window', Row - 10, Column - 5, 'white', 'false')
disp_message (WindowID, 'R' + NumCircles + ': ' + Radius$'.3', 'window', (i - 1) * 50 + 30, 450, 'white', 'false')
disp_message (WindowID, 'D_avg: ' + AvgDist$'.3', 'window', ((i - 1) * 50) + 50, 450, 'white', 'false')
endfor
*
dev_update_window ('on')
算法思路
1.全局阈值快速分割(fast_threshold),取出大致轮廓,不过很杂乱
2.计算连通区域(connection),求出联通的区域
3.选择形状特征区域(select_shape),(ConnectedRegions, SelectedRegions, [‘outer_radius’,‘anisometry’,‘area’], ‘and’, [5,1,100], [50,1.8,99999]),选择圆形和尺寸更好筛选
4.变换区域形状(shape_trans),(SelectedRegions, RegionTrans, ‘outer_circle’),求出最大外接圆
5.使用圆形结构膨胀区域(dilation_circle)
6.在把得到的区域和原图进行与运算,得到纯圆孔reduce_domain。
这样的几步就可以得到纯圆孔了。