Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

OpenCV tests

Pau1d3249f93a06
diff --git a/test/color/test_hls.py b/test/color/test_hls.py
index 1e5314034..eeba1647d 100644
--- a/test/color/test_hls.py
+++ b/test/color/test_hls.py
@@ -4,35 +4,37 @@ import kornia
import kornia.testing as utils # test utils
from test.common import device_type
+import cv2
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-
class TestRgbToHls:
def test_rgb_to_hls(self):
- data = torch.tensor([[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
+ data = torch.rand(3,5,5)
- [[8., 8.],
- [8., 8.]]])
+ # OpenCV
+ data_cv = data.numpy().transpose(1, 2, 0)
+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)
- expected = torch.tensor([[[0.0641, 0.07138],
- [0.07138, 0.07138]],
+ h_expected = expected[:,:,0]/360.
+ l_expected = expected[:,:,1]
+ s_expected = expected[:,:,2]
- [[0.0569, 0.0588],
- [0.0588, 0.0588]],
+ # Kornia
+ f = kornia.color.RgbToHls()
+ result = f(data)
- [[0.4483, 0.4667],
- [0.4667, 0.4667]]])
+ h = result[0,:,:]
+ l = result[1,:,:]
+ s = result[2,:,:]
- f = kornia.color.RgbToHls()
- assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)
+ assert_allclose(h, h_expected)
+ assert_allclose(l, l_expected)
+ assert_allclose(s, s_expected)
def test_batch_rgb_to_hls(self):
diff --git a/test/color/test_hsv.py b/test/color/test_hsv.py
index 341cce647..f04bc715c 100644
--- a/test/color/test_hsv.py
+++ b/test/color/test_hsv.py
@@ -4,6 +4,8 @@ import kornia
import kornia.testing as utils # test utils
from test.common import device_type
+import cv2
+
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
@@ -13,26 +15,27 @@ class TestRgbToHsv:
def test_rgb_to_hsv(self):
- data = torch.tensor([[[21., 22.],
- [22., 22.]],
+ data = torch.rand(3,5,5)
- [[13., 14.],
- [14., 14.]],
+ # OpenCV
+ data_cv = data.numpy().transpose(1, 2, 0)
+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)
- [[8., 8.],
- [8., 8.]]])
+ h_expected = expected[:,:,0]/360.
+ s_expected = expected[:,:,1]
+ v_expected = expected[:,:,2]
- expected = torch.tensor([[[0.0641, 0.0714],
- [0.0714, 0.0714]],
+ # Kornia
+ f = kornia.color.RgbToHsv()
+ result = f(data)
- [[0.6190, 0.6364],
- [0.6364, 0.6364]],
+ h = result[0,:,:]
+ s = result[1,:,:]
+ v = result[2,:,:]
- [[21.0000 / 255, 22.0000 / 255],
- [22.0000 / 255, 22.0000 / 255]]])
-
- f = kornia.color.RgbToHsv()
- assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(h, h_expected)
+ assert_allclose(s, s_expected)
+ assert_allclose(v, v_expected)
def test_batch_rgb_to_hsv(self):