Halcon 直线拟合fit_line_contour_xld详解

fit_line_contour_xld

原型

fit_line_contour_xld(Contours : : Algorithm, MaxNumPoints, ClippingEndPoints, Iterations, ClippingFactor : RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)

功能

根据XLD轮廓拟合直线

参数列表

Contours (input_object) :输入的XLD轮廓
Algorithm (input_control):直线拟合算法( ‘drop’, ‘gauss’, ‘huber’, ‘regression’, ‘tukey’)
MaxNumPoints (input_control):参与拟合直线的最大轮廓点数(设置-1代表所有点参与计算;最小值要大于等于2,因为至少两点才能确认一条直线)
ClippingEndPoints (input_control) :拟合直线时,需要忽略轮廓起始处点个数。(一个XLD轮廓会被线分割成很多个小轮廓)
Iterations (input_control) :最大迭代次数。(不适用于’regression’)
ClippingFactor (input_control):剔除异常值因子。(算法 ‘huber’ 、‘drop’ 设置1.0,'tukey’设置2.0)
RowBegin (output_control) :拟合后的线段起始点行坐标
ColBegin (output_control) :拟合后的线段起始点列坐标
RowEnd (output_control) :拟合后的线段终点点行坐标
ColEnd (output_control) :拟合后的线段终点点列坐标
Nr (output_control) :直线方程法线式x相乘系数
Nc (output_control) :直线方程法线式y相乘系数
Dist (output_control) :坐标系原点到线段的距离

详解

fit_line_contour_xld目的是将一个XLD轮廓拟合成一条线段。几何数学上获取线段的起始点坐标,以及直线法线式方程中的Nr,Nc向量。
法线式方程如下:在这里插入图片描述
这个算子在工业检测中有很大用处,比如:
1、精细寻找产品的边界用于角度计算
2、拟合两条边,然后求交点,做定位用
3、求解参与拟合的点到直线的距离,可以做异常值检测

拟合直线的算法有以下几种:
‘regression’:回归,标准的最小二乘法拟合
‘huber’:加权的最小二乘法拟合,异常值的影响被减小。
‘tukey’:加权的最小二乘法拟合,异常值被忽略,Halcon推荐方法。
‘drop’:加权的最小二乘法拟合,异常值被忽略。
‘gauss’:加权的最小二乘法拟合,异常值去除是根据轮廓点距拟合直线距离的平均值以及标准差方式决定。

Part1

For ‘huber’, ‘tukey’, and ‘drop’ a robust error statistics is used to estimate the standard deviation of the distances from the contour points without outliers from the approximating line. The parameter ClippingFactor (a scaling factor for the standard deviation) controls the amount of outliers: The smaller the value chosen for ClippingFactor the more outliers are detected. The detection of outliers is repeated. The parameter Iterations specifies the number of iterations.
没有原码,根据上面说明文档结合实际项目应用经验,说说我的理解:针对’huber’, ‘tukey’, 'drop’这三种算法,首先是根据所有点近似拟合一条直线,然后计算原始轮廓上每一点到拟合直线的距离,再根据统计学中标准差方式去除异常点。
假设有100个点参与拟合直线,其中有20个点是偏离直线的异常点,那么通过标准差的方式100个点对应100个统计值,按照距离直线远近从大到小排序。如果都参与拟合直线,拟合的直线和实际会有偏差,原因是其中混杂了异常值,所以为了确保拟合的准确度,可以通过参数 ClippingFactor 控制要排除的异常点数量,值越小,参与拟合的异常值越多,拟合的准确度越低。
为了进一步提高拟合精度,参数Iterations 控制迭代次数,也就是说每次都会重复以上步骤。

Part2

In the mode ‘regression’ this value is ignored. Note that in the approach of Tukey (‘tukey’), the outliers are removed before performing the approximation and all other points are weighted, whereas in the approach of Huber (‘huber’), the outliers still have a small influence.
需要注意的是这个值对方法’regression’ 不起作用;方法’tukey’在拟合直线时去除了所有的异常值;异常值对方法’huber’依然会存在影响。
Particularly, for outliers the optimization is influenced linearly and for points with a smaller distance it is influenced to the square. In practice, the approach of Tukey is recommended.
虽然可以通过计算标准差、参数ClippingFactor 控制异常值个数、Iterations 增加迭代次数尽可能避免异常值对拟合直线的影响。但求解最优直线方程始终是有限的,只能说无限逼近。有的小伙伴会说,不是有ClippingFactor 、Iterations 这两个参数可以控制吗,那这两个参数如何组合才是最优?ClippingFactor 、Iterations 大了,会造成拟合的点变少,而且也许去掉的所谓‘异常值’,实际是正常值呢;值太小也会有同样的问题,所以只能说是无限接近,始终会存在误差。Halcon推荐使用Tukey 方法。

Part3

To reduce the computational load, the fitting of lines can be restricted to a subset of the contour points: If a value other than -1 is assigned to MaxNumPoints, only up to MaxNumPoints points - uniformly distributed over the contour - are used.
介绍的是MaxNumPoints 参数,目的是提高拟合效率。如果你的轮廓比较长,轮廓点较多,又想节约时间,不妨考虑通过这个参数控制要参与拟合的轮廓点。如果不在乎时间,默认值-1即可。需要注意的时除了-1外,这个值最小要为2,因为至少两个点才能确定一条线段。

Part4

The start point and the end point of a line segment is determined by projecting the first and the last point of the corresponding contour to the approximating line. Due to artifacts in the pre-processing the start and end points of a contour might be faulty. Therefore, it is possible to exclude ClippingEndPoints points at the beginning and at the end of a contour from the line fitting. However, they are still used for the determination of the start point and the end point of the line segment.
The minimum necessary number of contour points for fitting a line is two. Therefore, it is required that the number of contour points is at least .
介绍的是ClippingEndPoints参数。获取的轮廓一般头尾会有缺陷,比如可能是相邻区域的衔处,预处理获取轮廓的时候会多出来一部分弧度或者线条,因此需要设置参数ClippingEndPoints控制去除头尾的轮廓点数。去除的头尾轮廓点数始终是ClippingEndPoints值的2倍,因此如果这个参数大于0,就必须要求参与拟合直线的轮廓至少有2+2ClippingEndPoints个点。虽然头尾去除了2ClippingEndPoints个点,但拟合的直线头尾位置依然是根据原始轮廓的起点确定的,不会因为去除了部分点造成拟合的线段变短了。

举例

1、介绍的是在实际应用中寻找产品边缘点,然后根据边缘点拟合直线,最后计算出产品偏移角度,根据角度可以进行后续纠偏等操作
在这里插入图片描述
2、介绍的是在实际应用中根据寻点拟合两个边界直线,然后求两直线交点作为定位点。这样的好处是定位点比直接不拟合直线求交点的精度要精准的多。
在这里插入图片描述

  • 14
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Halcon中的fit_line_contour_xld算子可以用OpenCV中的fitLine函数来实现。fitLine函数可以拟合一条直线,从而实现对轮廓的拟合。具体步骤如下: 1. 读入图像,并将其转换为灰度图像。 2. 对图像进行二值化处理。 3. 使用findContours函数查找轮廓。 4. 对轮廓进行拟合,使用fitLine函数。 5. 计算拟合直线的斜率和截距。 6. 绘制检测结果,显示图像。 下面是一个简单的示例代码: ``` import cv2 import numpy as np # 读入图像并转换为灰度图像 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化图像 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 对轮廓进行拟合 rows,cols = img.shape[:2] vx,vy,x,y = cv2.fitLine(contours[0], cv2.DIST_L2, 0, 0.01, 0.01) lefty = int((-x*vy/vx) + y) righty = int(((cols-x)*vy/vx)+y) # 绘制检测结果 cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在此示例中,我们首先读取一张图像,然后将其转换为灰度图像并进行二值化处理。接下来,我们使用findContours函数查找轮廓,并使用fitLine函数对轮廓进行拟合。然后,我们计算拟合直线的斜率和截距,并使用cv2.line函数在图像上绘制该直线。最后,我们显示图像并输出结果。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值