Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix merge master conflicts

edgarriba15083f878016
diff --cc docs/source/color.rst
index 1ceb360bc,b422e0f79..37c3eef4b
--- a/docs/source/color.rst
+++ b/docs/source/color.rst
@@@ -5,14 -5,16 +5,16 @@@ torchgeometry.colo
The functions in this section perform various color space conversions.
-.. autoclass:: RgbToGrayscale
+.. autofunction:: normalize
.. autofunction:: rgb_to_grayscale
-.. autoclass:: RgbToHsv
.. autofunction:: rgb_to_hsv
-.. autoclass:: HsvToRgb
+ .. autofunction:: hsv_to_rgb
-.. autoclass:: RgbToBgr
.. autofunction:: rgb_to_bgr
-.. autoclass:: BgrToRgb
.. autofunction:: bgr_to_rgb
.. autoclass:: Normalize
-.. autofunction:: normalize
+.. autoclass:: RgbToGrayscale
+.. autoclass:: RgbToHsv
++.. autoclass:: HsvToRgb
+.. autoclass:: RgbToBgr
+.. autoclass:: BgrToRgb
diff --cc test/test_color.py
index 8bebda844,76517018e..cdc236b94
--- a/test/test_color.py
+++ b/test/test_color.py
@@@ -108,11 -275,11 +108,11 @@@ class TestRgbToHsv
[[0.6190, 0.6364],
[0.6364, 0.6364]],
- [[21.0000, 22.0000],
- [22.0000, 22.0000]]])
+ [[21.0000 / 255, 22.0000 / 255],
+ [22.0000 / 255, 22.0000 / 255]]])
- f = image.RgbToHsv()
+ f = color.RgbToHsv()
- assert_allclose(f(data), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
def test_batch_rgb_to_hsv(self):
@@@ -131,15 -298,12 +131,12 @@@
[[0.6190, 0.6364],
[0.6364, 0.6364]],
- [[21.0000, 22.0000],
- [22.0000, 22.0000]]]) # 3x2x2
+ [[21.0000 / 255, 22.0000 / 255],
+ [22.0000 / 255, 22.0000 / 255]]]) # 3x2x2
- f = image.RgbToHsv()
+ f = color.RgbToHsv()
data = data.repeat(2, 1, 1, 1) # 2x3x2x2
- print(data.shape)
expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
- print(expected.shape)
- print(f(data).shape)
- assert_allclose(f(data), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
def test_gradcheck(self):
diff --cc torchgeometry/color/_color.py
index c11a7d70c,499c1406f..8c1a590b2
--- a/torchgeometry/color/_color.py
+++ b/torchgeometry/color/_color.py
@@@ -2,11 -2,79 +2,76 @@@ import torc
import torch.nn as nn
+ class HsvToRgb(nn.Module):
-
- """Convert image from HSV to Rgb
++ r"""Convert image from HSV to Rgb
+ The image data is assumed to be in the range of (0, 1).
+
+ args:
+ image (torch.Tensor): RGB image to be converted to HSV.
+
+ returns:
+ torch.tensor: HSV version of the image.
+
+ shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ """
+
+ def __init__(self) -> None:
+ super(HsvToRgb, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return hsv_to_rgb(image)
+
+
+ def hsv_to_rgb(image):
-
- """Convert an HSV image to RGB
++ r"""Convert an HSV image to RGB
+ The image data is assumed to be in the range of (0, 1).
+
+ Args:
+ input (torch.Tensor): RGB Image to be converted to HSV.
+
+
+ Returns:
+ torch.Tensor: HSV version of the image.
+ """
+
+ if not torch.is_tensor(image):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
+ .format(image.shape))
+
+ h: torch.Tensor = image[..., 0, :, :]
+ s: torch.Tensor = image[..., 1, :, :]
+ v: torch.Tensor = image[..., 2, :, :]
+
+ hi: torch.Tensor = torch.floor(h * 6)
+ f: torch.Tensor = h * 6 - hi
+ p: torch.Tensor = v * (1 - s)
+ q: torch.Tensor = v * (1 - f * s)
+ t: torch.Tensor = v * (1 - (1 - f) * s)
+
+ out: torch.Tensor = torch.stack([hi, hi, hi], dim=-3) % 6
+
+ out[out == 0]: torch.Tensor = torch.stack((v, t, p), dim=-3)[out == 0]
+ out[out == 1]: torch.Tensor = torch.stack((q, v, p), dim=-3)[out == 1]
+ out[out == 2]: torch.Tensor = torch.stack((p, v, t), dim=-3)[out == 2]
+ out[out == 3]: torch.Tensor = torch.stack((p, q, v), dim=-3)[out == 3]
+ out[out == 4]: torch.Tensor = torch.stack((t, p, v), dim=-3)[out == 4]
+ out[out == 5]: torch.Tensor = torch.stack((v, p, q), dim=-3)[out == 5]
+
+ return out
+
+
class RgbToHsv(nn.Module):
+ r"""Convert image from RGB to HSV.
- """Convert image from RGB to HSV.
The image data is assumed to be in the range of (0, 1).
-
args:
image (torch.Tensor): RGB image to be converted to HSV.
@@@ -194,7 -264,13 +259,13 @@@ class RgbToGrayscale(nn.Module)
def rgb_to_grayscale(input: torch.Tensor) -> torch.Tensor:
r"""Convert an RGB image to grayscale.
- See :class:`~torchgeometry.image.RgbToGrayscale` for details.
+ See :class:`~torchgeometry.color.RgbToGrayscale` for details.
+
+ Args:
+ input (torch.Tensor): Image to be converted to grayscale.
+
+ Returns:
+ torch.Tensor: Grayscale version of the image.
"""
if not torch.is_tensor(input):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(