Mahotas 简明教程

Mahotas - RGB to XYZ Conversion

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

在 XYZ 色彩空间中 −

  1. Y 组件表示颜色的亮度或亮度。

  2. X 和 Z 组件确定色度坐标或颜色在光谱上的位置。

  3. 通过组合 X、Y 和 Z 的不同值,可以在 XYZ 色彩空间内表示任何可见的颜色。

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

RGB to XYZ Conversion in Mahotas

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

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

  1. Normalize the RGB values − 将像素的 RGB 值(通常表示为 0 到 255 之间的整数)标准化为 0 到 1 之间的标准化范围。此步骤确保 RGB 值一致且可比较。

  2. Gamma Correction − 在从 RGB 转换为 XYZ 之前,Mahotas 对 RGB 值应用伽马校正。伽马校正调整图像的亮度级别,确保得到的 XYZ 值更准确地表示原始颜色。

  3. Linearize the RGB Values − 伽马校正后,RGB 值将转换为线性颜色空间。在此线性 RGB 颜色空间中,强度值与实际物理光强度成正比。此线性转换允许进行更准确的颜色计算。

  4. Conversion Matrix − Mahotas 使用一个转换矩阵将线性 RGB 值转换为 XYZ 值。转换矩阵表示 RGB 和 XYZ 颜色空间之间的关系。它包含确定每个颜色通道对最终 XYZ 值的贡献量的系数。

  5. Output − 应用转换矩阵后,Mahotas 将 XYZ 值作为输出提供。这些 XYZ 值以更知觉纯色并更接近人眼感知颜色的颜色空间表示输入 RGB 图像的颜色。

Using the mahotas.colors.rgb2xyz() Function

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

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

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

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

其中,

  1. rgb − RGB 颜色空间中的输入图像。

  2. dtype (optional) − 返回图像的数据类型(默认为 float)

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

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

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

rgb xyz image

Using Conversion Matrix

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

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

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 颜色空间中的相应值。

Example

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

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

上述代码的输出如下:

rgb xyz image1