I have a NumPy array consisting of only the 0 and 1 elements as follows:
import numpy as np
data = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 0],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 0],
[0, 0 , 1 , 1 , **1** , 1 , 1 , 0],
[0, 0 , 1 , 1 , 1 , 1 , 1 , 1],
[1, 1 , 1 , 1 , 1 , 1 , 1 , 0],
[1, 1 , 0 , 0 , 0 , 0 , 0 , 0]])
I have to find out the indices of the element 1 which is surrounded by 1 in 2 by 2 pixels in every directions.
The location of the expected answer is shown in bold.
I am looking for easier and faster way of doing it.
You could use some
signal processing
-Output -
Listed in this section is an alternative approach with
ndimage
to perform the same convolution as with the previous approach, keeping rest of the steps same. Here's the code to get the convolution outputfilt_out
-This is easy with a basic morphological operation: