kornia.color

The functions in this section perform various color space conversions.

rgb_to_bgr(image: torch.Tensor) → torch.Tensor[source]

Convert a RGB image to BGR.

See RgbToBgr for details.

Parameters

image (torch.Tensor) – RGB Image to be converted to BGR.

Returns

BGR version of the image.

Return type

torch.Tensor

rgb_to_grayscale(input: torch.Tensor) → torch.Tensor[source]

Convert a RGB image to grayscale.

See RgbToGrayscale for details.

Parameters

input (torch.Tensor) – RGB image to be converted to grayscale.

Returns

Grayscale version of the image.

Return type

torch.Tensor

rgb_to_hsv(image: torch.Tensor) → torch.Tensor[source]

Convert an RGB image to HSV.

Parameters

input (torch.Tensor) – RGB Image to be converted to HSV.

Returns

HSV version of the image.

Return type

torch.Tensor

rgb_to_hls(image: torch.Tensor) → torch.Tensor[source]

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

Parameters

input (torch.Tensor) – RGB Image to be converted to HLS.

Returns

HLS version of the image.

Return type

torch.Tensor

rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) → torch.Tensor[source]

Converts a RGB image to Luv.

See RgbToLuv for details.

Parameters
  • image (torch.Tensor) – RGB image

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

Returns

Luv image

Return type

torch.Tensor

rgb_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) → torch.Tensor[source]

Convert image from RGB to RGBA.

See RgbToRgba for details.

Parameters
Returns

RGBA version of the image.

Return type

torch.Tensor

rgb_to_xyz(image: torch.Tensor) → torch.Tensor[source]

Converts a RGB image to XYZ.

See RgbToXyz for details.

Parameters

image (torch.Tensor) – RGB Image to be converted to XYZ.

Returns

XYZ version of the image.

Return type

torch.Tensor

rgb_to_ycbcr(image: torch.Tensor) → torch.Tensor[source]

Convert an RGB image to YCbCr.

Parameters

image (torch.Tensor) – RGB Image to be converted to YCbCr.

Returns

YCbCr version of the image.

Return type

torch.Tensor

rgb_to_yuv(input: torch.Tensor) → torch.Tensor[source]

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

Parameters

input (torch.Tensor) – RGB Image to be converted to YUV.

Returns

YUV version of the image.

Return type

torch.Tensor

See RgbToYuv for details.

rgba_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Convert image from RGBA to RGB.

See RgbaToRgb for details.

Parameters

image (torch.Tensor) – RGBA Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

rgba_to_bgr(image: torch.Tensor) → torch.Tensor[source]

Convert image from RGBA to BGR.

See RgbaToBgr for details.

Parameters

image (torch.Tensor) – RGBA Image to be converted to BGR.

Returns

BGR version of the image.

Return type

torch.Tensor

bgr_to_grayscale(input: torch.Tensor) → torch.Tensor[source]

Convert a BGR image to grayscale.

See BgrToGrayscale for details.

Parameters

input (torch.Tensor) – BGR image to be converted to grayscale.

Returns

Grayscale version of the image.

Return type

torch.Tensor

bgr_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Convert a BGR image to RGB.

See BgrToRgb for details.

Parameters

image (torch.Tensor) – BGR Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

bgr_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) → torch.Tensor[source]

Convert image from BGR to RGBA.

See BgrToRgba for details.

Parameters
Returns

RGBA version of the image.

Return type

torch.Tensor

hls_to_rgb(image: torch.Tensor) → torch.Tensor[source]

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

Parameters

input (torch.Tensor) – HLS Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

hsv_to_rgb(image: torch.Tensor) → torch.Tensor[source]

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

Parameters

input (torch.Tensor) – HSV Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) → torch.Tensor[source]

Converts a Luv image to RGB.

See LuvToRgb for details.

Parameters
  • image (torch.Tensor) – Luv image

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

Returns

RGB image

Return type

torch.Tensor

ycbcr_to_rgb(image: torch.Tensor) → torch.Tensor[source]

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

Parameters

image (torch.Tensor) – YCbCr Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

yuv_to_rgb(input: torch.Tensor) → torch.Tensor[source]

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

Parameters

input (torch.Tensor) – YUV Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

See YuvToRgb for details.

xyz_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Converts a XYZ image to RGB.

See XyzToRgb for details.

Parameters

image (torch.Tensor) – XYZ Image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

Modules

class RgbToGrayscale[source]

convert RGB image to grayscale version of image.

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

Parameters

input (torch.Tensor) – RGB image to be converted to grayscale.

Returns

grayscale version of the image.

Return type

torch.Tensor

shape:
  • input: \((*, 3, H, W)\)

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

reference:

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> gray = kornia.color.RgbToGrayscale()
>>> output = gray(input)  # 2x1x4x5
class BgrToGrayscale[source]

convert BGR image to grayscale version of image.

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

Parameters

input (torch.Tensor) – BGR image to be converted to grayscale.

Returns

grayscale version of the image.

Return type

torch.Tensor

shape:
  • input: \((*, 3, H, W)\)

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

reference:

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> gray = kornia.color.BgrToGrayscale()
>>> output = gray(input)  # 2x1x4x5
class RgbToHsv[source]

Convert image from RGB to HSV.

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

Parameters

image (torch.Tensor) – RGB image to be converted to HSV.

Returns

HSV version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> hsv = kornia.color.RgbToHsv()
>>> output = hsv(input)  # 2x3x4x5
class HsvToRgb[source]

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

Parameters

image (torch.Tensor) – HSV image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = kornia.color.HsvToRgb()
>>> output = rgb(input)  # 2x3x4x5
class RgbToHls[source]

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

Parameters

image (torch.Tensor) – RGB image to be converted to HLS.

Returns

HLS version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> hls = kornia.color.RgbToHls()
>>> output = hls(input)  # 2x3x4x5
class HlsToRgb[source]

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

Parameters

image (torch.Tensor) – HLS image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

reference:

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = kornia.color.HlsToRgb()
>>> output = rgb(input)  # 2x3x4x5
class RgbToBgr[source]

Convert image from RGB to BGR.

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

Returns

BGR version of the image.

Return type

torch.Tensor

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

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> bgr = kornia.color.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.

Return type

torch.Tensor

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

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = kornia.color.BgrToRgb()
>>> output = rgb(input)  # 2x3x4x5
class RgbToYuv[source]

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

Parameters

image (torch.Tensor) – RGB image to be converted to YUV.

Returns

YUV version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples::
>>> input = torch.rand(2, 3, 4, 5)
>>> yuv = kornia.color.RgbToYuv()
>>> output = yuv(input)  # 2x3x4x5
Reference::

[1] https://es.wikipedia.org/wiki/YUV#RGB_a_Y’UV

class YuvToRgb[source]

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

Parameters

image (torch.Tensor) – YUV image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples::
>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = kornia.color.YuvToRgb()
>>> output = rgb(input)  # 2x3x4x5
class RgbToRgba(alpha_val: Union[float, torch.Tensor])[source]

Convert image from RGB to RGBA.

Add an alpha channel to existing RGB image.

Parameters

alpha_val (float, torch.Tensor) – A float number for the alpha value.

Returns

RGBA version of the image.

Return type

torch.Tensor

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

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

Examples:

>>> input = torch.rand(2, 3, 4, 5)
>>> rgba = kornia.color.RgbToRgba(1.)
>>> output = rgba(input)  # 2x4x4x5
class BgrToRgba(alpha_val: Union[float, torch.Tensor])[source]

Convert image from BGR to RGBA.

Add an alpha channel to existing BGR image.

Parameters

alpha_val (float, torch.Tensor) – A float number for the alpha value.

Returns

RGBA version of the image.

Return type

torch.Tensor

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

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

Examples:

>>> input = torch.rand(2, 3, 4, 5)
>>> rgba = kornia.color.BgrToRgba(1.)
>>> output = rgba(input)  # 2x4x4x5
class RgbaToRgb[source]

Convert image from RGBA to RGB.

Remove an alpha channel from RGB image.

Returns

RGB version of the image.

Return type

torch.Tensor

shape:
  • image: \((*, 4, H, W)\)

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

Examples:

>>> input = torch.rand(2, 4, 4, 5)
>>> rgba = kornia.color.RgbaToRgb()
>>> output = rgba(input)  # 2x3x4x5
class RgbaToBgr[source]

Convert image from RGBA to BGR.

Remove an alpha channel from BGR image.

Returns

BGR version of the image.

Return type

torch.Tensor

shape:
  • image: \((*, 4, H, W)\)

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

Examples:

>>> input = torch.rand(2, 4, 4, 5)
>>> rgba = kornia.color.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).

Parameters

image (torch.Tensor) – RGB image to be converted to XYZ.

Returns

XYZ version of the image.

Return type

torch.Tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples

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

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

class XyzToRgb[source]

Converts an image from XYZ to RGB

Parameters

image (torch.Tensor) – XYZ image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples

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

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

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.

Parameters

image (torch.Tensor) – RGB image to be converted to Luv.

Returns

Luv version of the image.

Return type

torch.Tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples

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

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

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

[3] http://www.poynton.com/ColorFAQ.html

class LuvToRgb[source]

Converts an image from Luv to RGB

Parameters

image (torch.Tensor) – Luv image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.Tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = kornia.color.LuvToRgb()
>>> 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] http://www.poynton.com/ColorFAQ.html

class YcbcrToRgb[source]

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

Parameters

image (torch.Tensor) – YCbCr image to be converted to RGB.

Returns

RGB version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples:

>>> import torch
>>> import kornia
>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = kornia.color.YcbcrToRgb()
>>> output = rgb(input)  # 2x3x4x5
class RgbToYcbcr[source]

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

Parameters

image (torch.Tensor) – RGB image to be converted to YCbCr.

Returns

YCbCr version of the image.

Return type

torch.tensor

shape:
  • image: \((*, 3, H, W)\)

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

Examples:

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