scikit-image/scikit-image — pull request #761 by kemaleren (2013-10-07 23:49:15+00:00)
Title: optimized label2rgb()

The current implementation of ``label2rgb()`` loops over all labels, creating a mask for each. This approach is far too slow for large images with lots of labels. I have rewritten it to instead create an index array which maps labels to colors. Then the final image can be created in one go, using NumPy indexing and array operations.

Here is a simple benchmark:

import numpy as np
from skimage.color import label2rgb

test_image = np.zeros(10000).reshape(100, 100)
test_labels = np.arange(10000).reshape(100, 100)
timeit label2rgb(test_labels, test_image)

The old version takes 1.04 seconds. The new version takes 0.034 seconds, for a speedup of about 30.6.