I have a 2D array, where I label clusters using the ndimage.label()
function like this:
import numpy as np
from scipy.ndimage import label
input_array = np.array([[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 1]])
labeled_array, _ = label(input_array)
# Result:
# labeled_array == [[0, 1, 1, 0],
# [1, 1, 0, 0],
# [0, 0, 0, 2],
# [0, 0, 0, 2]]
I can get the element counts, the centroids or the bounding box of the labeled clusters. But I would like to also get the coordinates of each element in clusters. Something like this (the data structure doesn't have to be like this, any data structure is okay):
{
1: [(0, 1), (0, 2), (1, 0), (1, 1)], # Coordinates of the elements that have the label "1"
2: [(2, 3), (3, 3)] # Coordinates of the elements that have the label "2"
}
I can loop over the label list and call np.where()
for each one of them but I wonder if there is a way to do this without a loop, so that it would be faster?