news 2026/2/28 8:22:07

【Halcon-2D测量】get_metrology_object_result_contour 函数功能(用于获取计量对象实例的结果轮廓(XLD)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【Halcon-2D测量】get_metrology_object_result_contour 函数功能(用于获取计量对象实例的结果轮廓(XLD)

HALCON算子get_metrology_object_result_contour全解析

一、算子核心定位

get_metrology_object_result_contour是HALCON 2D计量(2D Metrology)模块的可视化核心算子,核心功能是获取经apply_metrology_model拟合后的计量对象实例的结果轮廓(XLD形式)——它基于拟合后的几何参数(如圆的圆心/半径、矩形的中心/角度/尺寸)生成对应形状的亚像素XLD轮廓,是将抽象的测量数值转化为直观可视化轮廓的关键工具,常用于验证拟合结果的形状/位置是否符合预期。

二、算法核心原理

  1. 有效性校验:验证输入的计量模型句柄(MetrologyHandle)、计量对象索引(Index)、实例编号(Instance)的有效性,确保指向已完成拟合的计量对象/实例;
  2. 拟合参数读取:从计量模型中读取指定对象/实例的拟合几何参数(如圆的row/column/radius、矩形的row/column/phi/length1/length2);
  3. 轮廓生成:根据几何参数和Resolution(相邻轮廓点距离),生成对应形状的亚像素XLD轮廓:
    • 圆形/椭圆:按分辨率均匀生成圆周/椭圆周的轮廓点;
    • 矩形/直线:按分辨率生成边的轮廓点;
  4. 分辨率约束:若输入的Resolution低于最小有效值(1.192e-7),自动将其重置为该最小值;
  5. 结果返回:将生成的XLD轮廓按“先第一个对象的所有实例,再第二个对象的所有实例”的顺序封装为数组,输出到Contour参数;
  6. 无修改逻辑:仅生成并返回轮廓,不改变计量模型/对象的任何属性。

三、参数全详解

(一)输入参数(Input Parameters)

参数名类型功能说明默认值关键取值规则注意事项
MetrologyHandlemetrology_model → (handle)指定要查询的2D计量模型句柄-必须为已有效创建/加载的计量模型句柄1. 需通过create_metrology_model生成,未被clear_metrology_model释放;
2. 无效句柄直接抛出“句柄无效”异常
Indexinteger(-array) → (integer / string)指定要查询的计量对象索引0可选值:
▪ ‘all’:查询模型内所有计量对象的轮廓;
▪ 整数/整数数组:查询指定索引的计量对象(如0、[0,1])
整数索引必须是get_metrology_object_indices返回的有效索引,否则抛异常
Instanceinteger(-array) → (string / integer)指定要查询的实例编号‘all’可选值:
▪ ‘all’:查询指定计量对象的所有实例轮廓;
▪ 整数/整数数组:查询指定实例(如0、[0,1])
1. 实例编号从0开始,需≤get_metrology_object_num_instances返回的实例数-1;
2. 若实例不存在(如指定1但仅检测到1个实例),抛异常
Resolutionreal → (real)轮廓相邻点的欧氏距离(像素)1.5取值规则:
▪ 最小值:1.192e-7;
▪ 推荐值:1.0~5.0(兼顾精度与效率);
▪ 约束:Resolution ≥ 1.192e-7
1. 值越小,轮廓点越多、形状越精细,但生成速度越慢;
2. 低于最小值时,算子自动重置为1.192e-7

(二)输出参数(Output Parameters)

参数名类型功能说明关联说明
Contourxld_cont(-array) → object返回拟合后的XLD轮廓数组1. 每个实例对应一个XLD轮廓;
2. 轮廓坐标为图像像素坐标(不受相机参数影响);
3. 顺序:先第一个计量对象的所有实例,再第二个计量对象的所有实例;
4. 可直接用dev_display可视化

四、使用关键注意事项

  1. 执行时序约束:必须在调用apply_metrology_model(完成拟合)后调用本算子,否则Contour返回空数组;若拟合未找到目标(实例数为0),也返回空数组;
  2. 分辨率权衡
    • 高分辨率(小值,如0.5):轮廓更精细,适合高精度可视化,但生成耗时更长;
    • 低分辨率(大值,如5.0):轮廓点数少,生成快,适合快速预览;
    • 默认值1.5是“精度-效率”的平衡值,满足大部分场景需求;
  3. 坐标特性:生成的轮廓始终以像素为单位,即使设置了相机参数/测量平面(公制坐标),也不会转换为公制,仅反映图像中的像素位置;
  4. 多线程特性
    • 多线程类型:可重入(能与非排他算子并行运行);
    • 多线程范围:全局(可从任意线程调用);
    • 无并行优化:仅单线程生成轮廓,无性能加速;
  5. 返回值规则:执行成功返回2(H_MSG_TRUE),参数无效(如索引/实例错误、Resolution低于最小值且未自动修正)时直接抛出异常。

五、算子调用链路

(一)前置算子(Possible Predecessors)

  • create_metrology_model:创建空的2D计量模型(基础前置);
  • add_metrology_object_*(如add_metrology_object_rectangle2_measure/add_metrology_object_circle_measure):向模型添加计量对象;
  • apply_metrology_model:执行检测拟合(生成轮廓的必要前置);
  • get_metrology_object_num_instances:(可选)获取实例数,用于精准指定Instance

(二)后置算子(Possible Successors)

  • dev_display:可视化显示轮廓,验证拟合结果的形状/位置;
  • gen_region_contour_xld:将XLD轮廓转化为区域,用于后续形态学操作;
  • clear_metrology_model:释放计量模型句柄(收尾操作)。

六、与相似算子的核心差异

算子名称核心区别适用场景
get_metrology_object_result_contour返回拟合后的目标轮廓XLD(基于拟合参数生成)可视化拟合结果的形状/位置、验证拟合精度
get_metrology_object_result返回拟合后的数值参数(如圆心、半径、角度)获取精准的测量数值、用于尺寸判定/计算
get_metrology_object_measures返回测量区域轮廓+原始边缘点坐标(无拟合逻辑)调试测量区域范围、验证原始边缘检测结果

七、典型应用示例(HDevelop 代码)

*This example shows the usage of the metrology model*to measure circles and rectangles with subpixel*accuracy under challenging conditions easily.**Display initializationsdev_update_off()read_image(Image,'pads')get_image_size(Image,Width,Height)dev_close_window()dev_open_window_fit_image(Image,0,0,-1,-1,WindowHandle)set_display_font(WindowHandle,14,'mono','true','false')**Define the approximate position and the measure*toleranceforthe circles RowCircle:=[52:89:500]CircleInitRow:=[RowCircle,RowCircle]CircleInitColumn:=[gen_tuple_const(6,348),gen_tuple_const(6,438)]gen_cross_contour_xld(Cross1,CircleInitRow,CircleInitColumn,6,0.785398)CircleInitRadius:=[gen_tuple_const(6,23),gen_tuple_const(6,23)]CircleRadiusTolerance:=12*Define the approximate position and the measure*toleranceforthe rectangles2 RectangleInitRow:=[410,410]RectangleInitColumn:=[215,562]RectangleInitPhi:=[0,0]RectangleInitLength1:=[85,85]RectangleInitLength2:=[88,88]RectangleTolerance:=10**Prepare the metrology model data structurecreate_metrology_model(MetrologyHandle)*Setting the image width in advance is not*necessary,but improves the runtime of the*first measurement.set_metrology_model_image_size(MetrologyHandle,Width,Height)*Add the metrology rectangle objects to the model*as defined aboveadd_metrology_object_rectangle2_measure(MetrologyHandle,RectangleInitRow,RectangleInitColumn,RectangleInitPhi,RectangleInitLength1,RectangleInitLength2,RectangleTolerance,5,.5,1,[],[],MetrologyRectangleIndices)*Add the metrology circle objects to the model*as defined aboveadd_metrology_object_circle_measure(MetrologyHandle,CircleInitRow,CircleInitColumn,CircleInitRadius,CircleRadiusTolerance,5,1.5,2,[],[],MetrologyCircleIndices)*It is possible to measure more than one circle/rectangle/line/ellipse*instance per metrology object in one call.*Since we like to measure two circles per object,*we set'num_instances'to2.set_metrology_object_param(MetrologyHandle,MetrologyCircleIndices,'num_instances',2)*Setting'measure_transition'to'uniform'assures*that only consistent circles are returned,that have*either only edges from bright to dark or vice versa.*Since the consistency check increases runtime,it is*switched of bydefault.*In this example however,it is safer toswitchit on,*because both negative and positive edges are present.set_metrology_object_param(MetrologyHandle,MetrologyCircleIndices,'measure_transition','uniform')*Setting the minimum score can make the results more robustset_metrology_object_param(MetrologyHandle,MetrologyCircleIndices,'min_score',.9)**Perform the measurement*apply_metrology_model(Image,MetrologyHandle)*get_metrology_object_result(MetrologyHandle,MetrologyRectangleIndices,'all','result_type','all_param',RectangleParameter)*Extract the parametersforbetter readability Sequence:=[0:5:|RectangleParameter|-1]RectangleRow:=RectangleParameter[Sequence]RectangleColumn:=RectangleParameter[Sequence+1]RectanglePhi:=RectangleParameter[Sequence+2]RectangleLength1:=RectangleParameter[Sequence+3]RectangleLength2:=RectangleParameter[Sequence+4]**Access the results of the circle measurementget_metrology_object_result(MetrologyHandle,MetrologyCircleIndices,'all','result_type','all_param',CircleParameter)*Extract the parametersforbetter readability Sequence:=[0:3:|CircleParameter|-1]CircleRow:=CircleParameter[Sequence]CircleColumn:=CircleParameter[Sequence+1]CircleRadius:=CircleParameter[Sequence+2]**Display the results**Get measured contoursget_metrology_object_result_contour(Contours,MetrologyHandle,'all','all',1.5)*Get the contours of the measure regions*and the coordinates of the edge points*that were the basisforfitting the circles and rectanglesget_metrology_object_measures(Contour,MetrologyHandle,'all','all',Row1,Column1)gen_cross_contour_xld(Cross,Row1,Column1,6,0.785398)*Display everything Color:=['gray','cyan','green']dev_display(Image)dev_set_line_width(1)dev_set_color(Color[0])dev_display(Contour)dev_set_color(Color[1])dev_display(Cross)dev_set_line_width(2)dev_set_color(Color[2])dev_display(Contours)Message:=Color[2]+': Measurement result'Message[1]:=Color[1]+': Edge candidate points'Message[2]:=Color[0]+': Measure regions'disp_message(WindowHandle,Message,'window',12,12,'black','true')stop()*Clean up memoryclear_metrology_model(MetrologyHandle)

八、总结

关键点回顾

  1. get_metrology_object_result_contour核心作用是基于拟合参数生成可视化XLD轮廓,是将数值测量结果转化为直观形状的关键工具;
  2. Resolution控制轮廓精细度(值越小越精细),最小值为1.192e-7,低于该值会自动重置;
  3. 轮廓始终以像素坐标返回,需在apply_metrology_model后调用,否则返回空轮廓。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/22 15:08:28

终极轻量级CSS框架Chota:3kb实现专业前端开发

终极轻量级CSS框架Chota:3kb实现专业前端开发 【免费下载链接】chota A micro (3kb) CSS framework 项目地址: https://gitcode.com/gh_mirrors/ch/chota 在当今前端开发领域,框架臃肿化已成为普遍问题,而Chota轻量级CSS框架以其仅3kb…

作者头像 李华
网站建设 2026/2/24 3:16:39

Fabric8 Kubernetes Java客户端:云原生开发的终极利器

Fabric8 Kubernetes Java客户端:云原生开发的终极利器 【免费下载链接】kubernetes-client Java client for Kubernetes & OpenShift 项目地址: https://gitcode.com/gh_mirrors/ku/kubernetes-client 在当今云原生技术蓬勃发展的时代,Kuber…

作者头像 李华
网站建设 2026/2/22 14:29:01

调研也能“秒出专业问卷”?百考通AI平台,让每个人都是调研高手!

还在为设计问卷熬夜改稿?明明有清晰的研究问题,却不知如何转化为科学、中立、逻辑严谨的题目?担心措辞不当引发偏差,或结构混乱导致填写中断?别再让这些技术门槛阻碍你的学术进展或项目落地!百考通全新升级…

作者头像 李华
网站建设 2026/2/23 6:13:27

Calflops终极指南:3分钟快速掌握深度学习模型FLOPs计算技巧

Calflops终极指南:3分钟快速掌握深度学习模型FLOPs计算技巧 【免费下载链接】calculate-flops.pytorch The calflops is designed to calculate FLOPs、MACs and Parameters in all various neural networks, such as Linear、 CNN、 RNN、 GCN、Transformer(Bert、…

作者头像 李华
网站建设 2026/2/26 5:32:44

Il2CppInspector 终极指南:Unity逆向工程的强力工具

Il2CppInspector是一款专为Unity IL2CPP逆向工程设计的自动化工具,能够帮助开发者和安全研究人员深入分析Unity游戏和应用的二进制结构。通过本指南,您将掌握使用Il2CppInspector进行高效逆向分析的核心技巧。 【免费下载链接】Il2CppInspector Powerful…

作者头像 李华