After reading the docs and some examples I figured out the way it is supposed to be done.
These are the steps :
- Collecting the points around which you plan to draw ellipse.
I have done this using a C++ std::vector
. - Declare a
Rotated Rect
variable. - Initialize this variable using the value returned by
cv::fitEllipse
method. - Next in line is to find the
center
and size
of the Rotated Rect
. - Then we draw the ellipse .
cv::RotatedRect _Ellipse['value you want'];
_Ellipse[i] = cv::fitEllipse(points_vector);
cv::Point2f center = cv::Point2f(_Ellipse[i].center.x ,_Ellipse[i].center.y );
cv::Size2f size = cv::Size2f(_Ellipse[i].size.width,_Ellipse[i].size.width);
cv::Size2f size = cv::Size2f(_Ellipse[i].size.width,_Ellipse[i].size.height);
cv::RotatedRect ellipse_rect = cv::RotatedRect(center, size, _Ellipse[i].angle);
cv::ellipse(ref_image, ellipse_rect, cv::Scalar(0,0,255), -1, 8);
I am still not completely sure about this because it is drawing a circle instead of ellipse, but this is like the gist of what is to be done.
Thanks.
EDIT
I made a silly mistake while defining Size2f
(width
instead of height
) . Now I am getting proper ellipse.
You Can use cv::fitellipse function to do that. Here's an example how to use it:
Link
Write something like:
cv::RotatedRect rect = cv::fitEllipse(yourPoints)
All Information of the Ellipse are then stored in the RotatedRect e.g.: float centerX = rect.center.x; float a = rect.size.width;
However, as far as i know the fitellipse() function is currently bugged meaning that in some cases it fails returning the correct ellipse.