Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Adjust hue change range

Pauda6eeee2270a
diff --git a/kornia/color/adjust.py b/kornia/color/adjust.py
index 0b7bdcf20..8bf11bbcb 100644
--- a/kornia/color/adjust.py
+++ b/kornia/color/adjust.py
@@ -4,6 +4,7 @@ import torch
import torch.nn as nn
from kornia.color.hsv import rgb_to_hsv, hsv_to_rgb
+from kornia.geometry import pi
def adjust_saturation(input: torch.Tensor, saturation_factor: Union[float, torch.Tensor]) -> torch.Tensor:
@@ -59,15 +60,15 @@ def adjust_hue(input: torch.Tensor, hue_factor: Union[float, torch.Tensor]) -> t
if not isinstance(hue_factor, (float, torch.Tensor)):
raise TypeError(f"The hue_factor should be a float number or torch.Tensor in the range between"
- f" [-0.5, 0.5]. Got {type(hue_factor)}")
+ f" [-PI, PI]. Got {type(hue_factor)}")
if isinstance(hue_factor, float):
hue_factor = torch.tensor([hue_factor])
hue_factor = hue_factor.to(input.device).to(input.dtype)
- if ((hue_factor < -0.5) | (hue_factor > 0.5)).any():
- raise ValueError(f"Hue-factor must be in the range [-0.5, 0.5]. Got {hue_factor}")
+ if ((hue_factor < -pi) | (hue_factor > pi)).any():
+ raise ValueError(f"Hue-factor must be in the range [-PI, PI]. Got {hue_factor}")
for _ in input.shape[1:]:
hue_factor = torch.unsqueeze(hue_factor, dim=-1)
@@ -79,7 +80,7 @@ def adjust_hue(input: torch.Tensor, hue_factor: Union[float, torch.Tensor]) -> t
h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3)
# transform the hue value and appl module
- h_out: torch.Tensor = torch.frac(h + hue_factor)
+ h_out: torch.Tensor = torch.fmod(h + hue_factor, 2*pi)
# pack back back the corrected hue
x_adjusted: torch.Tensor = torch.cat([h_out, s, v], dim=-3)
@@ -229,9 +230,9 @@ class AdjustHue(nn.Module):
Args:
input (torch.Tensor): Image/Tensor to be adjusted in the shape of (\*, N).
- hue_factor (float): How much to shift the hue channel. Should be in [-0.5, 0.5]. 0.5
- and -0.5 give complete reversal of hue channel in HSV space in positive and negative
- direction respectively. 0 means no shift. Therefore, both -0.5 and 0.5 will give an
+ hue_factor (float): How much to shift the hue channel. Should be in [-PI, PI]. PI
+ and -PI give complete reversal of hue channel in HSV space in positive and negative
+ direction respectively. 0 means no shift. Therefore, both -PI and PI will give an
image with complementary colors while 0 gives the original image.
Returns:
diff --git a/test/color/test_adjust.py b/test/color/test_adjust.py
index d813d01d6..e4cc2d547 100644
--- a/test/color/test_adjust.py
+++ b/test/color/test_adjust.py
@@ -2,6 +2,7 @@ import pytest
import kornia
import kornia.testing as utils # test utils
+from kornia.geometry import pi
from test.common import device_type
import torch
@@ -105,7 +106,7 @@ class TestAdjustHue:
[[.25, .25],
[.25, .25]]]]) # 2x3x2x2
- f = kornia.color.AdjustHue(torch.tensor([-.5, .5]))
+ f = kornia.color.AdjustHue(torch.tensor([-pi, pi]))
result = f(data)
assert_allclose(result, result.flip(0))