# kornia.color¶

The functions in this section perform various color space conversions.

grayscale_to_rgb(image)[source]

Convert a grayscale image to RGB version of image.

The image data is assumed to be in the range of (0, 1).

Parameters

image (Tensor) – grayscale image to be converted to RGB with shape $$(*,1,H,W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*,3,H,W)$$.

Example

>>> input = torch.randn(2, 1, 4, 5)
>>> gray = grayscale_to_rgb(input) # 2x3x4x5

rgb_to_bgr(image)[source]

Convert a RGB image to BGR.

Parameters

image (Tensor) – RGB Image to be converted to BGRof of shape $$(*,3,H,W)$$.

Return type

Tensor

Returns

BGR version of the image with shape of shape $$(*,3,H,W)$$.

Note

See a working example here.

Example

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

rgb_to_grayscale(image, rgb_weights=tensor([0.299, 0.587, 0.114]))[source]

Convert a RGB image to grayscale version of image.

The image data is assumed to be in the range of (0, 1).

Parameters
• image (Tensor) – RGB image to be converted to grayscale with shape $$(*,3,H,W)$$.

• rgb_weights (Tensor, optional) – Weights that will be applied on each channel (RGB). The sum of the weights should add up to one. Default: tensor([0.2990, 0.5870, 0.1140])

Return type

Tensor

Returns

grayscale version of the image with shape $$(*,1,H,W)$$.

Note

See a working example here.

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> gray = rgb_to_grayscale(input) # 2x1x4x5

rgb_to_hsv(image, eps=1e-06)[source]

Convert an image from RGB to HSV.

The image data is assumed to be in the range of (0, 1).

Parameters
• image (Tensor) – RGB Image to be converted to HSV with shape of $$(*, 3, H, W)$$.

• eps (float, optional) – scalar to enforce numarical stability. Default: 1e-06

Return type

Tensor

Returns

HSV version of the image with shape of $$(*, 3, H, W)$$. The H channel values are in the range 0..2pi. S and V are in the range 0..1.

Note

See a working example here.

Example

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

rgb_to_hls(image, eps=1e-08)[source]

Convert a RGB image to HLS.

The image data is assumed to be in the range of (0, 1).

NOTE: this method cannot be compiled with JIT in pytohrch < 1.7.0

Parameters
• image (Tensor) – RGB image to be converted to HLS with shape $$(*, 3, H, W)$$.

• eps (float, optional) – epsilon value to avoid div by zero. Default: 1e-08

Return type

Tensor

Returns

HLS version of the image with shape $$(*, 3, H, W)$$.

Example

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

rgb_to_luv(image, eps=1e-12)[source]

Converts a RGB image to Luv.

The image data is assumed to be in the range of $$[0, 1]$$. Luv color is computed using the D65 illuminant and Observer 2.

Parameters
• image (Tensor) – RGB Image to be converted to Luv with shape $$(*, 3, H, W)$$.

• eps (float, optional) – for numerically stability when dividing. Default: 1e-12

Return type

Tensor

Returns

Luv version of the image with shape $$(*, 3, H, W)$$.

Example

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

rgb_to_lab(image)[source]

Converts a RGB image to Lab.

The image data is assumed to be in the range of $$[0, 1]$$. Lab color is computed using the D65 illuminant and Observer 2.

Parameters

image (Tensor) – RGB Image to be converted to Lab with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

Lab version of the image with shape $$(*, 3, H, W)$$.

Example

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

rgb_to_rgba(image, alpha_val)[source]

Convert an image from RGB to RGBA.

Parameters
• image (Tensor) – RGB Image to be converted to RGBA of shape $$(*,3,H,W)$$.

• alpha_val (float, torch.Tensor) – A float number for the alpha value or a tensor of shape $$(*,1,H,W)$$.

Return type

Tensor

Returns

RGBA version of the image with shape $$(*,4,H,W)$$.

Note

The current functionality is NOT supported by Torchscript.

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_rgba(input, 1.) # 2x4x4x5

rgb_to_xyz(image)[source]

Converts a RGB image to XYZ.

Parameters

image (Tensor) – RGB Image to be converted to XYZ with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

XYZ version of the image with shape $$(*, 3, H, W)$$.

Example

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

rgb_to_ycbcr(image)[source]

Convert an RGB image to YCbCr.

Parameters

image (Tensor) – RGB Image to be converted to YCbCr with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

YCbCr version of the image with shape $$(*, 3, H, W)$$.

Examples

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

rgb_to_yuv(image)[source]

Convert an RGB image to YUV.

The image data is assumed to be in the range of (0, 1).

Parameters

image (Tensor) – RGB Image to be converted to YUV with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

YUV version of the image with shape $$(*, 3, H, W)$$.

Example

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

rgb_to_linear_rgb(image)[source]

Convert an sRGB image to linear RGB. Used in colorspace conversions.

Parameters

image (Tensor) – sRGB Image to be converted to linear RGB of shape $$(*,3,H,W)$$.

Return type

Tensor

Returns

linear RGB version of the image with shape of $$(*,3,H,W)$$.

Example

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

linear_rgb_to_rgb(image)[source]

Convert a linear RGB image to sRGB. Used in colorspace conversions.

Parameters

image (Tensor) – linear RGB Image to be converted to sRGB of shape $$(*,3,H,W)$$.

Return type

Tensor

Returns

sRGB version of the image with shape of shape $$(*,3,H,W)$$.

Example

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

rgba_to_rgb(image)[source]

Convert an image from RGBA to RGB.

Parameters

image (Tensor) – RGBA Image to be converted to RGB of shape $$(*,4,H,W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*,3,H,W)$$.

Example

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

rgba_to_bgr(image)[source]

Convert an image from RGBA to BGR.

Parameters

image (Tensor) – RGBA Image to be converted to BGR of shape $$(*,4,H,W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*,3,H,W)$$.

Example

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

bgr_to_grayscale(image)[source]

Convert a BGR image to grayscale.

The image data is assumed to be in the range of (0, 1). First flips to RGB, then converts.

Parameters

image (Tensor) – BGR image to be converted to grayscale with shape $$(*,3,H,W)$$.

Return type

Tensor

Returns

grayscale version of the image with shape $$(*,1,H,W)$$.

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> gray = bgr_to_grayscale(input) # 2x1x4x5

bgr_to_rgb(image)[source]

Convert a BGR image to RGB.

Parameters

image (Tensor) – BGR Image to be converted to BGR of shape $$(*,3,H,W)$$.

Return type

Tensor

Returns

RGB version of the image with shape of shape $$(*,3,H,W)$$.

Note

See a working example here.

Example

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

bgr_to_rgba(image, alpha_val)[source]

Convert an image from BGR to RGBA.

Parameters
Return type

Tensor

Returns

RGBA version of the image with shape $$(*,4,H,W)$$.

Note

The current functionality is NOT supported by Torchscript.

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = bgr_to_rgba(input, 1.) # 2x4x4x5

hls_to_rgb(image)[source]

Convert a HLS image to RGB.

The image data is assumed to be in the range of (0, 1).

Parameters

image (Tensor) – HLS image to be converted to RGB with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*, 3, H, W)$$.

Example

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

hsv_to_rgb(image)[source]

Convert an image from HSV to RGB.

The H channel values are assumed to be in the range 0..2pi. S and V are in the range 0..1.

Parameters

image (Tensor) – HSV Image to be converted to HSV with shape of $$(*, 3, H, W)$$.

Return type

Tensor

Returns

RGB version of the image with shape of $$(*, 3, H, W)$$.

Example

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

lab_to_rgb(image, clip=True)[source]

Converts a Lab image to RGB.

Parameters
• image (Tensor) – Lab image to be converted to RGB with shape $$(*, 3, H, W)$$.

• clip (bool, optional) – Whether to apply clipping to insure output RGB values in range $$[0, 1]$$. Default: True

Return type

Tensor

Returns

Lab version of the image with shape $$(*, 3, H, W)$$.

Example

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

luv_to_rgb(image, eps=1e-12)[source]

Converts a Luv image to RGB.

Parameters
• image (Tensor) – Luv image to be converted to RGB with shape $$(*, 3, H, W)$$.

• eps (float, optional) – for numerically stability when dividing. Default: 1e-12

Return type

Tensor

Returns

Luv version of the image with shape $$(*, 3, H, W)$$.

Example

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

ycbcr_to_rgb(image)[source]

Convert an YCbCr image to RGB.

The image data is assumed to be in the range of (0, 1).

Parameters

image (Tensor) – YCbCr Image to be converted to RGB with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*, 3, H, W)$$.

Examples

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

yuv_to_rgb(image)[source]

Convert an YUV image to RGB.

The image data is assumed to be in the range of (0, 1).

Parameters

image (Tensor) – YUV Image to be converted to RGB with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*, 3, H, W)$$.

Example

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

xyz_to_rgb(image)[source]

Converts a XYZ image to RGB.

Parameters

image (Tensor) – XYZ Image to be converted to RGB with shape $$(*, 3, H, W)$$.

Return type

Tensor

Returns

RGB version of the image with shape $$(*, 3, H, W)$$.

Example

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


## Modules¶

class GrayscaleToRgb[source]

Module to convert a grayscale image to RGB version of image.

The image data is assumed to be in the range of (0, 1).

Shape:
• image: $$(*, 1, H, W)$$

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

reference:

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

Example

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

class RgbToGrayscale(rgb_weights=tensor([0.299, 0.587, 0.114]))[source]

Module to convert a RGB image to grayscale version of image.

The image data is assumed to be in the range of (0, 1).

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

• output: $$(*, 1, H, W)$$

reference:

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

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> gray = RgbToGrayscale()
>>> output = gray(input)  # 2x1x4x5

class BgrToGrayscale[source]

Module to convert a BGR image to grayscale version of image.

The image data is assumed to be in the range of (0, 1). First flips to RGB, then converts.

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

• output: $$(*, 1, H, W)$$

reference:

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

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> gray = BgrToGrayscale()
>>> output = gray(input)  # 2x1x4x5

class RgbToHsv(eps=1e-06)[source]

Convert an image from RGB to HSV.

The image data is assumed to be in the range of (0, 1).

Parameters

eps (float, optional) – scalar to enforce numarical stability. Default: 1e-06

Returns

HSV version of the image.

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

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

Example

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

class HsvToRgb[source]

Convert an image from HSV to RGB.

H channel values are assumed to be in the range 0..2pi. S and V are in the range 0..1.

Returns

RGB version of the image.

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

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

Example

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

class RgbToHls[source]

Convert an image from RGB to HLS.

The image data is assumed to be in the range of (0, 1).

Returns

HLS version of the image.

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

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

Examples

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

class HlsToRgb[source]

Convert an image from HLS to RGB.

The image data is assumed to be in the range of (0, 1).

Returns

RGB version of the image.

Shape:
• input: $$(*, 3, H, W)$$

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

Reference:

https://en.wikipedia.org/wiki/HSL_and_HSV

Examples

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

class RgbToBgr[source]

Convert an image from RGB to BGR.

The image data is assumed to be in the range of (0, 1).

Returns

BGR version of the image.

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

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

Example

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

class BgrToRgb[source]

Convert image from BGR to RGB.

The image data is assumed to be in the range of (0, 1).

Returns

RGB version of the image.

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

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

Example

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

class RgbToYuv[source]

Convert an image from RGB to YUV.

The image data is assumed to be in the range of (0, 1).

Returns

YUV version of the image.

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

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

Examples

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

Reference::
class YuvToRgb[source]

Convert an image from YUV to RGB.

The image data is assumed to be in the range of (0, 1).

Returns

RGB version of the image.

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

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

Examples

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

class RgbToRgba(alpha_val)[source]

Convert an image from RGB to RGBA.

Add an alpha channel to existing RGB image.

Parameters

alpha_val (Union[float, Tensor]) – A float number for the alpha value or a tensor of shape $$(*,1,H,W)$$.

Returns

RGBA version of the image with shape $$(*,4,H,W)$$.

Return type

torch.Tensor

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

• output: $$(*, 4, H, W)$$

Note

The current functionality is NOT supported by Torchscript.

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> rgba = RgbToRgba(1.)
>>> output = rgba(input)  # 2x4x4x5

class BgrToRgba(alpha_val)[source]

Convert an image from BGR to RGBA.

Add an alpha channel to existing RGB image.

Parameters

alpha_val (Union[float, Tensor]) – A float number for the alpha value or a tensor of shape $$(*,1,H,W)$$.

Returns

RGBA version of the image with shape $$(*,4,H,W)$$.

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

• output: $$(*, 4, H, W)$$

Note

The current functionality is NOT supported by Torchscript.

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> rgba = BgrToRgba(1.)
>>> output = rgba(input)  # 2x4x4x5

class RgbaToRgb[source]

Convert an image from RGBA to RGB.

Remove an alpha channel from RGB image.

Returns

RGB version of the image.

Shape:
• image: $$(*, 4, H, W)$$

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

Example

>>> input = torch.rand(2, 4, 4, 5)
>>> rgba = RgbaToRgb()
>>> output = rgba(input)  # 2x3x4x5

class RgbaToBgr[source]

Convert an image from RGBA to BGR.

Remove an alpha channel from BGR image.

Returns

BGR version of the image.

Shape:
• image: $$(*, 4, H, W)$$

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

Example

>>> input = torch.rand(2, 4, 4, 5)
>>> rgba = RgbaToBgr()
>>> output = rgba(input)  # 2x3x4x5

class RgbToXyz[source]

Converts an image from RGB to XYZ.

The image data is assumed to be in the range of (0, 1).

Returns

XYZ version of the image.

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

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

Examples

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

Reference:
class XyzToRgb[source]

Converts an image from XYZ to RGB.

Returns

RGB version of the image.

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

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

Examples

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

Reference:
class RgbToLuv[source]

Converts an image from RGB to Luv.

The image data is assumed to be in the range of $$[0, 1]$$. Luv color is computed using the D65 illuminant and Observer 2.

Returns

Luv version of the image.

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

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

Examples

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

Reference:
class LuvToRgb[source]

Converts an image from Luv to RGB.

Returns

RGB version of the image.

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

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

Examples

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


References

class YcbcrToRgb[source]

Convert an image from YCbCr to Rgb.

The image data is assumed to be in the range of (0, 1).

Returns

RGB version of the image.

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

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

Examples

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

class RgbToYcbcr[source]

Convert an image from RGB to YCbCr.

The image data is assumed to be in the range of (0, 1).

Returns

YCbCr version of the image.

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

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

Examples

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

class RgbToLab[source]

Converts an image from RGB to Lab.

The image data is assumed to be in the range of $$[0, 1]$$. Lab color is computed using the D65 illuminant and Observer 2.

Returns

Lab version of the image.

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

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

Examples

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

Reference:
class LabToRgb[source]

Converts an image from Lab to RGB.

Returns

RGB version of the image. Range may not be in $$[0, 1]$$.

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

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

Examples

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


References

class LinearRgbToRgb[source]

Convert a linear RGB image to sRGB.

Applies gamma correction to linear RGB values, at the end of colorspace conversions, to get sRGB.

Returns

sRGB version of the image.

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

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

Example

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


References

class RgbToLinearRgb[source]

Convert an image from sRGB to linear RGB.

Reverses the gamma correction of sRGB to get linear RGB values for colorspace conversions. The image data is assumed to be in the range of $$[0, 1]$$

Returns

Linear RGB version of the image.

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

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

Example

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


References