Implementation:Kornia Kornia Luv Conversion
| 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*u*v* color space using the D65 illuminant.
Description
luv.py is a module in the Kornia library's color subpackage implementing bidirectional RGB-to-Luv (CIE L*u*v*) color space conversion, translated from scikit-image. The conversion pipeline follows: sRGB -> linear RGB -> XYZ -> Luv, using the D65 white point reference. The rgb_to_luv function computes L (lightness), u, and v chromaticity coordinates with an epsilon parameter for numerical stability. The luv_to_rgb function reverses the pipeline: Luv -> XYZ -> linear RGB -> sRGB. Each function has a corresponding nn.Module wrapper (RgbToLuv and LuvToRgb). The module depends on rgb.py and xyz.py for intermediate conversions.
Usage
Import this module when you need perceptually uniform chromaticity coordinates, for example in color constancy algorithms, illumination estimation, or any application where uniform perceptual distances in both lightness and chromaticity are important.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/color/luv.py
- Lines: 1-204
Signature
def rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor: ...
def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor: ...
class RgbToLuv(nn.Module):
def forward(self, image: torch.Tensor) -> torch.Tensor: ...
class LuvToRgb(nn.Module):
def forward(self, image: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.color import rgb_to_luv, luv_to_rgb
from kornia.color import RgbToLuv, LuvToRgb
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| image (rgb_to_luv) | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). Values in range [0, 1]. |
| eps (rgb_to_luv) | float | No | Epsilon for numerical stability. Default: 1e-12. |
| image (luv_to_rgb) | torch.Tensor | Yes | Luv image with shape (*, 3, H, W). |
| eps (luv_to_rgb) | float | No | Epsilon for numerical stability. Default: 1e-12. |
Outputs
| Name | Type | Description |
|---|---|---|
| rgb_to_luv return | torch.Tensor | Luv image with shape (*, 3, H, W). L (lightness), u, and v chromaticity. |
| luv_to_rgb return | torch.Tensor | sRGB image with shape (*, 3, H, W). |
Usage Examples
Basic Usage
import torch
from kornia.color import rgb_to_luv, luv_to_rgb
# Convert RGB to Luv
rgb = torch.rand(1, 3, 128, 128)
luv = rgb_to_luv(rgb)
print(luv.shape) # torch.Size([1, 3, 128, 128])
# Convert Luv back to RGB
rgb_back = luv_to_rgb(luv)
print(rgb_back.shape) # torch.Size([1, 3, 128, 128])
# Using nn.Module wrappers
from kornia.color import RgbToLuv, LuvToRgb
to_luv = RgbToLuv()
to_rgb = LuvToRgb()
luv_out = to_luv(rgb)
rgb_out = to_rgb(luv_out)