How can I get ellipse coefficient from fitEllipse function of OpenCV?

The function fitEllipse returns a RotatedRect that contains all the parameters of the ellipse.

An ellipse is defined by 5 parameters:

  • xc : x coordinate of the center
  • yc : y coordinate of the center
  • a : major semi-axis
  • b : minor semi-axis
  • theta : rotation angle

You can obtain these parameters like:

RotatedRect e = fitEllipse(points);

float xc    = e.center.x;
float yc    = e.center.y;
float a     = e.size.width  / 2;    // width >= height
float b     = e.size.height / 2;
float theta = e.angle;              // in degrees

You can draw an ellipse with the function ellipse using the RotatedRect:

ellipse(image, e, Scalar(0,255,0)); 

or, equivalently using the ellipse parameters:

ellipse(res, Point(xc, yc), Size(a, b), theta, 0.0, 360.0, Scalar(0,255,0));

If you need the values of the coefficients of the implicit equation, you can do like (from Wikipedia):

enter image description here

So, you can get the parameters you need from the RotatedRect, and you don’t need to change the function fitEllipse.
The solve function is used to solve linear systems or least-squares problems. Using the SVD decomposition method the system can be over-defined and/or the matrix src1 can be singular.

For more details on the algorithm, you can see the paper of Fitzgibbon that proposed this fit ellipse method.

Leave a Comment