# How to get the x,y coordinates and the color of pixels inside a "circle"

Hello, I am trying to find a ready function that I give it a position of an image (x,y coordinates) and a radius r and returns me the pixels (x,y coordinates and color of each pixel ) inside that “invisible” circle. Is there something ready, a function for example? Or I will need to reinvent the wheel… The image is a UAV RGB image and the radius is in meters so I need to translate to something in pixels… I don’t know if I have confused you…

I don’t know if there is method to get pixels in circle.

But there are functions to draw one image on another image using mask with black&white (or grayscale) image with circle.

So you could use this method to copy pixels from one image to empty image and make caculations on this image, and later use the same method to move it back to original image.

But maybe it could be simpler to crop rectange with data (because it is easy), make calculations on this rectangle, and later draw it back using mask with circle.

In both situations you work with rectangle and make calculations for pixels which you don’t need - and they may also change value in calculations for other pixels.

But if you need use pixels only in circle then other idea is to use circle formula

``````x**2 + y**2 = r**2
``````

to calculate

``````start_x,  end_x
``````

for every `"y"` in circle

And then you can make calculation for every row of image only in region `"start_x, end_x"`

This code gives me values for circle in (0,0) and r=10 because it simpler to see if it works but for other circle use different `cx, cy, r`

``````r = 10
cx = 0
cy = 0

for y in range(-r, +r+1):
x = (r**2 - y**2)**0.5

x1 = -x + cx
x2 = +x + cx

y = y +cy

print(f'y: {y:3} | x: [{x1:7.02f} ... {x2:7.02f}]')
``````

Result:

``````y: -10 | x: [   0.00 ...    0.00]
y:  -9 | x: [  -4.36 ...    4.36]
y:  -8 | x: [  -6.00 ...    6.00]
y:  -7 | x: [  -7.14 ...    7.14]
y:  -6 | x: [  -8.00 ...    8.00]
y:  -5 | x: [  -8.66 ...    8.66]
y:  -4 | x: [  -9.17 ...    9.17]
y:  -3 | x: [  -9.54 ...    9.54]
y:  -2 | x: [  -9.80 ...    9.80]
y:  -1 | x: [  -9.95 ...    9.95]
y:   0 | x: [ -10.00 ...   10.00]
y:   1 | x: [  -9.95 ...    9.95]
y:   2 | x: [  -9.80 ...    9.80]
y:   3 | x: [  -9.54 ...    9.54]
y:   4 | x: [  -9.17 ...    9.17]
y:   5 | x: [  -8.66 ...    8.66]
y:   6 | x: [  -8.00 ...    8.00]
y:   7 | x: [  -7.14 ...    7.14]
y:   8 | x: [  -6.00 ...    6.00]
y:   9 | x: [  -4.36 ...    4.36]
y:  10 | x: [   0.00 ...    0.00]
``````
1 Like