Skip to content

šŸ Geometric Computer Vision Library for Spatial AI

Make Morphology compatible with both OpenCV and Scipy (#1084)

Manza12225160cbda96
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 667cdf798..341aa49cc 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -138,7 +138,7 @@ def convert_points_from_homogeneous(points: torch.Tensor, eps: float = 1e-8) ->
if len(points.shape) < 2:
raise ValueError("Input must be at least a 2D tensor. Got {}".format(points.shape))
- # we check for points at infinity
+ # we check for points at max_val
z_vec: torch.Tensor = points[..., -1:]
# set the results of division by zeror/near-zero to 1.0
diff --git a/kornia/morphology/morphology.py b/kornia/morphology/morphology.py
index 29e9f1eac..d6d9e0cea 100644
--- a/kornia/morphology/morphology.py
+++ b/kornia/morphology/morphology.py
@@ -5,16 +5,33 @@ import torch.nn.functional as F
# Dilation
-def dilation(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def dilation(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the dilated image applying the same kernel in each channel.
The kernel must have 2 dimensions.
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Dilated image with shape :math:`(B, C, H, W)`.
@@ -44,27 +61,54 @@ def dilation(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[i
# pad
pad_e: List[int] = [origin[1], se_w - origin[1] - 1, origin[0], se_h - origin[0] - 1]
- output: torch.Tensor = F.pad(tensor, pad_e, mode='constant', value=0.0)
+ if border_type == 'geodesic':
+ border_value = -max_val
+ border_type = 'constant'
+ output: torch.Tensor = F.pad(tensor, pad_e, mode=border_type, value=border_value)
# computation
+ if structuring_element is None:
+ neighborhood = torch.zeros_like(kernel)
+ neighborhood[kernel == 0] = -max_val
+ else:
+ neighborhood = structuring_element
+ neighborhood[kernel == 0] = -max_val
+
output = output.unfold(2, se_h, 1).unfold(3, se_w, 1)
- output, _ = torch.max(output + kernel.flip((0, 1)), 4)
+ output, _ = torch.max(output + neighborhood.flip((0, 1)), 4)
output, _ = torch.max(output, 4)
return output
# Erosion
-def erosion(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def erosion(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the eroded image applying the same kernel in each channel.
The kernel must have 2 dimensions.
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Eroded image with shape :math:`(B, C, H, W)`.
@@ -94,27 +138,54 @@ def erosion(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[in
# pad
pad_e: List[int] = [origin[1], se_w - origin[1] - 1, origin[0], se_h - origin[0] - 1]
- output: torch.Tensor = F.pad(tensor, pad_e, mode='constant', value=1.0)
+ if border_type == 'geodesic':
+ border_value = max_val
+ border_type = 'constant'
+ output: torch.Tensor = F.pad(tensor, pad_e, mode=border_type, value=border_value)
# computation
+ if structuring_element is None:
+ neighborhood = torch.zeros_like(kernel)
+ neighborhood[kernel == 0] = -max_val
+ else:
+ neighborhood = structuring_element
+ neighborhood[kernel == 0] = -max_val
+
output = output.unfold(2, se_h, 1).unfold(3, se_w, 1)
- output, _ = torch.min(output - kernel, 4)
+ output, _ = torch.min(output - neighborhood, 4)
output, _ = torch.min(output, 4)
return output
# Opening
-def opening(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def opening(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the opened image, (that means, dilation after an erosion) applying the same kernel in each channel.
The kernel must have 2 dimensions.
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Opened image with shape :math:`(B, C, H, W)`.
@@ -137,20 +208,53 @@ def opening(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[in
if len(kernel.shape) != 2:
raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))
- return dilation(erosion(tensor, kernel, origin=origin), kernel, origin=origin)
+ return dilation(
+ erosion(
+ tensor,
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ ),
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ )
# Closing
-def closing(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def closing(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the closed image, (that means, erosion after a dilation) applying the same kernel in each channel.
The kernel must have 2 dimensions.
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Closed image with shape :math:`(B, C, H, W)`.
@@ -173,11 +277,35 @@ def closing(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[in
if len(kernel.shape) != 2:
raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))
- return erosion(dilation(tensor, kernel, origin=origin), kernel, origin=origin)
+ return erosion(
+ dilation(
+ tensor,
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ ),
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ )
# Morphological Gradient
-def gradient(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def gradient(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the morphological gradient of an image.
That means, (dilation - erosion) applying the same kernel in each channel.
@@ -185,9 +313,18 @@ def gradient(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[i
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Gradient image with shape :math:`(B, C, H, W)`.
@@ -198,11 +335,35 @@ def gradient(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[i
>>> gradient_img = gradient(tensor, kernel)
"""
- return dilation(tensor, kernel, origin=origin) - erosion(tensor, kernel, origin=origin)
+ return dilation(
+ tensor,
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ ) - erosion(
+ tensor,
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ )
# Top Hat
-def top_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def top_hat(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the top hat tranformation of an image.
That means, (image - opened_image) applying the same kernel in each channel.
@@ -212,9 +373,18 @@ def top_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[in
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Top hat transformated image with shape :math:`(B, C, H, W)`.
@@ -237,11 +407,27 @@ def top_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[in
if len(kernel.shape) != 2:
raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))
- return tensor - opening(tensor, kernel, origin=origin)
+ return tensor - opening(
+ tensor,
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ )
# Bottom Hat
-def bottom_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List[int]] = None) -> torch.Tensor:
+def bottom_hat(
+ tensor: torch.Tensor,
+ kernel: torch.Tensor,
+ structuring_element: Optional[torch.Tensor] = None,
+ origin: Optional[List[int]] = None,
+ border_type: str = 'geodesic',
+ border_value: float = 0.0,
+ max_val: float = 1e4,
+) -> torch.Tensor:
r"""Returns the bottom hat tranformation of an image.
That means, (closed_image - image) applying the same kernel in each channel.
@@ -251,9 +437,18 @@ def bottom_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List
Args:
tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
- kernel (torch.Tensor): Structuring element with shape :math:`(k_x, k_y)`.
- origin (List[int], Tuple[int, int]): Origin of the structuring element. Default is None and uses the center of
- the structuring element as origin (rounding towards zero).
+ kernel (torch.Tensor): Positions of non-infinite elements of a flat structuring element. Non-zero values give
+ the set of neighbors of the center over which the operation is applied. Its shape is :math:`(k_x, k_y)`.
+ For full structural elements use torch.ones_like(structural_element).
+ structuring_element (torch.Tensor, optional): Structuring element used for the grayscale dilation. It may be a
+ non-flat structuring element.
+ origin (List[int], optional): Origin of the structuring element. Default is None and uses the center of
+ the structuring element as origin (rounding towards zero).
+ border_type (str, optional): It determines how the image borders are handled, where border_value is the value
+ when border_type is equal to ā€˜constant’. Default is ā€˜geodesic’ which ignores the values that are outside the
+ image when applying the operation.
+ border_value (float, optional): Value to fill past edges of input if border_type is ā€˜constant’. Default is 0.0.
+ max_val (float, optional): The value of the infinite elements in the kernel.
Returns:
torch.Tensor: Top hat transformated image with shape :math:`(B, C, H, W)`.
@@ -276,4 +471,15 @@ def bottom_hat(tensor: torch.Tensor, kernel: torch.Tensor, origin: Optional[List
if len(kernel.shape) != 2:
raise ValueError("Kernel size must have 2 dimensions. Got {}".format(kernel.dim()))
- return closing(tensor, kernel, origin=origin) - tensor
+ return (
+ closing(
+ tensor,
+ kernel=kernel,
+ structuring_element=structuring_element,
+ origin=origin,
+ border_type=border_type,
+ border_value=border_value,
+ max_val=max_val,
+ )
+ - tensor
+ )
diff --git a/test/morphology/test_bottom_hat.py b/test/morphology/test_bottom_hat.py
index caaa307a4..b5dce0821 100644
--- a/test/morphology/test_bottom_hat.py
+++ b/test/morphology/test_bottom_hat.py
@@ -3,10 +3,10 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import bottom_hat
+from kornia.morphology import bottom_hat
-class TestBlackHat:
+class TestBottomHat:
def test_smoke(self, device, dtype):
kernel = torch.rand(3, 3, device=device, dtype=dtype)
assert kernel is not None
@@ -18,15 +18,29 @@ class TestBlackHat:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert bottom_hat(img, krnl).shape == shape
- def test_value(self, device, dtype):
- input = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ def test_kernel(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.2, 0.0, 0.5], [0.0, 0.4, 0.0], [0.3, 0.0, 0.6]], device=device, dtype=dtype)[
None, None, :, :
]
- assert_allclose(bottom_hat(input, kernel), expected)
+ assert_allclose(bottom_hat(tensor, kernel), expected)
+
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[0.2, 0.0, 0.5], [0.0, 0.4, 0.0], [0.3, 0.0, 0.6]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ bottom_hat(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
def test_exception(self, device, dtype):
input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
diff --git a/test/morphology/test_closing.py b/test/morphology/test_closing.py
index 45f3d8f32..c77c4fe07 100644
--- a/test/morphology/test_closing.py
+++ b/test/morphology/test_closing.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import closing
+from kornia.morphology import closing
class TestClosing:
@@ -18,16 +18,30 @@ class TestClosing:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert closing(img, krnl).shape == shape
- def test_value(self, device, dtype):
+ def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.7, 1.0, 0.8], [0.7, 0.7, 0.8], [0.7, 0.9, 0.8]], device=device, dtype=dtype)[
None, None, :, :
]
assert_allclose(closing(tensor, kernel), expected)
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[0.7, 1.0, 0.8], [0.7, 0.7, 0.8], [0.7, 0.9, 0.8]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ closing(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
+
def test_exception(self, device, dtype):
tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
kernel = torch.ones(3, 3, device=device, dtype=dtype)
diff --git a/test/morphology/test_dilation.py b/test/morphology/test_dilation.py
index 90f778a54..3709032d6 100644
--- a/test/morphology/test_dilation.py
+++ b/test/morphology/test_dilation.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import dilation
+from kornia.morphology import dilation
class TestDilate:
@@ -18,16 +18,30 @@ class TestDilate:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert dilation(img, krnl).shape == shape
- def test_value(self, device, dtype):
+ def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[1.0, 1.0, 1.0], [0.7, 1.0, 0.8], [0.9, 0.9, 0.9]], device=device, dtype=dtype)[
None, None, :, :
]
assert_allclose(dilation(tensor, kernel), expected)
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[1.0, 1.0, 1.0], [0.7, 1.0, 0.8], [0.9, 0.9, 0.9]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ dilation(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
+
def test_exception(self, device, dtype):
tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
kernel = torch.ones(3, 3, device=device, dtype=dtype)
diff --git a/test/morphology/test_erosion.py b/test/morphology/test_erosion.py
index 333260c9c..a96a04f85 100644
--- a/test/morphology/test_erosion.py
+++ b/test/morphology/test_erosion.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import erosion
+from kornia.morphology import erosion
class TestErode:
@@ -18,16 +18,30 @@ class TestErode:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert erosion(img, krnl).shape == shape
- def test_value(self, device, dtype):
+ def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.5, 0.3, 0.3], [0.3, 0.3, 0.2], [0.4, 0.2, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
assert_allclose(erosion(tensor, kernel), expected)
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[0.5, 0.3, 0.3], [0.3, 0.3, 0.2], [0.4, 0.2, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ erosion(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
+
def test_exception(self, device, dtype):
tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
kernel = torch.ones(3, 3, device=device, dtype=dtype)
diff --git a/test/morphology/test_gradient.py b/test/morphology/test_gradient.py
index 2e8985079..f12c0bf72 100644
--- a/test/morphology/test_gradient.py
+++ b/test/morphology/test_gradient.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import gradient
+from kornia.morphology import gradient
class TestGradient:
@@ -18,16 +18,30 @@ class TestGradient:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert gradient(img, krnl).shape == shape
- def test_value(self, device, dtype):
+ def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.5, 0.7, 0.7], [0.4, 0.7, 0.6], [0.5, 0.7, 0.7]], device=device, dtype=dtype)[
None, None, :, :
]
assert_allclose(gradient(tensor, kernel), expected)
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[0.5, 0.7, 0.7], [0.4, 0.7, 0.6], [0.5, 0.7, 0.7]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ gradient(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
+
def test_exception(self, device, dtype):
tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
kernel = torch.ones(3, 3, device=device, dtype=dtype)
diff --git a/test/morphology/test_opening.py b/test/morphology/test_opening.py
index 6eed1974a..0940de775 100644
--- a/test/morphology/test_opening.py
+++ b/test/morphology/test_opening.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import opening
+from kornia.morphology import opening
class TestOpening:
@@ -18,16 +18,30 @@ class TestOpening:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert opening(img, krnl).shape == shape
- def test_value(self, device, dtype):
+ def test_kernel(self, device, dtype):
tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.5, 0.5, 0.3], [0.5, 0.3, 0.3], [0.4, 0.4, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
assert_allclose(opening(tensor, kernel), expected)
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -10.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[0.5, 0.5, 0.3], [0.5, 0.3, 0.3], [0.4, 0.4, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ opening(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
+
def test_exception(self, device, dtype):
tensor = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
kernel = torch.ones(3, 3, device=device, dtype=dtype)
diff --git a/test/morphology/test_top_hat.py b/test/morphology/test_top_hat.py
index 6780554fd..a275bb841 100644
--- a/test/morphology/test_top_hat.py
+++ b/test/morphology/test_top_hat.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-from kornia.morphology.morphology import top_hat
+from kornia.morphology import top_hat
class TestTopHat:
@@ -18,15 +18,29 @@ class TestTopHat:
krnl = torch.ones(kernel, device=device, dtype=dtype)
assert top_hat(img, krnl).shape == shape
- def test_value(self, device, dtype):
- input = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ def test_kernel(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
None, None, :, :
]
- kernel = torch.tensor([[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype)
+ kernel = torch.tensor([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.0, 0.5, 0.0], [0.2, 0.0, 0.5], [0.0, 0.5, 0.0]], device=device, dtype=dtype)[
None, None, :, :
]
- assert_allclose(top_hat(input, kernel), expected)
+ assert_allclose(top_hat(tensor, kernel), expected)
+
+ def test_structural_element(self, device, dtype):
+ tensor = torch.tensor([[0.5, 1.0, 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ structural_element = torch.tensor(
+ [[-1.0, 0.0, -1.0], [0.0, 0.0, 0.0], [-1.0, 0.0, -1.0]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[0.0, 0.5, 0.0], [0.2, 0.0, 0.5], [0.0, 0.5, 0.0]], device=device, dtype=dtype)[
+ None, None, :, :
+ ]
+ assert_allclose(
+ top_hat(tensor, torch.ones_like(structural_element), structuring_element=structural_element), expected
+ )
def test_exception(self, device, dtype):
input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)