I have an array of True
/False
values which I want to use as a repeating mask over another array of a different shape.
import numpy as np
mask = np.array([[ True, True],
[False, True]])
array = np.random.randint(10, size=(64, 64))
I want to apply this mask in a sliding window, similar to the where
function on the array. Currently, I use np.kron
to simply repeat the mask to match the dimensions of the array:
layout = np.ones((array.shape[0]//mask.shape[0], array.shape[1]//mask.shape[1]), dtype=bool)
mask = np.kron(layout, mask)
result = np.where(mask, array, 255) # example usage
Is there any elegant way to do this same operation, without repeating the mask
into the same shape as array
? I was hoping there would be some kind of sliding window technique or convolution/correlation.