[Numpy-discussion] Drawing circles in a numpy array
totonixsame at gmail.com
totonixsame at gmail.com
Tue Jan 11 11:13:03 EST 2011
On Mon, Jan 10, 2011 at 11:53 AM, totonixsame at gmail.com
<totonixsame at gmail.com> wrote:
> 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
Hi,
I've just seen I can do something like this:
>>> radius = 10
>>> a = np.zeros((512, 512)).astype('uint8')
>>> cx, cy = 100, 100 # The center of circle
>>> y, x = np.ogrid[-radius: radius, -radius: radius]
>>> index = x**2 + y**2 <= radius**2
>>> a[cy-radius:cy+radius, cx-radius:cx+radius][index] = 255
Numpy is very cool!
Is there other way of doing that? Only to know ...
Thanks!
Thiago Franco de Moraes
More information about the NumPy-Discussion
mailing list