Mahotas 简明教程

Mahotas - XYZ to LAB Conversion

我们在前面的教程中讨论了 XYZ 和 LAB 颜色空间。现在让我们讨论从 XYZ 颜色空间到 LAB 颜色空间的转换。

为了将 XYZ 转换为 LAB,我们需要使用特定公式进行一些计算。这些公式涉及根据参考白点调整 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

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

The XYZ to LAB conversion in mahotas involves the following steps −

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

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

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

The mahotas.colors.xyz2lab() function takes an XYZ image as input and returns the LAB version of the image.

The resulting LAB image retains the structure and overall content of the original XYZ image but updates the color of each pixel.

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

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

其中,

  1. xyz - 它是 XYZ 颜色空间中的输入图像。

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

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

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

xyz lab conversion

XYZ to LAB Conversion of a Random Image

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.

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.

The resulting image will be in the LAB color space with distinct lightness and color channels.

Example

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

上述代码的输出如下:

xyz lab conversion1