<p>我有一个使用OpenCV的网络摄像头,我正在尝试实时拟合一个椭圆。</p>
<p>我目前使用的代码可以工作,但它在很多时候都无法将椭圆拟合到图像中。我还可以采用其他哪些椭圆拟合图像的方法?</p>
<p>当前代码:</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) < 5:
break
elps = cv2.fitEllipse(cont)
return elps #only returns one ellipse for now
return None
</code></pre>
<p>其中<code>elps</code>的形式为<code>(x_centre,y_centre),(minor_axis,major_axis),angle</code></p>
<p>下面是一个例子,我想成功地拟合一个椭圆。当我不想使用此图像时,当前代码将失败。</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>