Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Lab Conversion

From Leeroopedia


Knowledge Sources
Domains Vision, Color_Processing
Last Updated 2026-02-09 15:00 GMT

Overview

This module provides differentiable conversion between RGB and CIE L*a*b* color space using the D65 illuminant.

Description

lab.py is a module in the Kornia library's color subpackage implementing bidirectional RGB-to-Lab (CIE L*a*b*) color space conversion, translated from scikit-image. The conversion pipeline is: sRGB -> linear RGB (gamma removal) -> XYZ (via the CIE RGB-to-XYZ matrix) -> Lab (nonlinear normalization with D65 white point). The rgb_to_lab function outputs L in [0, 100] and a, b in [-128, 127]. The lab_to_rgb function reverses this pipeline and includes an optional clipping step to constrain the output to [0, 1]. Each function has a corresponding nn.Module wrapper (RgbToLab and LabToRgb). The module depends on rgb.py (for sRGB/linear RGB conversion) and xyz.py (for XYZ conversion).

Usage

Import this module when you need perceptually uniform color differences (e.g., for color-based loss functions, image colorization, color transfer, or perceptual image quality metrics).

Code Reference

Source Location

Signature

def rgb_to_lab(image: torch.Tensor) -> torch.Tensor: ...
def lab_to_rgb(image: torch.Tensor, clip: bool = True) -> torch.Tensor: ...

class RgbToLab(nn.Module):
    def forward(self, image: torch.Tensor) -> torch.Tensor: ...

class LabToRgb(nn.Module):
    def forward(self, image: torch.Tensor, clip: bool = True) -> torch.Tensor: ...

Import

from kornia.color import rgb_to_lab, lab_to_rgb
from kornia.color import RgbToLab, LabToRgb

I/O Contract

Inputs

Name Type Required Description
image (rgb_to_lab) torch.Tensor Yes RGB image with shape (*, 3, H, W). Values in range [0, 1].
image (lab_to_rgb) torch.Tensor Yes Lab image with shape (*, 3, H, W). L in [0, 100], a in [-128, 127], b in [-128, 127].
clip (lab_to_rgb) bool No Whether to clamp the output RGB values to [0, 1]. Default: True.

Outputs

Name Type Description
rgb_to_lab return torch.Tensor Lab image with shape (*, 3, H, W). L in [0, 100], a and b in [-128, 127].
lab_to_rgb return torch.Tensor RGB image with shape (*, 3, H, W). Values in [0, 1] if clip=True.

Usage Examples

Basic Usage

import torch
from kornia.color import rgb_to_lab, lab_to_rgb

# Convert RGB to Lab
rgb = torch.rand(1, 3, 128, 128)
lab = rgb_to_lab(rgb)
print(lab.shape)  # torch.Size([1, 3, 128, 128])
# L channel range: roughly [0, 100]
# a, b channels range: roughly [-128, 127]

# Convert Lab back to RGB
rgb_back = lab_to_rgb(lab, clip=True)
print(rgb_back.shape)  # torch.Size([1, 3, 128, 128])

# Compute perceptual color distance (Delta E) between two images
rgb_a = torch.rand(1, 3, 64, 64)
rgb_b = torch.rand(1, 3, 64, 64)
lab_a = rgb_to_lab(rgb_a)
lab_b = rgb_to_lab(rgb_b)
delta_e = torch.sqrt(((lab_a - lab_b) ** 2).sum(dim=1))
print(delta_e.shape)  # torch.Size([1, 64, 64])

# Using nn.Module wrappers
from kornia.color import RgbToLab
to_lab = RgbToLab()
lab_out = to_lab(rgb)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment