Implementation:Kornia Kornia Yuv Conversion
| Knowledge Sources | |
|---|---|
| Domains | Vision, Color_Processing |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This module provides differentiable conversions between RGB and YUV color spaces, including full YUV 4:4:4, chroma-subsampled YUV 4:2:0, and YUV 4:2:2 formats.
Description
yuv.py is a module in the Kornia library's color subpackage implementing bidirectional RGB-to-YUV color space conversions. The YUV model follows M/PAL values as defined in ITU-R BT.470-5. Three resolution variants are provided:
- YUV 4:4:4: Full-resolution conversion via rgb_to_yuv / yuv_to_rgb using a 3x3 linear transformation matrix.
- YUV 4:2:0: Chroma-subsampled conversion via rgb_to_yuv420 / yuv420_to_rgb where UV planes are half-resolution in both dimensions.
- YUV 4:2:2: Chroma-subsampled conversion via rgb_to_yuv422 / yuv422_to_rgb where UV planes are half-resolution horizontally.
The output ranges are Y in (0, 1), U in (-0.436, 0.436), and V in (-0.615, 0.615). Each function has a corresponding nn.Module wrapper class.
Usage
Import this module when you need to convert between RGB and YUV color spaces, for example when working with video compression, broadcast standards, or luma-chroma separation in image processing pipelines.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/color/yuv.py
- Lines: 1-470
Signature
def rgb_to_yuv(image: torch.Tensor) -> torch.Tensor: ...
def rgb_to_yuv420(image: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: ...
def rgb_to_yuv422(image: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: ...
def yuv_to_rgb(image: torch.Tensor) -> torch.Tensor: ...
def yuv420_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor: ...
def yuv422_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor: ...
class RgbToYuv(nn.Module): ...
class RgbToYuv420(nn.Module): ...
class RgbToYuv422(nn.Module): ...
class YuvToRgb(nn.Module): ...
class Yuv420ToRgb(nn.Module): ...
class Yuv422ToRgb(nn.Module): ...
Import
from kornia.color import rgb_to_yuv, yuv_to_rgb
from kornia.color import rgb_to_yuv420, yuv420_to_rgb
from kornia.color import rgb_to_yuv422, yuv422_to_rgb
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| image (rgb_to_yuv) | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). Values in range (0, 1). |
| image (rgb_to_yuv420) | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). H and W must be evenly divisible by 2. |
| image (rgb_to_yuv422) | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). W must be evenly divisible by 2. |
| image (yuv_to_rgb) | torch.Tensor | Yes | YUV image with shape (*, 3, H, W). Y in (0,1), U in (-0.436, 0.436), V in (-0.615, 0.615). |
| imagey (yuv420_to_rgb) | torch.Tensor | Yes | Y plane with shape (*, 1, H, W). H and W must be evenly divisible by 2. |
| imageuv (yuv420_to_rgb) | torch.Tensor | Yes | UV planes with shape (*, 2, H/2, W/2). |
| imagey (yuv422_to_rgb) | torch.Tensor | Yes | Y plane with shape (*, 1, H, W). |
| imageuv (yuv422_to_rgb) | torch.Tensor | Yes | UV planes with shape (*, 2, H, W/2). |
Outputs
| Name | Type | Description |
|---|---|---|
| rgb_to_yuv return | torch.Tensor | YUV image with shape (*, 3, H, W). |
| rgb_to_yuv420 return | tuple[torch.Tensor, torch.Tensor] | Y plane (*, 1, H, W) and UV planes (*, 2, H/2, W/2). |
| rgb_to_yuv422 return | tuple[torch.Tensor, torch.Tensor] | Y plane (*, 1, H, W) and UV planes (*, 2, H, W/2). |
| yuv_to_rgb return | torch.Tensor | RGB image with shape (*, 3, H, W). |
| yuv420_to_rgb return | torch.Tensor | RGB image with shape (*, 3, H, W). |
| yuv422_to_rgb return | torch.Tensor | RGB image with shape (*, 3, H, W). |
Usage Examples
Basic Usage
import torch
from kornia.color import rgb_to_yuv, yuv_to_rgb, rgb_to_yuv420, yuv420_to_rgb
# Full YUV 4:4:4 conversion
rgb = torch.rand(2, 3, 64, 64)
yuv = rgb_to_yuv(rgb)
print(yuv.shape) # torch.Size([2, 3, 64, 64])
# Round-trip conversion
rgb_back = yuv_to_rgb(yuv)
print(rgb_back.shape) # torch.Size([2, 3, 64, 64])
# YUV 4:2:0 with chroma subsampling
y_plane, uv_planes = rgb_to_yuv420(rgb)
print(y_plane.shape) # torch.Size([2, 1, 64, 64])
print(uv_planes.shape) # torch.Size([2, 2, 32, 32])
# Reconstruct RGB from YUV 4:2:0
rgb_from_420 = yuv420_to_rgb(y_plane, uv_planes)
print(rgb_from_420.shape) # torch.Size([2, 3, 64, 64])
# Using nn.Module wrapper
from kornia.color import RgbToYuv
converter = RgbToYuv()
yuv_out = converter(rgb)