news 2026/9/11 20:07:18

Halcon 定位加二位测量找直线

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Halcon 定位加二位测量找直线

图像:

代码:

dev_close_window () dev_open_window (0, 0, 512, 512, 'black', WindowHandle) *加载图像 read_image (Image,'//DESKTOP-V21FG2T/Share/2.bmp') threshold(Image,Region, 50, 100) connection(Region, ConnectedRegions) select_shape(ConnectedRegions, SelectedRegion, 'area', 'and', 100000, 9999999) * 获取最小外接矩形(水平矩形) smallest_rectangle1 (SelectedRegion, Row1, Column1, Row2, Column2) Width := Column2 - Column1 + 1 Height := Row2 - Row1 + 1 * 获取四条边的中心点 * 上边中心点 TopCenterRow := Row1 TopCenterCol := (Column2+Column1) / 2 * 下边中心点 BottomCenterRow := Row2 BottomCenterCol := (Column2+Column1) / 2 * 左边中心点 LeftCenterRow := (Row2+Row1) / 2 LeftCenterCol := Column1 * 右边中心点 RightCenterRow := (Row2+Row1) / 2 RightCenterCol := Column2 *界面操作得到测量区域,转到视野上画线 * draw_line (WindowHandle, Row1, Column1, Row2, Column2) * shapePararm:=[Row1,Column1,Row2,Column2] * 创建一个空元组来存储选择的对象 gen_empty_obj(EmptyObject) * 确定了测量区域的上线的中心和下线的中心 shapePararm:=[TopCenterRow,TopCenterCol,BottomCenterRow,BottomCenterCol] * 确定了测量区域的左线的中心和右线的中心 shapePararm1:=[LeftCenterRow,LeftCenterCol,RightCenterRow,RightCenterCol] params:=[shapePararm,shapePararm1] for i := 0 to 1 by 1 * 提取当前参数 *创建直线模型 create_metrology_model (MetrologyHandle) *设置直线模型图像大小,即需要被检测的图像大小 set_metrology_model_image_size (MetrologyHandle, Width, Height) *添加线模型通用测量对象 sharp参数设置为line Index:输出创建测量对象的索引值 if (i == 0) * 处理第一条参数(i=0) add_metrology_object_generic (MetrologyHandle, 'line', shapePararm, 700, 10, 1, 10, [], [], Index) elseif (i == 1) * 处理第二条参数(i=1) add_metrology_object_generic (MetrologyHandle, 'line', shapePararm1, 700, 10, 1, 10, [], [], Index) endif *找线方式,暗 / 亮(positive)或亮 / 暗(negative)边缘,'uniform' 表示同时检测并分别拟合 set_metrology_object_param (MetrologyHandle, 'all','measure_transition', 'all') *设置卡尺数量 set_metrology_object_param (MetrologyHandle, 'all', 'num_measures', 30) *拟合数,每个计量对象最多拟合的有效实例数,达到后停止拟合 set_metrology_object_param (MetrologyHandle, 'all', 'num_instances', 4) *高斯平滑系数,值越大边缘越清晰,越不容易找到边缘,值越小,干扰边缘越多 set_metrology_object_param (MetrologyHandle, 'all', 'measure_sigma', 2) *测量双立方插入值,区别于bilinear双线性 set_metrology_object_param (MetrologyHandle, 'all', 'measure_interpolation', 'bicubic') *边缘振幅最小值,最小边缘幅度越大,要求找到的边缘越锐利(灰度变化明显),反而越不容易找到边缘 set_metrology_object_param (MetrologyHandle, 'all', 'measure_threshold', 20) *取值 all,first,best,last set_metrology_object_param (MetrologyHandle, 'all', 'measure_select', 'all') *判定有效拟合实例的最低分数(检测边缘数 / 最大测量区域数) set_metrology_object_param (MetrologyHandle, 'all', 'min_score', 0.4) *开始找边缘 apply_metrology_model (Image, MetrologyHandle) *获取模型的计量区域和边缘结果 get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Rows, Columns) *把所有点显示出来 gen_cross_contour_xld (Cross, Rows, Columns, 6, 0.785398) * 如果需要拟合直线 * gen_contour_polygon_xld (Contour, Rows, Columns) * fit_line_contour_xld (Contour, 'drop', -1, 0, 5, 2, TempRowBegin, TempColBegin, TempRowEnd, TempColEnd, TempNr, TempNc, TempDist) * 计算拟合的直线和点之间的距离 * distance_pl (Rows, Columns, TempRowBegin, TempColBegin, TempRowEnd, TempColEnd, Distance) * tuple_max (Distance, Max) * 在联系的删除点中是不是有足够的点来拟合 * 删除不符合点到直线距离的点 * tuple_less_elem (Distance, Max/2, Less) * tuple_find (Less, 0, Indices) * tuple_remove (Rows, Indices, ValidRows) * tuple_remove (Columns, Indices, ValidCols) *得到线的起点坐标并显示出来 get_metrology_object_result (MetrologyHandle, 0, 'all', 'result_type', 'all_param', Parameter) *获模型计量对象的结果轮廓 get_metrology_object_result_contour (Contour, MetrologyHandle, 0, 'all', 5) select_obj(Contour, ObjectSelected1, 1) select_obj(Contour, ObjectSelected2, 2) concat_obj(EmptyObject, ObjectSelected1, TempObject) concat_obj(TempObject, ObjectSelected2, EmptyObject) * EmptyObject := [EmptyObject, ObjectSelected1, ObjectSelected2] * select_obj(Contour, ObjectSelected3, 3) *释放测量句柄,很重要否则导入C#代码运行没多久就会崩溃程序 clear_metrology_object (MetrologyHandle, 'all') endfor dev_clear_window() dev_display(Image) dev_display(EmptyObject) * 提取直线参数 Lines1 := [] select_obj(EmptyObject, Line, 1) fit_line_contour_xld(Line, 'tukey', -1, 0, 5, 2, Row1, Col1, Row2, Col2, _, _, _) Lines1 := [Lines1, [Row1, Col1, Row2, Col2]] * 获取线段角度(返回弧度) orientation_xld(Line, AngleRad) * 转换为角度(°) AngleDeg := AngleRad * 180 / 3.1415926535 Lines2 := [] select_obj(EmptyObject, Line, 2) fit_line_contour_xld(Line, 'tukey', -1, 0, 5, 2, Row1, Col1, Row2, Col2, _, _, _) Lines2 := [Lines2, [Row1, Col1, Row2, Col2]] Lines3 := [] select_obj(EmptyObject, Line, 3) fit_line_contour_xld(Line, 'tukey', -1, 0, 5, 2, Row1, Col1, Row2, Col2, _, _, _) Lines3 := [Lines3, [Row1, Col1, Row2, Col2]] Lines4 := [] select_obj(EmptyObject, Line, 4) fit_line_contour_xld(Line, 'tukey', -1, 0, 5, 2, Row1, Col1, Row2, Col2, _, _, _) Lines4 := [Lines4, [Row1, Col1, Row2, Col2]] *计算交点 intersection_lines (Lines1[0], Lines1[1], Lines1[2], Lines1[3], Lines3[0], Lines3[1], Lines3[2], Lines3[3], Row13, Column13, IsOverlapping) intersection_lines (Lines1[0], Lines1[1], Lines1[2], Lines1[3], Lines4[0], Lines4[1], Lines4[2], Lines4[3], Row14, Column14, IsOverlapping) intersection_lines (Lines2[0], Lines2[1], Lines2[2], Lines2[3], Lines3[0], Lines3[1], Lines3[2], Lines3[3], Row23, Column23, IsOverlapping) intersection_lines (Lines2[0], Lines2[1], Lines2[2], Lines2[3], Lines4[0], Lines4[1], Lines4[2], Lines4[3], Row24, Column24, IsOverlapping) AverageRow := (Row13 + Row14 + Row23 + Row24) / 4 AverageColumn := (Column13 + Column14 + Column23 + Column24) / 4 dev_clear_window() dev_display(Image) gen_cross_contour_xld (Cross1, AverageRow, AverageColumn, 200, AngleRad)

效果:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/9 15:13:08

PDF-Extract-Kit持续集成:CI/CD流水线配置

PDF-Extract-Kit持续集成:CI/CD流水线配置 1. 背景与目标 1.1 项目背景 PDF-Extract-Kit 是一个基于深度学习的 PDF 智能内容提取工具箱,由开发者“科哥”二次开发构建。该工具集成了布局检测、公式识别、OCR 文字提取、表格解析等核心功能&#xff0…

作者头像 李华
网站建设 2026/9/2 16:17:25

Thinkphp-Laravel+uniapp微信小程序的研学旅游服务系统的设计与实现

目录摘要项目开发技术介绍PHP核心代码部分展示系统结论源码获取/同行可拿货,招校园代理摘要 研学旅游服务系统基于ThinkPHP-Laravel框架与UniApp技术栈开发,整合微信小程序实现多端协同服务。系统采用B/S架构,后端通过Laravel提供RESTful API接口&#…

作者头像 李华
网站建设 2026/9/10 19:19:57

导师推荐!专科生必用TOP8 AI论文平台:开题报告神器测评

导师推荐!专科生必用TOP8 AI论文平台:开题报告神器测评 2026年专科生论文写作工具测评:为何需要这份榜单? 随着AI技术在学术领域的广泛应用,越来越多的专科生开始借助AI工具提升论文写作效率。然而,面对市场…

作者头像 李华
网站建设 2026/9/2 21:47:21

Sambert-HifiGan在智能办公设备中的语音助手应用

Sambert-HifiGan在智能办公设备中的语音助手应用 引言:让语音助手“有情感”地说话 在智能办公场景中,语音助手正从简单的指令响应工具演变为具备自然交互能力的“数字员工”。然而,传统TTS(Text-to-Speech)系统合成的…

作者头像 李华