Mahotas 简明教程

Mahotas - RGB to LAB Conversion

LAB 颜色空间是一种逼近人类色彩感知的颜色模型。它将颜色信息分成三个通道 −

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

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

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

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

在从 RGB 转换为 LAB 的过程中,每个 RGB 像素值都将标准化为 0 和 1 的范围。

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

然后,应用各种数学变换,例如调整亮度、使色彩更符合我们的感知方式,以及将它们转换为 LAB 值。

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

在 Mahotas 中,我们可以使用 colors.rgb2lab() 函数将 RGB 图像转换为 LAB 图像。

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

Mahotas 中的 RGB 到 LAB 转换涉及以下步骤 −

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

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

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

  3. 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.

  4. 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.

  5. 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).

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

  7. 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

mahotas.colors.rgb2lab() 函数以 RGB 图像作为输入,并返回图像的 LAB 色彩空间版本。

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

所生成的 LAB 图像保留原始 RGB 图像的结构和内容,同时提供增强的色彩表示。

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

以下是 mahotas 中 rgb2lab() 函数的基本语法 −

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

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

其中,

where,

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

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

在以下示例中,我们使用 mh.colors.rgb2lab() 函数将 RGB 图像转换为 LAB 图像 −

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()

以下是上面代码的输出: -

Following is the output of the above code −

lab color space

RGB to LAB Conversion of a Random Image

我们可以通过以下方式将随机生成的 RGB 图像转换为 LAB 色彩空间 −

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

  1. First, defining the desired size of the image specifying its width and height.

  2. We also determine the color depth, usually 8−bit, which ranges from 0 to 255.

  3. Next, we generate random RGB values for each pixel in the image using the "random.randint()" function from NumPy.

  4. Once we have the RGB image, we proceed to convert it to the LAB color space.

结果图像将位于 LAB 颜色空间中,其中图像的亮度和颜色信息被分离到不同的通道中。

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

Example

以下示例显示了将随机生成的 RGB 图像转换为 LAB 颜色空间中的图像:

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 of the above code is as follows −

rgb lab image