Python将椭圆拟合到图像
我有一个使用OpenCV的网络摄像头提要,我正在尝试实时拟合椭圆 我目前使用的代码可以工作,但它在很多时候都无法将椭圆拟合到图像中。我还可以采用哪些其他的椭圆拟合图像的方法 当前代码:Python将椭圆拟合到图像,python,opencv,ellipse,Python,Opencv,Ellipse,我有一个使用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.fi
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
def find_ellipes(img):#img是我想要适合的灰度图像
ret,thresh=cv2。阈值(img,127255,0)
_,等高线,层次=cv2.findContours(阈值,1,2)
如果len(等高线)!=0:
对于cont in等高线:
如果长度(续)<5:
打破
elps=cv2.FIT椭圆(续)
return elps#目前只返回一个椭圆
一无所获
其中elps
的形式为(x\u中心,y\u中心),(短轴,长轴),角度
下面是一个我想要成功拟合椭圆的例子。当我不想使用此图像时,我的当前代码将失败
我将在函数之外定义轮廓,因为您不需要在此图像中不断重新定义轮廓
def create_ellipse(thresh,cnt):
ellipse = cv2.fitEllipse(cnt)
thresh = cv2.ellipse(thresh,ellipse,(0,255,0),2)
return thresh
这段代码所做的是我获取我的thresh图像流,并在上面添加一个椭圆。稍后在我的代码中,当我想调用它时,我会使用
thresh = create_ellipse(thresh,cnt)
结果证明我错了,只是从函数中得到了第一个椭圆。虽然我认为第一个计算出的椭圆是最正确的,但实际上我要做的是遍历所有的椭圆,然后选择最合适的一个,将图像中的对象限定在一起。图像正在不断更新(这是一个实时网络摄像头提要)。我遇到的问题是,它无法找到适合此图像的轮廓。