Skip to content

mpcompress.utils.transforms

Color space conversion utilities. Adapted from https://github.com/microsoft/DCVC/blob/main/src/utils/transforms.py

rgb2ycbcr

rgb2ycbcr(rgb: Tensor, is_bgr=False)

Convert RGB tensor to YCbCr color space.

Converts RGB values to YCbCr using ITU-R BT.709 standard weights. The output values are clamped to [0, 1] range.

Parameters:

Name Type Description Default
rgb Tensor

RGB tensor with shape (..., 3, H, W) or (..., C, H, W) where the last 3 channels are RGB.

required
is_bgr bool

If True, interpret input as BGR instead of RGB. Defaults to False.

False

Returns:

Name Type Description
ycbcr Tensor

YCbCr tensor with shape (..., 3, H, W), values clamped to [0, 1].

ycbcr2rgb

ycbcr2rgb(ycbcr: Tensor, is_bgr=False, clamp=True)

Convert YCbCr tensor to RGB color space.

Converts YCbCr values to RGB using ITU-R BT.709 standard weights. Optionally clamps output values to [0, 1] range.

Parameters:

Name Type Description Default
ycbcr Tensor

YCbCr tensor with shape (..., 3, H, W) or (..., C, H, W) where the last 3 channels are YCbCr.

required
is_bgr bool

If True, output as BGR instead of RGB. Defaults to False.

False
clamp bool

If True, clamp output values to [0, 1] range. Defaults to True.

True

Returns:

Name Type Description
rgb Tensor

RGB tensor with shape (..., 3, H, W), optionally clamped to [0, 1].

ycbcr420_to_444_np

ycbcr420_to_444_np(y, uv, order=0, separate=False)

Convert YCbCr 4:2:0 format to 4:4:4 format using numpy.

Upsamples the chroma channels (UV) from half resolution to full resolution to match the luma channel (Y) resolution.

Parameters:

Name Type Description Default
y ndarray

Y channel float numpy array with shape (1, H, W).

required
uv ndarray

UV channels float numpy array with shape (2, H/2, W/2).

required
order int

Interpolation order for upsampling. 0 for nearest neighbor (default), 1 for bilinear interpolation.

0
separate bool

If True, return Y and UV separately. If False, return concatenated YCbCr array. Defaults to False.

False

Returns:

Name Type Description
out ndarray or tuple(ndarray, ndarray)

If separate is False, return yuv with shape (3, H, W). If separate is True, return (y, uv) with shapes (1, H, W) and (2, H/2, W/2).

yuv_444_to_420

yuv_444_to_420(yuv)

Convert YUV 4:4:4 format to 4:2:0 format.

Downsamples the chroma channels (UV) from full resolution to half resolution using average pooling, while keeping the luma channel (Y) at full resolution.

Parameters:

Name Type Description Default
yuv Tensor

YUV tensor with shape (B, 3, H, W) where channels are (Y, Cb, Cr).

required

Returns:

Name Type Description
y Tensor

Y channel with shape (B, 1, H, W).

uv Tensor

Downsampled UV channels with shape (B, 2, H/2, W/2).