073104_halcon的算子与函数调用

写在最前:个人学习记录用,有疏漏欢迎补充。

00-行文思路:

halcon中的算子是什么?格式是什么样的?怎么写一个算子?算子怎么调用?

随便写一句开始了!


01-算子是什么?

算子(operator)是什么:halcon团队封装,对图像进行处理,可实现某些功能的算法。

02-算子格式:

2.1-格式

格式:算子名称(图像输入参数图像输出参数控制输入参数控制输出参数)

例子:threshold(Image,Region,100,255) //对图像二值化,结果存入Region

输入:Image(图像)、100(灰度下限)、255(灰度上限)

输出:Region(二值区域)

注意:参数间用逗号分隔,部分算子支持默认参数

2.2-调用:

HDevelop脚本:直接输入算子,按回车执行。

read_image(Image, 'ic0.jpg') // 读取图像

threshold(Image, Region, 128, 255) // 阈值分割

disp_image(Image, '原始图像') // 显示图像

外部语言集成(如Python):通过库函数调用。

import halcon as ha

image = ha.read_image('particle.jpg')

region = ha.threshold(image, 128, 255)

03-怎样写一个算子?

3.1-基于HDevelop环境

打开算子创建向导
在HDevelop中选择 **Tools**** → ****Create Operator**,启动自定义算子向导。

  1. 定义算子接口
    • 输入/输出参数:通过向导设置参数名称、类型(如ImageRegionNumber等)及默认值(如'auto')。
    • 示例:定义一个“圆形区域裁剪”算子,输入参数为Image(原图)、Center(圆心坐标)、Radius(半径),输出为CroppedImage(裁剪后的图像)。
  2. 编写算子实现代码
    • 在向导生成的脚本框架中,调用Halcon基础算子完成逻辑。
    • 示例代码(圆形裁剪):
* 输入参数:Image, Center, Radius  
gen_circle (Circle, Center, Radius, 0)  // 生成圆形轮廓  
reduce_domain (Image, Circle, CroppedImage)  // 裁剪图像
  1. 编译与测试
    • 点击 **Compile** 编译算子,若无错误则可在HDevelop中直接调用4。
    • 测试时输入示例数据(如read_image(Image, 'test.jpg')),验证输出是否符合预期。

3.2-封装为库函数

若需跨程序复用,可将算子封装为库函数

  1. 在向导中选择 **Library Function** 类型,指定存储路径。
  2. 通过 **Manage Functions**** → ****Add Library** 加载库文件,之后可在任意HDevelop脚本中调用。

04-场景举例

4.1 算子分类与实例

类别 算子名称 功能 典型参数 示例
图像输入/输出 <font style="color:rgba(0, 0, 0, 0.86);">read_image</font> 读取图像文件 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
(输出图像变量)、文件路径
<font style="color:rgba(0, 0, 0, 0.86);">read_image(Image, 'test.jpg')</font>
<font style="color:rgba(0, 0, 0, 0.86);">write_image</font> 保存图像 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
、文件路径、格式(如<font style="color:rgba(0, 0, 0, 0.86);">'bmp'</font>
<font style="color:rgba(0, 0, 0, 0.86);">write_image(Image, 'output.bmp')</font>
图像预处理 <font style="color:rgba(0, 0, 0, 0.86);">gauss_filter</font> 高斯滤波去噪 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
(输入)、<font style="color:rgba(0, 0, 0, 0.86);">Sigma</font>
(标准差)
<font style="color:rgba(0, 0, 0, 0.86);">gauss_filter(GrayImage, SmoothedImage, 1.5)</font>
<font style="color:rgba(0, 0, 0, 0.86);">rgb1_to_gray</font> RGB转灰度 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
(输入)、<font style="color:rgba(0, 0, 0, 0.86);">GrayImage</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">rgb1_to_gray(Image, GrayImage)</font>
区域处理 <font style="color:rgba(0, 0, 0, 0.86);">threshold</font> 阈值分割 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
<font style="color:rgba(0, 0, 0, 0.86);">Region</font>
(输出)、灰度范围
<font style="color:rgba(0, 0, 0, 0.86);">threshold(GrayImage, Region, 100, 255)</font>
<font style="color:rgba(0, 0, 0, 0.86);">connection</font> 连通区域分析 <font style="color:rgba(0, 0, 0, 0.86);">Region</font>
<font style="color:rgba(0, 0, 0, 0.86);">ConnectedRegions</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">connection(Region, ConnectedRegions)</font>
特征提取 <font style="color:rgba(0, 0, 0, 0.86);">edges_sub_pix</font> 亚像素级边缘检测 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
<font style="color:rgba(0, 0, 0, 0.86);">Edges</font>
(输出)、边缘类型
<font style="color:rgba(0, 0, 0, 0.86);">edges_sub_pix(GrayImage, Edges, 'canny')</font>
<font style="color:rgba(0, 0, 0, 0.86);">harris_corner</font> Harris角点检测 <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
<font style="color:rgba(0, 0, 0, 0.86);">Corners</font>
(输出)、窗口大小
<font style="color:rgba(0, 0, 0, 0.86);">harris_corner(GrayImage, Corners, 5, 5, 0.04)</font>
形状匹配 <font style="color:rgba(0, 0, 0, 0.86);">create_shape_model</font> 创建形状模板 <font style="color:rgba(0, 0, 0, 0.86);">ReferenceImage</font>
<font style="color:rgba(0, 0, 0, 0.86);">ModelID</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">create_shape_model(Image, 'auto', 0, rad(360), ModelID)</font>
<font style="color:rgba(0, 0, 0, 0.86);">find_shape_model</font> 匹配形状模板 <font style="color:rgba(0, 0, 0, 0.86);">SearchImage</font>
<font style="color:rgba(0, 0, 0, 0.86);">ModelID</font>
<font style="color:rgba(0, 0, 0, 0.86);">Row/Column/Score</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">find_shape_model(Image, ModelID, 0, rad(360), 0.7, Row, Column, Angle, Score)</font>
测量与几何变换 <font style="color:rgba(0, 0, 0, 0.86);">fit_circle_contour_xld</font> 拟合圆形轮廓 <font style="color:rgba(0, 0, 0, 0.86);">Contours</font>
(输入)、<font style="color:rgba(0, 0, 0, 0.86);">Row/Column/Radius</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">fit_circle_contour_xld(Edges, 'geotukey', -1, 2, 0, 10, 2, Row, Column, Radius)</font>
<font style="color:rgba(0, 0, 0, 0.86);">affine_trans_image</font> 仿射变换(平移+旋转) <font style="color:rgba(0, 0, 0, 0.86);">Image</font>
<font style="color:rgba(0, 0, 0, 0.86);">HomMat2D</font>
(变换矩阵)、<font style="color:rgba(0, 0, 0, 0.86);">ImageAffine</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">affine_trans_image(Image, ImageAffine, HomMat2D, 'false')</font>
机器学习 <font style="color:rgba(0, 0, 0, 0.86);">train_svm</font> 训练支持向量机分类器 <font style="color:rgba(0, 0, 0, 0.86);">Features</font>
(特征)、<font style="color:rgba(0, 0, 0, 0.86);">Labels</font>
(标签)、<font style="color:rgba(0, 0, 0, 0.86);">SVMHandle</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">train_svm(Features, Labels, SVMHandle)</font>
3D视觉 <font style="color:rgba(0, 0, 0, 0.86);">read_object_model_3d</font> 读取3D模型 <font style="color:rgba(0, 0, 0, 0.86);">FileName</font>
(模型路径)、<font style="color:rgba(0, 0, 0, 0.86);">ModelID</font>
(输出)
<font style="color:rgba(0, 0, 0, 0.86);">read_object_model_3d('model.3d', ModelID)</font>

4.2 算子场景

场景 自定义算子功能 核心逻辑 输入/输出
ROI区域提取 按用户绘制的ROI裁剪图像 reduce_domain(Image, ROI, Result) Image(输入)、
ROI(用户绘制区域)、
Result(裁剪后图像)
特定缺陷检测 检测图像中的划痕(基于边缘特征) edges_sub_pix(GrayImage, Edges, 'canny') select_shape(Edges, Scratches, 'length', 'and', 50, 1000) GrayImage(灰度图)、
Scratches(输出缺陷区域)
多模板匹配 批量匹配不同角度的零件模板 循环调用
create_shape_model
+find_shape_model
,合并结果
ReferenceImages
(模板库)、SearchImage
(待检图像)、Matches
(输出匹配位置)

⚠️注意事项:

  • 参数类型
  • 图像用Image类型变量,区域用Region,轮廓用XLD,数值用NumberTuple
  • 部分算子支持'auto'参数(如create_shape_modelAngleStart/AngleExtent)。
  • 调试工具
  • HDevelop中可通过dev_display显示中间结果,或用disp_message输出调试信息。
  • 资源管理
  • 及时释放对象(如clear_image(Image)),避免内存泄漏

05-参考

  1. HDevelop帮助文档:内置“Operator Creation”章节,包含图形化教程和代码示例
  2. Halcon安装目录下的examples文件夹中包含多个自定义算子案例(如custom_operator.hdev
  3. https://juejin.cn/post/7223653429356101690
  4. https://blog.51cto.com/hopeblaze/12830913
posted @ 2025-07-31 15:02  黛色星霜  阅读(125)  评论(0)    收藏  举报