OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Fri, 09 Oct 2015 16:44:59 -0500Angles in ellipse() functionhttp://answers.opencv.org/question/14541/angles-in-ellipse-function/Hi,
I am trying to draw an ellipse using OpenCV. But I have difficulty in understanding its angle arguments. Even it is not clear from the picture in OpenCV docs.
I tried following line:
cv2.ellipse(img,(256,256),(200,100),0,90,180,(0,0,255),4,cv2.CV_AA)
I got the result as below:
![image description](/upfiles/13702492172353122.png)
That means `startangle` is taken from positive x axis in clockwise direction and that is same for `endangle`.
**Now I tried this :**
cv2.ellipse(img,(256,256),(200,100),0,45,225,(0,0,255),4,cv2.CV_AA)
I got the output as below :
![image description](/upfiles/13702495225245992.png)
See, the `startangle` and `endangle` is not 45 and 225 here.
**What is the problem here? Did I miss something ? Or is it a bug?**
Mon, 03 Jun 2013 03:54:17 -0500http://answers.opencv.org/question/14541/angles-in-ellipse-function/Answer by Guanta for <p>Hi,</p>
<p>I am trying to draw an ellipse using OpenCV. But I have difficulty in understanding its angle arguments. Even it is not clear from the picture in OpenCV docs.</p>
<p>I tried following line:</p>
<pre><code>cv2.ellipse(img,(256,256),(200,100),0,90,180,(0,0,255),4,cv2.CV_AA)
</code></pre>
<p>I got the result as below:</p>
<p><img alt="image description" src="/upfiles/13702492172353122.png"></p>
<p>That means <code>startangle</code> is taken from positive x axis in clockwise direction and that is same for <code>endangle</code>.</p>
<p><strong>Now I tried this :</strong></p>
<pre><code>cv2.ellipse(img,(256,256),(200,100),0,45,225,(0,0,255),4,cv2.CV_AA)
</code></pre>
<p>I got the output as below :</p>
<p><img alt="image description" src="/upfiles/13702495225245992.png"></p>
<p>See, the <code>startangle</code> and <code>endangle</code> is not 45 and 225 here. </p>
<p><strong>What is the problem here? Did I miss something ? Or is it a bug?</strong></p>
http://answers.opencv.org/question/14541/angles-in-ellipse-function/?answer=14562#post-id-14562It is not a bug, the reason is that it is measured in arc-angles from an ellipse, not from a circle. I ran in the same troubles some weeks ago. The phenomen is visualized in paragraph (59) of [http://mathworld.wolfram.com/Ellipse.html](http://mathworld.wolfram.com/Ellipse.html) . It follows that you cannot compute the angle via atan2(y-center.y,x-center.x) since the points are not lying on a circle.
I had the problem that I wanted to draw an elliptical arc given the extremal points and all parameters of the ellipse except start and end angle via OpenCV, my solution was to convert the ellipse to a polygon and cut out the area where my extremal-points were located, so after cv::ellipse2poly():
void cutOut(const std::vector<cv::Point> & pts,
std::vector<cv::Point> & poly,
const cv::Point2d & ext1, const cv::Point2d & ext2) const
{
// whole ellipse
if ( ext1 == cv::Point2d(0.0,0.0)
&& ext2 == cv::Point2d(0.0,0.0) )
{
return;
}
// get closest points on ellipse to given extremal points
double min1 = std::numeric_limits<double>::max();
double min2 = std::numeric_limits<double>::max();
int min_ind1, min_ind2;
for( size_t i = 0; i < pts.size(); i++ ) {
double n1 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext1);
if ( n1 < min1 ) {
min_ind1 = i;
min1 = n1;
}
double n2 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext2);
if ( n2 < min2 ) {
min_ind2 = i;
min2 = n2;
}
}
// copy points to poly
if ( min_ind2 == min_ind1 ) {
poly = pts;
}
if ( min_ind2 < min_ind1 ) {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.end());
poly.insert(poly.end(),pts.begin(), pts.begin()+min_ind2+1);
} else {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.begin()+min_ind2+1);
}
}
I guess from this post: [http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes](http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes) you can derive a direct solution?!
Please let me know, if you know a better (= mathematical) solution than my quick & dirty hack ;) .Mon, 03 Jun 2013 07:02:05 -0500http://answers.opencv.org/question/14541/angles-in-ellipse-function/?answer=14562#post-id-14562Comment by Abid Rahman K for <p>It is not a bug, the reason is that it is measured in arc-angles from an ellipse, not from a circle. I ran in the same troubles some weeks ago. The phenomen is visualized in paragraph (59) of <a href="http://mathworld.wolfram.com/Ellipse.html"></a><a href="http://mathworld.wolfram.com/Ellipse.html">http://mathworld.wolfram.com/Ellipse.html</a> . It follows that you cannot compute the angle via atan2(y-center.y,x-center.x) since the points are not lying on a circle. </p>
<p>I had the problem that I wanted to draw an elliptical arc given the extremal points and all parameters of the ellipse except start and end angle via OpenCV, my solution was to convert the ellipse to a polygon and cut out the area where my extremal-points were located, so after cv::ellipse2poly():</p>
<pre><code>void cutOut(const std::vector<cv::Point> & pts,
std::vector<cv::Point> & poly,
const cv::Point2d & ext1, const cv::Point2d & ext2) const
{
// whole ellipse
if ( ext1 == cv::Point2d(0.0,0.0)
&& ext2 == cv::Point2d(0.0,0.0) )
{
return;
}
// get closest points on ellipse to given extremal points
double min1 = std::numeric_limits<double>::max();
double min2 = std::numeric_limits<double>::max();
int min_ind1, min_ind2;
for( size_t i = 0; i < pts.size(); i++ ) {
double n1 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext1);
if ( n1 < min1 ) {
min_ind1 = i;
min1 = n1;
}
double n2 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext2);
if ( n2 < min2 ) {
min_ind2 = i;
min2 = n2;
}
}
// copy points to poly
if ( min_ind2 == min_ind1 ) {
poly = pts;
}
if ( min_ind2 < min_ind1 ) {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.end());
poly.insert(poly.end(),pts.begin(), pts.begin()+min_ind2+1);
} else {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.begin()+min_ind2+1);
}
}
</code></pre>
<p>I guess from this post: <a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes"></a><a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes">http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes</a> you can derive a direct solution?!</p>
<p>Please let me know, if you know a better (= mathematical) solution than my quick & dirty hack ;) .</p>
http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=14685#post-id-14685+1 - Thank you. Hmm. it all looks a little dirty. Actually for drawing functions, circular angles were more better.Wed, 05 Jun 2013 04:11:26 -0500http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=14685#post-id-14685Comment by Guanta for <p>It is not a bug, the reason is that it is measured in arc-angles from an ellipse, not from a circle. I ran in the same troubles some weeks ago. The phenomen is visualized in paragraph (59) of <a href="http://mathworld.wolfram.com/Ellipse.html"></a><a href="http://mathworld.wolfram.com/Ellipse.html">http://mathworld.wolfram.com/Ellipse.html</a> . It follows that you cannot compute the angle via atan2(y-center.y,x-center.x) since the points are not lying on a circle. </p>
<p>I had the problem that I wanted to draw an elliptical arc given the extremal points and all parameters of the ellipse except start and end angle via OpenCV, my solution was to convert the ellipse to a polygon and cut out the area where my extremal-points were located, so after cv::ellipse2poly():</p>
<pre><code>void cutOut(const std::vector<cv::Point> & pts,
std::vector<cv::Point> & poly,
const cv::Point2d & ext1, const cv::Point2d & ext2) const
{
// whole ellipse
if ( ext1 == cv::Point2d(0.0,0.0)
&& ext2 == cv::Point2d(0.0,0.0) )
{
return;
}
// get closest points on ellipse to given extremal points
double min1 = std::numeric_limits<double>::max();
double min2 = std::numeric_limits<double>::max();
int min_ind1, min_ind2;
for( size_t i = 0; i < pts.size(); i++ ) {
double n1 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext1);
if ( n1 < min1 ) {
min_ind1 = i;
min1 = n1;
}
double n2 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext2);
if ( n2 < min2 ) {
min_ind2 = i;
min2 = n2;
}
}
// copy points to poly
if ( min_ind2 == min_ind1 ) {
poly = pts;
}
if ( min_ind2 < min_ind1 ) {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.end());
poly.insert(poly.end(),pts.begin(), pts.begin()+min_ind2+1);
} else {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.begin()+min_ind2+1);
}
}
</code></pre>
<p>I guess from this post: <a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes"></a><a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes">http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes</a> you can derive a direct solution?!</p>
<p>Please let me know, if you know a better (= mathematical) solution than my quick & dirty hack ;) .</p>
http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=72744#post-id-72744well, I haven't provided a mathematical way to draw only the points necessary, instead you need to compute all points and afterwards cut those points out which you are interested in.Fri, 09 Oct 2015 16:44:59 -0500http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=72744#post-id-72744Comment by volodia for <p>It is not a bug, the reason is that it is measured in arc-angles from an ellipse, not from a circle. I ran in the same troubles some weeks ago. The phenomen is visualized in paragraph (59) of <a href="http://mathworld.wolfram.com/Ellipse.html"></a><a href="http://mathworld.wolfram.com/Ellipse.html">http://mathworld.wolfram.com/Ellipse.html</a> . It follows that you cannot compute the angle via atan2(y-center.y,x-center.x) since the points are not lying on a circle. </p>
<p>I had the problem that I wanted to draw an elliptical arc given the extremal points and all parameters of the ellipse except start and end angle via OpenCV, my solution was to convert the ellipse to a polygon and cut out the area where my extremal-points were located, so after cv::ellipse2poly():</p>
<pre><code>void cutOut(const std::vector<cv::Point> & pts,
std::vector<cv::Point> & poly,
const cv::Point2d & ext1, const cv::Point2d & ext2) const
{
// whole ellipse
if ( ext1 == cv::Point2d(0.0,0.0)
&& ext2 == cv::Point2d(0.0,0.0) )
{
return;
}
// get closest points on ellipse to given extremal points
double min1 = std::numeric_limits<double>::max();
double min2 = std::numeric_limits<double>::max();
int min_ind1, min_ind2;
for( size_t i = 0; i < pts.size(); i++ ) {
double n1 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext1);
if ( n1 < min1 ) {
min_ind1 = i;
min1 = n1;
}
double n2 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext2);
if ( n2 < min2 ) {
min_ind2 = i;
min2 = n2;
}
}
// copy points to poly
if ( min_ind2 == min_ind1 ) {
poly = pts;
}
if ( min_ind2 < min_ind1 ) {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.end());
poly.insert(poly.end(),pts.begin(), pts.begin()+min_ind2+1);
} else {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.begin()+min_ind2+1);
}
}
</code></pre>
<p>I guess from this post: <a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes"></a><a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes">http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes</a> you can derive a direct solution?!</p>
<p>Please let me know, if you know a better (= mathematical) solution than my quick & dirty hack ;) .</p>
http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=72648#post-id-72648@Guanta, what you mean - better solution?Thu, 08 Oct 2015 08:31:44 -0500http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=72648#post-id-72648Comment by Guanta for <p>It is not a bug, the reason is that it is measured in arc-angles from an ellipse, not from a circle. I ran in the same troubles some weeks ago. The phenomen is visualized in paragraph (59) of <a href="http://mathworld.wolfram.com/Ellipse.html"></a><a href="http://mathworld.wolfram.com/Ellipse.html">http://mathworld.wolfram.com/Ellipse.html</a> . It follows that you cannot compute the angle via atan2(y-center.y,x-center.x) since the points are not lying on a circle. </p>
<p>I had the problem that I wanted to draw an elliptical arc given the extremal points and all parameters of the ellipse except start and end angle via OpenCV, my solution was to convert the ellipse to a polygon and cut out the area where my extremal-points were located, so after cv::ellipse2poly():</p>
<pre><code>void cutOut(const std::vector<cv::Point> & pts,
std::vector<cv::Point> & poly,
const cv::Point2d & ext1, const cv::Point2d & ext2) const
{
// whole ellipse
if ( ext1 == cv::Point2d(0.0,0.0)
&& ext2 == cv::Point2d(0.0,0.0) )
{
return;
}
// get closest points on ellipse to given extremal points
double min1 = std::numeric_limits<double>::max();
double min2 = std::numeric_limits<double>::max();
int min_ind1, min_ind2;
for( size_t i = 0; i < pts.size(); i++ ) {
double n1 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext1);
if ( n1 < min1 ) {
min_ind1 = i;
min1 = n1;
}
double n2 = cv::norm(cv::Point2d(pts[i].x,pts[i].y) - ext2);
if ( n2 < min2 ) {
min_ind2 = i;
min2 = n2;
}
}
// copy points to poly
if ( min_ind2 == min_ind1 ) {
poly = pts;
}
if ( min_ind2 < min_ind1 ) {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.end());
poly.insert(poly.end(),pts.begin(), pts.begin()+min_ind2+1);
} else {
poly.insert(poly.begin(), pts.begin()+min_ind1, pts.begin()+min_ind2+1);
}
}
</code></pre>
<p>I guess from this post: <a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes"></a><a href="http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes">http://stackoverflow.com/questions/197649/how-to-calculate-center-of-an-ellipse-by-two-points-and-radius-sizes</a> you can derive a direct solution?!</p>
<p>Please let me know, if you know a better (= mathematical) solution than my quick & dirty hack ;) .</p>
http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=14690#post-id-14690Yeah, I don't like it much either and would appreciate a better solution, too.Wed, 05 Jun 2013 05:13:07 -0500http://answers.opencv.org/question/14541/angles-in-ellipse-function/?comment=14690#post-id-14690