I am looking for the following. I have a numpy array which is labeled as regions. The numpy array represents a segmented image. A region is a number of adjacent cells with the same value. Each region has its own unique value. A simplified version with 3 regions would look like this:
x = np.array([[1, 1, 1], [1, 1, 2], [2, 2, 2], [3, 3, 3]], np.int32)
output:
array([[1, 1, 1],
[1, 1, 2],
[2, 2, 2],
[3, 3, 3]])
In the above example we have 3 separate regions, each labeled with an unique value (1,2,3 in this case).
What I want is the value of adjacent (neighbor) regions for each individual region. So in this case:
- Region 1 is adjacent to region 2
- Region 2 is adjacent to region 1 and 3
- Region 3 is adjacent to region 2
What would be the most elegant and fastest way of achieving this?
Many thanks!