Search code examples
pythonimage-processinggeometrypixelroi

How to access the inner and outer pixels of a circle ROI on an image?


I'm trying to solve a problem and am quite stuck on how to approach it. I have an image and I want to get all the pixels inside the circle area of interest.

The radius of the circle is 150 pixels and the centre point of the ROI is [256, 256].

I have tried to draw the circle on the image, but I couldn't find a way to access those pixels from that.

img = imread('Model/m1.png') * 255
row = size(img, 0) #imgWidth
col = size(img, 1) #imgHeight
px = row * col

circle = Circle((256, 256), 150, fill = False)
fig, ax = subplots()
ax.add_patch(circle)
imshow(img, cmap='gray')
show()

I was thinking maybe using the equation of a circle: (x - a)**2 + (y - b)**2 == r**2

But I'm not quite sure as wouldn't that only give you the pixels of the circle edge?

Any ideas or tips would be much appreciated.


Solution

  • Inequality (x - a)**2 + (y - b)**2 <= r**2 allows to walk through all pixels inside circle.

    In practice you can scan Y-coordinates from b-r to b+r and walk through horizontal lines of corresponding X-range a - sqrt(r**2 - (y-b)**2) .. a + sqrt(r**2 - (y-b)**2)