π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(enhance): wrap negative hue sums into [0, 2Ο) in adjust_hue_raw (#5345)
niukanen10de32da25ac1
diff --git a/changelog.d/5345.breaking.md b/changelog.d/5345.breaking.mdnew file mode 100644index 000000000..7770ad90d--- /dev/null+++ b/changelog.d/5345.breaking.md@@ -0,0 +1 @@+`adjust_hue_raw` now wraps the shifted hue into [0, 2Ο), matching `rgb_to_hsv`. A negative sum used to stay negative: hue 0 shifted by -0.5 returned -0.5, and now returns 2Ο - 0.5. A sum that rounds up to 2Ο returns 0. `adjust_hue` output changes only by rounding.diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.pyindex 2a82954c8..ef7143a94 100644--- a/kornia/enhance/adjust.py+++ b/kornia/enhance/adjust.py@@ -262,7 +262,9 @@ def adjust_hue_raw(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> t# transform the hue value and appl moduledivisor: float = 2 * pi- h_out: torch.Tensor = torch.fmod(h + factor, divisor)+ h_out: torch.Tensor = torch.remainder(h + factor, divisor)+ # A small negative sum rounds up to the period itself (0 - 1e-8 gives 2Ο in float32); fold it to 0.+ h_out = h_out.masked_fill(h_out >= divisor, 0.0)# pack back back the corrected hueout: torch.Tensor = torch.cat([h_out, s, v], dim=-3)diff --git a/tests/enhance/test_adjust.py b/tests/enhance/test_adjust.pyindex 29bfd22ab..a6d623ca1 100644--- a/tests/enhance/test_adjust.py+++ b/tests/enhance/test_adjust.py@@ -269,6 +269,24 @@ class TestAdjustHue(BaseTester):img = torch.rand(batch_size, channels, height, width, device=device, dtype=torch.float64)self.gradcheck(kornia.enhance.adjust_hue, (img, 2.0))+ def test_hue_raw_wraps_negative_sum_5326(self, device, dtype):+ data = torch.tensor([[[[1.0, 0.0]]], [[[0.0, 1.0]]], [[[0.0, 0.0]]]], device=device, dtype=dtype).view(+ 1, 3, 1, 2+ )++ hsv = kornia.color.rgb_to_hsv(data)+ out = kornia.enhance.adjust_hue_raw(hsv, -0.5)++ self.assert_close(out[:, 0], torch.remainder(hsv[:, 0] - 0.5, 2 * pi))+ assert (out[:, 0] >= 0).all()++ def test_hue_raw_wrap_never_returns_the_period_5326(self, device, dtype):+ # 0 - eps / 16 is below 0, but 2Ο - eps / 16 rounds to 2Ο in this dtype; the wrapped hue must be 0, not 2Ο.+ hsv = torch.tensor([0.0, 0.5, 0.5], device=device, dtype=dtype).view(1, 3, 1, 1)+ factor = torch.tensor(-torch.finfo(dtype).eps / 16, device=device, dtype=dtype)+ hue = kornia.enhance.adjust_hue_raw(hsv, factor)[0, 0, 0, 0]+ assert hue.item() == 0.0+class TestAdjustGamma(BaseTester):@pytest.mark.parametrize("shape", [(3, 4, 4), (2, 3, 3, 3), (4, 3, 3, 1, 1)])