Scikit Image - Conversion Between RGB & HSV



The RGB (Red, Green, Blue) 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.

Whereas, the HSV (Hue, Saturation, Value) color model is different from RGB. It separates the color information from the brightness information.

HSV image in scikit-image

In scikit-image, both RGB and HSV images are represented as NumPy arrays with three channels: Red, Green, and Blue for RGB, and Hue, Saturation, and Value for HSV, respectively. The values of each channel range from 0 to 1 for floating-point images or from 0 to 255 for 8-bit images.

Typically, the image arrays have a shape of [height, width, 3], where height represents the image height, width represents the image width, and the last dimension denotes the three channels.

The Scikit-Image library provides functions like color.rgb2hsv() and color.hsv2rgb() to do conversions between RGB and HSV color models.

Converting RGB image to HSV

The skimage.color.rgb2hsv method is used to perform the conversion of an image from the RGB (Red, Green, Blue) color space to the HSV (Hue, Saturation, Value) color space.

Syntax

Following is the syntax of this method −

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

The method accepts the following parameters −

  • rgb: It is an array-like object representing the image in RGB format. The shape of the array should be at least 2-D with the last dimension having a size of 3, representing the three channels (Red, Green, and Blue).
  • channel_axis: An optional parameter indicating which axis of the array corresponds to channels. By default, it is set to -1, which corresponds to the last axis. This parameter was introduced in scikit-image new version 0.19.

The method returns a ndarray representing the image in HSV format. The dimensions of the output array are the same as the input array.

Example

The following example converts an RGB image to an HSV image. Here the RGB image is represented by a NumPy array.

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

# Create a sample RGB image
rgb_image = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
    [[255, 255, 0], [255, 0, 255], [0, 255, 255]]
], dtype=np.uint8)

# Convert RGB to HSV
hsv_image = color.rgb2hsv(rgb_image)

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

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

axes[1].imshow(hsv_image)
axes[1].set_title('Output HSV Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

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

Example

The following example converts a real RGB image to an HSV image.

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

# Read a RGB image 
rgb_image = io.imread('Images/Fruits.jpg')

# Convert RGB to HSV
hsv_image = color.rgb2hsv(rgb_image)

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

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

axes[1].imshow(hsv_image)
axes[1].set_title('Output HSV Image')
axes[1].axis('off')

plt.tight_layout()
plt.show()

Output

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

Converting HSV Image to RGB

The skimage.color.hsv2rgb method is used to perform the conversion from the HSV (Hue, Saturation, Value) color space to the RGB (Red, Green, Blue) color space. Following is the syntax of this method:

skimage.color.hsv2rgb(hsv, *, channel_axis=-1)

The method accepts the following parameters −

  • hsv: It is an array-like object representing the image in HSV format. The shape of the array should be at least 2-D with the last dimension having a size of 3, representing the three channels (Hue, Saturation, and Value).
  • channel_axis: An optional parameter indicating which axis of the array corresponds to channels. By default, it is set to -1, which corresponds to the last axis. This parameter was introduced in scikit-image new version 0.19.

The method returns a ndarray representing the image in RGB format. The dimensions of the output array are the same as the input array.

And the method will raise the ValueError. If the hsv input is not at least 2-D with a shape of (..., 3, ...), indicating the presence of the three channels.

Example

The following code converts an HSV image to an RGB image. Here the HSV image is represented by a NumPy array.

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

# Create a sample HSV image
hsv_image = np.array([[[0, 1, 1], [0.33333333, 1, 1], [0.66666667, 1, 1]],
                      [[0.16666667, 1, 1], [0.83333333, 1, 1], [0.5, 1, 1]]])

# Convert HSV to RGB
rgb_image = color.hsv2rgb(hsv_image)

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

axes[0].imshow(hsv_image)
axes[0].set_title('Input HSV 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

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

Example

The following example converts a real HSV image to an RGB image.

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

# Read the HSV image
hsv_image = io.imread('Images/HSV_Image.jpg')

# Convert HSV to RGB
rgb_image = color.hsv2rgb(hsv_image)

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

axes[0].imshow(hsv_image)
axes[0].set_title('Input HSV 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

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

It's important to note that the conversion between RGB and HSV color spaces may introduce some loss of precision due to the nature of the calculations involving integer arithmetic and rounding.

Advertisements