Mahotas - XYZ 到 LAB 转换



我们在之前的教程中讨论了 XYZ 和 LAB 色彩空间。现在让我们讨论一下如何将 XYZ 色彩空间转换为 LAB 色彩空间。

要将 XYZ 转换为 LAB,我们需要使用特定的公式进行一些计算。这些公式涉及根据参考白点调整 XYZ 值,参考白点代表了观察颜色的标准。

然后使用数学方程将调整后的值转换为 LAB 分量。

简单来说,XYZ 到 LAB 的转换使我们能够以更符合人眼感知的方式来表示颜色,从而更容易准确地分析和比较颜色。

Mahotas 中的 XYZ 到 LAB 转换

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

Mahotas 中的 XYZ 到 LAB 转换涉及以下步骤:

  • **规范化 XYZ 值** - 首先,我们需要通过将 XYZ 值除以白点值来规范化它们。白点表示被认为是纯白色的参考颜色。

    此规范化步骤确保颜色值相对于白点。

  • **计算 LAB 值** - 一旦 XYZ 值被规范化,Mahotas 使用特定的转换矩阵将它们转换为 LAB。这种转换考虑了人类颜色感知中的非线性,并相应地调整颜色值。

  • **获得 LAB 值** - 最后,Mahotas 提供了您开始使用的颜色的 LAB 值。然后可以使用生成的 LAB 值根据其亮度和两个颜色轴来描述颜色。

    • **L 分量** - LAB 中的 L 分量表示颜色的亮度,范围从 0 到 100。较高的值表示较亮的颜色,而较低的值表示较暗的颜色。

    • **A 和 B 分量** - LAB 中的 A 和 B 分量表示颜色信息。A 分量范围从绿色 (-) 到红色 (+),而 B 分量范围从蓝色 (-) 到黄色 (+)。

      这些分量提供了有关 XYZ 值的颜色特征的信息。

使用 mahotas.colors.xyz2lab() 函数

mahotas.colors.xyz2lab() 函数以 XYZ 图像作为输入,并返回图像的 LAB 版本。

生成的 LAB 图像保留了原始 XYZ 图像的结构和整体内容,但更新了每个像素的颜色。

语法

以下是 Mahotas 中 xyz2lab() 函数的基本语法:

mahotas.colors.xyz2lab(xyz, dtype={float})

其中,

  • **xyz** - 它是 XYZ 色彩空间中的输入图像。

  • **dtype(可选)** - 它是返回图像的数据类型(默认为浮点数)。

示例

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

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting RGB to XYZ
xyz_image = mh.colors.rgb2xyz(image)
# Converting XYZ to LAB
lab_image = mh.colors.xyz2lab(xyz_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the XYZ image
axes[0].imshow(xyz_image)
axes[0].set_title('XYZ 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()
输出

以下是上述代码的输出:

XYZ LAB Conversion

随机图像的 XYZ 到 LAB 转换

我们可以通过首先创建具有任何所需尺寸的图像,然后将随机值分配给每个像素的 X、Y 和 Z 通道,将随机生成的 XYZ 图像转换为 LAB 色彩空间。

X、Y 和 Z 通道表示不同的颜色分量。获得 XYZ 图像后,就可以将其转换为 LAB 图像。

生成的图像将位于 LAB 色彩空间中,具有不同的亮度和颜色通道。

示例

以下示例显示了将随机生成的 XYZ 图像转换为 LAB 图像:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Function to create XYZ image
def create_xyz_image(width, height):
   xyz_image = np.zeros((height, width, 3), dtype=np.float32)
   for y in range(height):
      for x in range(width):
         # Assign XYZ values to the pixel
         xyz_image[y, x, 0] = 0.035319468
         xyz_image[y, x, 1] = 0.655582062
         xyz_image[y, x, 2] = 0.157362328
   return xyz_image
# Defining the dimensions of the image
width = 512
height = 512
# Generating the XYZ image
xyz_image = create_xyz_image(width, height)
# Converting XYZ to LAB
lab_image = mh.colors.xyz2lab(xyz_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the XYZ image
axes[0].imshow(xyz_image)
axes[0].set_title('XYZ 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()

输出

上述代码的输出如下:

XYZ LAB Conversion1
广告

© . All rights reserved.