[Numpy-discussion] Drawing circles in a numpy array

totonixsame at gmail.com totonixsame at gmail.com
Mon Jan 10 08:53:05 EST 2011


Hi all,

I have this problem: Given some point draw a circle centered in this
point with radius r. I'm doing that using numpy this way (Snippet code
from here [1]):

>>> # Create the initial black and white image
>>> import numpy as np
>>> from scipy import ndimage
>>> a = np.zeros((512, 512)).astype(uint8) #unsigned integer type needed by watershed
>>> y, x = np.ogrid[0:512, 0:512]
>>> m1 = ((y-200)**2 + (x-100)**2 < 30**2)
>>> m2 = ((y-350)**2 + (x-400)**2 < 20**2)
>>> m3 = ((y-260)**2 + (x-200)**2 < 20**2)
>>> a[m1+m2+m3]=1
>>> imshow(a, cmap = cm.gray)# left plot in the image above

The problem is that it have to evaluate all values from 0 to image
size (in snippet, from 0 to 512 in X and Y dimensions). There is a
faster way of doing that? Without evaluate all that values? For
example: only evaluate from 0 to 30, in a circle centered in (0, 0)
with radius 30.

Thanks!
Thiago Franco de Moraes

[1] - http://www.scipy.org/Cookbook/Watershed



More information about the NumPy-Discussion mailing list