<Python, openCV> How can I use cv2.ellipse?
OpenCV2 for python has 2 functions
[Function 1]
- Python: cv2.ellipse (img, center, axes, angle, startAngle, endAngle, color [, thickness [, lineType [, shift]]]) → None
[Function 2]
- Python: cv2.ellipse (img, box, color [, thickness [, lineType]]) → None
I want to use [Function 1]
But when I use this code
cv2.ellipse (ResultImage, Circle, Size, Angle, 0, 360, Color, 2, cv2.CV_AA, 0)
Raises
TypeError: ellipse () accepts no more than 5 arguments (10 data)
could you help me?
The fact that Python does not support multicast by default does not help here: having two functions with the same name but with different parameters is not python. Therefore, the question arises: how can cv2 guess the version that we would like to name? I could not find an explicit document for this.
Anyway, after I faced the same problem with opencv 3.0.0-beta and Python 3.4.2, I finally found out that in my case one of the points of the circle was a float , and although I ran official code samples with 8 parameters, for some reason cv2 defaults to the 5-args function. Using int fixed the problem, so the error message was rather erroneous.
I think moving from Python 2 to 3 can lead to confusion in existing code, since integer divs return a float in Python 3.
Make sure that all parameters of the ellipse are int , otherwise it rises "TypeError: ellipse () takes no more than 5 arguments (10 data)". Had the same problem and set int parameters, fixed it.
Note that in Python you need to round the number first, and then use int (), since the int function will reduce the number:
x = 2.7, int (x) will be 2 not 3
I encountered the same error, and it turned out that I did not pass the correct minimum number of parameters (7) to the startAngle / endAngle form of the method. In my case, I did not have the "angle" parameter (the angle of rotation of the ellipse), which precedes the startAngle and endAngle parameters.
I assume your Circle or Size options are wrong ... they should be tuples, (x, y) for the center and (width, height) for the axes
cv2.ellipse(ResultImage, (centerX,centerY), (width,height), 0, 0, 180, yellow, 2) Other answers correctly state that invoking the [Function 1] version requires the use of int coordinates in some arguments ( center and axes , in particular). However, they do not mention that you can use the shift argument to maintain the precision of the “fractional bit” in the coordinates for sub-chain resolution.
Here's an example wrapper function for cv2.ellipse that can take float coordinates and pass them to int after overwriting them for use with the shift argument:
def draw_ellipse( img, center, axes, angle, startAngle, endAngle, color, thickness=3, lineType=cv2.LINE_AA, shift=10): center = ( int(round(center[0] * 2**shift)), int(round(center[1] * 2**shift)) ) axes = ( int(round(axes[0] * 2**shift)), int(round(axes[1] * 2**shift)) ) cv2.ellipse( img, center, axes, angle, startAngle, endAngle, color, thickness, lineType, shift) The shift parameter specifies the number of “fractional bits” in the center and axes coordinates, so why are coordinates multiplied by degrees 2 (multiplying by 2 coincides with shifting the bits into their integer binary representations by one by one on the left.) This shift trick is also convenient for many others opencv functions, but its use is not very well documented (especially in python).