I have two 2d bitewise arrays (1s and 0s only). I am doing
array2 = numpy.where( array1, 0, array2 )
which changes the values of array2
when array1
matches the condition. But what if I don't want to change the values of the very cells where the condition if satisfied, but their neighbors cells? I mean, instead of i,j
for which array1
evals to True
, I want to change
the subarray array2[i-1:i+2,j-1:j+2]
.
It is possible to avoid looping, which is really slow on big arrays?
see scipy.ndimage.filters.generic_filter
so in your example, the footprint
argument would be (3, 3)
, that is a neighborhood of 3 by 3 and the the function
argument should return if the condition is true or false;
something like:
func = lambda xs: np.any(xs) # or whatever appropriate
mask = generic_filter(array1, func, footprint=(3, 3))
array2[mask] = 0