YUV#

Note

Includes the chroma-subsampled 4:2:0 and 4:2:2 layouts.

Functions#

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

Convert an RGB image to YUV.

_images/rgb_to_yuv.png

The image data is assumed to be in the range of \((0, 1)\). The range of the output is of \((0, 1)\) to luma and the ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

The YUV model adopted here follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Convention:

Uses channels Y, U, V at axis -3. RGB inputs are unit-range; U and V are signed chroma components centered at zero.

Parameters:

image (Tensor) – RGB Image to be converted to YUV with shape \((*, 3, H, W)\).

Return type:

Tensor

Returns:

YUV version of the image with shape \((*, 3, H, W)\).

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 an YUV image to RGB.

The image data is assumed to be in the range of \((0, 1)\) for luma (Y). The ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

This is the exact inverse of rgb_to_yuv(): its kernel is that function’s kernel inverted rather than a separately rounded copy of the published inverse relations, so an RGB -> YUV -> RGB round trip is limited only by the precision of the input dtype.

Convention:

Expects Y, U, V at axis -3 in the unit-range/signed-chroma representation from rgb_to_yuv.

Parameters:

image (Tensor) – YUV Image to be converted to RGB with shape \((*, 3, H, W)\).

Return type:

Tensor

Returns:

RGB version of the image with shape \((*, 3, H, W)\).

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 420 (subsampled).

Input need to be padded to be evenly divisible by 2 horizontal and vertical.

The image data is assumed to be in the range of \((0, 1)\). The range of the output is of \((0, 1)\) to luma and the ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

The YUV model adopted here follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Convention:

Returns (Y, UV): Y has shape \((*, 1, H, W)\) and UV holds the U and V planes as two channels, \((*, 2, H/2, W/2)\). Height and width must be even.

Parameters:

image (Tensor) – RGB Image to be converted to YUV with shape \((*, 3, H, W)\).

Return type:

tuple[Tensor, Tensor]

Returns:

A torch.Tensor containing the Y plane with shape \((*, 1, H, W)\) A torch.Tensor containing the UV planes with shape \((*, 2, H/2, W/2)\)

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 an YUV420 image to RGB.

Input need to be padded to be evenly divisible by 2 horizontal and vertical.

The image data is assumed to be in the range of \((0, 1)\) for luma (Y). The ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Convention:

Expects the Y and two-plane UV tensors returned by rgb_to_yuv420. UV is upsampled by nearest-neighbor replication.

Parameters:
  • imagey (Tensor) – Y (luma) Image plane to be converted to RGB with shape \((*, 1, H, W)\).

  • imageuv (Tensor) – UV (chroma) Image planes to be converted to RGB with shape \((*, 2, H/2, W/2)\).

Return type:

Tensor

Returns:

RGB version of the image with shape \((*, 3, H, W)\).

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 422 (subsampled).

Input width must be evenly divisible by 2; height may be odd.

The image data is assumed to be in the range of \((0, 1)\). The range of the output is of \((0, 1)\) to luma and the ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

The YUV model adopted here follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Convention:

Returns (Y, UV): Y has shape \((*, 1, H, W)\) and UV holds the U and V planes as two channels, \((*, 2, H, W/2)\). Width must be even.

Parameters:

image (Tensor) – RGB Image to be converted to YUV with shape \((*, 3, H, W)\).

Return type:

tuple[Tensor, Tensor]

Returns:

A torch.Tensor containing the Y plane with shape \((*, 1, H, W)\) A torch.Tensor containing the UV planes with shape \((*, 2, H, W/2)\)

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 an YUV422 image to RGB.

Input width must be evenly divisible by 2; height may be odd.

The image data is assumed to be in the range of \((0, 1)\) for luma (Y). The ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Convention:

Expects the Y and two-plane UV tensors returned by rgb_to_yuv422. UV is upsampled along width by nearest-neighbor replication.

Parameters:
  • imagey (Tensor) – Y (luma) Image plane to be converted to RGB with shape \((*, 1, H, W)\).

  • imageuv (Tensor) – UV (chroma) Image planes to be converted to RGB with shape \((*, 2, H, W/2)\).

Return type:

Tensor

Returns:

RGB version of the image with shape \((*, 3, H, W)\).

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.

See the Convention block on rgb_to_yuv().

The image data is assumed to be in the range of \((0, 1)\).

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Returns:

YUV version of the image.

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

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

Examples

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

[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.

See the Convention block on yuv_to_rgb().

The image data is assumed to be in the range of \((0, 1)\) for luma (Y). The ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

This is the exact inverse of RgbToYuv, so an RGB -> YUV -> RGB round trip is limited only by the precision of the input dtype.

Returns:

RGB version of the image.

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

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

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 YUV420.

See the Convention block on rgb_to_yuv420().

Width and Height must be evenly divisible by 2.

The image data is assumed to be in the range of \((0, 1)\).

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Returns:

YUV420 version of the image.

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

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

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]))
Reference::

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

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

Convert an image from YUV to RGB.

See the Convention block on yuv420_to_rgb().

Width and Height must be evenly divisible by 2.

The image data is assumed to be in the range of \((0, 1)\) for luma (Y). The ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Returns:

RGB version of the image.

Shape:
  • imagey: \((*, 1, H, W)\)

  • imageuv: \((*, 2, H/2, W/2)\)

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

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 YUV422.

See the Convention block on rgb_to_yuv422().

Width must be evenly divisible by 2; height may be odd.

The image data is assumed to be in the range of \((0, 1)\).

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Returns:

YUV422 version of the image.

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

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

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]))
Reference::

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

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

Convert an image from YUV to RGB.

See the Convention block on yuv422_to_rgb().

Width must be evenly divisible by 2; height may be odd.

The image data is assumed to be in the range of \((0, 1)\) for luma (Y). The ranges of U and V are \((-0.436, 0.436)\) and \((-0.615, 0.615)\), respectively.

YUV formula follows M/PAL values (see BT.470-5, Table 2, items 2.5 and 2.6).

Returns:

RGB version of the image.

Shape:
  • imagey: \((*, 1, H, W)\)

  • imageuv: \((*, 2, H, W/2)\)

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

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])