How to get start and end coordinates (x, y) of major axis of a rotating ellipse in opencv?
<p>I am performing motion tracking of an object, and I am trying to identify the front and back of the object. The object is asymmetrical, which means that the centroid of the contour is closer to the front than the back. Using this information, I am approaching this as follows:</p>

<ol>
<li><p>Draw contours of object</p></li>
<li><p>Find centroid  </p>

<pre><code>centroidx, centroidy = int(moments['m10']/moments['m00']), int(moments['m10']/moments['m00'])</code></pre> </li>
<li><p>Draw bounding ellipse</p>

<pre><code>cv2.fitEllipse(contour)</code></pre> </li>
<li><p>Calculate major axis length as follows (and as shown in the figure)</p>

<pre><code>MAx, MAy = int(0.5 * ellipseMajorAxisx*math.sin(ellipseAngle)), int(0.5 * ellipseMajorAxisy*math.cos(ellipseAngle))</code></pre> </li>
<li><p>Calculate beginning and ending x, y coordinates of the major axis</p>

<pre><code>MAxtop, MAytop = int(ellipseCentrex + MAx), int(ellipseCentrey + MAy)
MAxbot, MAybot = int(ellipseCentrex - MAx), int(ellipseCentrey - MAy)</code></pre></li>
<li><p>Identify which of the points is closer to the centroid of the contour</p>

<pre><code>distancetop = math.sqrt((centroidx - MAxtop)**2 + (centroidy - MAytop)**2)
distancebot = math.sqrt((centroidx - MAxbot)**2 + (centroidy - MAybot)**2)
min(distancetop, distancebot)</code></pre></li>
</ol>

<p>The problem I am encountering is, while I get the "front" end of the ellipse correct most of the time, occasionally the point is a little bit away. As far as I have observed, this seems to be happening such that the x value is correct, but y value is different (in effect, I think this represents the major axis of an ellipse that is perpendicular to mine). I am not sure if this is an issue with opencv's calculation of angles or (more than likely) my calculations are incorrect. I do realize this is a complicated example, hope my figures help!</p>

<p><a href="https://i.stack.imgur.com/Aq50z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Aq50z.jpg" alt="Position1"></a>
<a href="https://i.stack.imgur.com/zUJcP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zUJcP.jpg" alt="Position2"></a></p>

<p>EDIT: When I get the wrong point, it is not from a perpendicular ellipse, but of a mirror image of my ellipse. And it happens with the x values too, not just y.</p>

<p>After following ssm's suggestion below, I am getting the desired point <em>most of the time</em>. The point still goes wrong occasionally, but "snaps back" into place soon after. For example, this is a few frames when this happens:
<a href="https://i.stack.imgur.com/pfdee.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pfdee.jpg" alt="improved"></a></p>

<p>By the way, the above images are after "correcting" for angle by using this code: </p>

<pre><code>        if angle &gt; 90:
            angle = 180 - angle
</code></pre>

<p>If I do not do the correction, I get the wrong point at other times, as shown below for the same frames.
<a href="https://i.stack.imgur.com/3q4IE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3q4IE.jpg" alt="angle not corrected"></a></p>

<p>So it looks like I get it right for some angles with angle correction and the other angles without correction. How do I get all the right points in both conditions?</p>

<p>(White dot inside the ellipse is the centroid of the contour, whereas the dot on or outside the ellipse is the point I am getting)</p>