Halcon测量专栏 您所在的位置:网站首页 直线度检测方法视频 Halcon测量专栏

Halcon测量专栏

2024-07-16 12:16| 来源: 网络整理| 查看: 265

1.前言 1.1什么是直线度

直线度公差指单一实际直线允许的变动全量。用于控制平面或空间直线的形状误差,其公差带根据不同的情况有几种不同的形式。

1.2直线度评价方式

直线度误差的检测一般可采用“与理想要素比较原则”、“测量特征原则”、和“控制实段边介原则”。其中“与理想要素比较原则”应用最多。这时理想直线采用以下两种形式来体现。 (1)实物形式体现理想直线,可采用平尺、刀口尺(样板直齿)、精密平板和拉紧钢丝等来体现理想直线。 (2)自然物理形式体现理想直线 ①利用光学原理制成的准直仪类仪器和各种干涉仪。 ②利用水平物理|生质构成的水平仪类仪器(框式水平仪、全像水平仪、电子水平仪等)。

1.3halcon实现方式

针对直线度的评价方式进行直线测量。1:先对初始直线进行直线测量,精准的判断出当前直线的起始和终点坐标。2:根据直线的角度和长度,以10个像素单位间隔进行测量矩形逐个测量,用于判断直线的误差。 halcon所采取的实现方式,是如上所述。绘制理想直线,然后判断边缘在直线上的波动值

2.halcon程序 2.1halcon程序 read_image (Image, 'D:/1NewWork/work/2.26/屏幕截图 2024-03-04 134150.png') *绘制直线ROI gen_region_line (ROI_0, 128.985, 117.243, 130.855, 750.06) ********************************直线测量********************************* create_metrology_model (MetrologyHandle) add_metrology_object_line_measure (MetrologyHandle, 128.985, 117.243, 130.855, 750.06, 20, 3, 1, 30, [], [], Index) set_metrology_object_param (MetrologyHandle, 'all', ['measure_transition','measure_select'], ['uniform','first']) apply_metrology_model (Image, MetrologyHandle) **************************************获取结果显示*********************************** get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'row', UsedRow) get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'column', UsedColumn) *显示测量矩形 get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1) *创建测量矩形中点 gen_cross_contour_xld (UsedEdges, UsedRow, UsedColumn, 10, 1) *获取直线ROI坐标 get_metrology_object_result_contour (ResultContours, MetrologyHandle, 'all', 'all', 1.5) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'row_begin', row1) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'row_end', row2) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'column_begin', column1) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'column_end', column2) ***********************************图像显示************** dev_display (Image) dev_display(Contours) dev_display(UsedEdges) dev_display (ResultContours) *获取直线角度 line_orientation (row1, column1, row2, column2, Phi) distance_pp (row1, column1, row2, column2, Distance) *以10个像素为单位做测量间隔 tuple_ceil (Distance/10, Ceil) get_image_size (Image, Width, Height) tuple_sin (Phi, Sin) tuple_cos (Phi, Cos) Dis:=[] gen_empty_obj (EmptyObject) for Index1 := 0 to Ceil-1 by 1 *创建测量矩形 gen_measure_rectangle2 (row1+10*Index1*Sin, column1+10*Index1*Cos, Phi-rad(90), 50, 5, Width, Height, 'nearest_neighbor', MeasureHandle) *绘制显示的矩形轮廓 gen_rectangle2_contour_xld (Rectangle, row1+10*Index1*Sin, column1+10*Index1*Cos, Phi-rad(90), 50, 5) dev_display (Image) dev_display (Rectangle) measure_pos (Image, MeasureHandle, 1, 30, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance1) *绘制采样点 gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 100, 1) *图像显示 concat_obj (Cross, EmptyObject, EmptyObject) distance_pl (RowEdge, ColumnEdge, row1, column1, row2, column2, Distance2) Dis:=[Dis,Distance2] endfor tuple_max (Dis, Max) tuple_min (Dis, Min) tuple_abs (Max, Max) tuple_abs (Min, Min) if (Max>=Min) Circle_Max:=Max else Circle_Max:=Min endif dev_display (Image) dev_display (ResultContours) dev_display (EmptyObject) 2.2halcon程序讲解 2.2.1读取图像和绘制ROI read_image (Image, 'D:/1NewWork/work/2.26/屏幕截图 2024-03-04 134150.png') *绘制直线ROI gen_region_line (ROI_0, 128.985, 117.243, 130.855, 750.06) ********************************直线测量********************************* create_metrology_model (MetrologyHandle) add_metrology_object_line_measure (MetrologyHandle, 128.985, 117.243, 130.855, 750.06, 20, 3, 1, 30, [], [], Index)

在这里插入图片描述

绘制ROI时,应该尽可能的绘制完好,贴合直线。但是如何绘制不好时也没有问题,在下一步直线测量中会对ROI的坐标进行矫正。

2.2.2直线测量矫正 ********************************直线测量********************************* create_metrology_model (MetrologyHandle) add_metrology_object_line_measure (MetrologyHandle, 128.985, 117.243, 130.855, 750.06, 20, 3, 1, 30, [], [], Index) set_metrology_object_param (MetrologyHandle, 'all', ['measure_transition','measure_select'], ['uniform','first']) apply_metrology_model (Image, MetrologyHandle) **************************************获取结果显示*********************************** get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'row', UsedRow) get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'column', UsedColumn) *显示测量矩形 get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row1, Column1) *创建测量矩形中点 gen_cross_contour_xld (UsedEdges, UsedRow, UsedColumn, 10, 1) *获取直线ROI坐标 get_metrology_object_result_contour (ResultContours, MetrologyHandle, 'all', 'all', 1.5) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'row_begin', row1) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'row_end', row2) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'column_begin', column1) get_metrology_object_result (MetrologyHandle, Index, 'all', 'result_type', 'column_end', column2)

对获取到的ROI进行直线测量,用于对直线ROI的矫正,和获取到基准测量直线,生成理想直线。

2.2.3直线差值检测 *获取直线角度 line_orientation (row1, column1, row2, column2, Phi) distance_pp (row1, column1, row2, column2, Distance) *以10个像素为单位做测量间隔 tuple_ceil (Distance/10, Ceil) get_image_size (Image, Width, Height) tuple_sin (Phi, Sin) tuple_cos (Phi, Cos) Dis:=[] gen_empty_obj (EmptyObject) for Index1 := 0 to Ceil-1 by 1 *创建测量矩形 gen_measure_rectangle2 (row1+10*Index1*Sin, column1+10*Index1*Cos, Phi-rad(90), 50, 5, Width, Height, 'nearest_neighbor', MeasureHandle) *绘制显示的矩形轮廓 gen_rectangle2_contour_xld (Rectangle, row1+10*Index1*Sin, column1+10*Index1*Cos, Phi-rad(90), 50, 5) dev_display (Image) dev_display (Rectangle) measure_pos (Image, MeasureHandle, 1, 30, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance1) *绘制采样点 gen_cross_contour_xld (Cross, RowEdge, ColumnEdge, 100, 1) *图像显示 concat_obj (Cross, EmptyObject, EmptyObject) distance_pl (RowEdge, ColumnEdge, row1, column1, row2, column2, Distance2) Dis:=[Dis,Distance2] endfor tuple_max (Dis, Max) tuple_min (Dis, Min) tuple_abs (Max, Max) tuple_abs (Min, Min) if (Max>=Min) Circle_Max:=Max else Circle_Max:=Min endif

在这里插入图片描述

这个不同于直线测量在于,直线测量会判断当前点是否符合拟合参数,如果不符合时则会自动剔除,但是我们需要获取到全部的拟合点,才能有效的判断直线度是否正确。 最终结果输出最大直线度。

2.2.3注意事项

当所被测量的区域有多条直线时,会干扰到直线度的测量。将下列算子中的“50”(测量矩形的高度)减小即可

add_metrology_object_line_measure (MetrologyHandle, 128.985, 117.243, 130.855, 750.06, 50, 3, 1, 20, [], [], Index) gen_measure_rectangle2 (row1+10*Index1*Sin, column1+10*Index1*Cos, Phi-rad(90), 50, 5, Width, Height, 'nearest_neighbor', MeasureHandle) 3.C#程序工具实现 #region // 直线查找 /// /// 直线查找 /// /// 输入图像 /// 输入ROI的开始横坐标 /// 输入ROI的结束横坐标 /// 输入ROI开始列坐标 /// 输入ROI的结束列坐标 /// 输入测量矩形高度 /// 输入测量矩形宽度 /// 输入测量矩形的高斯滤波值 /// 输入最小边缘对比度 /// 选择拟合边1(“uniform”:最接近ROI轮廓的拟合边;“positive”:由亮到暗,“negative”:由暗到亮) /// 选择拟合边2(“first”:所有轮廓的第一条边,“last”:所有轮廓的最后一个边,“all”:最接近ROI轮廓的拟合边) /// 结果轮廓 /// 结果轮廓开始横坐标 /// 结果轮廓结束横坐标 /// 结果轮廓开始列坐标 /// 结果轮廓结束列坐标 /// 拟合成功返回true,拟合失败返回false static public bool LineMeasure_(HObject image, HTuple BeginRow, HTuple BeginColumn, HTuple EndRow, HTuple EndColumn, HTuple MeasureHeight, HTuple MeasureWide, HTuple Sigma, HTuple Threshold, HTuple Measure_Set, HTuple Measure_Place, out HObject LineContours, out HTuple ResultRowBegin, out HTuple ResultColumnBegin, out HTuple ResultRowEnd, out HTuple ResultCloumnEnd) { HOperatorSet.GenEmptyObj(out LineContours); ResultRowBegin = null; ResultColumnBegin = null; ResultCloumnEnd = null; ResultRowEnd = null; try { HOperatorSet.CreateMetrologyModel(out HTuple metrologyHandle); HOperatorSet.AddMetrologyObjectLineMeasure(metrologyHandle, BeginRow, BeginColumn, EndRow, EndColumn, MeasureHeight, MeasureWide, Sigma, Threshold, new HTuple(), new HTuple(), out HTuple index); HOperatorSet.SetMetrologyObjectParam(metrologyHandle, new HTuple("all"), (new HTuple("measure_transition") ).TupleConcat("measure_select"), (new HTuple(Measure_Set) ).TupleConcat(Measure_Place)); //HOperatorSet.SetMetrologyObjectParam(metrologyHandle, new HTuple("all"), new HTuple("min_score"), 0.1); HOperatorSet.ApplyMetrologyModel(image, metrologyHandle); HOperatorSet.GetMetrologyObjectResultContour(out LineContours, metrologyHandle, new HTuple("all"), new HTuple("all") , new HTuple(1.5)); HOperatorSet.GetMetrologyObjectResult(metrologyHandle, index, new HTuple("all"), new HTuple("result_type"), new HTuple("row_begin"), out ResultRowBegin); HOperatorSet.GetMetrologyObjectResult(metrologyHandle, index, new HTuple("all"), new HTuple("result_type"), new HTuple("row_end"), out ResultRowEnd); HOperatorSet.GetMetrologyObjectResult(metrologyHandle, index, new HTuple("all"), new HTuple("result_type"), new HTuple("column_begin"), out ResultColumnBegin); HOperatorSet.GetMetrologyObjectResult(metrologyHandle, index, new HTuple("all"), new HTuple("result_type"), new HTuple("column_end"), out ResultCloumnEnd); HOperatorSet.TupleLength(ResultRowBegin, out HTuple length); if (length == 0) { return false; } return true; } catch (Exception ex) { return false; } } #endregion #region // 计算直线度 public static bool Straightness(HObject image, bool FastSet, HTuple ROIBeginRow, HTuple ROIBeginCloumn, HTuple ROIEndRow, HTuple ROIEndCloumn, out HObject lineContours, out HTuple resultRowBegin, out HTuple resultColumnBegin, out HTuple resultRowEnd, out HTuple resultCloumnEnd, out double straightness, out double[] Distances) { straightness = -1; HOperatorSet.GenEmptyObj(out lineContours); resultRowBegin = -1; resultRowEnd = -1; resultColumnBegin = -1; resultCloumnEnd = -1; Distances = new double[0]; try { bool line = false; if (FastSet) { line = Module.LineMeasure_(image, ROIBeginRow, ROIBeginCloumn, ROIEndRow, ROIEndCloumn, 20, 5, 2, 40, "uniform", "first", out lineContours, out resultRowBegin, out resultColumnBegin, out resultRowEnd, out resultCloumnEnd); HOperatorSet.TupleLength(resultRowEnd, out HTuple length); if (length==0) { return false; } double angle = Math.Atan2(resultRowBegin - resultRowEnd, resultColumnBegin - resultCloumnEnd)-Math.PI; HOperatorSet.DistancePp(resultRowBegin, resultColumnBegin, resultRowEnd, resultCloumnEnd, out HTuple Dis); double i = Dis/10; int k=(int)Math.Floor(i); HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height); HOperatorSet.TupleSin(angle, out HTuple Sin); HOperatorSet.TupleCos(angle, out HTuple Cos); Distances = new double[k]; for (int j = 0; j Distances[j] = 0; continue; } HOperatorSet.DistancePl(rowEdge, columnEdge, resultRowBegin, resultColumnBegin, resultRowEnd, resultCloumnEnd, out HTuple distance1); HOperatorSet.TupleLength(distance1, out HTuple length1); if (length1 == 0) { Distances[j] = 0; } else { Distances[j] = distance1; } } } straightness = Distances.Max(); return true; } catch (Exception) { return false; } } #endregion 总结

直线度在机械设计及其制造方面的检测非常常见,尤其涉及到高精度的检测上面。使用视觉对直线度进行检测可以在极短的时间内,完成直线测量,精确度高。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有