外接椭圆
我被分配了一个图形模块的作业,其中一部分是计算一组任意形状的最小外接椭圆。椭圆不必与轴对齐。
这是使用 AWT 形状在 java(euch)中工作的,因此我可以使用形状提供的所有工具来检查对象的包含/相交。
I have been given an assignement for a graphics module, one part of which is to calculate the minimum bounding ellipse of a set of arbitrary shapes. The ellipse doesn't have to be axis aligned.
This is working in java (euch) using the AWT shapes, so I can use all the tools shape provides for checking containment/intersection of objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找包围椭圆体的最小体积,或者在您的二维情况下,最小体积区域。这个优化问题是凸的并且可以有效地解决。查看我包含的链接中的 MATLAB 代码 - 实现很简单,不需要比矩阵求逆更复杂的东西。
对数学感兴趣的任何人都应该阅读本文档。
此外,绘制椭圆也很简单 - 可以在此处找到,但您将需要一个 MATLAB 特定的函数来生成椭圆上的点。
但是由于算法以矩阵形式返回椭圆的方程,
您可以使用此代码查看如何将方程转换为规范形式,
使用奇异值分解(SVD)。然后使用规范形式绘制椭圆非常容易。
以下是 MATLAB 代码在一组 10 个随机 2D 点(蓝色)上的结果。
其他方法,如 PCA 不保证分解得到的椭圆 (eigen/奇异值)将是最小边界椭圆,因为椭圆之外的点是方差的指示。
编辑:
因此,如果有人阅读该文档,有两种方法可以在 2D 中实现此目的:这是最佳算法的伪代码 - 次优算法在文档中进行了清楚的解释:
最佳算法:
You're looking for the Minimum Volume Enclosing Ellipsoid, or in your 2D case, the minimum area. This optimization problem is convex and can be solved efficiently. Check out the MATLAB code in the link I've included - the implementation is trivial and doesn't require anything more complex than a matrix inversion.
Anyone interested in the math should read this document.
Also, plotting the ellipse is also simple - this can be found here, but you'll need a MATLAB-specific function to generate points on the ellipse.
But since the algorithm returns the equation of the ellipse in the matrix form,
you can use this code to see how you can convert the equation to the canonical form,
using Singular Value Decomposition (SVD). And then it's quite easy to plot the ellipse using the canonical form.
Here's the result of the MATLAB code on a set of 10 random 2D points (blue).
Other methods like PCA does not guarantee that the ellipse obtained from the decomposition (eigen/singular value) will be minimum bounding ellipse since points outside the ellipse is an indication of the variance.
EDIT:
So if anyone read the document, there are two ways to go about this in 2D: here's the pseudocode of the optimal algorithm - the suboptimal algorithm is clearly explained in the document:
Optimal algorithm:
感谢 Jacob 的伪代码,我能够在 Java 中实现最小体积包围椭圆体 (MVEE)。有一些公共方法可以获取中心点、“A”矩阵,以及生成可用于渲染椭圆的坐标列表的方法。后者基于 Peter Lawrence 在原始 MVEE 的评论中发布的 MatLab 代码 代码。请注意,代码引用了一个名为“Eigen”的类 - Jama 的 EigenvalueDecomposition 类的修改版本(我取出了 Matrix 类依赖项)。我会添加它,但答案有 30k 字符限制...
这里是使用 10 个随机点和 0.001 容差的示例输出。椭圆是使用连接通过 Ellipse.getBoundingCooperatives() 方法使用 50 个点生成的点的直线来渲染的。
Thanks to Jacob's pseudocode I was able to implement the Minimum Volume Enclosing Ellipsoid (MVEE) in Java. There are public methods to get the center point, the "A" matrix, and a method to generate a list of coordinates that can be used to render the ellipse. The latter is based on MatLab code posted by Peter Lawrence in the comments to the original MVEE code. Note that the code references a class called "Eigen" - a modified version of Jama's EigenvalueDecomposition class (I took out Matrix class dependencies). I would add it but there is a 30k character limit to answers...
Here an example output using 10 random points and a tolerance of 0.001. The ellipse is rendered using straight lines connecting points generated via the Ellipse.getBoundingCoordinates() method using 50 points.