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.

Parameters

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_bgr(input) # 2x3x4x5
rgb_to_grayscale(image: torch.Tensor) → torch.Tensor[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 (torch.Tensor) – RGB image to be converted to grayscale with shape \((*,3,H,W)\).

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> gray = rgb_to_grayscale(input) # 2x1x4x5
rgb_to_hsv(image: torch.Tensor) → torch.Tensor[source]

Convert an 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 with shape of \((*, 3, H, W)\).

Returns

HSV version of the image with shape of \((*, 3, H, W)\).

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_hsv(input)  # 2x3x4x5
rgb_to_hls(image: torch.Tensor) → torch.Tensor[source]

Convert a RGB image 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 with shape \((*, 3, H, W)\).

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_hls(input)  # 2x3x4x5
rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) → torch.Tensor[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 (torch.Tensor) – RGB Image to be converted to Luv with shape \((*, 3, H, W)\).

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_luv(input)  # 2x3x4x5
rgb_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) → torch.Tensor[source]

Convert an image from RGB to RGBA.

Parameters
  • image (torch.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)\).

Returns

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

Return type

torch.Tensor

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: torch.Tensor) → torch.Tensor[source]

Converts a RGB image to XYZ.

Parameters

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_xyz(input)  # 2x3x4x5
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 with shape \((*, 3, H, W)\).

Returns

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

Return type

torch.Tensor

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_ycbcr(input)  # 2x3x4x5
rgb_to_yuv(image: 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

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_yuv(input)  # 2x3x4x5
rgba_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Convert an image from RGBA to RGB.

Parameters

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 4, 4, 5)
>>> output = rgba_to_rgb(input) # 2x3x4x5
rgba_to_bgr(image: torch.Tensor) → torch.Tensor[source]

Convert an image from RGBA to BGR.

Parameters

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 4, 4, 5)
>>> output = rgba_to_bgr(input) # 2x3x4x5
bgr_to_grayscale(image: torch.Tensor) → torch.Tensor[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 (torch.Tensor) – BGR image to be converted to grayscale with shape \((*,3,H,W)\).

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> gray = bgr_to_grayscale(input) # 2x1x4x5
bgr_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Convert a BGR image to RGB.

Parameters

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = bgr_to_rgb(input) # 2x3x4x5
bgr_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) → torch.Tensor[source]

Convert an image from BGR to RGBA.

Parameters
  • image (torch.Tensor) – BGR 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)\).

Returns

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

Return type

torch.Tensor

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: torch.Tensor) → torch.Tensor[source]

Convert a HLS image 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 woth shape \((*, 3, H, W)\).

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = hls_to_rgb(input)  # 2x3x4x5
hsv_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Convert an 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 HSV with shape of \((*, 3, H, W)\).

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = hsv_to_rgb(input)  # 2x3x4x5
luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) → torch.Tensor[source]

Converts a Luv image to RGB.

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

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = luv_to_rgb(input)  # 2x3x4x5
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 with shape \((*, 3, H, W)\).

Returns

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

Return type

torch.Tensor

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> output = ycbcr_to_rgb(input)  # 2x3x4x5
yuv_to_rgb(image: 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

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

Returns

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

Return type

torch.Tensor

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = yuv_to_rgb(input)  # 2x3x4x5
xyz_to_rgb(image: torch.Tensor) → torch.Tensor[source]

Converts a XYZ image to RGB.

Parameters

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

Returns

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

Return type

torch.Tensor

Example

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

Modules

class RgbToGrayscale[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[source]

Convert an image from RGB to HSV.

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

Returns

HSV version of the image.

Return type

torch.tensor

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.

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)\)

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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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

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

Examples

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

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

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.

Return type

torch.Tensor

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: Union[float, torch.Tensor])[source]

Convert an 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 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: Union[float, torch.Tensor])[source]

Convert an image from BGR to RGBA.

Add an alpha channel to existing RGB image.

Parameters

alpha_val (float, torch.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 = 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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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

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

Examples

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

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 = 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.

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 = 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.

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 = 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 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.

Return type

torch.Tensor

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.

Return type

torch.Tensor

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

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

Examples

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