rolled = np.roll(y, 1, axis=0)
rolled[(0), :] = False
z = np.logical_or(y, rolled)
rolled = np.roll(y, -1, axis=0)
rolled[(-1), :] = False
z = np.logical_or(z, rolled)
rolled = np.roll(y, 1, axis=1)
rolled[:, (0)] = False
z = np.logical_or(z, rolled)
rolled = np.roll(y, -1, axis=1)
rolled[:, (-1)] = False
z = np.logical_or(z, rolled)
neighbors = set(np.unique(np.extract(z, x))) - set([region])
print(neighbors)
Определить соседние регионы в массиве numpy