基于Halcon学习的边缘提取【二】circles.hdev例程

图像中的边被分割成直线和圆,对于作为圆一部分的边,将估计圆参数并显示生成的圆。

*读取图片
read_image (Image, 'double_circle')

*关闭窗口
dev_close_window ()
*获得图片的大小
get_image_size (Image, Width, Height)
*打开窗口
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)

*分割包含边的区域
*快速二值化
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)

*在包含边缘的图像子域中,提取亚像素精确边缘。

*使用Deriche、Lanser、Shen或Canny过滤器提取亚像素精确边缘。
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)
    *返回xld轮廓的全局属性值
    get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
    
    *Attrib有-1 0 1
    *将圆拟合到圆的圆弧线段
    if (Attrib > 0)
        *对XLD轮廓进行圆弧拟合
        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)

*读取图片
read_image (Image, 'double_circle')

*关闭窗口
dev_close_window ()

*获得图片的大小
get_image_size (Image, Width, Height)

*打开窗口
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)

*分割包含边的区域
*快速二值化
fast_threshold (Image, Region, 0, 120, 7)

*获得区域的边缘---对区域求轮廓
boundary (Region, RegionBorder, 'inner')

*膨胀
dilation_circle (RegionClipped, RegionDilation, 2.5)
*抠图
reduce_domain (Image, RegionDilation, ImageReduced)

*使用Deriche、Lanser、Shen或Canny过滤器提取亚像素精确边缘。
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)

    *返回xld轮廓的全局属性值
    get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
    
    *Attrib有-1 0 1
    *将圆拟合到圆的圆弧线段
    if (Attrib > 0)
        *对XLD轮廓进行圆弧拟合
        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)

 


clip_region_rel(Region : RegionClipped : Top, Bottom, Left, Right : )

功能:相对于其最小的周围矩形剪裁区域。

Region :输入区域;RegionClipped :输出区域缩小的区域;Top, Bottom, Left, Right:内缩量。

通过消除接近区域最小周围矩形的部分来减少区域。具体来说,这意味着该区域被剪裁为小

于或等于最小周围矩形的矩形

edges_sub_pix(Image : Edges : Filter, Alpha, Low, High : )

功能: 使用Deriche,Lanser,Shen或Canny过滤器提取亚像素精确边缘

Image :输入图像;Edges :输出的边缘;Alpha:滤波器宽度;

Low:滞后阈值操作的阈值下限;High: 滞后阈值操作的阈值上限

egment_contours_xld(Contours : ContoursSplit :Mode, SmoothCont, MaxLineDist1, MaxLineDist2 : )

功能:分割XLD轮廓为线段和圆弧或椭圆弧

Contours :输入轮廓;ContoursSplit:分割后的轮廓;Mode:轮廓分割模式;

SmoothCont:用于平滑轮廓的点数;MaxLineDist1:轮廓与近似线之间的最大距离(第一次

迭代);MaxLineDist2 :轮廓与近似线之间的最大距离(第二次迭代)

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值