【问题标题】:How to fit a bounding ellipse around a set of 2D points如何围绕一组 2D 点拟合边界椭圆
【发布时间】:2016-06-19 19:51:22
【问题描述】:

给定一组二维点(笛卡尔形式),我需要找到最小面积椭圆,使得该集合中的每个点都位于椭圆上或椭圆内。

我在这个网站上有 found the solution 的伪代码形式,但我尝试用 C++ 实现解决方案没有成功。

下图以图形方式说明了我的问题的解决方案是什么样的:

在我的尝试中,我使用Eigen 库对矩阵进行各种操作。

//The tolerance for error in fitting the ellipse
double tolerance = 0.2;
int n = 10; // number of points
int d = 2; // dimension
MatrixXd p = MatrixXd::Random(d,n); //Fill matrix with random points

MatrixXd q = p;
q.conservativeResize(p.rows() + 1, p.cols());

for(size_t i = 0; i < q.cols(); i++)
{
    q(q.rows() - 1, i) = 1;
}

int count = 1;
double err = 1;

const double init_u = 1.0 / (double) n;
MatrixXd u = MatrixXd::Constant(n, 1, init_u);


while(err > tolerance)
{
    MatrixXd Q_tr = q.transpose();
    cout << "1 " << endl;
    MatrixXd X = q * u.asDiagonal() * Q_tr;
    cout << "1a " << endl;
    MatrixXd M = (Q_tr * X.inverse() * q).asDiagonal();
    cout << "1b " << endl;



    int j_x, j_y;
    double maximum = M.maxCoeff(&j_x, &j_y);
    double step_size = (maximum - d - 1) / ((d + 1) * (maximum + 1));

    MatrixXd new_u = (1 - step_size) * u;
    new_u(j_x, 0) += step_size;

    cout << "2 " << endl;

    //Find err
    MatrixXd u_diff = new_u - u;
    for(size_t i = 0; i < u_diff.rows(); i++)
    {
        for(size_t j = 0; j < u_diff.cols(); j++)
            u_diff(i, j) *= u_diff(i, j); // Square each element of the matrix
    }
    err = sqrt(u_diff.sum());
    count++;
    u = new_u;
}

cout << "3 " << endl;
MatrixXd U = u.asDiagonal();
MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse();
MatrixXd c = p * u;

错误发生在以下行:

MatrixXd M = (Q_tr * X.inverse() * q).asDiagonal();

内容如下:

    run: /usr/include/eigen3/Eigen/src/Core/DenseBase.h:261: void Eigen::DenseBase<Derived>::resize(Eigen::Index, Eigen::Index) [with Derived = Eigen::Diagonal<Eigen::Matrix<double, -1, -1>, 0>; Eigen::Index = long int]: Assertion `rows == this->rows() && cols == this->cols() && "DenseBase::resize() does not actually allow to resize."' failed.
Aborted (core dumped)

有人可以指出为什么会出现这个错误,或者更好,给我一些建议,告诉我如何使用 C++ 将椭圆拟合到一组点?

【问题讨论】:

    标签: c++ pseudocode eigen


    【解决方案1】:

    使用 Eigen,您可以使用 .diagonal() 从矩阵中获取对角向量;您可以使用.asDiagonal() 将向量视为对角矩阵;但是您不能将密集矩阵视为对角矩阵。所以那行应该是

    MatrixXd M = (Q_tr * X.inverse() * q).diagonal(); 
    

    【讨论】:

    • 这完全解决了问题,算法找到了椭圆。看起来这个问题源于我对线性代数而不是 C++ 缺乏理解。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2015-07-13
    • 2012-12-20
    相关资源
    最近更新 更多