例程功能
检查所描绘工具的轮廓。将边界分割为线形和圆形,对于属于圆的边,将估计圆参数并显示生成。
代码如下
* Init window
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
*
* Segment a region containing the edges
fast_threshold (Image, Region, 0, 120, 7)
boundary (Region, RegionBorder, 'inner')
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)
dilation_circle (RegionClipped, RegionDilation, 2.5)
reduce_domain (Image, RegionDilation, ImageReduced)
*
* In the subdomain of the image containing the edges,
* extract subpixel precise edges.
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
select_obj (ContoursSplit, ObjectSelected, I)
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
* Fit a circle to the line segment that are arcs of a circle
if (Attrib > 0)
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
dev_display (ContCircle)
endif
endfor
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
要点
- 亚像素边缘提取比较耗时,出于对代码执行速度的要求,先裁剪再提取边缘;
fast_threshold (Image, Region, 0, 120, 7) // Image已读
boundary (Region, RegionBorder, 'inner')
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5) // 根据最小外接矩形裁剪区域
dilation_circle (RegionClipped, RegionDilation, 2.5) //做膨胀操作获取边界周围更多区域,对只取boundary有弥补作用
reduce_domain (Image, RegionDilation, ImageReduced)
- 像素提取后,按需打散很关键,有助于后续筛选;
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
get_contour_global_attrib_xld
返回轮廓全局属性值。使用了参数cont_approx
,值为-1
,轮廓与线段近似;值为0
,轮廓与椭圆弧近似;值为1
,轮廓与圆弧近似;- .根据轮廓拟合圆(这边作为后处理手段,使检测的每个轮廓为圆的一部分)
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)