Grayscale#

Grayscale images have one channel, (*, 1, H, W). rgb_to_grayscale() and bgr_to_grayscale() use ITU-R BT.601 luma weights on the values as given, without removing the sRGB curve; grayscale_to_rgb() repeats the channel.

Functions#

kornia.color.rgb_to_grayscale(image, rgb_weights=None)[source]#

Convert an RGB image to grayscale.

gray = rgb_to_grayscale(image) returns the weighted sum of the R, G and B channels of a (*, 3, H, W) image as one channel, (*, 1, H, W).

_images/rgb_to_grayscale.png
Convention:

Default weights are ITU-R BT.601 luma, (0.299, 0.587, 0.114) in RGB order, applied to the values as given: the sRGB curve is not removed first.

Warning

Explicit rgb_weights expect a floating image: they are cast to the image’s dtype, so on an integer image float weights truncate to zero and integer weights wrap. With the default weights a uint8 image is converted exactly, bit for bit as cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) does.

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

  • rgb_weights (Optional[Tensor], optional) – tensor of the R, G and B weights with shape (3,). Non-negative weights that sum to 1 keep the output in the range of the input. Default: None

Return type:

Tensor

Returns:

Grayscale image with shape (*, 1, H, W).

Note

See a working example here.

See also

Example

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

References

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

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

Convert a BGR image to grayscale.

gray = bgr_to_grayscale(image) converts a (*, 3, H, W) BGR image to (*, 1, H, W) with the default weights of rgb_to_grayscale().

Parameters:

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

Return type:

Tensor

Returns:

Grayscale image with shape (*, 1, H, W).

See also

Example

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

References

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

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

Convert a grayscale image to RGB.

rgb = grayscale_to_rgb(image) turns a (*, 1, H, W) grayscale image into a three-channel one.

_images/grayscale_to_rgb.png
Convention:

The singleton channel at axis -3 is repeated into R, G and B.

Parameters:

image (Tensor) – grayscale image with shape (*, 1, H, W).

Return type:

Tensor

Returns:

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

See also

Example

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

References

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

Modules#

class kornia.color.RgbToGrayscale(rgb_weights=None)[source]#

Convert an RGB image to grayscale with stored weights (module form).

conv = RgbToGrayscale(rgb_weights) stores the weights, and out = conv(image) is rgb_to_grayscale() with them. The default None keeps the default weights of the function.

Convention:
Parameters:

rgb_weights (Optional[Tensor], optional) – tensor of the R, G and B weights with shape (3,), or None. Default: None

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

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

See also

Example

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

Convert a BGR image to grayscale (module form).

out = BgrToGrayscale()(image) is bgr_to_grayscale().

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

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

See also

Example

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

Convert a grayscale image to RGB (module form).

out = GrayscaleToRgb()(image) is grayscale_to_rgb().

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

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

See also

Example

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