问题描述
我在Windows 8.1上使用OpenCV 2.4.11和Python 2.7.9。我试图将椭圆放到我的轮廓上,我遇到了一些我无法弄清楚的东西。
I am using OpenCV 2.4.11 with Python 2.7.9 on Windows 8.1. I was trying to fit ellipses onto my contours and I came across something that I can't figure out.
当我调用cv2.fitEllipse并获取返回值然后使用以下代码将返回值直接传递给cv2.ellipse,绘制到屏幕上的省略号是完美的,并且在我的轮廓周围做出最佳拟合:
When I call cv2.fitEllipse and get the return value and then pass the return value directly into cv2.ellipse with the following code, the ellipses drawn onto the screen are perfect and make the optimal fit around my contours:
contours, hierarchy = cv2.findContours(binaryimage,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for ind, cont in enumerate(contours):
elps = cv2.fitEllipse(cont)
#Feed elps directly into cv2.ellipse
cv2.ellipse(displayframe,elps,(0,0,255))
cv2.imshow("Perfectly fitted ellipses", displayframe)
以上结果是
但是,当我尝试解析实际的椭圆参数并通过传入t手动绘制椭圆软管参数(见下面的代码),它创建了一个椭圆形的膨胀版本,在轮廓周围给出了一个非常舒适(但令人烦恼)的空间支架,如下所示:
However, when I try to parse the actual ellipse parameters and draw the ellipse manually by passing in those parameters (see code below), it creates a "bloated" version of the ellipse that gives itself a very comfortable (yet annoying) bracket of space around the contour, as follows:
contours, hierarchy = cv2.findContours(binaryimage,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for ind, cont in enumerate(contours):
(x,y),(MA,ma),angle = cv2.fitEllipse(cont)
#feed the parsed parameters into cv2.ellipse
cv2.ellipse(displayframe,(x,y),(MA, ma),angle,0,360,(0,0,255))
cv2.imshow("Ellipses NOT fitting the contours properly",displayframe)
这种烦人现象的结果是:
The results for this annoying phenomena are:
是的,我知道使用第一种方法可以解决问题绘制正确的省略号。但是我想知道它为什么要这样做,因为实际上,我需要椭圆的参数才能进行一些blob跟踪,如果解析的参数最终会得到像这样宽泛的不适合的省略号,它真的吗准确?是否有cv2.ellipse()绘图功能的问题?什么出错了?来自cv2.fitEllipse函数的椭圆参数是否准确?
Yes, I know that using the first method solves the problem drawing the correct ellipses. But I want to know why it is doing this because, in actual fact, I will need the parameters of the ellipses in order to do some blob tracking and if the parameters that are parsed end up giving wide unfitting ellipses like that, is it really accurate? Is the problem with the cv2.ellipse() drawing function? Any ideas on what is going wrong? Are the ellipse parameters coming out of the cv2.fitEllipse function accurate?
推荐答案
您正在绘制返回边界的宽度和高度框。您应绘制一半宽度和高度,因为椭圆的轴将是其边界框的一半宽度和高度
You are plotting the width and height of the returned bounding box. You should plot half width and height, since the axes of the ellipse would be half width and height of its bounding box
cv2.ellipse(displayframe,(x,y),(MA/2, ma/2),angle,0,360,(0,0,255))
这篇关于使用带有不同参数的cv2.ellipse时椭圆拟合的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!