Search code examples
pythonopencvregion

Get PIXEL VALUES of a circle located?


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


Solution

  • 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