[SciPy-User] label2rgb equivalent
Sat Apr 3 10:39:50 CDT 2010
> Hi all,
> Is there an equivalent to the Matlab function label2rgb in scipy or
> numpy ?
matplotlib might have such a function, but numpy fancy indexing makes
this almost a one-liner:
Let "labels" be your label array (of arbitrary shape); assume "colors"
is an Nx3 array of rgb triples, where N > labels.max().
gives you an array of the same shape as labels except that it has an
extra dimension at the end containing the RGB triples. For example:
labels = numpy.random.random_integers(10, size=(10,10))
colors = numpy.arange(75).reshape((25,3))
labeled = colors[labels]
labeled.shape # (10,10,3)
you could use numpy.rollaxis to change which dimension the RGB data is
numpy.rollaxis(labeled, 2).shape # (3, 10, 10)
you could also have colors be a structured array...
As far as making the "colors" array, there are many strategies. You
could use random integers, or make a colormap (matplotlib has some
tools, or you could roll your own easily enough.
More information about the SciPy-User