0

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?

Community
  • 1
  • 1
jtlz2
  • 7,700
  • 9
  • 64
  • 114
  • can you provide an example of you data and what are exactly your *criteria* for masking? – Dalek Oct 21 '14 at 14:55
  • Sure, but how best to do this. I want to mask out any pixels in a master array that are within a radius r of a list of positions (x_i,y_i), so that I can draw random samples from the remaining pixels. I'll edit the question. – jtlz2 Oct 21 '14 at 15:31

1 Answers1

3

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

M456
  • 5,547
  • 2
  • 19
  • 14
  • That's amazing - thank you! So I've gone with something like: mask=skimage.morphology.dilation(~mask.toarray(),skimage.morphology.disk(r)).astype(bool) – jtlz2 Oct 21 '14 at 16:19
  • Though it seems quite slow - so any other ideas appreciated! – jtlz2 Oct 21 '14 at 16:33
  • struct = scipy.ndimage.generate_binary_structure(2, 1); mask2=scipy.ndimage.morphology.binary_dilation(mask,structure=struct).astype(bool) is a whole lot faster – jtlz2 Oct 22 '14 at 07:31