halcon-检测圆弧拟合圆

在一些特殊场合检测圆弧拟合圆

1,示例图

1.1原图

halcon-检测圆弧拟合圆_第1张图片 

1.2结果图

halcon-检测圆弧拟合圆_第2张图片

 

2.halcon代码

*读取图片
read_image (Image, 'double_circle')
* 
* 关闭窗口
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')

*通过区域的最小外接矩形,从矩形的 Top,Bottom,Left,Right四个方向裁剪区域,得到所需要的区域
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)

*用圆形结构元素扩张一个区域。
dilation_circle (RegionClipped, RegionDilation, 2.5)

*获得特定区域Region位置的图像
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)

*将一个XLD轮廓分割为直线段、圆(圆弧)、椭圆弧
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)

*计算个数
count_obj (ContoursSplit, Number)
dev_display (Image)

*定义region填充模式
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
    *选择一个元素
    select_obj (ContoursSplit, ObjectSelected, I)
    
    *返回XLD轮廓的全局属性值
    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)
         
        *用圆近似XLD轮廓
        fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
         
        *创建对应于圆或圆弧的XLD轮廓
        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)

你可能感兴趣的:(机器视觉,halcon,圆拟合)