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.

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_val fills the alpha channel; an int raises TypeError.

  • A tensor alpha_val must have shape (*, 1, H, W) equal to the image’s leading and spatial axes; it is not broadcast.

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

  • alpha_val (Union[float, Tensor]) – alpha, one float or a tensor.

Return type:

Tensor

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) returns image_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_color are unit range.

  • background_color is 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 raises ValueError.

Parameters:
Return type:

Tensor

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: call rgba_to_rgb() with background_color and then rgb_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:

Tensor

Returns:

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

See also

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, and out = conv(image) is rgb_to_rgba() with it.

Convention:
Parameters:

alpha_val (Union[float, Tensor]) – alpha, as in rgb_to_rgba(). A plain tensor is kept as a non-persistent buffer, so .to(device) moves it and state_dict omits it.

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

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

Note

The current functionality is NOT supported by Torchscript.

See also

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) is rgba_to_rgb() over its default white background.

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

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

See also

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) is rgba_to_bgr().

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

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

See also

Example

>>> input = torch.rand(2, 4, 4, 5)
>>> rgba = RgbaToBgr()
>>> output = rgba(input)  # 2x3x4x5