Esempio n. 1
0
def test_leave_labels_alone():
    labels = np.array([-1, 0, 1])
    labels_saved = labels.copy()

    label2rgb(labels)
    label2rgb(labels, bg_label=1)
    assert_array_equal(labels, labels_saved)
def test_avg():
    label_field = np.array([[1, 1, 1, 2],
                            [1, 2, 2, 2],
                            [3, 3, 3, 3]], dtype=np.uint8)
    r = np.array([[1., 1., 0., 0.],
                  [0., 0., 1., 1.],
                  [0., 0., 0., 0.]])
    g = np.array([[0., 0., 0., 1.],
                  [1., 1., 1., 0.],
                  [0., 0., 0., 0.]])
    b = np.array([[0., 0., 0., 1.],
                  [0., 1., 1., 1.],
                  [0., 0., 1., 1.]])
    image = np.dstack((r, g, b))
    out = label2rgb(label_field, image, kind='avg')
    rout = np.array([[0.5, 0.5, 0.5, 0.5],
                     [0.5, 0.5, 0.5, 0.5],
                     [0. , 0. , 0. , 0. ]])
    gout = np.array([[0.25, 0.25, 0.25, 0.75],
                     [0.25, 0.75, 0.75, 0.75],
                     [0.  , 0.  , 0.  , 0.  ]])
    bout = np.array([[0. , 0. , 0. , 1. ],
                     [0. , 1. , 1. , 1. ],
                     [0.5, 0.5, 0.5, 0.5]])
    expected_out = np.dstack((rout, gout, bout))
    assert_array_equal(out, expected_out)

    out_bg = label2rgb(label_field, image, bg_label=2, bg_color=(0, 0, 0),
                       kind='avg')
    expected_out_bg = expected_out.copy()
    expected_out_bg[label_field == 2] = 0
    assert_array_equal(out_bg, expected_out_bg)
Esempio n. 3
0
def test_label_consistency():
    """Assert that the same labels map to the same colors."""
    label_1 = np.arange(5).reshape(1, -1)
    label_2 = np.array([0, 1])
    colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1)]
    # Set alphas just in case the defaults change
    rgb_1 = label2rgb(label_1, colors=colors)
    rgb_2 = label2rgb(label_2, colors=colors)
    for label_id in label_2.flat:
        assert_close(rgb_1[label_1 == label_id], rgb_2[label_2 == label_id])
Esempio n. 4
0
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 = label2rgb(label, image=image, colors=colors, alpha=1, image_alpha=0)
    assert_close(rgb, [colors])
Esempio n. 5
0
def test_rgb():
    image = np.ones((1, 3))
    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 = label2rgb(label, image=image, colors=colors, alpha=1, image_alpha=1)
    assert_close(rgb, [colors])
Esempio n. 6
0
def overlay_labels(labels, image, colormap, alpha):
    """
    Overlay the labels on the image using the colormap
    :param

    labels : (M,N)
        A numpy array with integer entries which contains the labels.
        Labels must start from 0
    image : (M,N,3)
        A numpy array of type uint8 which contains the image data
    colormap : (N,3)
        A list which contains the colormaps. The entries are of type uint8
    alpha : float
        The opacity of the labelmap
    bglabel : int
        label of background class.
        Pixel labels as bg won't be overlayed with color

    Returns
    -------
    overlayedImg : (M,N,3)
        A numpy array of type uint8
    """

    colors = label2rgb(labels, colors=colormap)
    colors = colors.astype(np.float32)
    image = image.astype(np.float32)
    overlayedImg = colors*alpha + (1-alpha)*image
    overlayedImg = overlayedImg.astype(np.uint8)

    return overlayedImg
Esempio n. 7
0
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 = label2rgb(label, image=image, alpha=0, image_alpha=1)
    assert_close(rgb[..., 0], image)
    assert_close(rgb[..., 1], image)
    assert_close(rgb[..., 2], image)
Esempio n. 8
0
def test_avg():
    # label image
    label_field = np.array([[1, 1, 1, 2],
                            [1, 2, 2, 2],
                            [3, 3, 3, 3]], dtype=np.uint8)

    # color image
    r = np.array([[1., 1., 0., 0.],
                  [0., 0., 1., 1.],
                  [0., 0., 0., 0.]])
    g = np.array([[0., 0., 0., 1.],
                  [1., 1., 1., 0.],
                  [0., 0., 0., 0.]])
    b = np.array([[0., 0., 0., 1.],
                  [0., 1., 1., 1.],
                  [0., 0., 1., 1.]])
    image = np.dstack((r, g, b))

    # reference label-colored image
    rout = np.array([[0.5, 0.5, 0.5, 0.5],
                     [0.5, 0.5, 0.5, 0.5],
                     [0. , 0. , 0. , 0. ]])
    gout = np.array([[0.25, 0.25, 0.25, 0.75],
                     [0.25, 0.75, 0.75, 0.75],
                     [0.  , 0.  , 0.  , 0.  ]])
    bout = np.array([[0. , 0. , 0. , 1. ],
                     [0. , 1. , 1. , 1. ],
                     [0.5, 0.5, 0.5, 0.5]])
    expected_out = np.dstack((rout, gout, bout))

    # test standard averaging
    out = label2rgb(label_field, image, kind='avg')
    assert_array_equal(out, expected_out)

    # test averaging with custom background value
    out_bg = label2rgb(label_field, image, bg_label=2, bg_color=(0, 0, 0),
                       kind='avg')
    expected_out_bg = expected_out.copy()
    expected_out_bg[label_field == 2] = 0
    assert_array_equal(out_bg, expected_out_bg)

    # test default background color
    out_bg = label2rgb(label_field, image, bg_label=2, kind='avg')
    assert_array_equal(out_bg, expected_out_bg)
Esempio n. 9
0
def test_bg_and_color_cycle():
    image = np.zeros((1, 10))  # dummy image
    label = np.arange(10).reshape(1, -1)
    colors = [(1, 0, 0), (0, 0, 1)]
    bg_color = (0, 0, 0)
    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)
Esempio n. 10
0
def test_bg_and_color_cycle():
    image = np.zeros((1, 10))  # dummy image
    label = np.arange(10).reshape(1, -1)
    colors = [(1, 0, 0), (0, 0, 1)]
    bg_color = (0, 0, 0)
    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)
Esempio n. 11
0
def test_avg():
    label_field = np.array([[1, 1, 1, 2], [1, 2, 2, 2], [3, 3, 3, 3]],
                           dtype=np.uint8)
    r = np.array([[1., 1., 0., 0.], [0., 0., 1., 1.], [0., 0., 0., 0.]])
    g = np.array([[0., 0., 0., 1.], [1., 1., 1., 0.], [0., 0., 0., 0.]])
    b = np.array([[0., 0., 0., 1.], [0., 1., 1., 1.], [0., 0., 1., 1.]])
    image = np.dstack((r, g, b))
    out = label2rgb(label_field, image, kind='avg')
    rout = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5],
                     [0., 0., 0., 0.]])
    gout = np.array([[0.25, 0.25, 0.25, 0.75], [0.25, 0.75, 0.75, 0.75],
                     [0., 0., 0., 0.]])
    bout = np.array([[0., 0., 0., 1.], [0., 1., 1., 1.], [0.5, 0.5, 0.5, 0.5]])
    expected_out = np.dstack((rout, gout, bout))
    assert_array_equal(out, expected_out)

    out_bg = label2rgb(label_field,
                       image,
                       bg_label=2,
                       bg_color=(0, 0, 0),
                       kind='avg')
    expected_out_bg = expected_out.copy()
    expected_out_bg[label_field == 2] = 0
    assert_array_equal(out_bg, expected_out_bg)
Esempio n. 12
0
def test_avg():
    # label image
    label_field = np.array([[1, 1, 1, 2], [1, 2, 2, 2], [3, 3, 3, 3]],
                           dtype=np.uint8)

    # color image
    r = np.array([[1., 1., 0., 0.], [0., 0., 1., 1.], [0., 0., 0., 0.]])
    g = np.array([[0., 0., 0., 1.], [1., 1., 1., 0.], [0., 0., 0., 0.]])
    b = np.array([[0., 0., 0., 1.], [0., 1., 1., 1.], [0., 0., 1., 1.]])
    image = np.dstack((r, g, b))

    # reference label-colored image
    rout = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5],
                     [0., 0., 0., 0.]])
    gout = np.array([[0.25, 0.25, 0.25, 0.75], [0.25, 0.75, 0.75, 0.75],
                     [0., 0., 0., 0.]])
    bout = np.array([[0., 0., 0., 1.], [0., 1., 1., 1.], [0.5, 0.5, 0.5, 0.5]])
    expected_out = np.dstack((rout, gout, bout))

    # test standard averaging
    out = label2rgb(label_field, image, kind='avg')
    assert_array_equal(out, expected_out)

    # test averaging with custom background value
    out_bg = label2rgb(label_field,
                       image,
                       bg_label=2,
                       bg_color=(0, 0, 0),
                       kind='avg')
    expected_out_bg = expected_out.copy()
    expected_out_bg[label_field == 2] = 0
    assert_array_equal(out_bg, expected_out_bg)

    # test default background color
    out_bg = label2rgb(label_field, image, bg_label=2, kind='avg')
    assert_array_equal(out_bg, expected_out_bg)
Esempio n. 13
0
def test_uint_image(channel_axis):
    img = np.random.randint(0, 255, (10, 10), dtype=np.uint8)
    labels = np.zeros((10, 10), dtype=np.int64)
    labels[1:3, 1:3] = 1
    labels[6:9, 6:9] = 2
    output = label2rgb(labels,
                       image=img,
                       bg_label=0,
                       channel_axis=channel_axis)
    # Make sure that the output is made of floats and in the correct range
    assert np.issubdtype(output.dtype, np.floating)
    assert output.max() <= 1

    # size 3 (RGB) along the specified channel_axis
    new_axis = channel_axis % output.ndim
    assert output.shape[new_axis] == 3
Esempio n. 14
0
def test_overlay_custom_saturation():
    rgb_img = np.random.uniform(size=(10, 10, 3))
    labels = np.ones((10, 10), dtype=np.int64)
    labels[5:, 5:] = 2
    labels[:3, :3] = 0
    alpha = 0.3
    saturation = 0.3
    rgb = label2rgb(labels,
                    image=rgb_img,
                    alpha=alpha,
                    bg_label=0,
                    saturation=saturation)

    hsv = rgb2hsv(rgb_img)
    hsv[..., 1] *= saturation
    saturaded_img = hsv2rgb(hsv)

    # check that rgb part of input image is saturated, where labels=0
    assert_array_almost_equal(saturaded_img[:3, :3] * (1 - alpha), rgb[:3, :3])
Esempio n. 15
0
def test_label2rgb_shape_errors():
    img = np.random.randint(0, 255, (10, 10, 3), dtype=np.uint8)
    labels = np.zeros((10, 10), dtype=np.int64)
    labels[2:5, 2:5] = 1

    # mismatched 2D shape
    with pytest.raises(ValueError):
        label2rgb(labels, img[1:])

    # too many axes in img
    with pytest.raises(ValueError):
        label2rgb(labels, img[..., np.newaxis])

    # too many channels along the last axis
    with pytest.raises(ValueError):
        label2rgb(labels, np.concatenate((img, img), axis=-1))
Esempio n. 16
0
def test_negative_labels():
    labels = np.array([0, -1, -2, 0])
    rout = np.array([(0., 0., 0.), (0., 0., 1.), (1., 0., 0.), (0., 0., 0.)])
    assert_close(rout, label2rgb(labels, bg_label=0, alpha=1, image_alpha=1))
Esempio n. 17
0
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])
Esempio n. 18
0
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, bg_label=-1)
    assert_array_almost_equal(rgb, [colors])
Esempio n. 19
0
def test_saturation_warning():
    rgb_img = np.random.uniform(size=(10, 10, 3))
    labels = np.ones((10, 10), dtype=np.int64)
    with pytest.raises(UserWarning):
        label2rgb(labels, image=rgb_img, bg_label=0, saturation=2)
        label2rgb(labels, image=rgb_img, bg_label=0, saturation=-1)
Esempio n. 20
0
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])
Esempio n. 21
0
def test_nonconsecutive():
    labels = np.array([0, 2, 4, 0])
    colors = [(1, 0, 0), (0, 0, 1)]
    rout = np.array([(1., 0., 0.), (0., 0., 1.), (1., 0., 0.), (1., 0., 0.)])
    assert_close(rout, label2rgb(labels, colors=colors, alpha=1,
                                 image_alpha=1))
Esempio n. 22
0
def test_negative_labels():
    labels = np.array([0, -1, -2, 0])
    rout = np.array([(0., 0., 0.), (0., 0., 1.), (1., 0., 0.), (0., 0., 0.)])
    assert_close(rout, label2rgb(labels, bg_label=0, alpha=1, image_alpha=1))
Esempio n. 23
0
def test_nonconsecutive():
    labels = np.array([0, 2, 4, 0])
    colors=[(1, 0, 0), (0, 0, 1)]
    rout = np.array([(1., 0., 0.), (0., 0., 1.), (1., 0., 0.), (1., 0., 0.)])
    assert_close(rout, label2rgb(labels, colors=colors, alpha=1, image_alpha=1))
Esempio n. 24
0
def test_shape_mismatch():
    image = np.ones((3, 3))
    label = np.ones((2, 2))
    with pytest.raises(ValueError):
        label2rgb(image, label)
Esempio n. 25
0
def test_shape_mismatch():
    image = np.ones((3, 3))
    label = np.ones((2, 2))
    with testing.raises(ValueError):
        label2rgb(image, label, bg_label=-1)