Mahotas - RGB to LAB Conversion



The LAB color space is a color model that approximates human perception of color. It separates color information into three channels −

  • L (Lightness) − The L channel represents the perceived lightness (brightness) of the color. It ranges from 0 (darkest black) to 100 (brightest white).

  • A (Green-Red axis) − Represents the color’s position on the green−red axis. Negative values indicate green, and positive values indicates red.

  • B (Blue-Yellow axis) − Represents the color’s position on the blue-yellow axis. Negative values indicate blue, and positive values indicate yellow.

In the process of converting from RGB to LAB, each RGB pixel value is normalized to a range of 0 and 1.

Then, various mathematical transformations are applied, like adjusting the brightness, making the colors more accurate to how we perceive them, and converting them to LAB values.

These adjustments help us represent colors in a way that matches how humans see them.

RGB to LAB Conversion in Mahotas

In Mahotas, we can convert an RGB image to an LAB image using the colors.rgb2lab() function.

The RGB to LAB conversion in Mahotas involves the following steps −

  • Normalize RGB values − The RGB values of each pixel are first adjusted to a standardized range between 0 and 1.

  • Gamma correction − Gamma correction is applied to the normalized RGB values to adjust the brightness levels of the image.

  • Linearize RGB values − The gamma-corrected RGB values are transformed into a linear RGB color space, ensuring a linear relationship between the input and output values.

  • Convert to XYZ color space − Using a transformation matrix, the linear RGB values are converted to the XYZ color space, which represents the image's color information.

  • Calculate LAB values − From the XYZ values, LAB values are calculated using specific formulas, accounting for how our eyes perceive colors. The LAB color space separates brightness (L) from color components (A and B).

  • Apply reference white values − The LAB values are adjusted based on reference white values to ensure accurate color representation.

  • LAB representation − The resulting LAB values represent the image's color information. The L channel represents lightness, while the A and B channels represent color information along two axes.

Using the mahotas.colors.rgb2lab() Function

The mahotas.colors.rgb2lab() function takes an RGB image as input and returns the LAB color space version of the image.

The resulting LAB image retains the structure and content of the original RGB image while providing enhanced color representation.

Syntax

Following is the basic syntax of the rgb2lab() function in mahotas −

mahotas.colors.rgb2lab(rgb, dtype={float})

where,

  • rgb − It is the input image in RGB color space.

  • dtype (optional) − It is the data type of the returned image (default is float).

Example

In the following example, we are converting an RGB image to an LAB image using the mh.colors.rgb2lab() function −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Converting it to LAB
lab_image = mh.colors.rgb2lab(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the LAB image
axes[1].imshow(lab_image)
axes[1].set_title('LAB Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
Output

Following is the output of the above code −

LAB Color Space

RGB to LAB Conversion of a Random Image

We can convert a randomly generated RGB image to LAB color space by −

  • First, defining the desired size of the image specifying its width and height.
  • We also determine the color depth, usually 8−bit, which ranges from 0 to 255.
  • Next, we generate random RGB values for each pixel in the image using the "random.randint()" function from NumPy.
  • Once we have the RGB image, we proceed to convert it to the LAB color space.

The resulting image will be in the LAB color space, where the image's lightness and color information are separated into distinct channels.

Example

The following example shows conversion of a randomly generated RGB image to an image in LAB color space −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Creating a random RGB image
image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
# Converting it to LAB
lab_image = mh.colors.rgb2lab(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the LAB image
axes[1].imshow(lab_image)
axes[1].set_title('LAB Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

Output

Output of the above code is as follows −

RGB LAB Image
Advertisements