c++ - How to fit a bounding ellipse around a set of 2D points -


given set of 2d points (in cartesian form), need find minimum-area ellipse such every point in set lies either on or inside ellipse.

i have found solution in form of pseudo-code on site, attempt implement solution in c++ has been unsuccessful.

the following image illustrates graphically solution problem looks like:

a set of points bounding ellipse

in attempt, used eigen library various operations on matrices.

//the tolerance error in fitting ellipse double tolerance = 0.2; int n = 10; // number of points int d = 2; // dimension matrixxd p = matrixxd::random(d,n); //fill matrix random points  matrixxd q = p; q.conservativeresize(p.rows() + 1, p.cols());  for(size_t = 0; < 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 = 0; < 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 matrix     }     err = sqrt(u_diff.sum());     count++;     u = new_u; }  cout << "3 " << endl; matrixxd u = u.asdiagonal(); matrixxd = (1.0 / (double) d) * (p * u * p.transpose() - (p * u) * (p * u).transpose()).inverse(); matrixxd c = p * u; 

the error occurs on following line:

matrixxd m = (q_tr * x.inverse() * q).asdiagonal(); 

and reads follows:

    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() not allow resize."' failed. aborted (core dumped) 

can please point out why error occurring or better, give me advice on how fit ellipse set of points using c++?

with eigen, can diagonal vector matrix .diagonal(); can treat vector diagonal matrix .asdiagonal(); cannot treat dense matrix diagonal matrix. line should

matrixxd m = (q_tr * x.inverse() * q).diagonal();  

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

delphi - Take screenshot in webcam using VFrames in Console Application -

hibernate - Error accessing field [private java.lang.String entities.Register.numVocher] by reflection for persistent property [entities.Register#numVocher] -