[英]Python Fit ellipse to an image
我有一个使用OpenCV的网络摄像头,我正在尝试实时拟合椭圆。
我目前使用的代码可以工作,但很多时候它无法在图像中使用椭圆。 我还可以采用哪种椭圆拟合图像的方法?
当前代码:
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
elps
的形式(x_centre,y_centre),(minor_axis,major_axis),angle
以下是我想成功拟合椭圆的示例。 当我不想要它时,我当前的代码会失败。
结果我错了就是从函数中得到第一个椭圆。 虽然我认为第一个计算的椭圆是最正确的椭圆,但我实际要做的就是遍历所有的椭圆 - 并选择最合适的椭圆来限制图像中的对象。
我会在函数之外定义我的轮廓,因为您不需要在此图像中重新定义它们。
def create_ellipse(thresh,cnt):
ellipse = cv2.fitEllipse(cnt)
thresh = cv2.ellipse(thresh,ellipse,(0,255,0),2)
return thresh
这段代码正在做的是我拍摄我的阈值图像流并在其上添加一个椭圆。 稍后在我的代码中,当我想调用它时,我使用该行
thresh = create_ellipse(thresh,cnt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.