RGB#

rgb_to_bgr() reverses the channels. rgb_to_rgb255() and rgb255_to_rgb() scale between \([0, 1]\) and \([0, 255]\). normals_to_rgb255() and rgb255_to_normals() map surface normals between \([-1, 1]\) and \([0, 255]\) colours.

Functions#

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

Convert an RGB image to BGR.

bgr = rgb_to_bgr(image) reorders the channels of a (*, 3, H, W) RGB image to BGR, the order OpenCV uses.

_images/rgb_to_bgr.png
Convention:

Reverses the three channels at axis -3; it is its own inverse.

Parameters:

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

Return type:

Tensor

Returns:

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_bgr(input) # 2x3x4x5
kornia.color.rgb_to_rgb255(image)[source]#

Scale an RGB image from [0, 1] to [0, 255].

out = rgb_to_rgb255(image) multiplies a (*, 3, H, W) unit-range RGB image by 255. The result is a float image, not uint8.

Convention:

Values are clamped to \([0, 255]\) and not rounded.

Parameters:

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

Return type:

Tensor

Returns:

RGB image in \([0, 255]\) with shape (*, 3, H, W).

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_rgb255(input) # 2x3x4x5
kornia.color.rgb255_to_rgb(image)[source]#

Scale an RGB image from [0, 255] to [0, 1].

out = rgb255_to_rgb(image) divides a (*, 3, H, W) image in \([0, 255]\) by 255.

Convention:

Values outside \([0, 255]\) are not clipped.

Parameters:

image (Tensor) – RGB image in \([0, 255]\) with shape (*, 3, H, W).

Return type:

Tensor

Returns:

RGB image in \([0, 1]\) with shape (*, 3, H, W).

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb255_to_rgb(input) # 2x3x4x5
kornia.color.rgb255_to_normals(image)[source]#

Map an RGB image in [0, 255] to unit surface normals.

normals = rgb255_to_normals(image) maps each channel c of a (*, 3, H, W) image to 2 * c / 255 - 1 and scales every pixel’s three components to unit length.

Convention:
  • A pixel that maps to the zero vector, such as mid grey (127.5, 127.5, 127.5), stays zero.

  • Half inputs are computed in float32 and rounded to the input dtype once at return.

Parameters:

image (Tensor) – RGB image in \([0, 255]\) with shape (*, 3, H, W).

Return type:

Tensor

Returns:

Surface normals with shape (*, 3, H, W).

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb255_to_normals(input) # 2x3x4x5
kornia.color.normals_to_rgb255(image)[source]#

Map surface normals in [-1, 1] to RGB in [0, 255].

rgb = normals_to_rgb255(image) maps each component n of a (*, 3, H, W) normal map to 255 * (n + 1) / 2, so -1, 0 and 1 become 0, 127.5 and 255.

Convention:

Components outside \([-1, 1]\) are clamped; the vectors are not normalized.

Parameters:

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

Return type:

Tensor

Returns:

RGB image in \([0, 255]\) with shape (*, 3, H, W).

See also

Example

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

Modules#

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

Convert an image from RGB to BGR (module form).

out = RgbToBgr()(image) is rgb_to_bgr().

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

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> bgr = RgbToBgr()
>>> output = bgr(input)  # 2x3x4x5
class kornia.color.RgbToRgb255(*args, **kwargs)[source]#

Scale an RGB image from [0, 1] to [0, 255] (module form).

out = RgbToRgb255()(image) is rgb_to_rgb255().

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

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = RgbToRgb255()
>>> output = rgb(input)  # 2x3x4x5
class kornia.color.Rgb255ToRgb(*args, **kwargs)[source]#

Scale an RGB image from [0, 255] to [0, 1] (module form).

out = Rgb255ToRgb()(image) is rgb255_to_rgb().

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

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = Rgb255ToRgb()
>>> output = rgb(input)  # 2x3x4x5
class kornia.color.Rgb255ToNormals(*args, **kwargs)[source]#

Map an RGB image in [0, 255] to unit surface normals (module form).

out = Rgb255ToNormals()(image) is rgb255_to_normals().

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

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> normals = Rgb255ToNormals()
>>> output = normals(input)  # 2x3x4x5
class kornia.color.NormalsToRgb255(*args, **kwargs)[source]#

Map surface normals in [-1, 1] to RGB in [0, 255] (module form).

out = NormalsToRgb255()(image) is normals_to_rgb255().

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

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

See also

Example

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