Halcon之轮廓拟合,排序

1,直线拟合示例。

dev_close_window ()
read_image(Image, 'screw_thread')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
threshold (Image, Region, 0, 120)
closing_rectangle1 (Region, RegionClosing, 5, 5)
*进行仿射变换
hom_mat2d_identity (HomMat2DIdentity)
area_center (RegionClosing, Area, Row, Column)
orientation_region (RegionClosing, Phi)
hom_mat2d_rotate (HomMat2DIdentity, -Phi, Row, Column, HomMat2DRotate)
affine_trans_image (Image, ImageAffinTrans, HomMat2DRotate, 'constant', 'false')
*选择感兴趣的区域
rectangle1_domain (ImageAffinTrans, ImageReduced, 364, 100, 632, 900)
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 30, 40)
select_contours_xld (Edges, SelectedContours, 'contour_length', 700, 1000, -0.5, 0.5)
count_obj (SelectedContours, Number)
if(Number!=2)
    stop ()
else
    *进行直线拟合
    fit_line_contour_xld (SelectedContours, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
*生成直线
    gen_region_line (RegionLines, RowBegin, ColBegin, RowEnd, ColEnd)
    dev_clear_window ()
    dev_display (ImageReduced)
    dev_display (RegionLines)
endif

效果。

原图                                                                   直线拟合

2,圆拟合示例。

dev_close_window ()
read_image (Image, 'earth')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
dev_open_window (0, 0, Width*2, Height*2, 'black', WindowHandle)
dev_display (GrayImage)
threshold (GrayImage, Regions, 60, 255)
closing_circle (Regions, RegionClosing, 10.5)
*获取轮廓
gen_contour_region_xld (RegionClosing, Contours, 'border')
*代数距离测度拟合,geotukey拟合更精准,其不受圆残缺的影响
fit_circle_contour_xld (Contours, 'atukey', -1, 2, 0, 10, 1, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
*几何距离测度
fit_circle_contour_xld (Contours, 'geotukey', -1, 0, 0, 3, 2, Row1, Column1, Radius1, StartPhi1, EndPhi1, PointOrder1)
*绘制圆
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, 6.28318, 'positive', 1)
gen_circle_contour_xld (ContCircle1, Row1, Column1, Radius1, 0, 6.28318, 'positive', 1)

dev_clear_window ()
dev_display (GrayImage)
dev_display (ContCircle)
dev_set_color ('green')
dev_display (ContCircle1)

效果

原图                                                             圆拟合

3,矩形拟合示例。

read_image (Image, 'punched_holes')
fast_threshold (Image, Region, 128, 255, 20)
boundary (Region, RegionBorder, 'inner')
dilation_rectangle1 (RegionBorder, EdgeRoi, 7, 7)
reduce_domain (Image, EdgeRoi, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 1.7, 40, 120)
select_contours_xld (Edges, SelectedContours, 'contour_length', 500, 100000, -0.5, 0.5)
 
*进行矩形拟合
fit_rectangle2_contour_xld (SelectedContours, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
*绘制矩形
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)
dev_clear_window ()
dev_display (Image)
dev_display (Rectangle)

效果

原图                                                                矩形拟合

4,轮廓排序示例。

dev_close_window ()
read_image (Image, 'pumpe')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
fast_threshold (Image, Region, 0, 70, 150)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['outer_radius','anisometry','area'], 'and', [5,1,100], [50,1.8,5000])
shape_trans (SelectedRegions, RegionTrans, 'outer_circle')
dilation_circle (RegionTrans, RegionDilation, 5.5)
union1 (RegionDilation, RegionUnion)
reduce_domain (Image, RegionUnion, ImageReduced)
stop ()
emphasize (ImageReduced, ImageEmphasize, 6, 5, 1)
threshold_sub_pix (ImageReduced, Border, 80)
*选择轮廓
select_shape_xld (Border, SelectedXLD, ['contlength','outer_radius'], 'and', [70,15], [9999,9999])
*将轮廓分解为直线,圆弧等片段
segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 4, 2, 2)
select_shape_xld (ContoursSplit, SelectedXLD1, ['outer_radius','contlength'], 'and', [15,30], [45,99999])
*合并属于同于圆周的轮廓
union_cocircular_contours_xld (SelectedXLD1, UnionContours, 0.5, 0.1, 0.2, 30, 10, 10, 'true', 1)
*对其进行排序
sort_contours_xld (UnionContours, SortedContours, 'upper_left', 'true', 'column')
dev_clear_window ()
dev_display (ImageReduced)
dev_display (Image)
count_obj (SortedContours, Number)

for Index := 1 to Number by 1
    *获取轮廓的拟合
    select_obj (SortedContours, ObjectSelected, Index)
    fit_circle_contour_xld (Object
<think>好的,我现在需要解决用户的问题:如何使用Halcon计算同心圆轮廓之间的距离。首先,我需要回顾一下用户之前的提问和我的回答,确保这次回答保持一致,并且参考用户提供的引用内容。 用户之前的问题是关于Halcon轮廓旋转的方法,我给出了三种方法,包括绕原点、绕中心旋转和复合变换。现在用户的新问题是如何计算同心圆轮廓之间的距离。用户还提供了两个引用,分别是关于圆度误差的测量方法和使用select_shape算子选择圆形区域的方法。 首先,我需要明确“同心圆轮廓之间的距离”具体指什么。通常,同心圆之间的距离可能是两个圆之间的半径差。但用户可能指的是两个同心圆轮廓之间的径向距离,或者沿着某一方向的测量距离。需要进一步确认,但根据用户提供的引用,可能涉及到圆度误差中的同心圆半径差,所以可能用户需要的是两个同心圆之间的平均半径差或最大/最小半径差。 接下来,我需要考虑Halcon中处理同心圆轮廓的方法。可能的步骤包括: 1. 提取同心圆轮廓:使用边缘检测或阈值分割等方法获取两个同心圆的轮廓。 2. 拟合圆:使用拟合圆算子(如fit_circle_contour_xld)来得到两个圆的参数(圆心坐标和半径)。 3. 计算半径差:根据两个圆的半径计算它们之间的距离,即半径差。 用户提供的引用中提到了select_shape算子,可以用于筛选具有特定特征的区域,比如圆度较高的区域,这可能帮助用户更准确地选择同心圆轮廓。此外,引用中提到的外接圆和内接圆方法可能与圆度误差有关,但用户的问题是关于距离计算,所以可能需要调整这些方法。 可能的实现步骤: 1. 图像预处理:读取图像,进行滤波、阈值分割等操作,提取出同心圆区域。 2. 分割轮廓:使用edges_sub_pix或threshold_sub_pix提取亚像素精度的轮廓。 3. 选择同心圆:可能使用select_shape根据面积、圆度等参数筛选出内外圆轮廓。 4. 拟合圆:对每个轮廓进行圆拟合,得到圆心和半径。 5. 计算距离:计算两个圆的半径差,作为同心圆之间的距离。 需要注意的问题: - 轮廓提取的准确性:可能需要亚像素边缘检测来提高精度。 - 圆心是否一致:确保两个圆确实是同心的,否则可能需要先计算平均圆心或处理非同心的情况。 - 多个轮廓的处理:如果图像中有多个同心圆,需要正确配对内外圆。 用户提供的引用[2]中提到了select_shape算子,可以用'circularity'来筛选圆形的区域,这可能帮助用户排除非圆形的干扰区域,确保处理的是正确的同心圆。引用[1]中的外接圆和内接圆方法可能用于圆度误差的计算,但用户的问题是关于距离,所以可能需要结合这些方法,比如使用最小外接圆和最大内接圆来辅助确定同心圆的半径。 可能的代码结构: - 读取图像并预处理 - 提取轮廓 - 使用select_shape筛选圆形区域 - 根据面积或其他特征区分内外圆 - 拟合圆并计算半径差 此外,用户可能需要可视化结果,比如显示轮廓和圆心,以及输出半径差。需要确保代码示例清晰,并解释关键步骤。 需要验证是否有其他方法,比如直接测量轮廓点与圆心之间的距离,但拟合圆可能更直接准确。同时,考虑亚像素精度对结果的影响,确保使用合适的算子如fit_circle_contour_xld。 总结,解决步骤应包括预处理、轮廓提取、筛选、拟合和计算,结合用户提供的引用来优化筛选过程,确保准确选择同心圆轮廓,并正确计算它们之间的距离。</think>在Halcon中计算同心圆轮廓间距需要结合轮廓提取、圆拟合和几何计算,以下是具体实现方法和代码示例: ### 一、核心实现步骤 #### 1. 轮廓提取与筛选 ```python * 亚像素边缘检测 edges_sub_pix(Image, Edges, 'canny', 1.5, 20, 40) * 筛选闭合轮廓(同心圆通常为闭合区域) select_shape_xld(Edges, SelectedContours, 'contlength', 'and', 100, 99999) * 根据圆度筛选圆形轮廓[^2] select_shape_xld(SelectedContours, CircularContours, 'circularity', 'and', 0.9, 1.0) ``` #### 2. 区分内外圆 ```python * 按面积排序获取内外圆 area_center_xld(CircularContours, Areas, _, Rows, Columns) tuple_sort_index(Areas, Indices) smallest_index := Indices[0] largest_index := Indices[|Indices|-1] inner_contour := CircularContours[smallest_index] outer_contour := CircularContours[largest_index] ``` #### 3. 亚像素圆拟合 ```python * 内圆拟合 fit_circle_contour_xld(inner_contour, 'algebraic', -1, 0, 0, 3, 2, inner_row, inner_col, inner_radius, _, _) * 外圆拟合 fit_circle_contour_xld(outer_contour, 'algebraic', -1, 0, 0, 3, 2, outer_row, outer_col, outer_radius, _, _) ``` #### 4. 间距计算与验证 ```python * 计算同心圆间距 distance := outer_radius - inner_radius * 验证同心性(误差应小于1像素) center_distance := distance_pp(inner_row, inner_col, outer_row, outer_col) if (center_distance > 1.0) dev_error_var('检测到非同心圆结构', 1) endif ``` ### 二、数学原理 同心圆间距计算公式: $$ \Delta R = R_{outer} - R_{inner} $$ 坐标一致性验证公式: $$ \sqrt{(x_{outer}-x_{inner})^2 + (y_{outer}-y_{inner})^2} \leq \varepsilon $$ (其中$\varepsilon$为允许的圆心偏移阈值) ### 三、可视化方法 ```python * 绘制拟合结果 dev_set_color('green') dev_display(inner_contour) dev_set_color('red') dev_display(outer_contour) * 标注测量结果 set_tposition(3600, 10, 10) write_string(3600, '同心圆间距:'+distance+' pix') ``` ### 四、精度提升技巧 1. 使用$edges_sub_pix$获取亚像素轮廓[^1] 2. 采用$fit_circle_contour_xld$的'geometric'拟合模式 3. 多次测量取均值: ```python for i := 1 to 5 by 1 * 执行测量循环 endfor avg_distance := total_distance/5.0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值