user3704922
user3704922

Reputation: 73

How draw axis of ellipse

I am using fitellipse of Opencv and C++, and I'm getting these values:

/// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect( contours.size() );
vector<RotatedRect> minEllipse( contours.size() );

for( int i = 0; i < contours.size(); i++ )
{
   minRect[i] = minAreaRect( Mat(contours[i]) );

   if( contours[i].size() > 5 )
      minEllipse[i] = fitEllipse( Mat(contours[i]) );

   // ...
}

float xc    = minEllipse[element].center.x;
float yc    = minEllipse[element].center.y;
float a     = minEllipse[element].size.width  / 2;   
float b     = minEllipse[element].size.height / 2;
float theta = minEllipse[element].angle; 

But with these values how can I draw the axis of an ellipse, for example of the following ellipse? enter image description here

NOTE: Element is an ellipse stored in minEllipse.

Upvotes: 3

Views: 3010

Answers (3)

Bob__
Bob__

Reputation: 12799

You are probably looking for those formulas:

ct = cos(theta)
st = sin(theta)

LongAxix0.x = xc - a*ct
LongAxis0.y = yc - a*st
LongAxis1.x = xc + a*ct
LongAxix1.y = yc + a*st

ShortAxix0.x = xc - b*st
ShortAxix0.y = yc + b*ct
ShortAxis1.x = xc + b*st
ShortAxix2.y = yc - b*ct

Upvotes: 3

Ziezi
Ziezi

Reputation: 6477

But with these values how can I draw the axis of an ellipse?

The axis of the ellipse are passing through its centre:

float xc = minEllipse[element].center.x;
float yc = minEllipse[element].center.y;

the start and end points of the axis could be at an offset from the centre defined by the ellipse's width and height, i.e.:

// horizontal axis start/ end point

// coordinates
int HxStart = xc - size.width  / 2;
int HyStart = yc;

int HxEnd = xc + size.width  / 2;
int HyEnd = yc;

// points
Point Hstart(HxStart, HyStart);
Point Hend(HxEnd, HyEnd);

// horizontal axis
Line horizontalAxis(Hstart, Hend);

// vertical axis start/ end point
int VxStart = xc;
int VyStart = yc - size.height  / 2;

int VxEnd = xc;
int VyEnd = yc + size.height  / 2;

// ----//----

Now, you can rotate the axis (the above for points) by the provided angle theta, around the centre of the ellipse.

Having the above and knowing how to construct a line you can build the two axis at any given angle theta.

Upvotes: 0

Jan R&#252;egg
Jan R&#252;egg

Reputation: 10075

You can use minEllipse[element].points to get the four corners of the rotated bounding rectangle, like described here.

Then you only need to calculate the average of the two points on each side of the rectangle to get the endpoints for the axes...

Point2f vertices[4];
minEllipse[element].points(vertices);
line(image, (vertices[0] + vertices[1])/2, (vertices[2] + vertices[3])/2, Scalar(0,255,0));
line(image, (vertices[1] + vertices[2])/2, (vertices[3] + vertices[0])/2, Scalar(0,255,0));

Upvotes: 4

Related Questions