jtlz2
jtlz2

Reputation: 8437

numpy 2-D array: efficient way to create circular masks at all given positions

I have a sparse (100k / 20000^2) 2-D boolean numpy mask corresponding to the positions of objects.

I want to update the mask to set to True all pixels within a certain radius of a True pixel in the original mask. In other words, convolve the delta-function response with a circular aperture/kernel (in this case) response at each position.

Since the master array is large (i.e. 20000 x 20000), and there are 100k positions, I need speed and memory efficiency...

For example (see numpy create 2D mask from list of indices [+ then draw from masked array]):

import numpy
from scipy import sparse

xys=[(1,2),(3,4),(6,9),(7,3)]

master_array=numpy.ones((100,100))

coords = zip(*xys)
mask = sparse.coo_matrix((numpy.ones(len(coords[0])),coords),\
                         shape= master_array.shape, dtype=bool)

# Now mask all pixels within a radius r of every coordinate pair in the list
mask = cookieCutter(mask,r) # <--- I need an efficient cookieCutter function!

# Now sample the masked array
draws=numpy.random.choice(master_array[~mask.toarray()].flatten(),size=10)

Thanks!

(Follows on from numpy create 2D mask from list of indices [+ then draw from masked array])

Special case of a single position: How to apply a disc shaped mask to a numpy array?

Upvotes: 0

Views: 3138

Answers (1)

M456
M456

Reputation: 5797

Scikit-Image has a dilation function, which would serve your purpose.

Upvotes: 3

Related Questions