I want to extract the red ball from one picture and get the detected ellipse matrix in picture.
I threshold the picture, find the contour of red ball by using findContour() function and use fitEllipse() to fit an ellipse.
But what I want is to get coefficient of this ellipse. Because the fitEllipse() return a rotation rectangle (RotatedRect), so I need to re-write this function.
One Ellipse can be expressed as Ax^2 + By^2 + Cxy + Dx + Ey + F = 0; So I want to get u=(A,B,C,D,E,F) or u=(A,B,C,D,E) if F is 1 (to construct an ellipse matrix).
I read the source code of fitEllipse(), there are totally three SVD process, I think I can get the above coefficients from the results of those three SVD process. But I am quite confused what does each result (variable cv::Mat x) of each SVD process represent and why there are three SVD here?
Here is this function:
cv::RotatedRect cv::fitEllipse( InputArray _points ) { Mat points = _points.getMat(); int i, n = points.checkVector(2); int depth = points.depth(); CV_Assert( n >= 0 && (depth == CV_32F || depth == CV_32S)); RotatedRect box; if( n (); const Point2f* ptsf = points.ptr(); AutoBuffer _Ad(n*5), _bd(n); double *Ad = _Ad, *bd = _bd; // first fit for parameters A - E Mat A( n, 5, CV_64F, Ad ); Mat b( n, 1, CV_64F, bd ); Mat x( 5, 1, CV_64F, gfp ); for( i = 0; i min_eps ) t = gfp[2]/sin(-2.0 * rp[4]); else // ellipse is rotated by an integer multiple of pi/2 t = gfp[1] - gfp[0]; rp[2] = fabs(gfp[0] + gfp[1] - t); if( rp[2] > min_eps ) rp[2] = std::sqrt(2.0 / rp[2]); rp[3] = fabs(gfp[0] + gfp[1] + t); if( rp[3] > min_eps ) rp[3] = std::sqrt(2.0 / rp[3]); box.center.x = (float)rp[0] + c.x; box.center.y = (float)rp[1] + c.y; box.size.width = (float)(rp[2]*2); box.size.height = (float)(rp[3]*2); if( box.size.width > box.size.height ) { float tmp; CV_SWAP( box.size.width, box.size.height, tmp ); box.angle = (float)(90 + rp[4]*180/CV_PI); } if( box.angle 360 ) box.angle -= 360; return box; }
The source code link: https://github.com/Itseez/opencv/blob/master/modules/imgproc/src/shapedescr.cpp