π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): accept odd heights in YUV422 conversion (#5091)
Devulapalli Naga Sri Vaishnavi20933a19392c
diff --git a/changelog.d/5091.fixed.md b/changelog.d/5091.fixed.mdnew file mode 100644index 000000000..b3127c7d0--- /dev/null+++ b/changelog.d/5091.fixed.md@@ -0,0 +1,4 @@+`kornia.color.rgb_to_yuv422`, `yuv422_to_rgb`, `RgbToYuv422` and `Yuv422ToRgb` accept an odd height when the+width is even. 4:2:2 subsamples chroma along the width only, but the shape guard also required an even+height and raised `ShapeError`. Results for even heights are unchanged; the 4:2:0 functions still require an+even height and width.diff --git a/kornia/color/yuv.py b/kornia/color/yuv.pyindex df936c6c0..d4439485f 100644--- a/kornia/color/yuv.py+++ b/kornia/color/yuv.py@@ -118,7 +118,7 @@ def rgb_to_yuv420(image: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:def rgb_to_yuv422(image: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:r"""Convert an RGB image to YUV 422 (subsampled).- Input need to be padded to be evenly divisible by 2 horizontal and vertical.+ Input width must be evenly divisible by 2; height may be odd.The image data is assumed to be in the range of :math:`(0, 1)`. The range of the output is of:math:`(0, 1)` to luma and the ranges of U and V are :math:`(-0.436, 0.436)` and :math:`(-0.615, 0.615)`,@@ -144,8 +144,8 @@ def rgb_to_yuv422(image: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:"""KORNIA_CHECK_SHAPE(image, ["*", "3", "H", "W"])- if len(image.shape) < 2 or image.shape[-2] % 2 == 1 or image.shape[-1] % 2 == 1:- raise ShapeError(f"Input H&W must be evenly divisible by 2. Got {image.shape}")+ if image.shape[-1] % 2 == 1:+ raise ShapeError(f"Input W must be evenly divisible by 2. Got {image.shape}")yuvimage = rgb_to_yuv(image)@@ -257,7 +257,7 @@ def yuv420_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor:def yuv422_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor:r"""Convert an YUV422 image to RGB.- Input need to be padded to be evenly divisible by 2 horizontal and vertical.+ Input width must be evenly divisible by 2; height may be odd.The image data is assumed to be in the range of :math:`(0, 1)` for luma (Y). The ranges of U and V are:math:`(-0.436, 0.436)` and :math:`(-0.615, 0.615)`, respectively.@@ -283,8 +283,8 @@ def yuv422_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor:KORNIA_CHECK_SHAPE(imagey, ["*", "1", "H", "W"])KORNIA_CHECK_SHAPE(imageuv, ["*", "2", "H", "W"])- if len(imagey.shape) < 2 or imagey.shape[-2] % 2 == 1 or imagey.shape[-1] % 2 == 1:- raise ShapeError(f"Input H&W must be evenly divisible by 2. Got {imagey.shape}")+ if imagey.shape[-1] % 2 == 1:+ raise ShapeError(f"Input W must be evenly divisible by 2. Got {imagey.shape}")if (len(imageuv.shape) < 2@@ -399,7 +399,7 @@ class RgbToYuv420(nn.Module):class RgbToYuv422(nn.Module):r"""Convert an image from RGB to YUV422.- Width and Height must be evenly divisible by 2.+ Width must be evenly divisible by 2; height may be odd.The image data is assumed to be in the range of :math:`(0, 1)`.@@ -540,7 +540,7 @@ class Yuv420ToRgb(nn.Module):class Yuv422ToRgb(nn.Module):r"""Convert an image from YUV to RGB.- Width and Height must be evenly divisible by 2.+ Width must be evenly divisible by 2; height may be odd.The image data is assumed to be in the range of :math:`(0, 1)` for luma (Y). The ranges of U and V are:math:`(-0.436, 0.436)` and :math:`(-0.615, 0.615)`, respectively.diff --git a/tests/color/test_yuv.py b/tests/color/test_yuv.pyindex 935b9f6b2..786fd0557 100644--- a/tests/color/test_yuv.py+++ b/tests/color/test_yuv.py@@ -497,12 +497,6 @@ class TestRgbToYuv422(BaseTester):with pytest.raises(ShapeError):kornia.color.rgb_to_yuv422(torch.ones(3, 2, 1, device=device, dtype=dtype))- # Odd H is rejected too, even though 4:2:2 subsamples width only. Pinned because the- # guard is shared verbatim with rgb_to_yuv420 and may well be over-strict here: if it- # is ever relaxed to the width test alone, that is a behavior change, not a cleanup.- with pytest.raises(ShapeError):- kornia.color.rgb_to_yuv422(torch.ones(3, 3, 4, device=device, dtype=dtype))-@pytest.mark.parametrize("name", list(_REFERENCE_COLORS))def test_unit(self, device, dtype, name):rgb_values, yuv_values = _REFERENCE_COLORS[name]@@ -535,6 +529,30 @@ class TestRgbToYuv422(BaseTester):# both), so an axis swap in the subsample cannot pass by accident.assert not torch.allclose(uv[:, 0], uv[:, 1])+ @pytest.mark.parametrize("height", [1, 3, 5])+ def test_odd_height(self, device, dtype, height):+ # 4:2:2 pairs columns within a row, so an odd height is valid. Oracle as in test_unit_subsampling.+ rgb = _seeded_rand(2, 3, height, 4, seed=4224)+ reference = _rgb_to_yuv_reference(rgb)+ expected_uv = reference[..., 1:, :, :].reshape(2, 2, height, 2, 2).mean(-1)+ atol = _unit_atol(_FORWARD_ATOL, dtype)+ rgb = rgb.to(device=device, dtype=dtype)++ y, uv = kornia.color.rgb_to_yuv422(rgb)+ self.assert_close(y, reference[..., :1, :, :].to(device=device, dtype=dtype), atol=atol, rtol=0.0)+ self.assert_close(uv, expected_uv.to(device=device, dtype=dtype), atol=atol, rtol=0.0)+ module_y, module_uv = kornia.color.RgbToYuv422()(rgb)+ self.assert_close(module_y, y)+ self.assert_close(module_uv, uv)++ def test_odd_height_round_trip(self, device, dtype):+ # Repeating each horizontal pixel makes the 4:2:2 chroma subsample lossless.+ rgb = _seeded_rand(2, 3, 3, 2, seed=4227).repeat_interleave(2, dim=-1).to(device=device, dtype=dtype)+ rtol, atol = _round_trip_tol(dtype)++ y, uv = kornia.color.rgb_to_yuv422(rgb)+ self.assert_close(kornia.color.yuv422_to_rgb(y, uv), rgb, atol=atol, rtol=rtol)+def test_forth_and_back(self, device, dtype):# 1x2-constant input, so the horizontal chroma subsample is lossless.rtol, atol = _round_trip_tol(dtype)@@ -858,13 +876,6 @@ class TestYuv422ToRgb(BaseTester):imguv = torch.ones(2, 4, 1, device=device, dtype=dtype)kornia.color.yuv422_to_rgb(imgy, imguv)- # Odd luma H is rejected too, even though 4:2:2 subsamples width only. Pinned because- # the guard is shared verbatim with yuv420_to_rgb and may well be over-strict here.- with pytest.raises(ShapeError):- imgy = torch.ones(1, 3, 4, device=device, dtype=dtype)- imguv = torch.ones(2, 3, 2, device=device, dtype=dtype)- kornia.color.yuv422_to_rgb(imgy, imguv)-# Chroma must be exactly half the luma width.with pytest.raises(ShapeError):imgy = torch.ones(1, 4, 4, device=device, dtype=dtype)@@ -927,6 +938,19 @@ class TestYuv422ToRgb(BaseTester):# Rows carry different chroma, so a 2x2 upsample would not agree with the reference.assert not torch.allclose(uv[:, 0], uv[:, 1])+ @pytest.mark.parametrize("height", [1, 3, 5])+ def test_odd_height(self, device, dtype, height):+ # Chroma column k covers luma columns 2k and 2k + 1 of its own row. Oracle as in test_unit_upsampling.+ y = _seeded_yuv(2, 3, height, 4, seed=4225)[:, :1]+ uv = _seeded_yuv(2, 3, height, 2, seed=4226)[:, 1:]+ expected = _yuv_to_rgb_reference(torch.cat([y, uv[..., torch.arange(4) // 2]], dim=-3))+ y, uv = y.to(device=device, dtype=dtype), uv.to(device=device, dtype=dtype)++ rgb = kornia.color.yuv422_to_rgb(y, uv)+ atol = _unit_atol(_INVERSE_ATOL, dtype)+ self.assert_close(rgb, expected.to(device=device, dtype=dtype), atol=atol, rtol=0.0)+ self.assert_close(kornia.color.Yuv422ToRgb()(y, uv), rgb)+def test_forth_and_back(self, device, dtype):rtol, atol = _round_trip_tol(dtype)datay = _seeded_yuv(3, 4, 6, seed=4222)[:1].to(device=device, dtype=dtype)