YUV#

Functions#

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

Convert an RGB image to YUV.

yuv = rgb_to_yuv(image) converts a (*, 3, H, W) RGB image in \([0, 1]\) to analog YUV. Y is in \([0, 1]\), U in \([-0.436, 0.436]\) and V in \([-0.615, 0.615]\).

_images/rgb_to_yuv.png
Convention:

Channels are Y, U, V at axis -3, with the M/PAL coefficients (see BT.470-5, Table 2, items 2.5 and 2.6). U and V are signed and centred at zero. skimage rgb2yuv uses the same matrix with more digits, and OpenCV’s float COLOR_RGB2YUV adds 0.5 to U and V.

Parameters:

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

Return type:

Tensor

Returns:

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> rgb_to_yuv(input).shape
torch.Size([2, 3, 4, 5])
kornia.color.yuv_to_rgb(image)[source]#

Convert a YUV image to RGB.

rgb = yuv_to_rgb(image) converts a (*, 3, H, W) YUV image, with Y in \([0, 1]\), U in \([-0.436, 0.436]\) and V in \([-0.615, 0.615]\), to RGB. It inverts rgb_to_yuv() exactly, so a round trip is limited only by the precision of the dtype.

Convention:

Channels are Y, U, V at axis -3, as rgb_to_yuv() returns them. The result is not clipped.

Parameters:

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

Return type:

Tensor

Returns:

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

See also

Example

>>> input = torch.rand(2, 3, 4, 5)
>>> yuv_to_rgb(input).shape
torch.Size([2, 3, 4, 5])
kornia.color.rgb_to_yuv420(image)[source]#

Convert an RGB image to YUV 4:2:0 planes.

y, uv = rgb_to_yuv420(image) converts a (*, 3, H, W) RGB image in \([0, 1]\) to the full-resolution Y plane and the U and V planes at half the height and width.

Convention:

Y, U and V are as in rgb_to_yuv(). Each UV value is the mean of a 2x2 block of U or V, and the two planes are stacked as two channels. Height and width must be even; an odd size raises ShapeError.

Parameters:

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

Return type:

tuple[Tensor, Tensor]

Returns:

Tuple of the Y plane with shape (*, 1, H, W) and the UV planes with shape (*, 2, H/2, W/2).

See also

Example

>>> input = torch.rand(2, 3, 4, 6)
>>> y, uv = rgb_to_yuv420(input)
>>> y.shape, uv.shape
(torch.Size([2, 1, 4, 6]), torch.Size([2, 2, 2, 3]))
kornia.color.yuv420_to_rgb(imagey, imageuv)[source]#

Convert YUV 4:2:0 planes to RGB.

rgb = yuv420_to_rgb(imagey, imageuv) converts a full-resolution Y plane and half-resolution UV planes, as rgb_to_yuv420() returns them, to a (*, 3, H, W) RGB image.

Convention:

UV is upsampled by nearest-neighbor replication, then converted as in yuv_to_rgb(). Height and width must be even, and imageuv must be exactly half the size of imagey; otherwise ShapeError is raised.

Parameters:
  • imagey (Tensor) – Y plane with shape (*, 1, H, W).

  • imageuv (Tensor) – UV planes with shape (*, 2, H/2, W/2).

Return type:

Tensor

Returns:

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

See also

Example

>>> inputy = torch.rand(2, 1, 4, 6)
>>> inputuv = torch.rand(2, 2, 2, 3)
>>> yuv420_to_rgb(inputy, inputuv).shape
torch.Size([2, 3, 4, 6])
kornia.color.rgb_to_yuv422(image)[source]#

Convert an RGB image to YUV 4:2:2 planes.

y, uv = rgb_to_yuv422(image) converts a (*, 3, H, W) RGB image in \([0, 1]\) to the full-resolution Y plane and the U and V planes at half the width.

Convention:

Y, U and V are as in rgb_to_yuv(). Each UV value is the mean of two horizontal neighbors of U or V, and the two planes are stacked as two channels. Width must be even, and an odd width raises ShapeError. Height may be odd.

Parameters:

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

Return type:

tuple[Tensor, Tensor]

Returns:

Tuple of the Y plane with shape (*, 1, H, W) and the UV planes with shape (*, 2, H, W/2).

See also

Example

>>> input = torch.rand(2, 3, 4, 6)
>>> y, uv = rgb_to_yuv422(input)
>>> y.shape, uv.shape
(torch.Size([2, 1, 4, 6]), torch.Size([2, 2, 4, 3]))
kornia.color.yuv422_to_rgb(imagey, imageuv)[source]#

Convert YUV 4:2:2 planes to RGB.

rgb = yuv422_to_rgb(imagey, imageuv) converts a full-resolution Y plane and half-width UV planes, as rgb_to_yuv422() returns them, to a (*, 3, H, W) RGB image.

Convention:

UV is upsampled along width by nearest-neighbor replication, then converted as in yuv_to_rgb(). Width must be even and imageuv must be half the width of imagey with the same height; otherwise ShapeError is raised.

Parameters:
  • imagey (Tensor) – Y plane with shape (*, 1, H, W).

  • imageuv (Tensor) – UV planes with shape (*, 2, H, W/2).

Return type:

Tensor

Returns:

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

See also

Example

>>> inputy = torch.rand(2, 1, 4, 6)
>>> inputuv = torch.rand(2, 2, 4, 3)
>>> yuv422_to_rgb(inputy, inputuv).shape
torch.Size([2, 3, 4, 6])

Modules#

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

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

out = RgbToYuv()(image) is rgb_to_yuv().

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

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

See also

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> yuv = RgbToYuv()
>>> yuv(input).shape
torch.Size([2, 3, 4, 5])

References

[1] https://es.wikipedia.org/wiki/YUV#RGB_a_Y’UV

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

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

out = YuvToRgb()(image) is yuv_to_rgb().

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

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

See also

Examples

>>> input = torch.rand(2, 3, 4, 5)
>>> rgb = YuvToRgb()
>>> rgb(input).shape
torch.Size([2, 3, 4, 5])
class kornia.color.RgbToYuv420(*args, **kwargs)[source]#

Convert an image from RGB to YUV 4:2:0 planes (module form).

y, uv = RgbToYuv420()(image) is rgb_to_yuv420().

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

  • output: (*, 1, H, W) and (*, 2, H/2, W/2)

See also

Examples

>>> yuvinput = torch.rand(2, 3, 4, 6)
>>> yuv = RgbToYuv420()
>>> y, uv = yuv(yuvinput)
>>> y.shape, uv.shape
(torch.Size([2, 1, 4, 6]), torch.Size([2, 2, 2, 3]))

References

[1] https://es.wikipedia.org/wiki/YUV#RGB_a_Y’UV

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

Convert YUV 4:2:0 planes to RGB (module form).

rgb = Yuv420ToRgb()(inputy, inputuv) is yuv420_to_rgb().

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

  • inputuv: (*, 2, H/2, W/2)

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

See also

Examples

>>> inputy = torch.rand(2, 1, 4, 6)
>>> inputuv = torch.rand(2, 2, 2, 3)
>>> rgb = Yuv420ToRgb()
>>> rgb(inputy, inputuv).shape
torch.Size([2, 3, 4, 6])
class kornia.color.RgbToYuv422(*args, **kwargs)[source]#

Convert an image from RGB to YUV 4:2:2 planes (module form).

y, uv = RgbToYuv422()(image) is rgb_to_yuv422().

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

  • output: (*, 1, H, W) and (*, 2, H, W/2)

See also

Examples

>>> yuvinput = torch.rand(2, 3, 4, 6)
>>> yuv = RgbToYuv422()
>>> y, uv = yuv(yuvinput)
>>> y.shape, uv.shape
(torch.Size([2, 1, 4, 6]), torch.Size([2, 2, 4, 3]))

References

[1] https://es.wikipedia.org/wiki/YUV#RGB_a_Y’UV

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

Convert YUV 4:2:2 planes to RGB (module form).

rgb = Yuv422ToRgb()(inputy, inputuv) is yuv422_to_rgb().

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

  • inputuv: (*, 2, H, W/2)

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

See also

Examples

>>> inputy = torch.rand(2, 1, 4, 6)
>>> inputuv = torch.rand(2, 2, 4, 3)
>>> rgb = Yuv422ToRgb()
>>> rgb(inputy, inputuv).shape
torch.Size([2, 3, 4, 6])