Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add Dipet test and correct it without eps

Pau4743b3661196
diff --git a/kornia/color/hls.py b/kornia/color/hls.py
index 887fa1add..611f1ee2c 100644
--- a/kornia/color/hls.py
+++ b/kornia/color/hls.py
@@ -149,4 +149,7 @@ def rgb_to_hls(image):
h: torch.Tensor = 2. * pi * (60. * hi) / 360. # hue [0, 2*pi]
- return torch.stack([h, l, s], dim=-3)
+ image_hls: torch.Tensor = torch.stack([h, l, s], dim=-3)
+ image_hls[torch.isnan(image_hls)] = 0.
+
+ return image_hls
diff --git a/test/color/test_hls.py b/test/color/test_hls.py
index 37fe07667..9b337853f 100644
--- a/test/color/test_hls.py
+++ b/test/color/test_hls.py
@@ -173,3 +173,18 @@ class TestHlsToRgb:
actual = op_script(data)
expected = kornia.hls_to_rgb(data)
assert_allclose(actual, expected)
+
+ def test_nan(self):
+ np.random.seed(0)
+
+ img = (np.random.randint(0, 256, [1000, 1000, 3], np.uint8) / 255.0).astype(np.float32)
+
+ cv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
+
+ k_img = torch.from_numpy(np.transpose(img, [2, 0, 1]))
+
+ k_img = kornia.rgb_to_hls(k_img.float())
+ k_img[0] = k_img[0] * 360 / (2 * pi)
+ k_img = np.transpose(k_img.numpy(), [1, 2, 0])
+
+ assert_allclose(cv_img, k_img)
diff --git a/test/color/test_hsv.py b/test/color/test_hsv.py
index 0aa0a9caf..12a379bb8 100644
--- a/test/color/test_hsv.py
+++ b/test/color/test_hsv.py
@@ -174,3 +174,18 @@ class TestHsvToRgb:
actual = op_script(data)
expected = kornia.hsv_to_rgb(data)
assert_allclose(actual, expected)
+
+ def test_nan(self):
+ np.random.seed(0)
+
+ img = (np.random.randint(0, 256, [1000, 1000, 3], np.uint8) / 255.0).astype(np.float32)
+
+ cv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
+
+ k_img = torch.from_numpy(np.transpose(img, [2, 0, 1]))
+
+ k_img = kornia.rgb_to_hsv(k_img.float())
+ k_img[0] = k_img[0] * 360 / (2 * pi)
+ k_img = np.transpose(k_img.numpy(), [1, 2, 0])
+
+ assert_allclose(cv_img, k_img)