给定一组点,如何近似其形状的长轴?

发布于 2024-07-14 05:10:06 字数 869 浏览 6 评论 0原文

给定用户绘制的“形状”,我​​想对其进行“标准化”,以便它们都具有相似的大小和方向。 我们拥有的是一组点。 我可以使用边界框或圆圈来近似大小,但方向有点棘手。

我认为正确的方法是计算其长轴 ="http://en.wikipedia.org/wiki/Bounding_volume" rel="nofollow noreferrer">边界椭圆。 为此,您需要计算 协方差矩阵。 这样做可能对我的需要来说太复杂了,因为我正在寻找一些足够好的估计。 选择最小、最大和 20 个随机点可能是一些入门。 有没有一种简单的方法可以近似这个?

编辑: 我发现幂法来迭代近似特征向量。 维基百科文章。 到目前为止,我喜欢 大卫的回答

Given a "shape" drawn by the user, I would like to "normalize" it so they all have similar size and orientation. What we have is a set of points. I can approximate the size using bounding box or circle, but the orientation is a bit more tricky.

The right way to do it, I think, is to calculate the majoraxis of its bounding ellipse. To do that you need to calculate the eigenvector of the covariance matrix. Doing so likely will be way too complicated for my need, since I am looking for some good-enough estimate. Picking min, max, and 20 random points could be some starter. Is there an easy way to approximate this?

Edit:
I found Power method to iteratively approximate eigenvector. Wikipedia article.
So far I am liking David's answer.

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

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

发布评论

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

评论(4

自演自醉 2024-07-21 05:10:06

您将计算 2x2 矩阵的特征向量,这可以通过一些简单的公式来完成,因此并不那么复杂。 在伪代码中:

// sums are over all points
b = -(sum(x * x) - sum(y * y)) / (2 * sum(x * y))
evec1_x = b + sqrt(b ** 2 + 1)
evec1_y = 1
evec2_x = b - sqrt(b ** 2 + 1)
evec2_y = 1

如果您希望所选的点子集能够代表整个集合,您甚至可以通过仅对某些点求和来获得估计值来实现此目的。

编辑:我认为x和y必须转换为零均值,即首先从所有x、y中减去均值(eed3si9n)

You'd be calculating the eigenvectors of a 2x2 matrix, which can be done with a few simple formulas, so it's not that complicated. In pseudocode:

// sums are over all points
b = -(sum(x * x) - sum(y * y)) / (2 * sum(x * y))
evec1_x = b + sqrt(b ** 2 + 1)
evec1_y = 1
evec2_x = b - sqrt(b ** 2 + 1)
evec2_y = 1

You could even do this by summing over only some of the points to get an estimate, if you expect that your chosen subset of points would be representative of the full set.

Edit: I think x and y must be translated to zero-mean, i.e. subtract mean from all x, y first (eed3si9n).

醉态萌生 2024-07-21 05:10:06

这里有一个想法...如果您对这些点执行线性回归并使用结果线的斜率会怎样? 如果不是所有的点,至少是其中的一个样本。

r^2 值还会为您提供有关一般形状的信息。 越接近 0,形状越圆形/均匀(圆形/方形)。 越接近 1,形状越拉伸(椭圆形/矩形)。

Here's a thought... What if you performed a linear regression on the points and used the slope of the resulting line? If not all of the points, at least a sample of them.

The r^2 value would also give you information about the general shape. The closer to 0, the more circular/uniform the shape is (circle/square). The closer to 1, the more stretched out the shape is (oval/rectangle).

拥有 2024-07-21 05:10:06

此问题的最终解决方案是运行 PCA
我希望我能找到一个不错的小实现供您参考......

The ultimate solution to this problem is running PCA
I wish I could find a nice little implementation for you to refer to...

甜味拾荒者 2024-07-21 05:10:06

干得好! (假设 x 是 nx2 向量)

def majAxis(x):
    e,v = np.linalg.eig(np.cov(x.T)); return v[:,np.argmax(e)]

Here you go! (assuming x is a nx2 vector)

def majAxis(x):
    e,v = np.linalg.eig(np.cov(x.T)); return v[:,np.argmax(e)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文