Python Fit ellipse to an image
<p>I have a webcam feed using OpenCV, and I am trying to fit an ellipse in real time. </p>

<p>The code I am using at the moment works, but it fails to fit an ellipse to the image a lot of the time. What other methods of ellipse fitting to an image can I pursue?</p>

<p>Current code:</p>

<pre><code>def find_ellipses(img): #img is grayscale image of what I want to fit
        ret,thresh = cv2.threshold(img,127,255,0)
        _,contours,hierarchy = cv2.findContours(thresh, 1, 2)

        if len(contours) != 0:
            for cont in contours:
                if len(cont) &lt; 5:
                    break
                elps = cv2.fitEllipse(cont)
                return elps  #only returns one ellipse for now
        return None
</code></pre>

<p>Where <code>elps</code> is of the form <code>(x_centre,y_centre),(minor_axis,major_axis),angle</code></p>

<p>Here is an example of what I want to successfully fit an ellipse to. My current code fails with this image when I don't want it to.</p>

<p><a href="https://i.stack.imgur.com/FeSHf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FeSHf.png" alt="enter image description here"></a></p>