Home > front end >  Get PIXEL VALUES of a circle located?
Get PIXEL VALUES of a circle located?

Time:03-19

I have parameters of circle on a image: radius and coordinates of center. Is it possible to return the location of pixels along a circle? I need to get the all pixel values along the circle line. I tried get coordinates by next solution:

...
cx = int(img.shape[1]/2)
cy = int(img.shape[0]/2)
radius = 700
angle = np.linspace(0,  2 * np.pi, 360)
X = (np.round(cx   radius * np.cos(angle))).astype(int)
Y = (np.round(cy   radius * np.sin(angle))).astype(int)
...

But if angle is not agreed with radius I miss neighboring pixsels: enter image description here

CodePudding user response:

You can get a continuous circle as the outline of the domain X² Y² ≤ R². If you observe the symmetries, you will notice that the circle can be decomposed in 8 arcs. Consider the arc from (R, 0) to the intersection with Y=X (octant 2 in the picture): it is made of displacements from (X, Y) to (X, Y 1) or (X-1, Y 1) only. If we plug this in the disk equation, we need X² (Y 1)² ≤ R² or (X - 1)² (Y 1)² ≤ R², and we give priority to the first inequality if it can be satisfied (the second is automatically satisfied).

X, Y:= R, 0
while X > Y:
    # 8 symmetries
    Plot(X, Y);  Plot(Y, X);  Plot(-X, Y);  Plot(-Y, X); 
    Plot(X, -Y); Plot(Y, -X); Plot(-X, -Y); Plot(-Y, -X); 

    if X²   (Y   1)² > R²:
      X-= 1
    Y = 1

enter image description here

CodePudding user response:

You can never get all the points on a circle, as the points are continuous. What you can do is to get more points on the circle, so that the drawing looks continuous.

Change the number of samples to generate here from 360 to something greater, for example 720.

angle = np.linspace(0,  2 * np.pi, 720)

You can keep increasing until you're satisfied with the result.

However, as you are drawing with openCV and the points all have to be integers, you will more than likely still have gaps. That's why the best solution is to use openCV's drawing function

cv.circle(img,(x_cen,y_cen), radius, colour, thickness)
  • Related