Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement adjust_hue, adjust_saturation

edgarriba6d3fa1ac2f1f
diff --git a/kornia/__init__.py b/kornia/__init__.py
index f93fa94d2..5874614d2 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -26,6 +26,8 @@ from kornia.color import (
adjust_brightness,
adjust_contrast,
adjust_gamma,
+ adjust_hue,
+ adjust_saturation,
)
from kornia.contrib import (
extract_tensor_patches,
diff --git a/kornia/color/__init__.py b/kornia/color/__init__.py
index 5afcc42b7..ca9c7402a 100644
--- a/kornia/color/__init__.py
+++ b/kornia/color/__init__.py
@@ -4,7 +4,12 @@ from .rgb import RgbToBgr, rgb_to_bgr
from .hsv import RgbToHsv, rgb_to_hsv
from .hsv import HsvToRgb, hsv_to_rgb
from .normalize import Normalize, normalize
-from .adjust import AdjustBrightness, AdjustContrast, AdjustGamma, adjust_brightness, adjust_contrast, adjust_gamma
+from .adjust import (
+ AdjustBrightness, AdjustContrast, AdjustGamma, AdjustHue, AdjustSaturation,
+)
+from .adjust import (
+ adjust_brightness, adjust_contrast, adjust_gamma, adjust_hue, adjust_saturation,
+)
__all__ = [
"rgb_to_grayscale",
@@ -16,6 +21,8 @@ __all__ = [
"adjust_brightness",
"adjust_contrast",
"adjust_gamma",
+ "adjust_hue",
+ "adjust_saturation",
"RgbToGrayscale",
"BgrToRgb",
"RgbToBgr",
@@ -25,4 +32,6 @@ __all__ = [
"AdjustBrightness",
"AdjustContrast",
"AdjustGamma",
+ "AdjustHue",
+ "AdjustSaturation",
]
diff --git a/kornia/color/adjust.py b/kornia/color/adjust.py
index 3c6539e89..401c2e515 100644
--- a/kornia/color/adjust.py
+++ b/kornia/color/adjust.py
@@ -3,6 +3,69 @@ from typing import Union
import torch
import torch.nn as nn
+from kornia.color.hsv import rgb_to_hsv, hsv_to_rgb
+
+
+def adjust_saturation(input: torch.Tensor, saturation_factor: float) -> torch.Tensor:
+ r"""Adjust color saturation of an image.
+
+ See :class:`~kornia.color.AdjustSaturation` for details.
+ """
+
+ if not torch.is_tensor(input):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
+
+ if not isinstance(saturation_factor, float):
+ raise TypeError(f"The saturation_factor should be a float number.")
+
+ # convert the rgb image to hsv
+ x_hsv: torch.Tensor = rgb_to_hsv(input)
+
+ # unpack the hsv values
+ h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3)
+
+ # transform the hue value and appl module
+ s_out: torch.Tensor = torch.clamp(s * saturation_factor, min=0, max=1)
+
+ # pack back back the corrected hue
+ x_adjusted: torch.Tensor = torch.cat([h, s_out, v], dim=-3)
+
+ # convert back to rgb
+ out: torch.Tensor = hsv_to_rgb(x_adjusted)
+
+ return out
+
+
+def adjust_hue(input: torch.Tensor, hue_factor: float) -> torch.Tensor:
+ r"""Adjust hue of an image.
+
+ See :class:`~kornia.color.AdjustHue` for details.
+ """
+
+ if not torch.is_tensor(input):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
+
+ if not isinstance(hue_factor, float) and -0.5 < hue_factor < 0.5:
+ raise TypeError(f"The hue_factor should be a float number in the range between"
+ f" [-0.5, 0.5]. Got {type(hue_factor)}")
+
+ # convert the rgb image to hsv
+ x_hsv: torch.Tensor = rgb_to_hsv(input)
+
+ # unpack the hsv values
+ h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3)
+
+ # transform the hue value and appl module
+ h_out: torch.Tensor = (h * hue_factor) % 1
+
+ # pack back back the corrected hue
+ x_adjusted: torch.Tensor = torch.cat([h_out, s, v], dim=-3)
+
+ # convert back to rgb
+ out: torch.Tensor = hsv_to_rgb(x_adjusted)
+
+ return out
+
def adjust_gamma(input: torch.Tensor, gamma: float, gain: float = 1.) -> torch.Tensor:
r"""Perform gamma correction on an image.
@@ -96,6 +159,53 @@ def adjust_brightness(input: torch.Tensor,
return out
+class AdjustSaturation(nn.Module):
+ r"""Adjust color saturation of an image.
+
+ The input image is expected to be an RGB image in the range of [0, 1].
+
+ Args:
+ image (torch.Tensor): Image to be adjusted in the shape of (*, N).
+ saturation_factor (float): How much to adjust the saturation. 0 will give a black
+ and white image, 1 will give the original image while 2 will enhance the saturation
+ by a factor of 2.
+
+ Returns:
+ torch.Tensor: Adjusted image.
+ """
+
+ def __init__(self, saturation_factor: float) -> None:
+ super(AdjustSaturation, self).__init__()
+ self.saturation_factor: float = saturation_factor
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
+ return adjust_saturation(input, self.saturation_factor)
+
+
+class AdjustHue(nn.Module):
+ r"""Adjust hue of an image.
+
+ The input image is expected to be an RGB image in the range of [0, 1].
+
+ Args:
+ image (torch.Tensor): Image 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
+ image with complementary colors while 0 gives the original image.
+
+ Returns:
+ torch.Tensor: Adjusted image.
+ """
+
+ def __init__(self, hue_factor: float) -> None:
+ super(AdjustHue, self).__init__()
+ self.hue_factor: float = hue_factor
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
+ return adjust_hue(input, self.hue_factor)
+
+
class AdjustGamma(nn.Module):
r"""Perform gamma correction on an image.
diff --git a/test/color/test_adjust.py b/test/color/test_adjust.py
index f1a5314c2..4e40ef591 100644
--- a/test/color/test_adjust.py
+++ b/test/color/test_adjust.py
@@ -9,6 +9,38 @@ from torch.autograd import gradcheck
from torch.testing import assert_allclose
+class TestAdjustSaturation:
+ def test_saturation_one(self):
+ data = torch.tensor([[[.5, .5],
+ [.5, .5]],
+
+ [[.5, .5],
+ [.5, .5]],
+
+ [[.25, .25],
+ [.25, .25]]]) # 3x2x2
+
+ expected = data
+ f = kornia.color.AdjustSaturation(1.)
+ assert_allclose(f(data), expected)
+
+
+class TestAdjustHue:
+ def test_hue_one(self):
+ data = torch.tensor([[[.5, .5],
+ [.5, .5]],
+
+ [[.5, .5],
+ [.5, .5]],
+
+ [[.25, .25],
+ [.25, .25]]]) # 3x2x2
+
+ expected = data
+ f = kornia.color.AdjustHue(1.)
+ assert_allclose(f(data), expected)
+
+
class TestAdjustGamma:
def test_gamma_zero(self):
data = torch.tensor([[[1., 1.],
@@ -364,10 +396,11 @@ class TestAdjustBrightness:
[0., 0., .5]],
[[.6, .6, .8],
- [.6, .6, .8]],
+ [.6, .6, .8]],
[[1., 1., 0.],
- [.6, .4, .2]]]]) # 2x3x2x3
+ [.6, .4, .2]]]]) # 2x3x2x3
+
factor = torch.tensor([1.5, 2.])
f = kornia.color.AdjustBrightness(factor)
assert_allclose(f(data), expected)