0

I need to draw an arc (part of a circle) but I need the angles to be precise (float). But when I run the code below, I find 2 'bugs':

First, when the start/end angle is less than .5 it draws the 'arc' not at 0 degrees but at the CENTER OF THE ELLIPSE. Then, as it reaches 1 degree 2 degrees 3 degrees it draws an extra 'chunk' of the arc. Why am I not able to draw for example startAngle 0 and endAngle 2.7. Why will it only draw integer angles. The parameters in c++ seem to be 'double' and i'm using floats.

import numpy as np
import cv2
image = np.zeros((720,1280,3), np.uint8)


height, width = image.shape[0:2]
# Ellipse parameters
radius = 640
center = (0, height //2)
axes = (radius, radius)
angle = 0.
startAngle = .0001
endAngle = -.01
thickness = 200


for i in range(400):
    cv2.ellipse(image, center, axes, angle, startAngle, endAngle*float(i), (255,0,0), thickness)
    cv2.imshow('image', image)
    if cv2.waitKey(20) & 0xFF == 27:
        break
         
cv2.destroyAllWindows()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47
  • Hmm, the docs indeed look like showing double/floats, but GitHub shows ints (https://github.com/egonSchiele/OpenCV/blob/master/samples/cpp/fitellipse.cpp) as well as this SO answer https://stackoverflow.com/questions/56630157/understanding-the-ellipse-parameters-in-open-cv-using-python – Yohst Aug 22 '23 at 21:47
  • oh well i found the method taking floats but i see there is an explicit rounding: https://github.com/opencv/opencv/blob/4.x/modules/imgproc/src/drawing.cpp#L1960 – AwokeKnowing Aug 22 '23 at 22:31

1 Answers1

-2

The first bug, drawing a point in the center instead of at 0 degrees on the ellipse is due to this code:

enter image description here

basically, the points are calculated in a for loop, and when you do for(i=0;i<0+0;i++) you end up executing zero times, so the code just doesn't calculate the point, so it draws a point at the center.

As far as why it is rounded, the reason is that while the function takes a double, it is explicitly rounded. The reason for that seems to be so that the angle used in a lookup table of pre-calculated sine values. What happens is opencv decided to use the ellipse polygon points funtion to find the points of the arc. So if you can imagine it drawing a polygon approximation to an ellipse, then those points on the polygon are the only ones available as starting/ending points of the ellipse, and it is just arbitrarily chosen to be int in order to leverage the quick lookup table. Specifically, there's a 'delta' parameter that the loop steps by, and that, in the ellipse polygon point code is an int in arc radians. You could step by smaller values but then the lookup table wouldn't work, so you'd need to use a more fine-grained lookup or just use eg np.sine

enter image description here

At least you can see there the code which you could base your own ellipse drawing loop on.

AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47
  • 1
    Images of code in an answer? Come on, you've been here long enough to know better than that. – Dan Mašek Aug 23 '23 at 10:28
  • @DanMašek images are better than links. And code is not executable, nor useful in any way to be edited. You missed the point on why we usually want code, which is so we can test/modify it, which doesn't make sense in opencv context. You can edit it and add code if you want. – AwokeKnowing Aug 30 '23 at 23:57
  • https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors That sums it up. It's not really that hard to put that code in a snippet. I'm not sure why you think I should be the one doing that for you, I don't expect others to do that for me either. – Dan Mašek Aug 31 '23 at 00:03
  • Honestly, had you just fixed what I complained about, I'd have happily reversed my downvote (which I cast about a day after I complained). I'm still willing to do that, in spite of your reaction, but you still have to hunt down the other person that seems to have agreed with me. – Dan Mašek Aug 31 '23 at 00:11