0

I have a labelled array obtained by using scipy measure.label on a binary 2 dimensional array. For argument sake it might look like this:

[
[1,1,0,0,2],
[1,1,1,0,2],
[1,0,0,0,0],
[0,0,0,3,3]
]

I want to get the indices of each group of labels. So in this case:

[
[(0,0),(0,1),(1,0),(1,1),(1,2),(2,0)],
[(0,4),(1,4)],
[(3,3),(3,4)]
]

I can do this using builtin Python like so (n and m are the dimensions of the array):

_dict = {} 
for coords in itertools.product(range(n), range(m)):
    _dict.setdefault(labelled_array[coords], []).append(coords)
blobs = [np.array(item) for item in _dict.values()]

This is very slow (about 10 times slower than the initial labelling of the binary array using measure.label!)

Scipy also has a function find_objects:

from scipy import ndimage
objs = ndimage.find_objects(labelled_array)

From what I can gather though this is returning the bounding box for each group (object). I don't want the bounding box I want the exact coordinates of each value in the group.

I have also tried using np.where for each integer in the number of labels. This is very slow.

it also seems to me that what I'm tring to do here is something like the minesweeper algorithm. I suspect there must be an efficient solution using numpy or scipy.

Is there an efficient way to obtain these coordinates?

Neil
  • 3,020
  • 4
  • 25
  • 48
  • What is fast depends on the amount of distinct labels. For some `k < K` labels for an `MxN` array, just doing `k` separate `x == label[i]` operations is the fastest. – Mateen Ulhaq Aug 21 '21 at 23:19
  • @MateenUlhaq there are between 200 and 600 per array, hundreds of arrays. – Neil Aug 21 '21 at 23:22
  • Actually, perhaps one of the binning or `np.histogram`-like functions might work here. Perhaps `np.unique`. – Mateen Ulhaq Aug 21 '21 at 23:25
  • 1
    Perhaps this is relevant: https://stackoverflow.com/questions/30003068/how-to-get-a-list-of-all-indices-of-repeated-elements-in-a-numpy-array. EDIT: Apparently, I even left an answer there a few years ago: https://stackoverflow.com/a/53507580/365102 – Mateen Ulhaq Aug 21 '21 at 23:32

0 Answers0