通过给定点的线

发布于 2024-10-31 10:57:33 字数 348 浏览 1 评论 0原文

在此处输入图像描述

我试图找到图像绿色区域中对象外线的角度,如下所示如上图所示...

为此,我扫描了绿色区域并获取了点(如图所示的深蓝色点)...

如您所见,这些点没有形成直线,所以我找不到轻松角度。

所以我认为我必须找到一条中间道路 即找到一条线,使每个点和线之间的距离保持尽可能小。

那么我怎样才能找到这条线,使得每个点都暴露到它的最小距离……?

是否有任何算法可以实现这一点,或者除此之外还有什么好的方法吗?

enter image description here

I am trying to find the angle of the outer line of the object in the green region of the image as shown in the image above…

For that, I have scanned the green region and get the points (dark blue points as shown in the image)...

As you can see, the points are not making straight line so I can’t find angle easily.

So I think I have to find a middle way and
that is to find the line so that the distance between each point and line remain as minimum as possible.

So how can I find the line so that each point exposes minimum distance to it……?

Is there any algorithm for this or is there any good way other than this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月染过的梦 2024-11-07 10:57:33

显而易见的路线是通过这些点进行最小二乘线性回归

The obvious route would be to do a least-squares linear regression through the points.

蓝颜夕 2024-11-07 10:57:33

x on y 或 y on x 的标准最小二乘回归公式假设一个坐标没有误差,并将坐标与直线的偏差最小化。

然而,完全可以建立最小二乘计算,使得最小化的值是点到线的垂直距离的平方和。我不确定我是否能找到我做数学题的笔记本——那是二十多年前的事了——但我确实找到了我当时编写的实现算法的代码。

其中:

  • n = Σ 1
  • sx = Σ x
  • sx2 = Σ x2
  • sy = Σ y
  • sy2 = Σ y2
  • sxy = Σ x·y

您可以计算方差x 和 y 以及协方差:

  • vx = sx2 - ((sx * sx) / n)
  • vy = sy2 - ((sy * sy) / n)
  • v xy = sxy - ((sx * sy) / n)

现在,如果协方差为 0,则不存在线的外观。否则,斜率和截距可以通过以下公式找到:

  • slope = quad((vx - vy) / vxy, vxy)
  • intcpt = (sy - 斜率 * sx) / n

其中quad()是计算二次方程根的函数 x2 + b·x - 1与 c 具有相同的符号。在 C 中,那就是:

double quad(double b, double c)
{
    double b1;
    double q;

    b1 = sqrt(b * b + 4.0);
    if (c < 0.0)
        q = -(b1 + b) / 2;
    else
        q = (b1 - b) / 2;
    return (q);
}

从那里,你可以很容易地找到你的线的角度。

The standard least squares regression formulae for x on y or y on x assume there is no error in one coordinate and minimize the deviations in the coordinate from the line.

However, it is perfectly possible to set up a least squares calculation such that the value minimized is the sum of squares of the perpendicular distances of the points from the lines. I'm not sure whether I can locate the notebooks where I did the mathematics - it was over twenty years ago - but I did find the code I wrote at the time to implement the algorithm.

With:

  • n = ∑ 1
  • sx = ∑ x
  • sx2 = ∑ x2
  • sy = ∑ y
  • sy2 = ∑ y2
  • sxy = ∑ x·y

You can calculate the variances of x and y and the covariance:

  • vx = sx2 - ((sx * sx) / n)
  • vy = sy2 - ((sy * sy) / n)
  • vxy = sxy - ((sx * sy) / n)

Now, if the covariance is 0, then there is no semblance of a line. Otherwise, the slope and intercept can be found from:

  • slope = quad((vx - vy) / vxy, vxy)
  • intcpt = (sy - slope * sx) / n

Where quad() is a function that calculates the root of quadratic equation x2 + b·x - 1 with the same sign as c. In C, that would be:

double quad(double b, double c)
{
    double b1;
    double q;

    b1 = sqrt(b * b + 4.0);
    if (c < 0.0)
        q = -(b1 + b) / 2;
    else
        q = (b1 - b) / 2;
    return (q);
}

From there, you can find the angle of your line easily enough.

李白 2024-11-07 10:57:33

显然,该线将穿过平均点(x_average,y_average)。

对于方向,您可以使用以下算法(直接从最小化线和点之间的平均平方距离得出):

dx[i]=x[i]-x_average;
dy[i]=y[i]-y_average;

a=sum(dx[i]^2-dy[i]^2);
b=sum(2*dx[i]*dy[i]);

direction=atan2(b,a);

通常的线性回归在这里不起作用,因为它假设变量不对称 - 一个依赖于另一个,所以如果您将交换 x y,你会有另一个解决方案。

Obviously the line will pass through averaged point (x_average,y_average).

For direction you may use the following algorithm (derived directly from minimizing average square distance between line and points):

dx[i]=x[i]-x_average;
dy[i]=y[i]-y_average;

a=sum(dx[i]^2-dy[i]^2);
b=sum(2*dx[i]*dy[i]);

direction=atan2(b,a);

Usual linear regression will not work here, because it assumes that variables are not symmetric - one depends on other, so if you will swap x and y, you will have another solution.

ヅ她的身影、若隐若现 2024-11-07 10:57:33

霍夫变换可能也是一个不错的选择:

http://en.wikipedia.org/wiki/Hough_transform

The hough transform might be also a good option:

http://en.wikipedia.org/wiki/Hough_transform

独孤求败 2024-11-07 10:57:33

您可能会尝试搜索“总最小二乘”或“最小正交距离”,但当我尝试时,我发现没有任何立即适用的内容。

无论如何,假设你有点 x[],y[],并且该线由 a*x+b*y+c = 0 表示,其中hypot(a,b) = 1。最小正交距离线是最小化 Sum{ (a*x[i]+b*y[i]+c)^2}。一些代数表明:

c 是 -(a*X+b*Y),其中 X 是 x 的平均值,Y 是 y 的平均值。

(a,b) 是 C 的特征向量,对应于它的较小特征值,其中 C 是 x 和 y 的协方差矩阵

You might try searching for "total least squares", or "least orthogonal distance" but when I tried that I saw nothing immediately applicable.

Anyway suppose you have points x[],y[], and the line is represented by a*x+b*y+c = 0, where hypot(a,b) = 1. The least orthogonal distance line is the one that minimises Sum{ (a*x[i]+b*y[i]+c)^2}. Some algebra shows that:

c is -(a*X+b*Y) where X is the mean of the x's and Y the mean of the y's.

(a,b) is the eigenvector of C corresponding to it's smaller eigenvalue, where C is the covariance matrix of the x's and y's

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文