如何从一系列点计算一条线?
可能是一个简单的问题,但到目前为止我找不到简单的解决方案。我正在为一个非常具体的用例开发一个简单的图像识别软件。
给定的是一堆应该在一条直线上的点。然而,有些点放置错误并且偏离了线。特别是在线条末端附近,点可能或多或少不准确。
示例:
X // this guy is off
X // this one even more
X // looks fine
X
X
X // a mistake in the middle
X
X // another mistake, not as bad as the previous
X
X
X
X
X // we're off the line again
线的大致方向已知,在本例中,它是垂直的。示例中的实际线实际上是垂直的,具有轻微的对角斜度。
我只对无限线感兴趣(即它的斜率和偏移量),端点的位置并不重要。
作为附加信息(不确定是否重要),2 个点不可能水平相邻。示例:
X
X
X
X X // cannot happen
X
X
性能并不重要。我正在使用 C# 工作,但我对任何语言都很好,或者只是一个通用的想法。
Probably an easy question, but I could not find an easy solution so far. I'm working on a simple image recognition software for a very specific use case.
Given is a bunch of points that are supposedly on a straight line. However, some of the points are mistakenly placed and away from the line. Especially near the ends of the line, points may happen to be more or less inaccurate.
Example:
X // this guy is off
X // this one even more
X // looks fine
X
X
X // a mistake in the middle
X
X // another mistake, not as bad as the previous
X
X
X
X
X // we're off the line again
The general direction of the line is known, in this case, it's vertical. The actual line in the example is in fact vertical with slight diagonal slope.
I'm only interested in the infinite line (that is, it's slope and offset), the position of the endpoints is not important.
As additional information (not sure if it is important), it is impossible for 2 points to lie next to each other horizontally. Example:
X
X
X
X X // cannot happen
X
X
Performance is not important. I'm working in C#, but I'm fine with any language or just a generic idea, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道没有异常值,线性回归(如其他人提到的)是很好的。
如果确实有异常值,那么我最喜欢的方法之一是中位数中线法:
http://education.uncc.edu/droyster/courses/ spring00/maed3103/Median-Median_Line.htm
基本上,您按 X 值对点进行排序,然后将点分成三个相等大小的组(最小值、中值和最大值)。最终斜率是穿过小组中位数和大组中位数的线的斜率。中间组的中位数与其他中位数一起使用来计算最终的偏移/截距。
这是一个简单的算法,可以在多种图形计算器上找到。
通过取三个中位数,您可以完全忽略任何异常值(最左、最右、最上或最下)。
下图显示了一组具有几个大异常值的数据的线性回归和中位数线。
Linear regression (as mentioned by others) is good if you know you do not have outliers.
If you do have outliers, then one of my favorite methods is the median median line method:
http://education.uncc.edu/droyster/courses/spring00/maed3103/Median-Median_Line.htm
Basically you sort the points by the X values and then split the points up into three equal sized groups (smallest values, medium values, and largest values). The final slope is the slope of the line going through the median of the small group and through the median of the large group. The median of the middle group is used with the other medians to calculate the final offset/intercept.
This is a simple algorithm that can be found on several graphing calculators.
By taking the three medians, you are completely ignoring any outliers (either on the far left, far right, far up, or far down).
The image below shows the linear regression and median-median lines for a set of data with a couple of large outliers.
我认为您正在寻找通过线性回归进行最小二乘拟合
I think you're looking for Least squares fit via Linear Regression
迈克是当场!使用以下内容:
使用 beta1 作为斜率,使用 beta0 作为 y 轴截距!
Mike is spot on! Use the following:
Use beta1 as the slope and beta0 as the y-intercept!