π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
rgb to hsv
diegovd0296a7e9724c00eb
diff --git a/test/test_image.py b/test/test_image.pyindex d685d584a..02e239e47 100644--- a/test/test_image.py+++ b/test/test_image.py@@ -184,6 +184,93 @@ class TestLaplacian:raise_exception=True)+class TestRgbToHsv:++ def test_rgb_to_hsv(self):++ data = torch.tensor([[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]])++ expected = 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.RgbToHsV()+ assert_allclose(f(data), expected, atol=1e-4, rtol=1e-5)++ def test_batch_rgb_to_hsv(self):++ data = torch.tensor([[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]) # 3x2x2++ expected = 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.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)++ def test_gradcheck(self):++ data = torch.tensor([[[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]]) # 3x2x2++ data = utils.tensor_to_gradcheck_var(data) # to var++ assert gradcheck(image.RgbToHsV(), (data,),+ raise_exception=True)++ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:++ return image.rgb_to_hsv(data)+ data = torch.tensor([[[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]]) # 3x2x2++ actual = op.script(data)+ expected = image.rgb_to_hsv(data)+ assert_allclose(actual, expected)++class TestBgRToRgb:def test_bgr_to_rgb(self):diff --git a/torchgeometry/image/color.py b/torchgeometry/image/color.pyindex d933c0c25..2271932e1 100644--- a/torchgeometry/image/color.py+++ b/torchgeometry/image/color.py@@ -27,7 +27,7 @@ class RgbToHsV(nn.Module):return rgb_to_hsv(image)-def rgb_to_hsv(rgb): # TODO+def rgb_to_hsv(image):"""Convert an RGB image to HSV@@ -38,7 +38,6 @@ def rgb_to_hsv(rgb): # TODOtorch.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)))@@ -47,17 +46,20 @@ def rgb_to_hsv(rgb): # TODOraise ValueError("Input size must have a shape of (*, 3, H, W). Got {}".format(image.shape))- input_shape = rgb.shape- rgb = rgb.reshape(-1, 3)- r, g, b = rgb[:, 0], rgb[:, 1], rgb[:, 2]+ torch.autograd.set_detect_anomaly(True)+ r = image[..., 0, :, :]+ g = image[..., 1, :, :]+ b = image[..., 2, :, :]- maxc = np.maximum(np.maximum(r, g), b)- minc = np.minimum(np.minimum(r, g), b)- v = maxc+ maxc = image.max(-3)[0]+ minc = image.min(-3)[0]++ v = maxc # brightnessdeltac = maxc - minc- s = deltac / maxc- deltac[deltac == 0] = 1+ s = deltac.clone() / v # saturation++ deltac[deltac == 0] = 1 # avoid division by zerorc = (maxc - r) / deltacgc = (maxc - g) / deltacbc = (maxc - b) / deltac@@ -68,9 +70,8 @@ def rgb_to_hsv(rgb): # TODOh[minc == maxc] = 0.0h = (h / 6.0) % 1.0- res = np.dstack([h, s, v])- return res.reshape(input_shape)- '''++ return torch.stack([h, s, v], dim=-3)class RgbToBgr(nn.Module):