Given an N*N array of 0 and 1, I want to build the list of clusters (a cluster being a set of connected points labeled by 1).
scipy.ndimage.label
is very useful because it tells you which points are connected.
But I would like also to have periodic boundary conditions on my array, i.e. points (0,j)
and (N,j)
are identified (like a plane that I glue to make a cylinder). So I need to tell scipy.ndimage.label that features are connected through the boundary.
For example, if my original array is:
In[187]: a = [[1, 1, 0, 0, 0, 0, 1, 1],[1, 1, 0, 1, 0, 0, 1, 1],[1, 1, 0, 0, 0, 1, 1, 1]]
labels = measurements.label(a)
print(labels)
Out [187]: (array([[1, 1, 0, 0, 0, 0, 2, 2],
[1, 1, 0, 3, 0, 0, 2, 2],
[1, 1, 0, 0, 0, 2, 2, 2]], dtype=int32), 3)
and I would like:
(array([[1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 3, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1]], dtype=int32), 2)
The structure parameter of label allows to specify connection (e.g. features connected even if they touch diagonally), could it be also used for that purpose?