π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
hsv to rgb
diegovd02969bee8655b5b4
diff --git a/test/test_image.py b/test/test_image.pyindex 9f681e7a9..1192b61c0 100644--- a/test/test_image.py+++ b/test/test_image.py@@ -187,6 +187,75 @@ class TestLaplacian:raise_exception=True)+class TestHsvToRgb:++ def test_hsv_to_rgb(self):++ expected = torch.tensor([[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]])++ data = torch.tensor([[[0.0641, 0.0714],+ [0.0714, 0.0714]],++ [[0.6190, 0.6364],+ [0.6364, 0.6364]],++ [[21.0000, 22.0000],+ [22.0000, 22.0000]]])++ f = image.HsvToRgb()+ assert_allclose(f(data), expected, atol=1e-3, rtol=1e-3)++ def test_batch_hsv_to_rgb(self):++ expected = torch.tensor([[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]) # 3x2x2++ data = torch.tensor([[[0.0641, 0.0714],+ [0.0714, 0.0714]],++ [[0.6190, 0.6364],+ [0.6364, 0.6364]],++ [[21.0000, 22.0000],+ [22.0000, 22.0000]]]) # 3x2x2++ f = image.HsvToRgb()+ data = data.repeat(2, 1, 1, 1) # 2x3x2x2+ expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2+ assert_allclose(f(data), expected, atol=1e-3, rtol=1e-3)++ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:++ return image.hsv_to_rgb(data)+ data = torch.tensor([[[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]]) # 3x2x2++ actual = op_script(data)+ expected = image.hsv_to_rgb(data)+ assert_allclose(actual, expected)++class TestRgbToHsv:def test_rgb_to_hsv(self):@@ -206,11 +275,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()- 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):@@ -229,26 +298,23 @@ class TestRgbToHsv:[[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]]]) # 3x2x2f = image.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):data = torch.tensor([[[[21., 22.],- [22., 22.]],+ [22., 22.]],- [[13., 14.],- [14., 14.]],+ [[13., 14.],+ [14., 14.]],- [[8., 8.],- [8., 8.]]]]) # 3x2x2+ [[8., 8.],+ [8., 8.]]]]) # 3x2x2data = utils.tensor_to_gradcheck_var(data) # to var@@ -261,13 +327,13 @@ class TestRgbToHsv:return image.rgb_to_hsv(data)data = torch.tensor([[[[21., 22.],- [22., 22.]],+ [22., 22.]],- [[13., 14.],- [14., 14.]],+ [[13., 14.],+ [14., 14.]],- [[8., 8.],- [8., 8.]]]]) # 3x2x2+ [[8., 8.],+ [8., 8.]]]]) # 3x2x2actual = op_script(data)expected = image.rgb_to_hsv(data)@@ -304,13 +370,13 @@ class TestBgrToRgb:# prepare input datadata = torch.tensor([[[[1., 1.],- [1., 1.]],+ [1., 1.]],- [[2., 2.],- [2., 2.]],+ [[2., 2.],+ [2., 2.]],- [[3., 3.],- [3., 3.]]],+ [[3., 3.],+ [3., 3.]]],[[[1., 1.],[1., 1.]],@@ -322,22 +388,22 @@ class TestBgrToRgb:[3., 3.]]]]) # 2x3x2x2expected = torch.tensor([[[[3., 3.],- [3., 3.]],+ [3., 3.]],- [[2., 2.],- [2., 2.]],+ [[2., 2.],+ [2., 2.]],- [[1., 1.],- [1., 1.]]],+ [[1., 1.],+ [1., 1.]]],- [[[3., 3.],- [3., 3.]],+ [[[3., 3.],+ [3., 3.]],- [[2., 2.],- [2., 2.]],+ [[2., 2.],+ [2., 2.]],- [[1., 1.],- [1., 1.]]]]) # 2x3x2x2+ [[1., 1.],+ [1., 1.]]]]) # 2x3x2x2f = image.BgrToRgb()out = f(data)@@ -446,13 +512,13 @@ class TestRgbToBgr:# prepare input datadata = torch.tensor([[[[1., 1.],- [1., 1.]],+ [1., 1.]],- [[2., 2.],- [2., 2.]],+ [[2., 2.],+ [2., 2.]],- [[3., 3.],- [3., 3.]]],+ [[3., 3.],+ [3., 3.]]],[[[1., 1.],[1., 1.]],@@ -464,22 +530,22 @@ class TestRgbToBgr:[3., 3.]]]]) # 2x3x2x2expected = torch.tensor([[[[3., 3.],- [3., 3.]],+ [3., 3.]],- [[2., 2.],- [2., 2.]],+ [[2., 2.],+ [2., 2.]],- [[1., 1.],- [1., 1.]]],+ [[1., 1.],+ [1., 1.]]],- [[[3., 3.],- [3., 3.]],+ [[[3., 3.],+ [3., 3.]],- [[2., 2.],- [2., 2.]],+ [[2., 2.],+ [2., 2.]],- [[1., 1.],- [1., 1.]]]]) # 2x3x2x2+ [[1., 1.],+ [1., 1.]]]]) # 2x3x2x2f = image.RgbToBgr()out = f(data)diff --git a/torchgeometry/image/__init__.py b/torchgeometry/image/__init__.pyindex 2dd1f2e9c..c21b59552 100644--- a/torchgeometry/image/__init__.py+++ b/torchgeometry/image/__init__.py@@ -6,6 +6,7 @@ from .color import rgb_to_grayscale, RgbToGrayscalefrom .color import BgrToRgb, bgr_to_rgbfrom .color import RgbToBgr, rgb_to_bgrfrom .color import RgbToHsv, rgb_to_hsv+from .color import HsvToRgb, hsv_to_rgbfrom .color import Normalize, normalizefrom .pyramid import pyrdown, pyrup, PyrDown, PyrUpfrom .sobel import SobelEdges, Sobel, sobel, sobel_edgesdiff --git a/torchgeometry/image/color.py b/torchgeometry/image/color.pyindex 9781868aa..6b4208cd8 100644--- a/torchgeometry/image/color.py+++ b/torchgeometry/image/color.py@@ -2,11 +2,78 @@ import torchimport torch.nn as nn+class HsvToRgb(nn.Module):++ """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 RGB image to HSV.+ 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):"""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.@@ -26,8 +93,10 @@ class RgbToHsv(nn.Module):return rgb_to_hsv(image)+# Inspired by https://gist.github.com/PolarNick239/691387158ff1c41ad73cdef rgb_to_hsv(image):"""Convert an RGB image to HSV.+ The image data is assumed to be in the range of (0, 1).Args:input (torch.Tensor): RGB Image to be converted to HSV.