Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add Bgr2Gray

HassanAlsamahic351c143e447
diff --git a/docs/source/color.rst b/docs/source/color.rst
index 9e9cd49ec..2933888ac 100644
--- a/docs/source/color.rst
+++ b/docs/source/color.rst
@@ -6,6 +6,7 @@ kornia.color
The functions in this section perform various color space conversions.
.. autofunction:: rgb_to_grayscale
+.. autofunction:: bgr_to_grayscale
.. autofunction:: rgb_to_hsv
.. autofunction:: hsv_to_rgb
.. autofunction:: rgb_to_hls
@@ -20,6 +21,7 @@ The functions in this section perform various color space conversions.
.. autofunction:: adjust_gamma
.. autoclass:: RgbToGrayscale
+.. autoclass:: BgrToGrayscale
.. autoclass:: RgbToHsv
.. autoclass:: HsvToRgb
.. autoclass:: RgbToHls
diff --git a/kornia/__init__.py b/kornia/__init__.py
index c0d7cfee4..7e2286eae 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -18,6 +18,7 @@ from kornia import utils
from kornia.color import (
rgb_to_grayscale,
+ bgr_to_grayscale,
bgr_to_rgb,
rgb_to_bgr,
rgb_to_hsv,
diff --git a/kornia/color/__init__.py b/kornia/color/__init__.py
index fd1038e0b..a72909a6c 100644
--- a/kornia/color/__init__.py
+++ b/kornia/color/__init__.py
@@ -1,4 +1,5 @@
from .gray import rgb_to_grayscale, RgbToGrayscale
+from .gray import bgr_to_grayscale, BgrToGrayscale
from .rgb import BgrToRgb, bgr_to_rgb
from .rgb import RgbToBgr, rgb_to_bgr
from .hsv import RgbToHsv, rgb_to_hsv
@@ -15,6 +16,7 @@ from .adjust import (
__all__ = [
"rgb_to_grayscale",
+ "bgr_to_grayscale",
"bgr_to_rgb",
"rgb_to_bgr",
"rgb_to_hsv",
@@ -28,6 +30,7 @@ __all__ = [
"adjust_hue",
"adjust_saturation",
"RgbToGrayscale",
+ "BgrToGrayscale",
"BgrToRgb",
"RgbToBgr",
"RgbToHsv",
diff --git a/kornia/color/gray.py b/kornia/color/gray.py
index 64b8f1fc2..ea24dd6a3 100644
--- a/kornia/color/gray.py
+++ b/kornia/color/gray.py
@@ -17,6 +17,9 @@ class RgbToGrayscale(nn.Module):
- input: :math:`(*, 3, H, W)`
- output: :math:`(*, 1, H, W)`
+ reference:
+ https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+
Examples::
>>> input = torch.rand(2, 3, 4, 5)
@@ -50,7 +53,64 @@ def rgb_to_grayscale(input: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(input.shape))
- # https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+
r, g, b = torch.chunk(input, chunks=3, dim=-3)
gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.110 * b
return gray
+
+
+class BgrToGrayscale(nn.Module):
+ r"""convert image to grayscale version of image.
+
+ the image data is assumed to be in the range of (0, 1).
+
+ args:
+ input (torch.Tensor): BGR image to be converted to grayscale.
+
+ returns:
+ torch.Tensor: grayscale version of the image.
+
+ shape:
+ - input: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 1, H, W)`
+
+ reference:
+ https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+
+ Examples::
+
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> gray = kornia.color.BgrToGrayscale()
+ >>> output = gray(input) # 2x1x4x5
+ """
+
+ def __init__(self) -> None:
+ super(BgrToGrayscale, self).__init__()
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
+ return bgr_to_grayscale(input)
+
+
+def bgr_to_grayscale(input: torch.Tensor) -> torch.Tensor:
+ r"""Convert an BGR image to grayscale.
+
+ See :class:`~kornia.color.BgrToGrayscale` for details.
+
+ Args:
+ input (torch.Tensor): BGR image to be converted to grayscale.
+
+ Returns:
+ torch.Tensor: Grayscale version of the image.
+ """
+ if not torch.is_tensor(input):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(input)))
+
+ if len(input.shape) < 3 and input.shape[-3] != 3:
+ raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
+ .format(input.shape))
+
+
+ b, g, r = torch.chunk(input, chunks=3, dim=-3)
+ gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.110 * b
+ return gray
diff --git a/test/color/test_gray.py b/test/color/test_gray.py
index 4a2ed1882..467887a3c 100644
--- a/test/color/test_gray.py
+++ b/test/color/test_gray.py
@@ -34,3 +34,30 @@ class TestRgbToGrayscale:
gray = kornia.color.RgbToGrayscale()
gray_traced = torch.jit.trace(kornia.color.RgbToGrayscale(), img)
assert_allclose(gray(img), gray_traced(img))
+
+
+class TestBgrToGrayscale:
+ def test_bgr_to_grayscale(self):
+ channels, height, width = 3, 4, 5
+ img = torch.ones(channels, height, width)
+ assert kornia.bgr_to_grayscale(img).shape == (1, height, width)
+
+ def test_bgr_to_grayscale_batch(self):
+ batch_size, channels, height, width = 2, 3, 4, 5
+ img = torch.ones(batch_size, channels, height, width)
+ assert kornia.bgr_to_grayscale(img).shape == \
+ (batch_size, 1, height, width)
+
+ def test_gradcheck(self):
+ batch_size, channels, height, width = 2, 3, 4, 5
+ img = torch.ones(batch_size, channels, height, width)
+ img = utils.tensor_to_gradcheck_var(img) # to var
+ assert gradcheck(kornia.bgr_to_grayscale, (img,), raise_exception=True)
+
+ @pytest.mark.skip(reason="turn off all jit for a while")
+ def test_jit(self):
+ batch_size, channels, height, width = 2, 3, 64, 64
+ img = torch.ones(batch_size, channels, height, width)
+ gray = kornia.color.BgrToGrayscale()
+ gray_traced = torch.jit.trace(kornia.color.BgrToGrayscale(), img)
+ assert_allclose(gray(img), gray_traced(img))