Mahotas 简明教程

Mahotas - XYZ to RGB Conversion

我们在前面的教程中学习了XYZ颜色空间、RGB颜色空间以及XYZ到RGB的转换。现在我们来讨论XYZ颜色空间到RGB颜色空间的转换。

We learnt about XYZ color space, RGB color space, and RGB to XYZ conversion in our previous tutorial. Now let us discuss about the conversion of XYZ color space to RGB color space.

当我们从XYZ转换为RGB时,我们取颜色的XYZ值(表示其感知属性),并将其转换为红色、绿色和蓝色值。

When we convert from XYZ to RGB, we are taking the XYZ values of a color, which represent its perceptual properties, and transforming them into red, green, and blue values.

此转换允许我们将颜色表示为适合在特定设备或屏幕上显示的格式。

This conversion allows us to represent the color in a format that is suitable for display on a particular device or screen.

XYZ to RGB Conversion in Mahotas

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

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

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

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

  1. Normalize the XYZ values − Normalize the X, Y, and Z values so that they range between 0 and 1. This step ensures that the XYZ values are relative to a reference white point and allows for consistent color calculations.

  2. Convert normalized XYZ to linear RGB − Next, use a conversion matrix to convert the normalized XYZ values to linear RGB values. The conversion matrix specifies how the XYZ coordinates contribute to the red, green, and blue components of the resulting color. The matrix multiplication is performed to obtain the linear RGB values.

  3. Apply gamma correction − Gamma correction adjusts the brightness of the RGB values to match the response of the human visual system.

  4. Scale the RGB values − After gamma correction, the RGB values are typically in the range of 0 to 1. To represent the colors in the 8−bit range (0−255), you need to scale the RGB values. Multiply each of the gamma−corrected RGB values by 255 to bring them to the appropriate scale.

  5. Result − Once the scaling is applied, you have obtained the RGB color values. These values represent the intensities of the red, green, and blue channels of the resulting color.

Using the mahotas.colors.xyz2rgb() Function

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

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

最终得到的 RGB 图像保留了原始 XYZ 图像的结构和内容,但会丢失一些色彩细节。

The resulting RGB image retains the structure and content of the original XYZ image, however some color detail is lost.

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

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

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

其中,

where,

  1. xyz − It is the input image in XYZ color space.

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

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

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

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to XYZ
xyz_image = mh.colors.rgb2xyz(image)
# Converting back to RGB (lossy)
rgb_image = mh.colors.xyz2rgb(xyz_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original XYZ image
axes[0].imshow(xyz_image)
axes[0].set_title('XYZ Image')
axes[0].set_axis_off()
# Displaying the RGB image
axes[1].imshow(rgb_image)
axes[1].set_title('RGB 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 rgb conversion

Using Transformation Matrix

我们可以使用变换矩阵将 XYZ 图像转换为 RGB 图像。变换矩阵有一组用于将 XYZ 像素转换为 RGB 像素的值。

We can use a transformation matrix to convert an XYZ image to an RGB image. The transformation matrix has a set of values that are used to transform a XYZ pixel to RGB pixel.

  1. The XYZ pixels are converted to RGB pixels by doing matrix multiplication between the transformation matrix and the XYZ image.

  2. We achieve this by using the dot() function in the numpy library.

  3. The values of each pixel are then normalized from the range of 0 to 1 (intensity range of XYZ color) to the range of 0 to 255 (intensity range of RGB colors) by multiplying by 255 and then dividing by the maximum intensity of that pixel to obtain the RGB image.

Example

以下示例显示了如何使用变换矩阵将 XYZ 图像转换为 RGB 图像

The following example shows conversion of an XYZ image to an RGB image using transformation matrix −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Function to convert XYZ to RGB
def xyz_to_rgb(xyz_image):
   # XYZ to RGB conversion matrix
   xyz_to_rgb_matrix = np.array([[3.2406, -1.5372, -0.4986],
   [-0.9689, 1.8758, 0.0415],[0.0557, -0.2040, 1.0570]])
   # Perform the XYZ to RGB conversion using matrix multiplication
   rgb_image = np.dot(xyz_image, xyz_to_rgb_matrix.T)
   # Scale the RGB values from the range [0, 1] to [0, 255]
   rgb_image = (rgb_image * 255.0 / np.max(rgb_image)).astype(np.uint8)
   return rgb_image
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to XYZ
xyz_image = mh.colors.rgb2xyz(image)
# Converting back to RGB (lossy)
rgb_image = xyz_to_rgb(xyz_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original XYZ image
axes[0].imshow(xyz_image)
axes[0].set_title('XYZ Image')
axes[0].set_axis_off()
# Displaying the RGB image
axes[1].imshow(rgb_image)
axes[1].set_title('RGB 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 −

xyz rgb conversion1