π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): wrap rounded HSV hue endpoint to zero (#5356)
AhmetHakanBezir701dceee9466
diff --git a/changelog.d/5356.fixed.md b/changelog.d/5356.fixed.mdnew file mode 100644index 000000000..db6b22475--- /dev/null+++ b/changelog.d/5356.fixed.md@@ -0,0 +1,3 @@+`rgb_to_hsv` returns hue 0 instead of the period itself for a hue just below 0: `[1, 0, 6e-8]` used to return+`6.2831855` in float32, above 2Ο, and `[1, 0, 0.003]` returned `6.28125`, float16's 2Ο. Hues now stay in `[0, 2Ο)` in+every dtype, as documented and as `adjust_hue_raw` returns them. All other values are unchanged.diff --git a/kornia/color/hsv.py b/kornia/color/hsv.pyindex 042a0da55..2c0c1c30b 100644--- a/kornia/color/hsv.py+++ b/kornia/color/hsv.py@@ -41,7 +41,7 @@ def rgb_to_hsv(image: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:Returns:HSV version of the image with shape of :math:`(*, 3, H, W)`.- The H channel values are in the range 0..2pi. S and V are in the range 0..1.+ The H channel values are in the range [0, 2pi). S and V are in the range 0..1... note::See a working example `here <https://www.kornia.org/tutorials/nbs/color_conversions.html>`__.@@ -81,6 +81,9 @@ def rgb_to_hsv(image: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:h = h / deltach = (h / 6.0) % 1.0h = 2.0 * math.pi * h # we return 0/2pi output+ # A tiny negative hue rounds up to the period itself, in the modulo (float32) or only in the scaling+ # (float16: 2Ο * (1 - 2**-11) rounds to 6.28125); fold it to 0, as adjust_hue_raw does.+ h = h.masked_fill(h >= 2.0 * math.pi, 0.0)return torch.stack((h, s, v), dim=-3)diff --git a/tests/color/test_hsv.py b/tests/color/test_hsv.pyindex 8870b499d..3e545abec 100644--- a/tests/color/test_hsv.py+++ b/tests/color/test_hsv.py@@ -49,6 +49,23 @@ class TestRgbToHsv(BaseTester):with pytest.raises(ValueError):assert kornia.color.rgb_to_hsv(img)+ def test_hue_period_endpoint(self, device, dtype):+ # A tiny negative hue can round to the excluded upper endpoint after modulo.+ delta = torch.finfo(dtype).eps+ image = torch.tensor([[1.0, 1.0, 1.0], [0.0, delta, 0.0], [delta, 0.0, 0.0]], device=device, dtype=dtype)+ image = image.reshape(1, 3, 1, 3)+ hsv = kornia.color.rgb_to_hsv(image)+ hue = hsv[:, 0]+ assert hue[0, 0, 0] == 0+ assert hue[0, 0, 1] > 0+ assert hue[0, 0, 2] == 0+ assert torch.all((hue >= 0) & (hue < 2 * math.pi))+ self.assert_close(kornia.color.hsv_to_rgb(hsv), image)+ # This hue is the largest below one turn, 1 - delta / 2. In float16, 2Ο * (1 - 2**-11) rounds to 6.28125,+ # float16's 2Ο, only when scaled to radians; the comparison rounds 2Ο to the dtype, as adjust_hue_raw does.+ near_seam = torch.tensor([1.0, 0.0, 3 * delta], device=device, dtype=dtype).view(1, 3, 1, 1)+ assert kornia.color.rgb_to_hsv(near_seam)[0, 0, 0, 0] < 2 * math.pi+def test_unit(self, device, dtype):data = torch.tensor([