Identify contiguous regions in 2D numpy array Identify contiguous regions in 2D numpy array numpy numpy

Identify contiguous regions in 2D numpy array


You're looking for scipy.ndimage.label, more info here. label returns an array the same shape as the input where each "unique feature has a unique value", so if you want the indices of the features you can do something like:

labels, numL = label(array)label_indices = [(labels == i).nonzero() for i in xrange(1, numL+1)]