
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
Mahotas - XYZ to LAB Conversion
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.
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.
The adjusted values are then transformed into LAB components using mathematical equations.
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 −
-
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
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.
Syntax
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).
Example
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()
Output
Following is the output of the above code −

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()
Output
Output of the above code is as follows −
