Kernels#

Kernel generators, useful with the Filtering API or to inspect what a filter applies.

kornia.filters.get_gaussian_kernel1d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#

Return Gaussian filter coefficients.

Parameters:
  • kernel_size (int) – filter size. It should be odd and positive.

  • sigma (float | Tensor) – gaussian standard deviation.

  • force_even (bool, optional) – overrides requirement for odd kernel size. Default: False

  • device (Optional[device], optional) – This value will be used if sigma is a float. Device desired to compute. Default: None

  • dtype (Optional[dtype], optional) – This value will be used if sigma is a float. Dtype desired for compute. Default: None

Return type:

Tensor

Returns:

gaussian filter coefficients with shape \((B, \text{kernel_size})\).

Examples

>>> get_gaussian_kernel1d(3, 2.5)
tensor([[0.3243, 0.3513, 0.3243]])
>>> get_gaussian_kernel1d(5, 1.5)
tensor([[0.1201, 0.2339, 0.2921, 0.2339, 0.1201]])
>>> get_gaussian_kernel1d(5, torch.tensor([[1.5], [0.7]]))
tensor([[0.1201, 0.2339, 0.2921, 0.2339, 0.1201],
        [0.0096, 0.2054, 0.5699, 0.2054, 0.0096]])
kornia.filters.get_gaussian_erf_kernel1d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#

Return Gaussian filter coefficients by interpolating the error function.

Adapted from: Project-MONAI/MONAI.

Parameters:
  • kernel_size (int) – filter size. It should be odd and positive.

  • sigma (float | Tensor) – gaussian standard deviation. If a tensor, should be in a shape \((B, 1)\)

  • force_even (bool, optional) – overrides requirement for odd kernel size. Default: False

  • device (Optional[device], optional) – This value will be used if sigma is a float. Device desired to compute. Default: None

  • dtype (Optional[dtype], optional) – This value will be used if sigma is a float. Dtype desired for compute. Default: None

Return type:

Tensor

Returns:

1D tensor with gaussian filter coefficients. Shape \((B, \text{kernel_size})\)

Examples

>>> get_gaussian_erf_kernel1d(3, 2.0)
tensor([[0.3195, 0.3611, 0.3195]])
>>> get_gaussian_erf_kernel1d(5, 1.5)
tensor([[0.1226, 0.2331, 0.2887, 0.2331, 0.1226]])
>>> get_gaussian_erf_kernel1d(5, torch.tensor([[1.5], [2.1]]))
tensor([[0.1226, 0.2331, 0.2887, 0.2331, 0.1226],
        [0.1574, 0.2198, 0.2456, 0.2198, 0.1574]])
kornia.filters.get_gaussian_discrete_kernel1d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#

Return Gaussian filter coefficients based on the modified Bessel functions.

Adapted from: Project-MONAI/MONAI.

Parameters:
  • kernel_size (int) – filter size. It should be odd and positive.

  • sigma (float | Tensor) – gaussian standard deviation. If a tensor, should be in a shape \((B, 1)\)

  • force_even (bool, optional) – overrides requirement for odd kernel size. Default: False

  • device (Optional[device], optional) – This value will be used if sigma is a float. Device desired to compute. Default: None

  • dtype (Optional[dtype], optional) – This value will be used if sigma is a float. Dtype desired for compute. Default: None

Return type:

Tensor

Returns:

1D tensor with gaussian filter coefficients. With shape \((B, \text{kernel_size})\)

Examples

>>> get_gaussian_discrete_kernel1d(3, 2.5)
tensor([[0.3235, 0.3531, 0.3235]])
>>> get_gaussian_discrete_kernel1d(5, 1.5)
tensor([[0.1096, 0.2323, 0.3161, 0.2323, 0.1096]])
>>> get_gaussian_discrete_kernel1d(5, torch.tensor([[1.5],[2.4]]))
tensor([[0.1096, 0.2323, 0.3161, 0.2323, 0.1096],
        [0.1635, 0.2170, 0.2389, 0.2170, 0.1635]])
kornia.filters.get_gaussian_kernel2d(kernel_size, sigma, force_even=False, *, device=None, dtype=None)[source]#

Return Gaussian filter matrix coefficients.

Parameters:
  • kernel_size (tuple[int, int] | int) – filter sizes in the y and x direction. Sizes should be odd and positive.

  • sigma (tuple[float, float] | Tensor) – gaussian standard deviation in the y and x.

  • force_even (bool, optional) – overrides requirement for odd kernel size. Default: False

  • device (Optional[device], optional) – This value will be used if sigma is a float. Device desired to compute. Default: None

  • dtype (Optional[dtype], optional) – This value will be used if sigma is a float. Dtype desired for compute. Default: None

Return type:

Tensor

Returns:

2D tensor with gaussian filter matrix coefficients.

Shape:
  • Output: \((B, \text{kernel_size}_x, \text{kernel_size}_y)\)

Examples

>>> get_gaussian_kernel2d((5, 5), (1.5, 1.5))
tensor([[[0.0144, 0.0281, 0.0351, 0.0281, 0.0144],
         [0.0281, 0.0547, 0.0683, 0.0547, 0.0281],
         [0.0351, 0.0683, 0.0853, 0.0683, 0.0351],
         [0.0281, 0.0547, 0.0683, 0.0547, 0.0281],
         [0.0144, 0.0281, 0.0351, 0.0281, 0.0144]]])
>>> 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]]])
>>> get_gaussian_kernel2d((5, 5), torch.tensor([[1.5, 1.5]]))
tensor([[[0.0144, 0.0281, 0.0351, 0.0281, 0.0144],
         [0.0281, 0.0547, 0.0683, 0.0547, 0.0281],
         [0.0351, 0.0683, 0.0853, 0.0683, 0.0351],
         [0.0281, 0.0547, 0.0683, 0.0547, 0.0281],
         [0.0144, 0.0281, 0.0351, 0.0281, 0.0144]]])
kornia.filters.get_hanning_kernel1d(kernel_size, device=None, dtype=None)[source]#

Return Hanning (also known as Hann) kernel, used in signal processing and KCF tracker.

\[\begin{split}w(n) = 0.5 - 0.5cos\\left(\\frac{2\\pi{n}}{M-1}\\right) \\qquad 0 \\leq n \\leq M-1\end{split}\]

See further in numpy docs https://numpy.org/doc/stable/reference/generated/numpy.hanning.html

Parameters:
  • kernel_size (int) – The size the of the kernel. It should be positive.

  • device (Optional[device], optional) – tensor device desired to create the kernel Default: None

  • dtype (Optional[dtype], optional) – tensor dtype desired to create the kernel Default: None

Returns:

(text{kernel_size}) .. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right)

Return type:

1D tensor with Hanning filter coefficients. Shape math

Examples

>>> get_hanning_kernel1d(4)
tensor([0.0000, 0.7500, 0.7500, 0.0000])
kornia.filters.get_hanning_kernel2d(kernel_size, device=None, dtype=None)[source]#

Return 2d Hanning kernel, used in signal processing and KCF tracker.

Parameters:
  • kernel_size (tuple[int, int] | int) – The size of the kernel for the filter. It should be positive.

  • device (Union[str, device, None], optional) – tensor device desired to create the kernel Default: None

  • dtype (Optional[dtype], optional) – tensor dtype desired to create the kernel Default: None

Returns:

math:(text{kernel_size[0], kernel_size[1]}) .. math:: w(n) = 0.5 - 0.5cos\left(\frac{2\pi{n}}{M-1}\right)

Return type:

2D tensor with Hanning filter coefficients. Shape

kornia.filters.get_laplacian_kernel1d(kernel_size, *, device=None, dtype=torch.float32)[source]#

Return the coefficients of a 1D Laplacian filter.

Parameters:
  • kernel_size (int) – filter size. It should be odd and positive.

  • device (Optional[device], optional) – tensor device desired to create the kernel Default: None

  • dtype (dtype, optional) – tensor dtype desired to create the kernel Default: torch.float32

Return type:

Tensor

Returns:

1D tensor with laplacian filter coefficients.

Shape:
  • Output: math:(text{kernel_size})

Examples

>>> get_laplacian_kernel1d(3)
tensor([ 1., -2.,  1.])
>>> get_laplacian_kernel1d(5)
tensor([ 1.,  1., -4.,  1.,  1.])
kornia.filters.get_laplacian_kernel2d(kernel_size, *, device=None, dtype=torch.float32)[source]#

Return Gaussian filter matrix coefficients.

Parameters:
  • kernel_size (tuple[int, int] | int) – filter size should be odd.

  • device (Optional[device], optional) – tensor device desired to create the kernel Default: None

  • dtype (dtype, optional) – tensor dtype desired to create the kernel Default: torch.float32

Return type:

Tensor

Returns:

2D tensor with laplacian filter matrix coefficients.

Shape:
  • Output: \((\text{kernel_size}_x, \text{kernel_size}_y)\)

Examples

>>> get_laplacian_kernel2d(3)
tensor([[ 1.,  1.,  1.],
        [ 1., -8.,  1.],
        [ 1.,  1.,  1.]])
>>> get_laplacian_kernel2d(5)
tensor([[  1.,   1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.,   1.],
        [  1.,   1., -24.,   1.,   1.],
        [  1.,   1.,   1.,   1.,   1.],
        [  1.,   1.,   1.,   1.,   1.]])
kornia.filters.get_motion_kernel2d(kernel_size, angle, direction=0.0, mode='nearest')[source]#

Return 2D motion blur filter.

Parameters:
  • kernel_size (int) – motion kernel width and height. It should be odd and positive.

  • angle (Tensor | float) – angle of the motion blur in degrees (anti-clockwise rotation).

  • direction (Tensor | float, optional) – forward/backward direction of the motion blur. Lower values towards -1.0 will point the motion blur towards the back (with angle provided via angle), while higher values towards 1.0 will point the motion blur forward. A value of 0.0 leads to a uniformly (but still angled) motion blur. Default: 0.0

  • mode (str, optional) – interpolation mode for rotating the kernel. 'bilinear' or 'nearest'. Default: "nearest"

Return type:

Tensor

Returns:

The motion blur kernel of shape \((B, k_\text{size}, k_\text{size})\).

Examples

>>> get_motion_kernel2d(5, 0., 0.)
tensor([[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.2000, 0.2000, 0.2000, 0.2000, 0.2000],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]])
>>> get_motion_kernel2d(3, 215., -0.5)
tensor([[[0.0000, 0.0000, 0.1667],
         [0.0000, 0.3333, 0.0000],
         [0.5000, 0.0000, 0.0000]]])