Scikit Image - Painting Images with Labels



In general, painting images with labels refers to the process of visually representing labeled regions or objects in an image using colors. This technique is commonly used in computer vision and image analysis tasks to enhance and understand the Image data.

Painting images with labels are commonly used in various fields such as medical imaging, object detection, semantic segmentation, and image annotation tasks.

In scikit-image library we can use the color.label2rgb() method to superimpose colors on a grayscale image by using an array of labels to encode regions that should be represented with the same color.

The color.label2rgb() function

The color.label2rgb() function is used to generate an RGB image where color-coded labels are painted over the image.

Syntax

Following is the syntax of this method −

skimage.color.label2rgb(label, image=None, colors=None, alpha=0.3, bg_label=0, bg_color=(0, 0, 0), image_alpha=1, kind='overlay', *, saturation=0, channel_axis=-1)

Parameters

  • label: An integer array of labels with the same shape as the image.
  • image(optional): An image used as the underlay for the labels. It should have the same shape as the labels, optionally with an additional RGB channel. If the image is an RGB image, it is converted to grayscale before coloring.
  • colors(optional): A list of colors. If the number of labels exceeds the number of colors, the colors are cycled.
  • alpha(optional): The opacity of the colorized labels. This parameter is ignored if the image is set to None.
  • bg_label(optional): The label that is treated as the background. If bg_label is specified, bg_color is None, and kind is set to 'overlay', the background is not painted with any colors.
  • image_alpha(optional): The opacity of the image.
  • kind(optional): The kind of color image desired. It can be set to 'overlay' or 'avg'. 'overlay' cycles over defined colors and overlays the colored labels on the original image, while 'avg' replaces each labeled segment with its average color, creating a stained-class or pastel painting appearance.
  • saturation(optional): A parameter to control the saturation applied to the original image between fully saturated (original RGB, saturation=1) and fully unsaturated (grayscale, saturation=0). This parameter only applies when kind is set to 'overlay'.
  • channel_axis(optional): This parameter indicates which axis of the output array will correspond to channels. If the image is provided, this must also match the axis of the image that corresponds to channels.

Return Value

This method returns an ndarray of float values representing the blended image of the cycling colormap (colors) for each distinct value in the label, combined with the image at a certain alpha value. The shape of the result is the same as the image.

Example

The following example paints an image with labels using the colors.label2rgb() function.

import numpy as np
import matplotlib.pyplot as plt
from skimage.color import label2rgb

# Generate an image (5x5) and some random labels
image = np.array([[198, 10, 100],
                  [78, 100, 199],
                  [220, 128, 70]], dtype=np.uint8)

labels = np.array([[0, 2, 2],
                  [2, 1, 1],
                  [0, 2, 1]])

# Paint the image with labels.
painted_image = label2rgb(labels, colors=['red', 'green'], image=image, bg_label=0, image_alpha=0.5)

# Display the original image and the painted image.
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))
 
axes[0].imshow(image)
axes[0].set_title('Input Image')
axes[0].axis('off')
 
axes[1].imshow(painted_image)
axes[1].set_title('Output Painted Image with Labels')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements