I'm looking to solve the following problem. I have a numpy array which is labeled to regions from 1 to n. Let's say this is the array:
x = np.array([[1, 1, 1, 4], [1, 1, 2, 4], [1, 2, 2, 4], [5, 5, 3, 4]], np.int32)
array([[1, 1, 1, 4],
[1, 1, 2, 4],
[1, 2, 2, 4],
[5, 5, 3, 4]])
A region are the combined cells in the numpy array with a unique value. So in this example x has 5 regions; region 1 which consists of 5 cells, region 2 which consists of 3 cells etc. Now, I determine the adjacent regions of each region with the following lines of code:
n = x.max()
tmp = np.zeros((n+1, n+1), bool)
# check the vertical adjacency
a, b = x[:-1, :], x[1:, :]
tmp[a[a!=b], b[a!=b]] = True
# check the horizontal adjacency
a, b = x[:, :-1], x[:, 1:]
tmp[a[a!=b], b[a!=b]] = True
# register adjacency in both directions (up, down) and (left,right)
result = (tmp | tmp.T)
result = result.astype(int)
np.column_stack(np.nonzero(result))
resultlist = [np.flatnonzero(row) for row in result[1:]]
Which gives me a list of each region with its adjacent regions:
[array([2, 4, 5], dtype=int64),
array([1, 3, 4, 5], dtype=int64),
array([2, 4, 5], dtype=int64),
array([1, 2, 3], dtype=int64),
array([1, 2, 3], dtype=int64)]
Which works really well. However, I would like to count the amount of cells of each adjacent region and return this output. So, for region 2, in this example would mean a total of 7 adjacent regions (three 1s, two 4s, one 3 and one 5). Therefore:
- 2 is surrounded for 43% by 1
- 2 is surround for 14 % by 5
- 2 is surrounded for 14% by 3
- 2 is surrounded for 29% by 4
How could I best adjust the above code to include the amount of cells for each adjacent region? Many thanks guys!