CIE Lab#

Functions#

kornia.color.rgb_to_lab(image)[source]#

Convert an sRGB image to Lab.

lab = rgb_to_lab(image) converts a (*, 3, H, W) sRGB image in \([0, 1]\) to CIE Lab with the D65 white point and the 2° observer. L is in \([0, 100]\), and a and b lie within \([-128, 127]\).

_images/rgb_to_lab.png
Convention:

Float16 and bfloat16 inputs are computed in float32 and rounded to the input dtype at return. Channels are L, a, b at axis -3. The sRGB curve is removed inside, so pass sRGB, not linear RGB. The scale is that of OpenCV’s float32 Lab, not of its 8-bit Lab, where L is scaled to 0..255 and a and b are offset by 128. Values match skimage rgb2lab. OpenCV 5’s float COLOR_RGB2Lab quantizes its input, so it differs by less than 1 in each channel.

Parameters:

image (Tensor) – sRGB image with shape (*, 3, H, W).

Return type:

Tensor

Returns:

Lab image with shape (*, 3, H, W).

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_lab(input)  # 2x3x4x5
kornia.color.lab_to_rgb(image, clip=True)[source]#

Convert a Lab image to sRGB.

rgb = lab_to_rgb(image) converts a (*, 3, H, W) CIE Lab image with the D65 white point and the 2° observer to sRGB. L is expected in \([0, 100]\), and a and b in \([-128, 127]\).

Convention:

Float16 and bfloat16 inputs are computed in float32 and rounded to the input dtype at return. Channels are L, a, b at axis -3.

Parameters:
  • image (Tensor) – Lab image with shape (*, 3, H, W).

  • clip (bool, optional) – clamp the result to \([0, 1]\). With False, out-of-gamut Lab values give RGB values outside it. Default: True

Return type:

Tensor

Returns:

sRGB image with shape (*, 3, H, W).

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = lab_to_rgb(input)  # 2x3x4x5

Modules#

class kornia.color.RgbToLab(*args, **kwargs)[source]#

Convert an image from sRGB to Lab (module form).

out = RgbToLab()(image) is rgb_to_lab().

Convention:
Shape:
  • image: (*, 3, H, W)

  • output: (*, 3, H, W)

See also

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> lab = RgbToLab()
>>> output = lab(input)  # 2x3x4x5

References

[1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html

[2] https://www.easyrgb.com/en/math.php

[3] torch/image

class kornia.color.LabToRgb(*args, **kwargs)[source]#

Convert an image from Lab to sRGB (module form).

out = LabToRgb()(image, clip) is lab_to_rgb(). clip is an argument of the call, not of the constructor.

Convention:
Shape:
  • image: (*, 3, H, W)

  • output: (*, 3, H, W)

See also

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = LabToRgb()
>>> output = rgb(input)  # 2x3x4x5

References

[1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html

[2] https://www.easyrgb.com/en/math.php

[3] torch/image