0
点赞
收藏
分享

微信扫一扫

Halcon实现卡尺找圆


  • 摘要 Halcon使用metrology相关算子实现卡尺找圆功能。

前言

  • 纪录最近学习的halcon算子,如何使用算子实现卡尺找圆功能。

实现步骤:

1. 图像初始化与预处理:

  • 窗口初始化:关闭现有窗口,创建新的显示窗口(512×512,黑色背景)。
  • 读取图像:加载目标图像并显示。
  • 灰度转换:将彩色图像转为灰度图像(rgb1_to_gray)。

2. 交互式圆形区域选择

  • 通过 draw_circle 绘制一个粗略的圆形区域(圆心 (Row, Column) 和半径 Radius),作为卡尺测量的初始位置。

3. 创建计量模型(Metrology Model)

  • 初始化模型:create_metrology_model 创建一个空的计量模型容器。
  • 使用add_metrology_object_circle_measure 添加圆形测量对象,将绘制的圆添加到模型中。
  • 关键参数包括:
  • 20:卡尺数量(沿圆周均匀分布的测量点)。
  • 5:卡尺宽度(每个测量区域的宽度)。
  • 1:卡尺高度(每个测量区域的高度)。
  • 30:边缘幅度阈值(过滤弱边缘)。

4. 执行测量与边缘检测

  • 应用模型到图像:apply_metrology_model 在灰度图像上执行测量,通过卡尺沿初始圆周边界搜索真实边缘点。
  • 获取测量结果:
  • get_metrology_object_measures:提取所有卡尺的测量点坐标(Row1, Column1)。
  • get_metrology_object_result:获取拟合后的精确圆参数(圆心坐标、半径)。

5. 结果显示

  • 轮廓提取与显示:get_metrology_object_result_contour 生成拟合圆的轮廓。
  • 用绿色(dev_set_color)高亮显示最终检测到的圆(dev_display)。

预览

运行效果

Halcon实现卡尺找圆_Halcon

代码

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
*读取图像
read_image (Image, 'D:/Image/1.png')
*显示图像
dev_display (Image)
*图像灰度转换
rgb1_to_gray (Image, GrayImage)
*绘制圆形
draw_circle (WindowHandle, Row, Column, Radius)
*创建计量模型
create_metrology_model (MetrologyHandle)
*添加圆形计量模型
add_metrology_object_circle_measure (MetrologyHandle, Row, Column, Radius, 20, 5, 1, 30, [], [], Index)
*获取计量对象模型轮廓
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index, 1.5)
*获取计量模型测量结果
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)
*应用计量模型到图像
apply_metrology_model (GrayImage, MetrologyHandle)
*获取计量模型结果对象
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'all_param', Parameter)
*获取计量模型对象结果轮廓
get_metrology_object_result_contour (Contour, MetrologyHandle, Index, 'all', 1.5)
*显示轮廓
dev_set_color ('green')
dev_display (Contour)

官方注释代码(使用软件翻译)

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'D:/Image/1.png')
dev_display (Image)
**灰度转换
rgb1_to_gray (Image, GrayImage)

draw_circle (WindowHandle, Row, Column, Radius)
**Create the data structure that is needed to measure geometric shapes.
**创建计量模型:创建测量几何形状所需的数据结构,
create_metrology_model (MetrologyHandle)

** Add a circle or a circular arc to a metrology model.
****添加圆测量计量对象:添加一个圆或圆弧到计量模型,输出Index
**MetrologyHandle:计量模型句柄
*Row :所在行
*Column :所在列
*Radius :半径
*MesureLength1 :测量长度1
*MesureLength2 :测量长度2
*MesureSigma :测量平滑系数
*MesureThreshold :测量阈值
*GenparamName :参数名
*GenparamValue :参数值
*Index :索引
add_metrology_object_circle_measure (MetrologyHandle, Row, Column, Radius, 20, 5, 1, 30, [], [], Index)


****Query the model contour of a metrology object in image coordinates.
****获取计量对象模型轮廓: 在图像坐标系中查询计量对象的模型轮廓
**Contour :输出轮廓
**MetrologyHandle:计量模型句柄
**Index : 计量对象索引(Index of the metrology object.)
**Resolution:相邻轮廓点之间的距离(Distance between neighboring contour points.)。
get_metrology_object_model_contour (Contour1, MetrologyHandle, Index, 1.5)

**Get the measure regions and the results of the edge location for the metrology objects of a metrology model.
**获取一个计量模型中,计量对象的测量区域和边缘位置定位结果 
**Contours :测量区域的矩形xld轮廓(Rectangular XLD Contours of measure regions.)
**MetrologyHandle :计量对象句柄
**Index :计量对象索引
**Transition :极性选择: 任意极性/由暗到明(0->255)/由明到暗(255->0)(Select light/dark or dark/light edges.)
**Row1:所在行
**Column1:所在列
get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1)

**Measure and fit the geometric shapes of all metrology objects of a metrology model.
**应用计量模型:测量和拟合一个计量模型中所有计量对象的几何形状
**确定边缘位置
**拟合边缘位置几何形状
apply_metrology_model (GrayImage, MetrologyHandle)

****get the results of the measurement of a metrology model.
****获取计量对象结果:获取计量模型的测量结果
**MetrologyHandle:计量句柄
**Index:计量索引
**Instance:计量对象实例(Instance of the metrology object.)
**GenParamName:通用参数名
**GenParamValue:通用参数值
**Parameter:返回结果
get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'all_param', Parameter)
* Query the result contour of a metrology object.
*获取计量对象结果轮廓:查询计量对象的结果轮廓
**Contour :获取的结果轮廓
**MetrologyHandle:计量句柄
**Instance:计量对象实例(Instance of the metrology object.)
**Resolution:相邻轮廓点之间的距离(Distance between neighboring contour points.)。
get_metrology_object_result_contour (Contour, MetrologyHandle, Index, 'all', 1.5)
dev_set_color ('green')
dev_display (Contour)

结语

  • 既是分享,也是备份。

最后





举报

相关推荐

0 条评论