I am using numpy is Python. I have an image loaded up into numpy 2-dimensional array as:
[
[...], # row0
[...], # row1
[...], # row2
...
]
I need to get all the index positions of all pixels where either (only one of the following) north, south, east, or west adjacent pixels are of certain value. In my case if any of the 4 adjacent pixels is 0.
If a is your original array, define a bunch of slices:
The reason for taking the smaller
a_is that I don’t know what you want to do with the edge cases, where e.g. the North pixel might not exist.I build an array of the count of adjacent zeros, and use that to get the positions which are adjacent to exactly one zero. Output: