Add `label2rgb` #485

Merged
merged 7 commits into from Jun 23, 2013

Projects

None yet

4 participants

@tonysyu
Member
tonysyu commented Mar 30, 2013

This PR adds a function to overlay colorized labels on top of an image. You can see the results of this function in the modified plot_join_segmentations.py example.

Not included in the example: You can also use a matplotlib colormap to choose colors. For example:

colors = plt.cm.Set1(np.linspace(0, 1, 12))
colorized = label2rgb(label, image=image, colors=colors)
@stefanv
Member
stefanv commented Mar 31, 2013

@tonysyu This is very nice! Could you also have a look at #428 and see if there is any common functionality that can be pulled out?

@tonysyu
Member
tonysyu commented Mar 31, 2013

Do you mean the color definitions? That should be easy---just a couple of API decisions:

  • RGB floats vs uint8
  • color dict vs module variables
    If PR #428 is going to be merged soon, I can consolidate the color defs afterwards---either in this PR or in a separate PR.
@stefanv stefanv and 1 other commented on an outdated diff Apr 1, 2013
skimage/color/setup.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+
+def configuration(parent_package='', top_path=None):
+ from numpy.distutils.misc_util import Configuration
+
+ config = Configuration('color', parent_package, top_path)
+ config.add_data_files('colors.ini')
@stefanv
stefanv Apr 1, 2013 Member

Should the Bento file also be updated?

@tonysyu
tonysyu Apr 2, 2013 Member

Good point. My first reaction was to look for where the plugin *.ini files were added, but I don't see those in bento.info. Am I missing something?

@tonysyu
Member
tonysyu commented May 6, 2013

Update: I ended up changing the API to the function. image_label2rgb is now named label2rgb and more importantly, the input image is now an optional argument (which required switching the order of the first 2 arguments). Now the function behaves like the matlab equivalent when no image is given, but has the added bonus of an image underlay (when specified).

Also, I rebased the branch because of merge conflicts.

@stefanv stefanv and 1 other commented on an outdated diff May 6, 2013
skimage/color/colorlabel.py
+
+import numpy as np
+
+from skimage import img_as_float
+from skimage._shared.utils import is_str
+from .colorconv import rgb2gray, gray2rgb
+
+
+__all__ = ['color_dict', 'label2rgb', 'DEFAULT_COLORS']
+
+
+DEFAULT_COLORS = ('red', 'blue', 'yellow', 'magenta', 'green',
+ 'indigo', 'darkorange', 'cyan', 'pink', 'yellowgreen')
+
+
+def _load_colors():
@stefanv
stefanv May 6, 2013 Member

Why don't we just strip out the first line of colors.ini, make it a .py, and then import it and use its namespace as dictionary?

@tonysyu
tonysyu May 7, 2013 Member

Hmm, I guess I've just internalized the idea that it's best to keep data in data files, not python modules. But I guess that applies more to configuration data than to application data.

Is the idea here to have the colors available both as module attributes and a dict?

@stefanv
stefanv May 28, 2013 Member

I just thought it would be an easy way to get syntax checking for free, and to avoid having to have a loader function,

@tonysyu tonysyu referenced this pull request May 26, 2013
Closed

Re-added novice module #521

@tonysyu tonysyu Change color definitions from ini file to python module
This simplifies the code and allows users to use either `color_dict` or module
variables, e.g., `rgb_colors.white`.
20401d3
@tonysyu
Member
tonysyu commented Jun 20, 2013

@stefanv I finally got around to implementing your suggestion of switching from defining colors in a python module instead of an ini file, and it did indeed simplify things quite a bit.

One last thing I'm uncertain about: Does it make sense to name the kwarg image, or should it be more explicit, like underlay. The later better describes what the kwarg does, but might be a bit more difficult to parse (mentally).

@ahojnnes
Member

I personally find image better… apart from that I think this is ready to merge.

This was referenced Jun 21, 2013
@ahojnnes
Member

Addition: I would use uppercase names for the colors.

@tonysyu
Member
tonysyu commented Jun 22, 2013

As for the kwarg name, I think I agree, so I'll leave it as-is.

As for uppercase names, I'm not sure if I agree. I understand the reasons (PEP8 compatibility, globals should be easily identifiable, etc.). That said, I think it makes the names quite-a-bit-less readable. That's my perspective, but I'm happy to change it if others feel strongly about it.

Also, right now, the names are the same as Matplotlib (not that I think Matplotlib is necessarily a good model for everything, but it might be nice to be able to switch back-and-forth).

@jni
Contributor
jni commented Jun 22, 2013

I'm not a fan of all-uppercase names either.

@stefanv stefanv merged commit 0a727e3 into scikit-image:master Jun 23, 2013

1 check passed

default The Travis CI build passed
Details
@stefanv
Member
stefanv commented Jun 23, 2013

@tonysyu Thanks--so many colors! With this merged, we can move on to the novice module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment