π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Adjust hue change range
Pauda6eeee2270a
diff --git a/kornia/color/adjust.py b/kornia/color/adjust.pyindex 0b7bdcf20..8bf11bbcb 100644--- a/kornia/color/adjust.py+++ b/kornia/color/adjust.py@@ -4,6 +4,7 @@ import torchimport torch.nn as nnfrom kornia.color.hsv import rgb_to_hsv, hsv_to_rgb+from kornia.geometry import pidef 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]) -> tif 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]) -> th, 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 huex_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 animage with complementary colors while 0 gives the original image.Returns:diff --git a/test/color/test_adjust.py b/test/color/test_adjust.pyindex d813d01d6..e4cc2d547 100644--- a/test/color/test_adjust.py+++ b/test/color/test_adjust.py@@ -2,6 +2,7 @@ import pytestimport korniaimport kornia.testing as utils # test utils+from kornia.geometry import pifrom test.common import device_typeimport 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))