Implementation:Kornia Kornia Ycbcr Conversion
| Knowledge Sources | |
|---|---|
| Domains | Vision, Color_Processing |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This module provides differentiable conversion between RGB and YCbCr color spaces, as well as extraction of the luma (Y) channel.
Description
ycbcr.py is a module in the Kornia library's color subpackage implementing bidirectional RGB-to-YCbCr color space conversion. The conversion uses the ITU-R BT.601 luma coefficients (Y = 0.299*R + 0.587*G + 0.114*B) and derives Cb and Cr with a 0.5 offset to keep values positive. The module provides three functional APIs:
- rgb_to_ycbcr: Converts RGB to YCbCr (3-channel output).
- rgb_to_y: Extracts only the luma (Y) channel (1-channel output).
- ycbcr_to_rgb: Converts YCbCr back to RGB with output clamped to [0, 1].
Each conversion function (except rgb_to_y) has a corresponding nn.Module wrapper (RgbToYcbcr and YcbcrToRgb).
Usage
Import this module when you need to separate luma from chroma for JPEG-style processing, super-resolution (operating on the Y channel), or when working with video/image compression pipelines that use YCbCr.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/color/ycbcr.py
- Lines: 1-177
Signature
def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor: ...
def rgb_to_y(image: torch.Tensor) -> torch.Tensor: ...
def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor: ...
class RgbToYcbcr(nn.Module):
def forward(self, image: torch.Tensor) -> torch.Tensor: ...
class YcbcrToRgb(nn.Module):
def forward(self, image: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.color import rgb_to_ycbcr, ycbcr_to_rgb, rgb_to_y
from kornia.color import RgbToYcbcr, YcbcrToRgb
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| image (rgb_to_ycbcr) | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). Values in range (0, 1). |
| image (rgb_to_y) | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). Values in range (0, 1). |
| image (ycbcr_to_rgb) | torch.Tensor | Yes | YCbCr image with shape (*, 3, H, W). Values in range (0, 1). |
Outputs
| Name | Type | Description |
|---|---|---|
| rgb_to_ycbcr return | torch.Tensor | YCbCr image with shape (*, 3, H, W). Y (luma), Cb (blue-difference chroma), Cr (red-difference chroma). |
| rgb_to_y return | torch.Tensor | Luma channel with shape (*, 1, H, W). |
| ycbcr_to_rgb return | torch.Tensor | RGB image with shape (*, 3, H, W), clamped to [0, 1]. |
Usage Examples
Basic Usage
import torch
from kornia.color import rgb_to_ycbcr, ycbcr_to_rgb, rgb_to_y
# Convert RGB to YCbCr
rgb = torch.rand(1, 3, 128, 128)
ycbcr = rgb_to_ycbcr(rgb)
print(ycbcr.shape) # torch.Size([1, 3, 128, 128])
# Extract only the luma (Y) channel
y_channel = rgb_to_y(rgb)
print(y_channel.shape) # torch.Size([1, 1, 128, 128])
# Convert YCbCr back to RGB
rgb_back = ycbcr_to_rgb(ycbcr)
print(rgb_back.shape) # torch.Size([1, 3, 128, 128])
# Super-resolution: operate on Y channel only
from kornia.color import RgbToYcbcr, YcbcrToRgb
to_ycbcr = RgbToYcbcr()
to_rgb = YcbcrToRgb()
ycbcr_out = to_ycbcr(rgb)
# Apply super-resolution model to ycbcr_out[:, 0:1, :, :]
# Then reconstruct: rgb_sr = to_rgb(ycbcr_sr)