Skip to content Skip to sidebar Skip to footer

Fast Random To Unique Relabeling Of Numpy 2d Regions (without Loops)

I have a large numpy 2d array (10000,10000) in which regions (clusters of cells with the same number) are randomly labeled. As a result, some separate regions were assigned to the

Solution 1:

Let's cheat and just use some high-quality library (scikit-image) which offers exactly this.

You may learn from it's implementation or just use it!

import numpy as np
from skimage.measure import label

random_arr = np.array([[1,1,3,3],[1,2,2,3],[2,2,1,1],[3,3,3,1]])
labels = label(random_arr, connectivity=1)  # neighborhood-definition here!

print(labels)

Output

[[1 1 2 2]
 [1 3 3 2]
 [3 3 4 4]
 [5 5 5 4]]

EDIT: Like mentioned by Jeon in the comments, scipy's scipy.ndimage.measurements.label might also be a candidate if one does not want to use one more extra library! Thanks for the comment Jeon!

Post a Comment for "Fast Random To Unique Relabeling Of Numpy 2d Regions (without Loops)"