From fa01b936a66c1c2bb78c5213f27e6c0188d970bd Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Mon, 6 May 2013 00:18:09 -0500 Subject: [PATCH] API Change: `image_label2rgb` changed to `label2rgb`. The name change reflects a switch in the argument order (and makes the image an optional argument). --- .../applications/plot_coins_segmentation.py | 4 +-- doc/examples/plot_join_segmentations.py | 8 ++--- doc/examples/plot_label.py | 4 +-- skimage/color/__init__.py | 4 +-- skimage/color/colorlabel.py | 35 +++++++++++-------- skimage/color/tests/test_colorlabel.py | 23 +++++++----- 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/doc/examples/applications/plot_coins_segmentation.py b/doc/examples/applications/plot_coins_segmentation.py index 55ba5c0c..fa7aa1f4 100644 --- a/doc/examples/applications/plot_coins_segmentation.py +++ b/doc/examples/applications/plot_coins_segmentation.py @@ -160,11 +160,11 @@ individually. """ -from skimage.color import image_label2rgb +from skimage.color import label2rgb segmentation = ndimage.binary_fill_holes(segmentation - 1) labeled_coins, _ = ndimage.label(segmentation) -image_label_overlay = image_label2rgb(coins, labeled_coins) +image_label_overlay = label2rgb(labeled_coins, image=coins) plt.figure(figsize=(6, 3)) plt.subplot(121) diff --git a/doc/examples/plot_join_segmentations.py b/doc/examples/plot_join_segmentations.py index b8bbd6c3..2cafab15 100644 --- a/doc/examples/plot_join_segmentations.py +++ b/doc/examples/plot_join_segmentations.py @@ -17,7 +17,7 @@ import matplotlib.pyplot as plt from skimage.filter import sobel from skimage.segmentation import slic, join_segmentations from skimage.morphology import watershed -from skimage.color import image_label2rgb +from skimage.color import label2rgb from skimage import data @@ -48,15 +48,15 @@ fig, axes = plt.subplots(ncols=4, figsize=(9, 2.5)) axes[0].imshow(coins, cmap=plt.cm.gray, interpolation='nearest') axes[0].set_title('Image') -color1 = image_label2rgb(coins, seg1, bg_label=0) +color1 = label2rgb(seg1, image=coins, bg_label=0) axes[1].imshow(color1, interpolation='nearest') axes[1].set_title('Sobel+Watershed') -color2 = image_label2rgb(coins, seg2, image_alpha=0.5) +color2 = label2rgb(seg2, image=coins, image_alpha=0.5) axes[2].imshow(color2, interpolation='nearest') axes[2].set_title('SLIC superpixels') -color3 = image_label2rgb(coins, segj, image_alpha=0.5) +color3 = label2rgb(segj, image=coins, image_alpha=0.5) axes[3].imshow(color3, interpolation='nearest') axes[3].set_title('Join') diff --git a/doc/examples/plot_label.py b/doc/examples/plot_label.py index 6ff61fa6..0542d68c 100644 --- a/doc/examples/plot_label.py +++ b/doc/examples/plot_label.py @@ -21,7 +21,7 @@ from skimage.filter import threshold_otsu from skimage.segmentation import clear_border from skimage.morphology import label, closing, square from skimage.measure import regionprops -from skimage.color import image_label2rgb +from skimage.color import label2rgb image = data.coins()[50:-50, 50:-50] @@ -38,7 +38,7 @@ clear_border(cleared) label_image = label(cleared) borders = np.logical_xor(bw, cleared) label_image[borders] = -1 -image_label_overlay = image_label2rgb(image, label_image) +image_label_overlay = label2rgb(label_image, image=image) fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(image_label_overlay) diff --git a/skimage/color/__init__.py b/skimage/color/__init__.py index de24fbe9..9202bee9 100644 --- a/skimage/color/__init__.py +++ b/skimage/color/__init__.py @@ -41,7 +41,7 @@ from .colorconv import (convert_colorspace, is_rgb, is_gray) -from .colorlabel import color_dict, image_label2rgb +from .colorlabel import color_dict, label2rgb __all__ = ['convert_colorspace', @@ -87,4 +87,4 @@ __all__ = ['convert_colorspace', 'is_rgb', 'is_gray', 'color_dict', - 'image_label2rgb'] + 'label2rgb'] diff --git a/skimage/color/colorlabel.py b/skimage/color/colorlabel.py index cc295204..4e2c2f9a 100644 --- a/skimage/color/colorlabel.py +++ b/skimage/color/colorlabel.py @@ -11,7 +11,7 @@ from skimage._shared.utils import is_str from .colorconv import rgb2gray, gray2rgb -__all__ = ['color_dict', 'image_label2rgb', 'DEFAULT_COLORS'] +__all__ = ['color_dict', 'label2rgb', 'DEFAULT_COLORS'] DEFAULT_COLORS = ('red', 'blue', 'yellow', 'magenta', 'green', @@ -45,22 +45,22 @@ def _rgb_vector(color): return np.array(color[:3]).reshape(1, 3) -def image_label2rgb(image, label, colors=None, alpha=0.3, - bg_label=-1, bg_color=None, image_alpha=1): +def label2rgb(label, image=None, colors=None, alpha=0.3, + bg_label=-1, bg_color=None, image_alpha=1): """Return an RGB image where color-coded labels are painted over the image. Parameters ---------- - image : array - Input image. If the input is an RGB image, it's converted to grayscale - before coloring. label : array Integer array of labels with the same shape as `image`. + image : array + Image used as underlay for labels. If the input is an RGB image, it's + converted to grayscale before coloring. colors : list List of colors. If the number of labels exceeds the number of colors, then the colors are cycled. alpha : float [0, 1] - Opacity of colorized labels. + Opacity of colorized labels. Ignored if image is `None`. bg_label : int Label that's treated as the background. bg_color : str or array @@ -69,18 +69,23 @@ def image_label2rgb(image, label, colors=None, alpha=0.3, image_alpha : float [0, 1] Opacity of the image. """ - if not image.shape[:2] == label.shape: - raise ValueError("`image` and `label` must be the same shape") - - if image.min() < 0: - warnings.warn("Negative intensities in `image` are not supported") - if colors is None: colors = DEFAULT_COLORS colors = [_rgb_vector(c) for c in colors] - image = img_as_float(rgb2gray(image)) - colorized = gray2rgb(image) * image_alpha + (1 - image_alpha) + if image is None: + colorized = np.zeros(label.shape + (3,), dtype=np.float64) + # Opacity doesn't make sense if no image exists. + alpha = 1 + else: + if not image.shape[:2] == label.shape: + raise ValueError("`image` and `label` must be the same shape") + + if image.min() < 0: + warnings.warn("Negative intensities in `image` are not supported") + + image = img_as_float(rgb2gray(image)) + colorized = gray2rgb(image) * image_alpha + (1 - image_alpha) labels = list(set(label.flat)) color_cycle = itertools.cycle(colors) diff --git a/skimage/color/tests/test_colorlabel.py b/skimage/color/tests/test_colorlabel.py index 35e3ea36..fa6ffcf3 100644 --- a/skimage/color/tests/test_colorlabel.py +++ b/skimage/color/tests/test_colorlabel.py @@ -2,14 +2,14 @@ import itertools import numpy as np from numpy import testing -from skimage.color.colorlabel import image_label2rgb +from skimage.color.colorlabel import label2rgb from numpy.testing import assert_array_almost_equal as assert_close def test_shape_mismatch(): image = np.ones((3, 3)) label = np.ones((2, 2)) - testing.assert_raises(ValueError, image_label2rgb, image, label) + testing.assert_raises(ValueError, label2rgb, image, label) def test_rgb(): @@ -17,7 +17,7 @@ def test_rgb(): label = np.arange(3).reshape(1, -1) colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # Set alphas just in case the defaults change - rgb = image_label2rgb(image, label, colors=colors, alpha=1, image_alpha=1) + rgb = label2rgb(label, image=image, colors=colors, alpha=1, image_alpha=1) assert_close(rgb, [colors]) @@ -25,18 +25,25 @@ def test_alpha(): image = np.random.uniform(size=(3, 3)) label = np.random.randint(0, 9, size=(3, 3)) # If we set `alpha = 0`, then rgb should match image exactly. - rgb = image_label2rgb(image, label, alpha=0, image_alpha=1) + rgb = label2rgb(label, image=image, alpha=0, image_alpha=1) assert_close(rgb[..., 0], image) assert_close(rgb[..., 1], image) assert_close(rgb[..., 2], image) +def test_no_input_image(): + label = np.arange(3).reshape(1, -1) + colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] + rgb = label2rgb(label, colors=colors) + assert_close(rgb, [colors]) + + def test_image_alpha(): image = np.random.uniform(size=(1, 3)) label = np.arange(3).reshape(1, -1) colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # If we set `image_alpha = 0`, then rgb should match label colors exactly. - rgb = image_label2rgb(image, label, colors=colors, alpha=1, image_alpha=0) + rgb = label2rgb(label, image=image, colors=colors, alpha=1, image_alpha=0) assert_close(rgb, [colors]) @@ -46,7 +53,7 @@ def test_color_names(): cnames = ['red', 'lime', 'blue'] colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] # Set alphas just in case the defaults change - rgb = image_label2rgb(image, label, colors=cnames, alpha=1, image_alpha=1) + rgb = label2rgb(label, image=image, colors=cnames, alpha=1, image_alpha=1) assert_close(rgb, [colors]) @@ -55,8 +62,8 @@ def test_bg_and_color_cycle(): label = np.arange(10).reshape(1, -1) colors = [(1, 0, 0), (0, 0, 1)] bg_color = (0, 0, 0) - rgb = image_label2rgb(image, label, bg_label=0, bg_color=bg_color, - colors=colors, alpha=1) + rgb = label2rgb(label, image=image, bg_label=0, bg_color=bg_color, + colors=colors, alpha=1) assert_close(rgb[0, 0], bg_color) for pixel, color in zip(rgb[0, 1:], itertools.cycle(colors)): assert_close(pixel, color)