I am trying to implement an image processing algorithm that involves calculating the product of a 4-adjacent neighborhood for each cell. That is, calculate a new matrix Y for X, where y[i, j] = x[i-1, j] * x[i, j-1] * x[i+1, j] * x[i, j+1] . Unsent neighbors should be ignored.
Now I can only think about it: use scipy.ndimage.filters.correlate and pass the weights with zeros and one 1 to get four matrices, each of which contains a neighbor for each cell in a direction, for example, passing through weight = [[0, 0, 0], [1, 0, 0], [1, 1]] , and I get a[i, j] = x[i-1, j] , and with other weights I can get b[i, j] = x[i, j-1] , c[i, j] = x[i+1, j] , d[i, j] = x[i, j+1] . Then I use np.multiply to calculate the product of these four matrices.
However, this approach is too slow, and I can not ignore the boundaries. Is there any other way to do this with numpy / scipy, so I don't need to resort to loops?
source share