RGBA#
rgb_to_rgba() appends an alpha channel. rgba_to_rgb() composites the alpha over a background, white by
default; rgba_to_bgr() composites over white only.
Tip
Learn more: https://en.wikipedia.org/wiki/RGBA_color_model
Functions#
- kornia.color.rgb_to_rgba(image, alpha_val)[source]#
Convert an RGB image to RGBA by appending an alpha channel.
rgba = rgb_to_rgba(image, alpha_val)returns a(*, 4, H, W)image whose first three channels are the RGB image and whose fourth is the alpha value.- Convention:
A float
alpha_valfills the alpha channel; anintraisesTypeError.A tensor
alpha_valmust have shape(*, 1, H, W)equal to the image’s leading and spatial axes; it is not broadcast.
- Parameters:
- Return type:
- Returns:
RGBA image with shape
(*, 4, H, W).
Note
The current functionality is NOT supported by Torchscript.
See also
rgba_to_rgb(): the other direction, which composites the alpha over a background.bgr_to_rgba(): takes a BGR image; the output is RGBA either way.RgbToRgba: the module form, with the alpha stored.
Example
>>> input = torch.rand(2, 3, 4, 5) >>> output = rgb_to_rgba(input, 1.) # 2x4x4x5
- kornia.color.rgba_to_rgb(image, background_color=None)[source]#
Convert an RGBA image to RGB by compositing it over a background.
rgb = rgba_to_rgb(image, background_color)returnsimage_rgb * alpha + background * (1 - alpha)for a(*, 4, H, W)image, over white by default.- Convention:
Alpha is straight, not premultiplied, and in \([0, 1]\); color channels and
background_colorare unit range.background_coloris a tuple or list of three floats, a(3,)tensor used like that tuple, or a tensor with three channels at axis -3 that broadcasts against the image, such as(3, 1, 1),(*, 3, 1, 1)or(*, 3, H, W). Any other 1D or 2D tensor raisesValueError.
- Parameters:
- Return type:
- Returns:
RGB image with shape
(*, 3, H, W).
See also
rgb_to_rgba(): the other direction, which appends an alpha.rgba_to_bgr(): composites over white only and returns BGR.RgbaToRgb: the module form, which composites over white.
Example
>>> rgba_image = torch.rand(2, 4, 32, 32) >>> # Test with default white background >>> rgb_image_default = rgba_to_rgb(rgba_image) # 2x3x32x32 >>> # Test with a custom background color >>> rgb_image_custom = rgba_to_rgb(rgba_image, background_color=(0.0, 0.0, 1.0)) # 2x3x32x32
- kornia.color.rgba_to_bgr(image)[source]#
Convert an RGBA image to BGR by compositing it over white.
bgr = rgba_to_bgr(image)composites a(*, 4, H, W)image over white and returns it in BGR order. It takes no background: callrgba_to_rgb()withbackground_colorand thenrgb_to_bgr()for another one.- Convention:
Composites as
rgba_to_rgb()does, so color and alpha are unit range and alpha is straight.
- Parameters:
image (
Tensor) – RGBA image with shape(*, 4, H, W).- Return type:
- Returns:
BGR image with shape
(*, 3, H, W).
See also
bgr_to_rgba(): the other direction, which appends an alpha and returns RGBA.RgbaToBgr: the module form.
Example
>>> input = torch.rand(2, 4, 4, 5) >>> output = rgba_to_bgr(input) # 2x3x4x5
Modules#
- class kornia.color.RgbToRgba(alpha_val)[source]#
Convert an image from RGB to RGBA (module form).
conv = RgbToRgba(alpha_val)stores the alpha, andout = conv(image)isrgb_to_rgba()with it.- Convention:
See the convention block of
rgb_to_rgba().
- Parameters:
alpha_val (
Union[float,Tensor]) – alpha, as inrgb_to_rgba(). A plain tensor is kept as a non-persistent buffer, so.to(device)moves it andstate_dictomits it.
- Shape:
image:
(*, 3, H, W)output:
(*, 4, H, W)
Note
The current functionality is NOT supported by Torchscript.
See also
rgb_to_rgba(): the one-call form.BgrToRgba: takes a BGR image.
Example
>>> input = torch.rand(2, 3, 4, 5) >>> rgba = RgbToRgba(1.) >>> output = rgba(input) # 2x4x4x5
- class kornia.color.RgbaToRgb(*args, **kwargs)[source]#
Convert an image from RGBA to RGB (module form).
out = RgbaToRgb()(image)isrgba_to_rgb()over its default white background.- Convention:
See the convention block of
rgba_to_rgb().
- Shape:
image:
(*, 4, H, W)output:
(*, 3, H, W)
See also
rgba_to_rgb(): the one-call form, which also takes abackground_color.RgbaToBgr: returns BGR.
Example
>>> input = torch.rand(2, 4, 4, 5) >>> rgba = RgbaToRgb() >>> output = rgba(input) # 2x3x4x5
- class kornia.color.RgbaToBgr(*args, **kwargs)[source]#
Convert an image from RGBA to BGR (module form).
out = RgbaToBgr()(image)isrgba_to_bgr().- Convention:
See the convention block of
rgba_to_bgr().
- Shape:
image:
(*, 4, H, W)output:
(*, 3, H, W)
See also
rgba_to_bgr(): the one-call form.RgbaToRgb: returns RGB.
Example
>>> input = torch.rand(2, 4, 4, 5) >>> rgba = RgbaToBgr() >>> output = rgba(input) # 2x3x4x5