Halcon测量专栏-直线度

Halcon测量专栏-直线度直线度公差指单一实际直线允许的变动全量

大家好,欢迎来到IT知识分享网。

1.前言

1.1什么是直线度

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

1.2直线度评价方式

1.3halcon实现方式

2.halcon程序

2.1halcon程序

read_image (Image, 'D:/1NewWork/work/2.26/屏幕截图 2024-03-04 .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 .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 // 直线查找 /// <summary> /// 直线查找 /// </summary> /// <param name="image">输入图像</param> /// <param name="BeginRow">输入ROI的开始横坐标</param> /// <param name="EndRow">输入ROI的结束横坐标</param> /// <param name="BeginColumn">输入ROI开始列坐标</param> /// <param name="EndColumn">输入ROI的结束列坐标</param> /// <param name="MeasureHeight">输入测量矩形高度</param> /// <param name="MeasureWide">输入测量矩形宽度</param> /// <param name="Sigma">输入测量矩形的高斯滤波值</param> /// <param name="Threshold">输入最小边缘对比度</param> /// <param name="Measure_Set">选择拟合边1(“uniform”:最接近ROI轮廓的拟合边;“positive”:由亮到暗,“negative”:由暗到亮)</param> /// <param name="Measure_Place">选择拟合边2(“first”:所有轮廓的第一条边,“last”:所有轮廓的最后一个边,“all”:最接近ROI轮廓的拟合边)</param> /// <param name="LineContours">结果轮廓</param> /// <param name="ResultRowBegin">结果轮廓开始横坐标</param> /// <param name="ResultRowEnd">结果轮廓结束横坐标</param> /// <param name="ResultColumnBegin">结果轮廓开始列坐标</param> /// <param name="ResultCloumnEnd">结果轮廓结束列坐标</param> /// <returns>拟合成功返回true,拟合失败返回false</returns> 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 < k; j++) { 
    HOperatorSet.GenMeasureRectangle2(resultRowBegin + 10 * j * Sin, resultColumnBegin + 10 * j * Cos, -angle - Math.PI*0.5 , 20, 5, width, height, new HTuple("nearest_neighbor"), out HTuple measureHandle); HOperatorSet.MeasurePos(image, measureHandle, 2, 20, new HTuple("all"), new HTuple("all"), out HTuple rowEdge, out HTuple columnEdge, out HTuple amplitude, out HTuple distance); HOperatorSet.TupleLength(rowEdge, out HTuple length2); if (length2==0) { 
    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 

总结

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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/132149.html

(0)
上一篇 2025-08-02 18:20
下一篇 2025-08-02 18:26

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信