Learn Mahotas – RGB to XYZ Conversion work project make money

Mahotas – RGB to XYZ Conversion



The XYZ color space is a is a three−dimensional color model that represents the brightness, color, and intensity of that color, based on human perception.

In the XYZ color space −

  • The Y component represents the luminance or brightness of the color.
  • The X and Z components determine the chromaticity coordinates or the color”s position on the color spectrum.
  • By combining different values of X, Y, and Z, any visible color can be represented within the XYZ color space.

When we convert from RGB to XYZ, we are taking the red, green, and blue values of a color and changing them into different values called XYZ. This helps us separate the color information from the specifics of how it appears on a particular display or device.

RGB to XYZ Conversion in Mahotas

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

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

  • Normalize the RGB values − Normalize the RGB values of a pixel, generally represented as integers ranging from 0 to 255, to a normalized range between 0 and 1.

    This step ensures that the RGB values are consistent and comparable.

  • Gamma Correction − Before converting from RGB to XYZ, Mahotas applies gamma correction to the RGB values.

    Gamma correction adjusts the brightness levels of the image, ensuring that the resulting XYZ values are more accurate representations of the original colors.

  • Linearize the RGB Values − After gamma correction, the RGB values are converted to a linear color space. In this linear RGB color space, the intensity values are proportional to the actual physical light intensity.

    This linear transformation allows for more accurate color calculations.

  • Conversion Matrix − Mahotas uses a conversion matrix to transform the linear RGB values into XYZ values. The conversion matrix represents the relationship between the RGB and XYZ color spaces.

    It contains coefficients that determine how much of each color channel contributes to the resulting XYZ values.

  • Output − After applying the conversion matrix, Mahotas provides the XYZ values as the output. These XYZ values represent the color of the input RGB image in a color space that is more perceptually uniform and closer to how the human visual system perceives colors

Using the mahotas.colors.rgb2xyz() Function

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

The resulting XYZ image retains the structure and content of the original RGB image.

Syntax

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

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

where,

  • rgb − It is the input image in RGB color space.

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

Example

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

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

Following is the output of the above code −

RGB XYZ Image

Using Conversion Matrix

Another approach that we can use to convert an RGB image to an XYZ image is by using a conversion matrix. The conversion matrix consists of coefficients that relate the RGB components of a pixel to the XYZ components.

These value of XYZ components of each pixel can be calculated as follows −

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

where X, Y, and Z values represent the corresponding values in the XYZ color space.

Example

The following example shows conversion of an RGB image to XYZ image using conversion matrix values for the RGB channels −

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

Output

Output of the above code is as follows −

RGB XYZ Image1

Leave a Reply

Your email address will not be published. Required fields are marked *