Scikit Image - Conversion Between Color & Gray Values



Color images can be represented using different color spaces, and one of the most common and widely used color spaces is the RGB color space. In the RGB color space, an image is represented by three separate channels: the red channel (R), the green channel (G), and the blue channel (B). Each channel represents the intensity of its respective color component at each pixel location in the image.

The RGB color model is an additive model used to create colors by combining red, green, and blue light. It is primarily used for sensing, representing, and displaying images in electronic systems, such as computers and televisions.

Color Images in Scikit

In scikit-image, an RGB image is represented as a NumPy array with three channels: Red, Green, and Blue. Each channel's values range from 0 to 1 for floating-point images or from 0 to 255 for 8-bit images. Typically, the RGB image array has a shape of [height, width, 3].

On the other hand, grayscale images are represented by a single channel that contains only the intensity values of each pixel. Grayscale images are often used when color information is not necessary or when working with black-and-white images.

The Scikit-Image library provides functions like rgb2gray and gray2rgb to do conversions between color and gray values.

Converting RGB image to a Grayscale image

The color.rgb2gray() method in the scikit-image library is used to compute the luminance of an RGB image. It takes an input RGB image and returns a grayscale image representing the luminance.

The luminance value of each grayscale pixel is calculated as the weighted sum of the corresponding red, green, and blue pixels using the formula:

Y = 0.2125 R + 0.7154 G + 0.0721 B

These weights are based on the perception of red, green, and blue by human vision and are commonly used by CRT phosphors.

Note:If the input image has an alpha channel, then the method will ignore it.

Syntax

Following is the syntax of this function −

skimage.color.rgb2gray(rgb, *, channel_axis=-1)

The parameter rgb is an array-like object representing the input image in RGB format.

This method returns an ndarray representing the luminance image, which is the same size as the input array but with the channel dimension removed.

Example

In the following example, the color.rgb2gray() method will convert a color image to a grayscale image.

from skimage import color, io
import matplotlib.pyplot as plt

# Read an RGB image
rgb_image = io.imread('Images/image_.jpg')
print('Shape of the input RGB image array:',rgb_image.shape)

# Convert RGB to Gray
gray_image = color.rgb2gray(rgb_image)

# Print the result
print('Shape of the output Gray image array:',gray_image.shape)

# Display the RGB and Gray images 
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 8))

axes[0].imshow(rgb_image)
axes[0].set_title('Input RGB Image')
axes[0].axis('off')

axes[1].imshow(gray_image, cmap=plt.cm.gray)
axes[1].set_title('Output Gray Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

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

Shape of the input RGB image array: (246, 462, 3)
Shape of the output Gray image array: (246, 462)
RGB

Converting Grayscale image to RGB image

This method is used to create an RGB representation of a gray-level image. If the input image is 1-dimensional with shape (M, ), the output RGB image will have shape (M, 3), where each grayscale pixel value is repeated three times to create an RGB representation. Following is the syntax of this method.

skimage.color.gray2rgb(image, *, channel_axis=-1)

The method accepts the following parameters −

  • image: It is an array-like object representing the input gray-level image.
  • channel_axis: An optional parameter indicating which axis of the output array will correspond to channels.

This method returns an ndarray representing the RGB image. A new dimension of length 3 is added to the input image, representing the RGB channels.

Example

The following example demonstrates the conversion of a grayscale image to an RGB image using color.gray2rgb() method.

from skimage import color, io
import matplotlib.pyplot as plt

# Read an RGB image as a grayscale image
gray_image = io.imread('F:/Images/Fruits.jpg', as_gray=True)

print('Shape of the input Gray image array:', gray_image.shape)

# Convert Gray to RGB
rgb_image = color.gray2rgb(gray_image)

# Print the result
print('Shape of the output RGB image array:', rgb_image.shape)

# Display the grayscale and RGB images
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10, 10))

axes[0].imshow(gray_image)
axes[0].set_title('Input Gray Image')
axes[0].axis('off')

axes[1].imshow(rgb_image)
axes[1].set_title('Output RGB Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

When you run the above program, it will produce the following output −

Shape of the input Gray image array: (257, 698)
Shape of the output RGB image array: (257, 698, 3)
GRAY
Advertisements