halcon 求外接圆的顶点
时间: 2025-05-31 16:51:21 浏览: 23
在 Halcon 中,计算形状的外接圆以及其顶点的方法可以通过一系列几何运算实现。以下是详细的说明和代码示例:
---
### 1. 外接圆的计算
Halcon 提供了多种工具来计算形状的外接圆。通常情况下,可以通过 `gen_circle_contour_xld` 函数绘制外接圆,并结合其他函数获取所需的参数。
#### 关键步骤:
- 使用 `draw_line_midnormal` 来生成三角形每一边的垂直平分线。
- 利用 `intersection_contours_xld` 寻找垂直平分线的交点作为外接圆的圆心。
- 计算任意一点到圆心的距离作为半径。
```hdevelop
* 绘制三条边的垂直平分线
draw_line_midnormal (RegionLines01, MidnormalContour1, LineLength)
draw_line_midnormal (RegionLines02, MidnormalContour2, LineLength)
draw_line_midnormal (RegionLines12, MidnormalContour3, LineLength)
* 获取两条垂直平分线的交点作为外接圆的圆心
intersection_contours_xld (MidnormalContour1, MidnormalContour2, 'mutual', RowCenter, ColumnCenter, IsOverlapping2)
* 设置颜色并显示交叉点
dev_set_color ('red')
disp_cross (WindowHandle, RowCenter[0], ColumnCenter[0], 20, 0)
* 计算圆心到某顶点的距离作为半径
distance_pp (Row[0], Column[0], RowCenter[0], ColumnCenter[0], DistanceR)
* 绘制外接圆
gen_circle_contour_xld (Circumcircle, RowCenter[0], ColumnCenter[0], DistanceR, 0, 6.28318, 'positive', 1)
disp_cross (WindowHandle, RowCenter, ColumnCenter, 20, 0)
dev_display (Circumcircle)
```
此方法基于几何原理,适用于已知三个顶点的情况[^3]。
---
### 2. 形状顶点的提取
如果目标形状并非简单的三角形或其他规则多边形,则需要先提取轮廓的关键点(即顶点)。这一步骤可通过以下两种方式完成:
#### 方法一:使用 `smallest_rectangle1`
对于凸多边形,可以直接调用 `smallest_rectangle1` 得到包围盒的四个角点。虽然这不是严格意义上的“顶点”,但在许多场景下已经足够接近实际需求。
```hdevelop
* 获取最小外接矩形的角点
smallest_rectangle1 (Contours, Row1, Column1, Row2, Column2, Phi)
* 显示结果
dev_set_color('green')
dev_draw_rectangle1(Row1, Column1, Row2, Column2, Phi)
```
#### 方法二:应用 `get_region_points`
对于不规则形状,可将其转换为区域后提取所有像素级坐标,再进一步筛选出符合条件的极值点。
```hdevelop
* 将轮廓转为区域
gen_region_contour_xld(Contours, Region, 'filled')
* 获取区域内所有的点
get_region_points(Region, RowPoints, ColPoints)
* 查找最左、最右、最高、最低点
row_min_max(RowPoints, MinRow, MaxRow)
column_min_max(ColPoints, MinCol, MaxCol)
```
以上操作能够有效定位形状的主要特征位置[^4]。
---
### 总结
综上所述,在 Halcon 环境下既可以通过构建垂直平分线求得精确的外接圆信息,也能借助内置算子快速估算对象的大致范围与关键部位分布状况。具体采用哪一种取决于应用场景的具体要求——前者适合处理少量固定结构的数据样本;后者更适合批量分析复杂形态的目标集合。
---
###
阅读全文
相关推荐












