Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

small fix in grayscale conversion and add opencv tests

edgarriba1e2e8f34d9f7
diff --git a/kornia/color/gray.py b/kornia/color/gray.py
index b84be69c6..fe3f7c96e 100644
--- a/kornia/color/gray.py
+++ b/kornia/color/gray.py
@@ -55,7 +55,7 @@ def rgb_to_grayscale(input: torch.Tensor) -> torch.Tensor:
.format(input.shape))
r, g, b = torch.chunk(input, chunks=3, dim=-3)
- gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.110 * b
+ gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
return gray
diff --git a/test/color/test_gray.py b/test/color/test_gray.py
index 27456e36a..bf9a84bb8 100644
--- a/test/color/test_gray.py
+++ b/test/color/test_gray.py
@@ -1,4 +1,5 @@
import pytest
+import cv2
import kornia
import kornia.testing as utils # test utils
@@ -21,6 +22,14 @@ class TestRgbToGrayscale:
assert kornia.rgb_to_grayscale(img).shape == \
(batch_size, 1, height, width)
+ def test_opencv(self, device):
+ batch_size, channels, height, width = 1, 3, 4, 5
+ img = torch.rand(batch_size, channels, height, width).to(device)
+ img_gray = kornia.rgb_to_grayscale(img)
+ img_np = kornia.tensor_to_image(img)
+ img_gray_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
+ assert_allclose(img_gray, kornia.image_to_tensor(img_gray_cv).to(device))
+
def test_gradcheck(self, device):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width).to(device)
@@ -48,6 +57,14 @@ class TestBgrToGrayscale:
assert kornia.bgr_to_grayscale(img).shape == \
(batch_size, 1, height, width)
+ def test_opencv(self, device):
+ batch_size, channels, height, width = 1, 3, 4, 5
+ img = torch.rand(batch_size, channels, height, width).to(device)
+ img_gray = kornia.bgr_to_grayscale(img)
+ img_np = kornia.tensor_to_image(img)
+ img_gray_cv = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
+ assert_allclose(img_gray, kornia.image_to_tensor(img_gray_cv).to(device))
+
def test_gradcheck(self, device):
batch_size, channels, height, width = 2, 3, 4, 5
img = torch.ones(batch_size, channels, height, width).to(device)