torchgeometry.image

get_gaussian_kernel(ksize: int, sigma: float) → torch.Tensor[source]

Function that returns Gaussian filter coefficients.

Parameters:
  • ksize (int) – filter size. It should be odd and positive.
  • sigma (float) – gaussian standard deviation.
Returns:

1D tensor with gaussian filter coefficients.

Return type:

Tensor

Shape:
  • Output: \((ksize,)\)

Examples:

>>> tgm.image.get_gaussian_kernel(3, 2.5)
tensor([0.3243, 0.3513, 0.3243])

>>> tgm.image.get_gaussian_kernel(5, 1.5)
tensor([0.1201, 0.2339, 0.2921, 0.2339, 0.1201])
get_gaussian_kernel2d(ksize: Tuple[int, int], sigma: Tuple[float, float]) → torch.Tensor[source]

Function that returns Gaussian filter matrix coefficients.

Parameters:
  • ksize (Tuple[int, int]) – filter sizes in the x and y direction. Sizes should be odd and positive.
  • sigma (Tuple[int, int]) – gaussian standard deviation in the x and y direction.
Returns:

2D tensor with gaussian filter matrix coefficients.

Return type:

Tensor

Shape:
  • Output: \((ksize_x, ksize_y)\)

Examples:

>>> tgm.image.get_gaussian_kernel2d((3, 3), (1.5, 1.5))
tensor([[0.0947, 0.1183, 0.0947],
        [0.1183, 0.1478, 0.1183],
        [0.0947, 0.1183, 0.0947]])

>>> tgm.image.get_gaussian_kernel2d((3, 5), (1.5, 1.5))
tensor([[0.0370, 0.0720, 0.0899, 0.0720, 0.0370],
        [0.0462, 0.0899, 0.1123, 0.0899, 0.0462],
        [0.0370, 0.0720, 0.0899, 0.0720, 0.0370]])
gaussian_blur(src: torch.Tensor, kernel_size: Tuple[int, int], sigma: Tuple[float, float]) → torch.Tensor[source]

Function that blurs a tensor using a Gaussian filter.

The operator smooths the given tensor with a gaussian kernel by convolving it to each channel. It suports batched operation.

Parameters:
  • src (Tensor) – the input tensor.
  • kernel_size (Tuple[int, int]) – the size of the kernel.
  • sigma (Tuple[float, float]) – the standard deviation of the kernel.
Returns:

the blurred tensor.

Return type:

Tensor

Shape:
  • Input: \((B, C, H, W)\)
  • Output: \((B, C, H, W)\)

Examples:

>>> input = torch.rand(2, 4, 5, 5)
>>> output = tgm.image.gaussian_blur(input, (3, 3), (1.5, 1.5))
class GaussianBlur(kernel_size: Tuple[int, int], sigma: Tuple[float, float])[source]

Creates an operator that blurs a tensor using a Gaussian filter.

The operator smooths the given tensor with a gaussian kernel by convolving it to each channel. It suports batched operation.

Parameters:
  • kernel_size (Tuple[int, int]) – the size of the kernel.
  • sigma (Tuple[float, float]) – the standard deviation of the kernel.
Returns:

the blurred tensor.

Return type:

Tensor

Shape:
  • Input: \((B, C, H, W)\)
  • Output: \((B, C, H, W)\)

Examples:

>>> input = torch.rand(2, 4, 5, 5)
>>> gauss = tgm.image.GaussianBlur((3, 3), (1.5, 1.5))
>>> output = gauss(input)  # 2x4x5x5