Python OpenCV椭圆-最多接受5个参数(给定8个)
在看了文档之后,我完全不明白为什么不能用OpenCV绘制椭圆 首先,我使用CV 2.4.9Python OpenCV椭圆-最多接受5个参数(给定8个),python,opencv,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, startAn
>>> 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)
我的椭圆如下所示:
>>> 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)
>>>
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')
显然,它可以通过以下方法修复
>>> 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)
>>>
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')
这是我的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>
[54]中的:cv2.\u版本__
Out[54]:“2.4.9”
在[55]中:frame=np.one((400400,3))
In[56]:cx,cy=201113
[57]中:ax1,ax2=37,27
在[58]中:角度=-108
在[59]中:中心=(cx,cy)
在[60]中:轴=(ax1、ax2)
在[61]中:cv2.椭圆(框架,中心,轴,角度,0,360,(255,0,0),2)
In[62]:plt.imshow(帧)
出[62]:
这起到了作用,并产生了以下结果:
>>> 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
模块的方式可能有问题
或者(更可能)您的
框架的类型/结构是什么?我遇到了同样的问题,我解决了它。我无法运行的第一行代码是:
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网站的链接:
(h,w)=图像.形状[:2]
(cX,cY)=(int(w*0.5),int(h*0.5))
#将图像分成四个矩形/段(左上角,
#右上、右下、左下)
段=[(0,cX,0,cY),(cX,w,0,cY),
(cX,w,cY,h),(0,cX,cY,h)]
#构造一个椭圆形遮罩,表示图像的中心
#形象
(axesX,axesY)=(int(w*0.75)/2,int(h*0.75)/2)
ellipMask=np.zero(image.shape[:2],dtype=“uint8”)
cv2.Ellipsk(ellipMask,(int(cX),int(cY)),(int(axesX),int(axesY)),0,0,360,255,-1)
实际上,如果从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,这将不起作用。我只是在我的终端上运行了这段代码,它也能起作用。我的猜测是frame
变量有问题。我有完全相同的问题,奇怪的是,它在某些情况下有效,而在其他情况下无效。在迁移到python 3.6时,我遇到了一些原本在python 2.7中运行良好的代码。我的代码进行除法来计算轴的值,结果是integer在2.7中,在Python3中变为float(Python3除法默认为float),它们必须是元组,而不是列表。否则,同样的,误导性的,错误。