Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OpenCV Ellipse - takes at most 5 arguments (8 given)

Tags:

python

opencv

I'm at a total loss here on why I can't draw an ellipse with OpenCV after looking at documentation.

First I'm using CV 2.4.9

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

Second, I'm attempting to use the following:

>>> 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

My ellipse looks like the following:

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)

However, running that gives me the following

>>> 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)
>>>

Help?


Edit:
I wanted to use the following as a frame

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

Apparently it can be fixed by using the following

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') 
like image 633
ist_lion Avatar asked Aug 20 '14 15:08

ist_lion


3 Answers

I Had same problem and I solved it. My first line of code that I couldn't run it was:

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

I found that axes (as well as center) has to be an integers tuple, not floats. Therefore below line was OK!

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

I think you should take are of other values to be in correct format.

Here is the link from Opencv's website: http://answers.opencv.org/question/30778/how-to-draw-ellipse-with-first-python-function/

like image 53
hassan ketabi Avatar answered Oct 18 '22 06:10

hassan ketabi


Here's my iPython session - which seemed to work fine:

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>

This worked - and generated the following:

enter image description here

So, a bit strange... Maybe there is something in the way you have imported the cv2 module?

Or (more likely) exactly what is the type/structure of your frame object?

like image 41
timlukins Avatar answered Oct 18 '22 05:10

timlukins


Actually, if you plot from 0° to 360°, you could use floats, by calling the function with a single ellipse parameter:

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

or in a one-liner:

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

This won't work if you want to add startAngle and stopAngle unfortunately.

like image 1
PJ127 Avatar answered Oct 18 '22 07:10

PJ127