Mahotas 简明教程
Mahotas - XYZ to LAB Conversion
我们在前面的教程中讨论了 XYZ 和 LAB 颜色空间。现在让我们讨论从 XYZ 颜色空间到 LAB 颜色空间的转换。
We have discussed about XYZ and LAB color space in our previous tutorial. Now let us discuss about the conversion of XYZ color space to LAB color space.
为了将 XYZ 转换为 LAB,我们需要使用特定公式进行一些计算。这些公式涉及根据参考白点调整 XYZ 值,这表示一种查看颜色的标准。
To convert XYZ to LAB, we need to perform some calculations using specific formulas. These formulas involve adjusting the XYZ values based on a reference white point, which represents a standard for viewing colors.
调整后的值然后使用数学方程转换为 LAB 组件。
The adjusted values are then transformed into LAB components using mathematical equations.
简单来说,XYZ 到 LAB 转换允许我们以更符合我们眼睛的感知方式来表示颜色,从而更容易准确分析和比较颜色。
In simple terms, the XYZ to LAB conversion allows us to represent colors in a way that aligns better with how our eyes perceive them, making it easier to analyze and compare colors accurately.
XYZ to LAB Conversion in Mahotas
在 Mahotas 中,我们可以使用 colors.xyz2lab() 函数将 XYZ 图像转换为 LAB 图像。
In Mahotas, we can convert an XYZ image to an LAB image using the colors.xyz2lab() function.
Mahotas 中的 XYZ 到 LAB 转换涉及以下步骤 −
The XYZ to LAB conversion in mahotas involves the following steps −
-
Normalize XYZ values − First, we need to normalize the XYZ values by dividing them by the white point values. The white point represents the reference color that is considered pure white. This normalization step ensures that the color values are relative to the white point.
-
Calculate LAB values − Once the XYZ values are normalized, mahotas uses a specific transformation matrix to convert them to LAB. This transformation takes into account the nonlinearities in human color perception and adjust the color values accordingly.
-
Obtain LAB values − Finally, mahotas provides the LAB values for the color you started with. The resulting LAB values can then be used to describe the color in terms of its lightness and the two color axes. L component − The L component in LAB represents the lightness of the color and ranges from 0 to 100. Higher values indicate brighter colors, while lower values indicate darker colors. A and B components − The A and B components in LAB represent the color information. The A component ranges from green (-) to red (), while the B component ranges from blue (-) to yellow (). These components provide information about the color characteristics of the XYZ values.
Using the mahotas.colors.xyz2lab() Function
mahotas.colors.xyz2lab() 函数将 XYZ 图像作为输入,并返回该图像的 LAB 版本。
The mahotas.colors.xyz2lab() function takes an XYZ image as input and returns the LAB version of the image.
所得的 LAB 图像保留了原始 XYZ 图像的结构和总体内容,但更新了每个像素的颜色。
The resulting LAB image retains the structure and overall content of the original XYZ image but updates the color of each pixel.
以下是 mahotas 中 xyz2lab() 函数的基本语法−
Following is the basic syntax of the xyz2lab() function in mahotas −
mahotas.colors.xyz2lab(xyz, dtype={float})
其中,
where,
-
xyz − It is the input image in XYZ color space.
-
dtype (optional − It is the data type of the returned image (default is float).
在以下示例中,我们使用 mh.colors.xyz2lab() 函数将 XYZ 图像转换为 LAB 图像 −
In the following example, we are converting an XYZ image to a LAB image using the mh.colors.xyz2lab() function −
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()
以下是上面代码的输出: -
Following is the output of the above code −
XYZ to LAB Conversion of a Random Image
我们可以通过首先创建一个任意维度的图像来将随机生成的 XYZ 图像转换为 LAB 颜色空间。接下来,为每个像素的 X、Y 和 Z 通道分配随机值。
We can convert a randomly generated XYZ image to LAB color space by first creating an image with any desired dimensions. Next, assign random values to the X, Y, and Z channels for each pixel.
X、Y 和 Z 通道表示不同的颜色分量。获得 XYZ 图像后,即可将其转换为 LAB 图像。
The X, Y, and Z channels represent different color components. Once you have the XYZ image, you can then convert it to an LAB image.
生成的图像将使用清晰度和颜色通道差异的 LAB 颜色空间。
The resulting image will be in the LAB color space with distinct lightness and color channels.
Example
以下示例展示了将随机生成的 XYZ 图像转换为 LAB 图像:
The following example shows conversion of a randomly generated XYZ image to an LAB image −
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()
上述代码的输出如下:
Output of the above code is as follows −