Cv.ellipse() Function

Hello everyone…
i’m new to OpenCV (with python) and am finding it difficult to understand the angle parameters for cv2.ellipse() as i tried the following lines of codes and its confusing:

import cv2
import numpy as np
img = np.zeros((512, 512, 3), np.uint8)     # Create a black image
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32) 
 cv2.ellipse(img, (300, 300), (100, 50), 45, 0, 180, (40, 100, 255), -1)  # Drawing an ellipse
cv2.ellipse(img, (256, 256), (200, 100), 0, 90, 180, (0, 0, 255), 4, cv.LINE_AA) # Drawing an ellipse

can somebody help me understand.

what are you trying to achieve, and where is the problem ?

docs might explain it please (finally) take a look.

2 Likes

If you have problem then use for-loop to draw it with different angles and you will see how it works.

import cv2
import numpy as np

steps = 10

img = np.zeros(((steps+1)*100, 200, 3), np.uint8)     # Create a black image

for number, angle in enumerate(range(0, 361, 360//steps)):
    y = 50 + 100 * number
    cv2.ellipse(img, (50, y), (50, 50), 0, angle, 360, (255, 0, 0), -1)  # Drawing an ellipse
    cv2.putText(img, f'{angle}-360', (25, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)

    cv2.ellipse(img, (150, y), (50, 50), 0, 0, angle, (0, 255, 0), -1)  # Drawing an ellipse
    cv2.putText(img, f'0-{angle}', (125, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)

cv2.imwrite('ellipse.png', img)

cv2.imshow('test', img)
cv2.waitKey(0)

import cv2
import numpy as np

steps = 10

first_animation = True

running = True
while running:

    for number, angle in enumerate(range(0, 361, 360//steps)):
        y = 50

        img = np.zeros((100, 100, 3), np.uint8)     # Create a black image
        
        if first_animation:
            cv2.ellipse(img, (50, y), (50, 50), 0, angle, 360, (0, 255, 0), -1)  # Drawing an ellipse
            cv2.putText(img, f'{angle}-360', (25, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
        else:
            cv2.ellipse(img, (50, y), (50, 50), 0, 0, angle, (0, 255, 0), -1)  # Drawing an ellipse
            cv2.putText(img, f'0-{angle}', (25, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)

        cv2.imwrite('frame-{}-{}.png'.format(int(not first_animation), number), img)
        cv2.imshow('test', img)
        key = cv2.waitKey(250) & 0xFF
        if key == 27:
            running = False
            break

    first_animation = not first_animation

And frames converted to animated GIF with imagemagick

convert frame*.png animation.gif

animation

1 Like

AS I indicated I am new to OpenCV and am learning using: OpenCv now on drawing the ellipse the Angle part was confusing me after experimenting with different values. like in the sample code I provided.

cv2.ellipse(img, (300, 300), (100, 50), 45, 0, 180, (40, 100, 255), -1)  # Drawing an ellipse
cv2.ellipse(img, (256, 256), (200, 100), 0, 90, 180, (0, 0, 255), 4, cv.LINE_AA) # Drawing an ellipse

so my question was like what is a starting/ending angle really mean? because from the two lines of code the behavior was surprising to me.

Thank you very much sir