API Change: image_label2rgb changed to label2rgb.

The name change reflects a switch in the argument order (and makes the image an optional argument).
This commit is contained in:
Tony S Yu
2013-05-06 00:18:09 -05:00
parent 29a29721c8
commit fa01b936a6
6 changed files with 45 additions and 33 deletions
@@ -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)
+4 -4
View File
@@ -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')
+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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']
+20 -15
View File
@@ -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)
+15 -8
View File
@@ -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)