第三课: 图像的 BLOB 分析处理流程(clip_region_rel,edges_sub_pix,segment_contours_xld等)---Circles.hdev

 所用到的算子:

1、boundary(Region : RegionBorder : BoundaryType : )  *提取区域的边界,实质是像素

      boundary (Region, RegionBorder, 'inner')

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

                  

       如果需要提取某区域中的一部分区域可以考虑使用该算子,如下图一到图二 :

                                                     

                                                                                                              图一

                                                              

                                                                                                            图二 

 3、    edges_sub_pix(Image : Edges : Filter, Alpha, Low, High : )*提取亚像素精密边缘轮廓

           参数: Low和High

           使用灰度直方图计算两个阀值,凡是像素灰度值大于高阀值的一定是轮廓; 凡是小于低阀值的一定不是轮廓;

            如果检测像素灰度值大于低阀值但又小于高阀值,那就要看这个像素的邻接像素中有没有超过高阀值的边缘像素:如果有的话那么它就是轮廓边缘了,否则他就不是轮廓边缘;

            alpha:

            指定值越小,平滑越强大,会减少边缘细节。

            Filter :

            Filter  = 'canny'刚好相反,alpha值越大,轮廓细节就越少(可能有一部分轮廓消失)

            如下:                          

            edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)

                                                                                 

4、segment_contours_xld(Contours : ContoursSplit : Mode, SmoothCont, MaxLineDist1, MaxLineDist2 : )

       将亚像素轮廓分割为值线段、圆、或圆弧

       Contours :需要进行分割的轮廓。

       ContoursSplit: 分割后的轮廓tuple。

       Mode:分割轮廓方式

       'lines'(使用直线段分割), 'lines_circles'(使用直线段和圆(弧)分割), 'lines_ellipses'(使用直线段和椭圆弧分割)。

       SmoothCont :轮廓平滑的参数,可以抑制在折线逼近过程中过短的线段,能更加逼近圆和椭圆。
        MaxLineDist1: 值越小,分割得到的线段、圆弧、或椭圆弧就越多

        MaxLineDist2 :值越小,分割得到的线段、圆弧、或椭圆弧就越多

        Remark:

        分割得到的轮廓是直线段、圆(圆弧)或者椭圆弧可以通过分割后轮廓的全局属性'cont_approx’参数的值来判断(参考               get_contour_global_attrib_xld)。

        如果'cont_approx'=-1,这一部分轮廓最适合被拟合为直线段。

        如果'cont_approx'=0,这一部分轮廓最适合被拟合为椭圆弧。

        如果'cont_approx'=1,这一部分轮廓最适合被拟合为圆弧。

5 、get_contour_global_attrib_xld(Contour : : Name : Attrib)

       返回亚像素轮廓的全局属性

       *通过segment_contours_xld算子得到轮廓被分割的部分然后通过get_contour_global_attrib_xld 算子的cont_approx               属性值判断被分割的轮廓部分是直线还是圆弧或椭圆弧

       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

6、 fit_circle_contour_xld,gen_circle_contour_xld

* The edges in the image are segmented into lines and circles.
* For the edges that are part of a circle, the circle parameters
* are estimated and the resulting circle is displayed.
read_image (Image, 'double_circle')
* 
* 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')
*通过区域的最小外接矩形,从矩形的TOP,Bottom,Left,Right四个方向裁剪区域
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)
*将XLD轮廓分割为线段和圆弧
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)
    *返回亚像素轮廓的全局属性
    *如果Attrib =-1,这一部分轮廓最适合被拟合为直线段。
    *如果Attrib =0,这一部分轮廓最适合被拟合为椭圆弧。
    *如果Attrib =1,这一部分轮廓最适合被拟合为圆弧。

    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)

<think>嗯,用户想找Halcon中计算圆倒角的方法或示例代码。首先,我需要回忆一下Halcon的相关函数和图像处理流程。根据之前的引用,最小外接圆函数smallest_circle可能有用,但倒角通常涉及边缘检测和几何分析。圆倒角可能是指圆角或圆弧过渡的边缘。在Halcon中,可能需要先提取边缘或区域,然后测量其曲率或拟合圆弧。比如,使用edges_sub_pix检测边缘,然后通过segment_contours分割轮廓,再拟合圆或圆弧。或者,用shape_trans将区域转换成圆角形状。用户提到的示例程序bottle.hdev可能涉及区域分割,但不确定是否直接处理圆倒角。可能需要结合Blob分析,找到相关区域后计算圆角参数。另外,投影变换中的坐标处理可能相关,但需要具体分析。参考引用中的hom_vector_to_proj_hom_mat2d用于投影变换,可能不直接相关。但轮廓顶点处理可能有用,比如获取边缘点后计算圆角半径。可能需要以下步骤:1.图像处理,增强边缘。2.提取目标区域或边缘。3.分析轮廓,找到拐角处的圆弧部分。4.使用拟合圆的方法(如smallest_circle)计算半径。此外,检查是否有直接测量圆倒角的算子,比如measure_pos或metrology模型来测量边缘的曲率半径。或者,使用get_contour_global_attrib_xld获取轮廓属性,分析曲率确定倒角半径。 需要确保语法正确,比如使用edges_sub_pix和fit_circle_contour_xld。然后组合这些步骤,给出示例代码,并解释关键参数。同时注意引用相关的Halcon函数文档,比如引用中的smallest_circle。</think>在Halcon中计算圆倒角通常需要结合边缘检测、几何拟合和区域分析。以下是基于轮廓分析的实现方法和示例代码: ### 方法步骤 1. **边缘检测**:使用亚像素级边缘检测 2. **轮廓分割**:将连续边缘分割为独立线段 3. **拐角识别**:通过曲率分析定位倒角区域 4. **圆拟合**:对拐角处的轮廓进行圆形拟合 ```python * 读取图像并预处理 read_image(Image, 'metal_part') threshold(Image, Region, 128, 255) connection(Region, ConnectedRegions) select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 10000) * 亚像素边缘检测 edges_sub_pix(Image, Edges, 'canny', 1.5, 20, 40) * 轮廓分割与选择 segment_contours_xld(Edges, ContoursSplit, 'lines_circles', 5, 4, 2) select_shape_xld(ContoursSplit, SelectedContours, 'contlength', 'and', 50, 99999) * 曲率分析定位倒角 get_contour_global_attrib_xld(SelectedContours, 'curvature', Curvature) find_funct_1d(Curvature, 'local_minima', 0.1, 1, 1, MinIndices) * 倒角区域拟合 gen_contour_polygon_xld(Contour, Row[MinIndices], Col[MinIndices]) fit_circle_contour_xld(Contour, 'geotukey', -1, 0, 0, 3, 2, RowC, ColumnC, Radius, StartPhi, EndPhi, PointOrder) ``` ### 关键函数说明 1. `edges_sub_pix`:检测亚像素精度的边缘,参数3控制平滑度,参数4/5为高低阈值[^2] 2. `segment_contours_xld`:分割轮廓为直线/圆弧段,参数4控制最大线段偏差[^3] 3. `fit_circle_contour_xld`:采用Tukey加权算法进行鲁棒圆拟合,参数6设置最大迭代次数[^1] ### 参数调整建议 - 边缘检测阈值影响倒角定位精度,建议通过`gray_histogram`分析后设置动态阈值 - 曲率分析时,`find_funct_1d`的平滑参数需根据实际曲率变化幅度调整 - 拟合算法选择: $$ R = \sqrt{(x_i - x_c)^2 + (y_i - y_c)^2} + \epsilon_i $$ 其中$(x_c,y_c)$为圆心坐标,$\epsilon_i$为残差项[^4]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值