Python OpenCV椭圆-最多接受5个参数(给定8个)

2024-06-21 20:42:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我完全不明白为什么在看了文档之后不能用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') 

Tags: inimgpilcv2framecolor椭圆module
3条回答

&13;
&13;
(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)

对我有用

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

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/

这是我的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对象的类型/结构是什么?

相关问题 更多 >