Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

laplacian tests pass

pulkit-khandelwal55dcedb43cd5
diff --git a/test/test_image.py b/test/test_image.py
index a8e378df9..53c9105d0 100644
--- a/test/test_image.py
+++ b/test/test_image.py
@@ -57,6 +57,7 @@ def test_get_laplacian_kernel(window_size):
assert kernel.shape == (window_size,)
assert kernel.sum().item() == pytest.approx(1.0)
+
@pytest.mark.parametrize("window_size", 5)
def test_get_laplacian_kernel2d(window_size):
kernel = image.get_laplacian_kernel2d(window_size)
diff --git a/torchgeometry/image/laplacian.py b/torchgeometry/image/laplacian.py
index 88924b3bb..0ac3b2d92 100644
--- a/torchgeometry/image/laplacian.py
+++ b/torchgeometry/image/laplacian.py
@@ -1,4 +1,4 @@
-#from typing import Tuple
+from typing import Tuple
import math
import torch
@@ -7,40 +7,40 @@ from torch.nn.functional import conv2d
def laplacian(window_size):
- r"""One could also use the Laplacian of Gaussian formula
- to design the filter.
- """
+ r"""One could also use the Laplacian of Gaussian formula
+ to design the filter.
+ """
- laplacian = torch.ones(window_size)
- laplacian[int(math.floor(window_size/2))] = 1 - window_size
- return laplacian
+ laplacian = torch.ones(window_size)
+ laplacian[int(math.floor(window_size / 2))] = 1 - window_size
+ return laplacian
def get_laplacian_kernel(ksize: int):
- r"""Function that returns the coefficients of a 1D Laplacian filter
+ r"""Function that returns the coefficients of a 1D Laplacian filter
- Args:
- ksize (int): filter size. It should be odd and positive.
+ Args:
+ ksize (int): filter size. It should be odd and positive.
- Returns:
- Tensor (float): 1D tensor with laplacian filter coefficients.
+ Returns:
+ Tensor (float): 1D tensor with laplacian filter coefficients.
- Shape:
- - Output: math:`(ksize, 0)`
+ Shape:
+ - Output: math:`(ksize, 0)`
- Examples::
- >>> tgm.image.get_laplacian_kernel(3)
- tensor([ 1., -2., 1.])
+ Examples::
+ >>> tgm.image.get_laplacian_kernel(3)
+ tensor([ 1., -2., 1.])
- >>> tgm.image.get_laplacian_kernel(5)
- tensor([ 1., 1., -4., 1., 1.])
+ >>> tgm.image.get_laplacian_kernel(5)
+ tensor([ 1., 1., -4., 1., 1.])
- """
- if not isinstance(ksize, int) or ksize % 2 == 0 or ksize <= 0:
- raise TypeError("ksize must be an odd positive integer. Got {}"
- .format(ksize))
- window_1d: torch.Tensor = laplacian(ksize)
- return window_1d
+ """
+ if not isinstance(ksize, int) or ksize % 2 == 0 or ksize <= 0:
+ raise TypeError("ksize must be an odd positive integer. Got {}"
+ .format(ksize))
+ window_1d: torch.Tensor = laplacian(ksize)
+ return window_1d
def get_laplacian_kernel2d(ksize: int) -> torch.Tensor:
@@ -75,13 +75,12 @@ def get_laplacian_kernel2d(ksize: int) -> torch.Tensor:
.format(ksize))
kernel = torch.ones((ksize, ksize))
- mid = int(math.floor((ksize/2)))
+ mid = int(math.floor((ksize / 2)))
kernel[mid, mid] = 1 - math.pow(ksize, 2)
kernel_2d: torch.Tensor = kernel
return kernel_2d
-
class LaplacianBlur(nn.Module):
r"""Creates an operator that blurs a tensor using a Laplacian filter.
@@ -145,7 +144,7 @@ class LaplacianBlur(nn.Module):
def laplacian_blur(src: torch.Tensor,
- kernel_size: int) -> torch.Tensor:
+ kernel_size: int) -> torch.Tensor:
r"""Function that blurs a tensor using a Laplacian filter.
The operator smooths the given tensor with a laplacian kernel by convolving