Understanding the ellipse parameters in Open CV using Python

Viewed 5793

I am trying to draw an arc using Open CV, using cv2.ellipse function I tried reading the documentation for the same, but I m finding it very confusing. It is an arc in my case so axes_x and axes_y are same, i.e the radius. What should be my axis, In which direction should I calculate the Start and the End angle? And what is this angle of rotation? Given is the function - cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])

import cv2
import numpy as np

def create_blank(height, width, color):

    blank_image = np.zeros((int(height), int(width), 3), np.uint8)
    blank_image[:, :] = color
    return blank_image

def draw_arc(image):
    height, width = image.shape[0:2]
    # Ellipse parameters
    radius = 100
    center = (width / 2, height/2)
    axes = (radius, radius)
    angle = 0
    startAngle = 135
    endAngle = 180
    cv2.line(image, (0, 150), (300, 150), (0, 0, 0), 2, cv2.CV_AA)
    cv2.line(image, (150, 0), (150, 300), (0, 0, 0), 2, cv2.CV_AA)
    cv2.ellipse(image, center, axes, angle, startAngle, endAngle, (0, 0, 0), 2, cv2.CV_AA)
    cv2.imshow("ellipse", image)


# Create new blank 300x150 white image
width, height = 300, 300
image = create_blank(width, height, color=WHITE)
draw_arc(image)

cv2.waitKey(0)
cv2.destroyAllWindows()

When my startAngle is 135 and endAngle is 180, the result looks like enter image description here

whereas when the startAngle is 0 and endAngle is 90, the result looks like enter image description here

So this makes it confusing, in which direction is the arc rotating.

1 Answers

You can really easily view how the change of parameters affect the drawing of the ellipse. Here is a simple code for it:

import numpy as np
import cv2

center = (200, 200) # x,y
axes = (100, 75) # first, second
angle = 0. # clockwise, first axis, starts horizontal
for i in range(360):
  image = np.zeros((400, 400, 3)) # creates a black image
  image = cv2.ellipse(image, center, axes, angle, 0., 360, (0,0,255))
  image = cv2.ellipse(image, center, axes, angle, 0., i, (0,255,0))
  cv2.imshow("image", image)
  cv2.waitKey(5)

cv2.waitKey(0)
cv2.destroyAllWindows()

This will do something like:

enter image description here

Lets go through the parameters:

center -> x and y tuple where the center of the ellipse is.

axes -> first and second axes radius (half the total size). The first one is the horizontal one if angle 0 is applied, the second one will be the vertical one.

angle -> The angle of the whole ellipse, i.e. if you move clockwise the first axis

startAngle -> where you want to start drawing your arc, for example 0 will be like my example image (in the first axis), but if the angle has a value, then the 0 will rotate the same way.

endAngle -> where you want to stop drawing, you can see that I vary it in my example to draw an increasing ellipse.

If you want an arc of a circle of radius 50px, lets say from 60 degrees up to 120 degrees, but in counterclockwise (360 - start/endAngle) you can do:

image = cv2.ellipse(image, (100,100), (50,50), 0.0, 360-120, 360-60, (0,255,0))

If you have doubts with any of them, feel free to ask in a comment

Related