YUV#
Tip
Learn more: https://en.wikipedia.org/wiki/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]\).
- 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
rgb2yuvuses the same matrix with more digits, and OpenCV’s floatCOLOR_RGB2YUVadds 0.5 to U and V.
- Parameters:
image (
Tensor) – RGB image with shape(*, 3, H, W).- Return type:
- Returns:
YUV image with shape
(*, 3, H, W).
See also
yuv_to_rgb(): the inverse.rgb_to_yuv420(): subsamples U and V by 2 in height and width.rgb_to_yuv422(): subsamples U and V by 2 in width.RgbToYuv: the module form.
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 invertsrgb_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:
- Returns:
RGB image with shape
(*, 3, H, W).
See also
rgb_to_yuv(): the inverse.yuv420_to_rgb(): takes Y and half-resolution UV planes.yuv422_to_rgb(): takes Y and half-width UV planes.YuvToRgb: the module form.
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 raisesShapeError.
- Parameters:
image (
Tensor) – RGB image with shape(*, 3, H, W).- Return type:
- Returns:
Tuple of the Y plane with shape
(*, 1, H, W)and the UV planes with shape(*, 2, H/2, W/2).
See also
yuv420_to_rgb(): the inverse, which restores the chroma resolution by replication.rgb_to_yuv422(): halves the chroma in width only.RgbToYuv420: the module form.
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, asrgb_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, andimageuvmust be exactly half the size ofimagey; otherwiseShapeErroris raised.
- Parameters:
- Return type:
- Returns:
RGB image with shape
(*, 3, H, W).
See also
rgb_to_yuv420(): the inverse, up to the chroma resolution lost.yuv422_to_rgb(): takes half-width UV planes.Yuv420ToRgb: the module form.
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 raisesShapeError. Height may be odd.
- Parameters:
image (
Tensor) – RGB image with shape(*, 3, H, W).- Return type:
- Returns:
Tuple of the Y plane with shape
(*, 1, H, W)and the UV planes with shape(*, 2, H, W/2).
See also
yuv422_to_rgb(): the inverse, which restores the chroma width by replication.rgb_to_yuv420(): halves the chroma in height as well.RgbToYuv422: the module form.
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, asrgb_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 andimageuvmust be half the width ofimageywith the same height; otherwiseShapeErroris raised.
- Parameters:
- Return type:
- Returns:
RGB image with shape
(*, 3, H, W).
See also
rgb_to_yuv422(): the inverse, up to the chroma resolution lost.yuv420_to_rgb(): takes half-resolution UV planes.Yuv422ToRgb: the module form.
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)isrgb_to_yuv().- Convention:
See the convention block of
rgb_to_yuv().
- Shape:
image:
(*, 3, H, W)output:
(*, 3, H, W)
See also
rgb_to_yuv(): the one-call form.YuvToRgb: the inverse.
Examples
>>> input = torch.rand(2, 3, 4, 5) >>> yuv = RgbToYuv() >>> yuv(input).shape torch.Size([2, 3, 4, 5])
References
- class kornia.color.YuvToRgb(*args, **kwargs)[source]#
Convert an image from YUV to RGB (module form).
out = YuvToRgb()(image)isyuv_to_rgb().- Convention:
See the convention block of
yuv_to_rgb().
- Shape:
image:
(*, 3, H, W)output:
(*, 3, H, W)
See also
yuv_to_rgb(): the one-call form.RgbToYuv: the inverse.
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)isrgb_to_yuv420().- Convention:
See the convention block of
rgb_to_yuv420().
- Shape:
image:
(*, 3, H, W)output:
(*, 1, H, W)and(*, 2, H/2, W/2)
See also
rgb_to_yuv420(): the one-call form.Yuv420ToRgb: the inverse.RgbToYuv422: halves the chroma in width only.
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
- class kornia.color.Yuv420ToRgb(*args, **kwargs)[source]#
Convert YUV 4:2:0 planes to RGB (module form).
rgb = Yuv420ToRgb()(inputy, inputuv)isyuv420_to_rgb().- Convention:
See the convention block of
yuv420_to_rgb().
- Shape:
inputy:
(*, 1, H, W)inputuv:
(*, 2, H/2, W/2)output:
(*, 3, H, W)
See also
yuv420_to_rgb(): the one-call form.RgbToYuv420: the inverse, up to the chroma resolution lost.Yuv422ToRgb: takes half-width UV planes.
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)isrgb_to_yuv422().- Convention:
See the convention block of
rgb_to_yuv422().
- Shape:
image:
(*, 3, H, W)output:
(*, 1, H, W)and(*, 2, H, W/2)
See also
rgb_to_yuv422(): the one-call form.Yuv422ToRgb: the inverse.RgbToYuv420: halves the chroma in height as well.
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
- class kornia.color.Yuv422ToRgb(*args, **kwargs)[source]#
Convert YUV 4:2:2 planes to RGB (module form).
rgb = Yuv422ToRgb()(inputy, inputuv)isyuv422_to_rgb().- Convention:
See the convention block of
yuv422_to_rgb().
- Shape:
inputy:
(*, 1, H, W)inputuv:
(*, 2, H, W/2)output:
(*, 3, H, W)
See also
yuv422_to_rgb(): the one-call form.RgbToYuv422: the inverse, up to the chroma resolution lost.Yuv420ToRgb: takes half-resolution UV planes.
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])