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.
Tip
Learn more: https://en.wikipedia.org/wiki/Grayscale
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).
- 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_weightsexpect 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 ascv2.cvtColor(img, cv2.COLOR_RGB2GRAY)does.- Parameters:
- Return type:
- Returns:
Grayscale image with shape
(*, 1, H, W).
Note
See a working example here.
See also
bgr_to_grayscale(): takes a BGR image, with the default weights only.rgb_to_y(): the Y channel of YCbCr, which has the same default weights and norgb_weights.grayscale_to_rgb(): repeats the channel back into three.RgbToGrayscale: the module form, withrgb_weightsstored.
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 ofrgb_to_grayscale().- Parameters:
image (
Tensor) – BGR image with shape(*, 3, H, W).- Return type:
- Returns:
Grayscale image with shape
(*, 1, H, W).
See also
rgb_to_grayscale(): takes an RGB image and acceptsrgb_weights.BgrToGrayscale: the module form.
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.
- 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:
- Returns:
RGB image with shape
(*, 3, H, W).
See also
rgb_to_grayscale(): the other direction, which weights the channels and cannot be undone.GrayscaleToRgb: the module form.
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, andout = conv(image)isrgb_to_grayscale()with them. The defaultNonekeeps the default weights of the function.- Convention:
See the convention block of
rgb_to_grayscale().
- Parameters:
rgb_weights (
Optional[Tensor], optional) – tensor of the R, G and B weights with shape(3,), orNone. Default:None
- Shape:
image:
(*, 3, H, W)output:
(*, 1, H, W)
See also
rgb_to_grayscale(): the one-call form.BgrToGrayscale: takes a BGR image, with the default weights only.GrayscaleToRgb: the other direction.
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)isbgr_to_grayscale().- Convention:
See the convention block of
rgb_to_grayscale().
- Shape:
image:
(*, 3, H, W)output:
(*, 1, H, W)
See also
bgr_to_grayscale(): the one-call form.RgbToGrayscale: takes an RGB image and acceptsrgb_weights.
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)isgrayscale_to_rgb().- Convention:
See the convention block of
grayscale_to_rgb().
- Shape:
image:
(*, 1, H, W)output:
(*, 3, H, W)
See also
grayscale_to_rgb(): the one-call form.RgbToGrayscale: the other direction.
Example
>>> input = torch.rand(2, 1, 4, 5) >>> rgb = GrayscaleToRgb() >>> output = rgb(input) # 2x3x4x5