Python OpenCV Ellipse - 最多需要5个参数(给定8个)

时间:2014-08-20 15:12:23

标签: python opencv

我完全不知道为什么在查看文档后我无法使用OpenCV绘制椭圆。

首先我使用CV 2.4.9

>>> cv2.__version__
'2.4.9'
>>>

其次,我试图使用以下内容:

>>> help(cv2.ellipse)
Help on built-in function ellipse in module cv2:

ellipse(...)
    ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[,
lineType[, shift]]]) -> None  or  ellipse(img, box, color[, thickness[, lineType
]]) -> None

我的椭圆看起来如下:

cx,cy = 201,113
ax1,ax2 =  37,27
angle = -108
center = (cx,cy)
axes = (ax1,ax2)

cv2.ellipse(frame, center, axes, angle, 0 , 360, (255,0,0), 2)

然而,运行它给了我以下

>>> cv2.ellipse(frame,center,axes,angle,0,360, (255,0,0), 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ellipse() takes at most 5 arguments (8 given)
>>>

帮助?


编辑:
我想将以下内容用作框架

cap = cv2.VideoCapture(fileLoc)
frame = cap.read()

显然可以使用以下

修复它
pil_im = Image.fromarray(frame)
cv2.ellipse(frame,center,axes,angle,0,360,(255,0,0), 2)
pil_im = Image.fromarray(raw_image)
pil_im.save('C:/Development/export/foo.jpg', 'JPEG') 

4 个答案:

答案 0 :(得分:14)

我有同样的问题,我解决了。我无法运行它的第一行代码是:

cv2.ellipse(ellipMask, (113.9, 155.7), (23.2, 15.2), 0.0, 0.0, 360.0, (255, 255, 255), -1);

我发现轴(以及中心)必须是整数元组,而不是浮点数。 因此下线确定!

cv2.ellipse(ellipMask, (113, 155), (23, 15), 0.0, 0.0, 360.0, (255, 255, 255), -1);

我认为你应该采用正确格式的其他值。

以下是Opencv网站的链接: http://answers.opencv.org/question/30778/how-to-draw-ellipse-with-first-python-function/

答案 1 :(得分:0)

这是我的iPython会话 - 似乎工作得很好:

In [54]: cv2.__version__
Out[54]: '2.4.9'

In [55]: frame = np.ones((400,400,3))

In [56]: cx,cy = 201,113

In [57]: ax1,ax2 =  37,27

In [58]: angle = -108

In [59]: center = (cx,cy)

In [60]: axes = (ax1,ax2)

In [61]: cv2.ellipse(frame, center, axes, angle, 0 , 360, (255,0,0), 2)

In [62]: plt.imshow(frame)
Out[62]: <matplotlib.image.AxesImage at 0x1134ad8d0>

这很有效 - 并产生了以下内容:

enter image description here

所以,有点奇怪......也许你导入cv2模块的方式有些什么?

或者(更有可能)你的frame对象的类型/结构是什么?

答案 2 :(得分:0)

(h, w) = image.shape[:2]
        (cX, cY) = (int(w * 0.5), int(h * 0.5))

        # divide the image into four rectangles/segments (top-left,
        # top-right, bottom-right, bottom-left)
        segments = [(0, cX, 0, cY), (cX, w, 0, cY),
                    (cX, w, cY, h), (0, cX, cY, h)]

        # construct an elliptical mask representing the center of the
        # image
        (axesX, axesY) = (int(w * 0.75) / 2, int(h * 0.75) / 2)
        ellipMask = np.zeros(image.shape[:2], dtype="uint8")
        cv2.ellipse(ellipMask, (int(cX), int(cY)), (int(axesX), int(axesY)), 0, 0, 360, 255, -1)

为我工作

答案 3 :(得分:0)

实际上,如果从0°到360°作图,则可以通过使用单个椭圆参数调用函数来使用浮点数:

ellipse_float = ((113.9, 155.7), (23.2, 15.2), 0.0)
cv2.ellipse(image, ellipse_float, (255, 255, 255), -1);

或单线:

cv2.ellipse(image, ((113.9, 155.7), (23.2, 15.2), 0.0), (255, 255, 255), -1);
# compared to the following which does not work if not grouping the ellipse paramters in a tuple
#cv2.ellipse(image, (113.9, 155.7), (23.2, 15.2), 0.0, 0, 360, (255, 255, 255), -1); # cryptic error

不幸的是,如果您要添加startAngle和stopAngle,则此方法将无效。