Mahotas 简明教程

Mahotas - RGB to XYZ Conversion

XYZ 色彩空间是一个三维色彩模型,它基于人类感知来表示颜色的亮度、颜色和强度。

The XYZ color space is a is a three−dimensional color model that represents the brightness, color, and intensity of that color, based on human perception.

在 XYZ 色彩空间中 −

In the XYZ color space −

  1. The Y component represents the luminance or brightness of the color.

  2. The X and Z components determine the chromaticity coordinates or the color’s position on the color spectrum.

  3. By combining different values of X, Y, and Z, any visible color can be represented within the XYZ color space.

当我们从 RGB 转换为 XYZ 时,我们取颜色的红色、绿色和蓝色值,并将其更改为称为 XYZ 的不同值。这有助于我们将颜色信息与它在特定显示器或设备上显示的具体信息区分开来。

When we convert from RGB to XYZ, we are taking the red, green, and blue values of a color and changing them into different values called XYZ. This helps us separate the color information from the specifics of how it appears on a particular display or device.

RGB to XYZ Conversion in Mahotas

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

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

Mahotas 中的 RGB 到 XYZ 的转换涉及以下步骤 -

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

  1. Normalize the RGB values − Normalize the RGB values of a pixel, generally represented as integers ranging from 0 to 255, to a normalized range between 0 and 1. This step ensures that the RGB values are consistent and comparable.

  2. Gamma Correction − Before converting from RGB to XYZ, Mahotas applies gamma correction to the RGB values. Gamma correction adjusts the brightness levels of the image, ensuring that the resulting XYZ values are more accurate representations of the original colors.

  3. Linearize the RGB Values − After gamma correction, the RGB values are converted to a linear color space. In this linear RGB color space, the intensity values are proportional to the actual physical light intensity. This linear transformation allows for more accurate color calculations.

  4. Conversion Matrix − Mahotas uses a conversion matrix to transform the linear RGB values into XYZ values. The conversion matrix represents the relationship between the RGB and XYZ color spaces. It contains coefficients that determine how much of each color channel contributes to the resulting XYZ values.

  5. Output − After applying the conversion matrix, Mahotas provides the XYZ values as the output. These XYZ values represent the color of the input RGB image in a color space that is more perceptually uniform and closer to how the human visual system perceives colors

Using the mahotas.colors.rgb2xyz() Function

mahotas.colors.rgb2xyz() 函数将 RGB 图像作为输入,并返回图像的 XYZ 颜色空间版本。

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

生成的 XYZ 图像保留了原始 RGB 图像的结构和内容。

The resulting XYZ image retains the structure and content of the original RGB image.

以下是 mahotas 中 rgb2xyz() 函数的基本语法 -

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

mahotas.colors.rgb2xyz(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.rgb2xyz() 函数将 RGB 图像转换为 XYZ 图像 -

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

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to XYZ
xyz_image = mh.colors.rgb2xyz(image)
# Create 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 XYZ image
axes[1].imshow(xyz_image)
axes[1].set_title('XYZ 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 −

rgb xyz image

Using Conversion Matrix

我们可以用来将 RGB 图像转换为 XYZ 图像的另一种方法是使用转换矩阵。转换矩阵由系数组成,这些系数将像素的 RGB 分量与 XYZ 分量相关联。

Another approach that we can use to convert an RGB image to an XYZ image is by using a conversion matrix. The conversion matrix consists of coefficients that relate the RGB components of a pixel to the XYZ components.

每个像素的 XYZ 分量的这些值可以按如下方式计算 -

These value of XYZ components of each pixel can be calculated as follows −

X = 0.412456 * r + 0.357576 * g + 0.180437 * b
Y = 0.212672 * r + 0.715152 * g + 0.072175 * b
Z = 0.019334 * r + 0.119193 * g + 0.950471 * b

其中 X, Y, and Z 值表示 XYZ 颜色空间中的相应值。

where X, Y, and Z values represent the corresponding values in the XYZ color space.

Example

以下示例显示了使用 RGB 通道的转换矩阵值将 RGB 图像转换为 XYZ 图像 -

The following example shows conversion of an RGB image to XYZ image using conversion matrix values for the RGB channels −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Function to convert RGB to XYZ
def rgb_to_xyz(rgb):
   height, width, _ = rgb.shape
   xyz_image = np.zeros((height, width, 3))
   for i in range(height):
      for j in range(width):
         # Separating the RGB image into individual channels
         r, g, b = rgb[i, j]
         x = 0.412456 * r + 0.357576 * g + 0.180437 * b
         y = 0.212672 * r + 0.715152 * g + 0.072175 * b
         z = 0.019334 * r + 0.119193 * g + 0.950471 * b
         xyz_image[i, j] = [x, y, z]
   return xyz_image
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to XYZ
xyz_image = rgb_to_xyz(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 XYZ image
axes[1].imshow(xyz_image)
axes[1].set_title('XYZ 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 xyz image1