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

前端 未结 2 555
逝去的感伤
逝去的感伤 2020-12-05 06:00

I want to extract the red ball from one picture and get the detected ellipse matrix in picture.

Here is my example:

I threshold the picture, find the contou

2条回答
  •  独厮守ぢ
    2020-12-05 06:52

    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):

    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.

提交回复
热议问题