Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

adding `rgb_to_y` (#1734)

nitaifingerhut3964ad54de62
diff --git a/kornia/color/__init__.py b/kornia/color/__init__.py
index 7f4ecafac..a6445bb2c 100644
--- a/kornia/color/__init__.py
+++ b/kornia/color/__init__.py
@@ -23,7 +23,7 @@ from .rgb import (
rgba_to_rgb,
)
from .xyz import RgbToXyz, XyzToRgb, rgb_to_xyz, xyz_to_rgb
-from .ycbcr import RgbToYcbcr, YcbcrToRgb, rgb_to_ycbcr, ycbcr_to_rgb
+from .ycbcr import RgbToYcbcr, YcbcrToRgb, rgb_to_y, rgb_to_ycbcr, ycbcr_to_rgb
from .yuv import (
RgbToYuv,
RgbToYuv420,
diff --git a/kornia/color/ycbcr.py b/kornia/color/ycbcr.py
index 19f8f0d1f..61f4ee063 100644
--- a/kornia/color/ycbcr.py
+++ b/kornia/color/ycbcr.py
@@ -1,8 +1,14 @@
import torch
import torch.nn as nn
+from torch import Tensor
-def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:
+def _rgb_to_y(r: Tensor, g: Tensor, b: Tensor) -> Tensor:
+ y: Tensor = 0.299 * r + 0.587 * g + 0.114 * b
+ return y
+
+
+def rgb_to_ycbcr(image: Tensor) -> Tensor:
r"""Convert an RGB image to YCbCr.
.. image:: _static/img/rgb_to_ycbcr.png
@@ -17,24 +23,51 @@ def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:
>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_ycbcr(input) # 2x3x4x5
"""
- if not isinstance(image, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
+ if not isinstance(image, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(image)}")
if len(image.shape) < 3 or image.shape[-3] != 3:
raise ValueError(f"Input size must have a shape of (*, 3, H, W). Got {image.shape}")
- r: torch.Tensor = image[..., 0, :, :]
- g: torch.Tensor = image[..., 1, :, :]
- b: torch.Tensor = image[..., 2, :, :]
+ r: Tensor = image[..., 0, :, :]
+ g: Tensor = image[..., 1, :, :]
+ b: Tensor = image[..., 2, :, :]
delta: float = 0.5
- y: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
- cb: torch.Tensor = (b - y) * 0.564 + delta
- cr: torch.Tensor = (r - y) * 0.713 + delta
+ y: Tensor = _rgb_to_y(r, g, b)
+ cb: Tensor = (b - y) * 0.564 + delta
+ cr: Tensor = (r - y) * 0.713 + delta
return torch.stack([y, cb, cr], -3)
-def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor:
+def rgb_to_y(image: Tensor) -> Tensor:
+ r"""Convert an RGB image to Y.
+
+ Args:
+ image: RGB Image to be converted to Y with shape :math:`(*, 3, H, W)`.
+
+ Returns:
+ Y version of the image with shape :math:`(*, 1, H, W)`.
+
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_y(input) # 2x1x4x5
+ """
+ if not isinstance(image, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(image)}")
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError(f"Input size must have a shape of (*, 3, H, W). Got {image.shape}")
+
+ r: Tensor = image[..., 0:1, :, :]
+ g: Tensor = image[..., 1:2, :, :]
+ b: Tensor = image[..., 2:3, :, :]
+
+ y: Tensor = _rgb_to_y(r, g, b)
+ return y
+
+
+def ycbcr_to_rgb(image: Tensor) -> Tensor:
r"""Convert an YCbCr image to RGB.
The image data is assumed to be in the range of (0, 1).
@@ -49,23 +82,23 @@ def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor:
>>> input = torch.rand(2, 3, 4, 5)
>>> output = ycbcr_to_rgb(input) # 2x3x4x5
"""
- if not isinstance(image, torch.Tensor):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
+ if not isinstance(image, Tensor):
+ raise TypeError(f"Input type is not a Tensor. Got {type(image)}")
if len(image.shape) < 3 or image.shape[-3] != 3:
raise ValueError(f"Input size must have a shape of (*, 3, H, W). Got {image.shape}")
- y: torch.Tensor = image[..., 0, :, :]
- cb: torch.Tensor = image[..., 1, :, :]
- cr: torch.Tensor = image[..., 2, :, :]
+ y: Tensor = image[..., 0, :, :]
+ cb: Tensor = image[..., 1, :, :]
+ cr: Tensor = image[..., 2, :, :]
delta: float = 0.5
- cb_shifted: torch.Tensor = cb - delta
- cr_shifted: torch.Tensor = cr - delta
+ cb_shifted: Tensor = cb - delta
+ cr_shifted: Tensor = cr - delta
- r: torch.Tensor = y + 1.403 * cr_shifted
- g: torch.Tensor = y - 0.714 * cr_shifted - 0.344 * cb_shifted
- b: torch.Tensor = y + 1.773 * cb_shifted
+ r: Tensor = y + 1.403 * cr_shifted
+ g: Tensor = y - 0.714 * cr_shifted - 0.344 * cb_shifted
+ b: Tensor = y + 1.773 * cb_shifted
return torch.stack([r, g, b], -3)
@@ -87,7 +120,7 @@ class RgbToYcbcr(nn.Module):
>>> output = ycbcr(input) # 2x3x4x5
"""
- def forward(self, image: torch.Tensor) -> torch.Tensor:
+ def forward(self, image: Tensor) -> Tensor:
return rgb_to_ycbcr(image)
@@ -109,5 +142,5 @@ class YcbcrToRgb(nn.Module):
>>> output = rgb(input) # 2x3x4x5
"""
- def forward(self, image: torch.Tensor) -> torch.Tensor:
+ def forward(self, image: Tensor) -> Tensor:
return ycbcr_to_rgb(image)
diff --git a/test/color/test_ycbcr.py b/test/color/test_ycbcr.py
index 06ca5b4b2..f0871a991 100644
--- a/test/color/test_ycbcr.py
+++ b/test/color/test_ycbcr.py
@@ -17,6 +17,13 @@ class TestRgbToYcbcr(BaseTester):
img = torch.ones(shape, device=device, dtype=dtype)
assert kornia.color.rgb_to_ycbcr(img).shape == shape
+ @pytest.mark.parametrize("shape", [(3, 4, 4), (2, 3, 4, 4)])
+ def test_rgb_to_y(self, device, dtype, shape):
+ img = torch.rand(*shape, device=device, dtype=dtype)
+ output_y = kornia.color.rgb_to_y(img)
+ output_ycbcr = kornia.color.rgb_to_ycbcr(img)
+ assert torch.equal(output_y, output_ycbcr[..., 0:1, :, :])
+
def test_exception(self, device, dtype):
with pytest.raises(TypeError):
assert kornia.color.rgb_to_ycbcr([0.0])