Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add auto-image docs in kornia.filters (#1129)

Edgar Riba8719e064ef2d
diff --git a/docs/.gitignore b/docs/.gitignore
index 92c1b7060..98a840bf4 100644
--- a/docs/.gitignore
+++ b/docs/.gitignore
@@ -2,3 +2,4 @@ build/doctest
build/html/.buildinfo
build/html/objects.inv
source/tutorials/*
+source/_static/img/*.png
diff --git a/docs/generate_example_images.py b/docs/generate_example_images.py
index 69d6c1633..2ac278465 100644
--- a/docs/generate_example_images.py
+++ b/docs/generate_example_images.py
@@ -35,6 +35,7 @@ def main():
BASE_IMAGE_URL2: str = "https://raw.githubusercontent.com/kornia/data/main/simba.png" # color
BASE_IMAGE_URL3: str = "https://raw.githubusercontent.com/kornia/data/main/girona.png" # enhance
BASE_IMAGE_URL4: str = "https://raw.githubusercontent.com/kornia/data/main/baby_giraffe.png" # morphology
+ BASE_IMAGE_URL5: str = "https://raw.githubusercontent.com/kornia/data/main/persistencia_memoria.jpg" # filters
OUTPUT_PATH = Path(__file__).absolute().parent / "source/_static/img"
os.makedirs(OUTPUT_PATH, exist_ok=True)
@@ -43,6 +44,7 @@ def main():
img2 = read_img_from_url(BASE_IMAGE_URL2, img1.shape[-2:])
img3 = read_img_from_url(BASE_IMAGE_URL3, img1.shape[-2:])
img4 = read_img_from_url(BASE_IMAGE_URL4)
+ img5 = read_img_from_url(BASE_IMAGE_URL5, (234, 320))
# TODO: make this more generic for modules out of kornia.augmentation
# Dictionary containing the transforms to generate the sample images:
@@ -215,6 +217,46 @@ def main():
sig = f"{fn_name}({', '.join([str(a) for a in args])})"
print(f"Generated image example for {fn_name}. {sig}")
+ # korna.filters module
+ mod = importlib.import_module("kornia.filters")
+ kernel = torch.tensor([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
+ transforms: dict = {
+ "box_blur": (((5, 5),), 1),
+ "median_blur": (((5, 5),), 1),
+ "gaussian_blur2d": (((5, 5), (1.5, 1.5)), 1),
+ "motion_blur": ((5, 90.0, 1.0), 1),
+ "max_blur_pool2d": ((5,), 1),
+ "blur_pool2d": ((5,), 1),
+ "unsharp_mask": (((5, 5), (1.5, 1.5)), 1),
+ "laplacian": ((5,), 1),
+ "sobel": ((), 1),
+ "spatial_gradient": ((), 1),
+ "canny": ((), 1),
+ }
+ # ITERATE OVER THE TRANSFORMS
+ for fn_name, (args, num_samples) in transforms.items():
+ img_in = img5.repeat(num_samples, 1, 1, 1)
+ args_in = (img_in, *args)
+ # import function and apply
+ fn = getattr(mod, fn_name)
+ out = fn(*args_in)
+ if fn_name in ("max_blur_pool2d", "blur_pool2d"):
+ out = K.geometry.resize(out, img_in.shape[-2:])
+ if fn_name == "canny":
+ out = out[1].repeat(1, 3, 1, 1)
+ if isinstance(out, torch.Tensor):
+ out = out.clamp(min=0.0, max=1.0)
+ if fn_name in ("laplacian", "sobel", "spatial_gradient", "canny"):
+ out = K.enhance.normalize_min_max(out)
+ if fn_name == "spatial_gradient":
+ out = out.permute(2, 1, 0, 3, 4).squeeze()
+ # save the output image
+ out = torch.cat([img_in[0], *[out[i] for i in range(out.size(0))]], dim=-1)
+ out_np = K.utils.tensor_to_image((out * 255.0).byte())
+ cv2.imwrite(str(OUTPUT_PATH / f"{fn_name}.png"), out_np)
+ sig = f"{fn_name}({', '.join([str(a) for a in args])})"
+ print(f"Generated image example for {fn_name}. {sig}")
+
if __name__ == "__main__":
main()
diff --git a/docs/source/contrib.rst b/docs/source/contrib.rst
index 10de0a6d6..52004ac4e 100644
--- a/docs/source/contrib.rst
+++ b/docs/source/contrib.rst
@@ -4,10 +4,8 @@ kornia.contrib
.. currentmodule:: kornia.contrib
.. autofunction:: extract_tensor_patches
-.. autofunction:: max_blur_pool2d
Module
------
.. autoclass:: ExtractTensorPatches
-.. autoclass:: MaxBlurPool2d
diff --git a/docs/source/filters.rst b/docs/source/filters.rst
index 9856e9909..6dba82a30 100644
--- a/docs/source/filters.rst
+++ b/docs/source/filters.rst
@@ -8,14 +8,29 @@ The functions in this sections perform various image filtering operations.
Blurring
--------
-.. autofunction:: filter2d
-.. autofunction:: filter3d
+.. autofunction:: blur_pool2d
.. autofunction:: box_blur
-.. autofunction:: median_blur
.. autofunction:: gaussian_blur2d
+.. autofunction:: max_blur_pool2d
+.. autofunction:: median_blur
.. autofunction:: motion_blur
.. autofunction:: unsharp_mask
+Edge detection
+--------------
+
+.. autofunction:: canny
+.. autofunction:: laplacian
+.. autofunction:: sobel
+.. autofunction:: spatial_gradient
+.. autofunction:: spatial_gradient3d
+
+Filtering API
+-------------
+
+.. autofunction:: filter2d
+.. autofunction:: filter3d
+
Kernels
-------
@@ -27,20 +42,12 @@ Kernels
.. autofunction:: get_laplacian_kernel2d
.. autofunction:: get_motion_kernel2d
-
-Edge detection
---------------
-
-.. autofunction:: laplacian
-.. autofunction:: sobel
-.. autofunction:: canny
-.. autofunction:: spatial_gradient
-.. autofunction:: spatial_gradient3d
-
Module
------
+.. autoclass:: BlurPool2D
.. autoclass:: BoxBlur
+.. autoclass:: MaxBlurPool2D
.. autoclass:: MedianBlur
.. autoclass:: GaussianBlur2d
.. autoclass:: Laplacian
diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.py
index 307489988..852346dae 100644
--- a/kornia/augmentation/container/augment.py
+++ b/kornia/augmentation/container/augment.py
@@ -19,8 +19,9 @@ class AugmentationSequential(ImageSequential):
r"""AugmentationSequential for handling multiple input types like inputs, masks, keypoints at once.
.. image:: https://kornia-tutorials.readthedocs.io/en/latest/_images/data_augmentation_sequential_5_1.png
-
+ :width: 49 %
.. image:: https://kornia-tutorials.readthedocs.io/en/latest/_images/data_augmentation_sequential_7_2.png
+ :width: 49 %
Args:
*args: a list of kornia augmentation modules.
diff --git a/kornia/contrib/extract_patches.py b/kornia/contrib/extract_patches.py
index fdb152f9a..700d4cd6a 100644
--- a/kornia/contrib/extract_patches.py
+++ b/kornia/contrib/extract_patches.py
@@ -36,20 +36,17 @@ class ExtractTensorPatches(nn.Module):
- a ``tuple`` of two ints -- in which case, the first `int` is used for
the height dimension, and the second `int` for the width dimension.
- Arguments:
- window_size (Union[int, Tuple[int, int]]): the size of the sliding
- window and the output patch size.
- stride (Optional[Union[int, Tuple[int, int]]]): stride of the
- sliding window. Default is 1.
- padding (Optional[Union[int, Tuple[int, int]]]): Zero-padding added to
- both side of the input. Default is 0.
+ Args:
+ window_size: the size of the sliding window and the output patch size.
+ stride: stride of the sliding window.
+ padding: Zero-padding added to both side of the input.
Shape:
- Input: :math:`(B, C, H, W)`
- Output: :math:`(B, N, C, H_{out}, W_{out})`
Returns:
- torch.Tensor: the tensor with the extracted patches.
+ the tensor with the extracted patches.
Examples:
>>> input = torch.arange(9.).view(1, 1, 3, 3)
diff --git a/kornia/filters/blur.py b/kornia/filters/blur.py
index d454b6aa1..7bdab4be5 100644
--- a/kornia/filters/blur.py
+++ b/kornia/filters/blur.py
@@ -12,6 +12,8 @@ def box_blur(
) -> torch.Tensor:
r"""Blurs an image using the box filter.
+ .. image:: _static/img/box_blur.png
+
The function smooths an image using the kernel:
.. math::
@@ -24,15 +26,14 @@ def box_blur(
\end{bmatrix}
Args:
- image (torch.Tensor): the image to blur with shape :math:`(B,C,H,W)`.
- kernel_size (Tuple[int, int]): the blurring kernel size.
- border_type (str): the padding mode to be applied before convolving.
- The expected modes are: ``'constant'``, ``'reflect'``,
- ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
- normalized (bool): if True, L1 norm of the kernel is set to 1.
+ image: the image to blur with shape :math:`(B,C,H,W)`.
+ kernel_size: the blurring kernel size.
+ border_type: the padding mode to be applied before convolving.
+ The expected modes are: ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
+ normalized: if True, L1 norm of the kernel is set to 1.
Returns:
- torch.Tensor: the blurred tensor with shape :math:`(B,C,H,W)`.
+ the blurred tensor with shape :math:`(B,C,H,W)`.
Example:
>>> input = torch.rand(2, 4, 5, 7)
@@ -61,14 +62,14 @@ class BoxBlur(nn.Module):
\end{bmatrix}
Args:
- kernel_size (Tuple[int, int]): the blurring kernel size.
- border_type (str): the padding mode to be applied before convolving.
+ kernel_size: the blurring kernel size.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
- normalized (bool): if True, L1 norm of the kernel is set to 1.
+ normalized: if True, L1 norm of the kernel is set to 1.
Returns:
- torch.Tensor: the blurred input tensor.
+ the blurred input tensor.
Shape:
- Input: :math:`(B, C, H, W)`
diff --git a/kornia/filters/blur_pool.py b/kornia/filters/blur_pool.py
index d733e04e9..fd3b1cf13 100644
--- a/kornia/filters/blur_pool.py
+++ b/kornia/filters/blur_pool.py
@@ -16,8 +16,8 @@ class BlurPool2D(nn.Module):
See :cite:`zhang2019shiftinvar` for more details.
Args:
- kernel_size (int): the kernel size for max pooling.
- stride (int): stride for pooling.
+ kernel_size: the kernel size for max pooling.
+ stride: stride for pooling.
Shape:
- Input: :math:`(B, C, H, W)`
@@ -57,13 +57,14 @@ class MaxBlurPool2D(nn.Module):
r"""Compute pools and blurs and downsample a given feature map.
Equivalent to ```nn.Sequential(nn.MaxPool2d(...), BlurPool2D(...))```
+
See :cite:`zhang2019shiftinvar` for more details.
Args:
- kernel_size (int): the kernel size for max pooling.
- stride (int): stride for pooling.
- max_pool_size (int): the kernel size for max pooling.
- ceil_mode (bool): should be true to match output size of conv2d with same kernel size.
+ kernel_size: the kernel size for max pooling.
+ stride: stride for pooling.
+ max_pool_size: the kernel size for max pooling.
+ ceil_mode: should be true to match output size of conv2d with same kernel size.
Shape:
- Input: :math:`(B, C, H, W)`
@@ -105,11 +106,15 @@ class MaxBlurPool2D(nn.Module):
def blur_pool2d(input: torch.Tensor, kernel_size: int, stride: int = 2):
r"""Compute blurs and downsample a given feature map.
+ .. image:: _static/img/blur_pool2d.png
+
+ See :class:`~kornia.filters.BlurPool2D` for details.
+
See :cite:`zhang2019shiftinvar` for more details.
Args:
- kernel_size (int): the kernel size for max pooling..
- ceil_mode (bool): should be true to match output size of conv2d with same kernel size.
+ kernel_size: the kernel size for max pooling..
+ ceil_mode: should be true to match output size of conv2d with same kernel size.
Shape:
- Input: :math:`(B, C, H, W)`
@@ -124,7 +129,7 @@ def blur_pool2d(input: torch.Tensor, kernel_size: int, stride: int = 2):
\text{kernel\_size}[1]}{\text{stride}[1]} + 1\right\rfloor
Returns:
- torch.Tensor: the transformed tensor.
+ the transformed tensor.
Note:
This function is tested against https://github.com/adobe/antialiased-cnns.
@@ -145,13 +150,15 @@ def max_blur_pool2d(
) -> torch.Tensor:
r"""Compute pools and blurs and downsample a given feature map.
- See :class:`~kornia.filters.MaxBlurPool2d` for details.
+ .. image:: _static/img/max_blur_pool2d.png
+
+ See :class:`~kornia.filters.MaxBlurPool2D` for details.
Args:
- kernel_size (int): the kernel size for max pooling.
- stride (int): stride for pooling.
- max_pool_size (int): the kernel size for max pooling.
- ceil_mode (bool): should be true to match output size of conv2d with same kernel size.
+ kernel_size: the kernel size for max pooling.
+ stride: stride for pooling.
+ max_pool_size: the kernel size for max pooling.
+ ceil_mode: should be true to match output size of conv2d with same kernel size.
Note:
This function is tested against https://github.com/adobe/antialiased-cnns.
diff --git a/kornia/filters/canny.py b/kornia/filters/canny.py
index 12d74871a..4f1a61252 100644
--- a/kornia/filters/canny.py
+++ b/kornia/filters/canny.py
@@ -23,18 +23,19 @@ def canny(
) -> Tuple[torch.Tensor, torch.Tensor]:
r"""Finds edges of the input image and filters them using the Canny algorithm.
+ .. image:: _static/img/canny.png
+
Args:
- input (torch.Tensor): input image tensor with shape :math:`(B,C,H,W)`.
- low_threshold (float): lower threshold for the hysteresis procedure. Default: 0.1.
- high_threshold (float): upper threshold for the hysteresis procedure. Default: 0.1.
- kernel_size (Tuple[int, int]): the size of the kernel for the gaussian blur.
- sigma (Tuple[float, float]): the standard deviation of the kernel for the gaussian blur.
- hysteresis (bool): if True, applies the hysteresis edge tracking.
+ input: input image tensor with shape :math:`(B,C,H,W)`.
+ low_threshold: lower threshold for the hysteresis procedure.
+ high_threshold: upper threshold for the hysteresis procedure.
+ kernel_size: the size of the kernel for the gaussian blur.
+ sigma: the standard deviation of the kernel for the gaussian blur.
+ hysteresis: if True, applies the hysteresis edge tracking.
Otherwise, the edges are divided between weak (0.5) and strong (1) edges.
- eps (float): regularization number to avoid NaN during backprop. Default: 1e-6.
+ eps: regularization number to avoid NaN during backprop.
Returns:
- Tuple[torch.Tensor, torch.Tensor]:
- the canny edge magnitudes map, shape of :math:`(B,1,H,W)`.
- the canny edge detection filtered by thresholds and hysteresis, shape of :math:`(B,1,H,W)`.
@@ -155,14 +156,14 @@ class Canny(nn.Module):
r"""Module that finds edges of the input image and filters them using the Canny algorithm.
Args:
- input (torch.Tensor): input image tensor with shape :math:`(B,C,H,W)`.
- low_threshold (float): lower threshold for the hysteresis procedure. Default: 0.1.
- high_threshold (float): upper threshold for the hysteresis procedure. Default: 0.1.
- kernel_size (Tuple[int, int]): the size of the kernel for the gaussian blur.
- sigma (Tuple[float, float]): the standard deviation of the kernel for the gaussian blur.
- hysteresis (bool): if True, applies the hysteresis edge tracking.
+ input: input image tensor with shape :math:`(B,C,H,W)`.
+ low_threshold: lower threshold for the hysteresis procedure.
+ high_threshold: upper threshold for the hysteresis procedure.
+ kernel_size: the size of the kernel for the gaussian blur.
+ sigma: the standard deviation of the kernel for the gaussian blur.
+ hysteresis: if True, applies the hysteresis edge tracking.
Otherwise, the edges are divided between weak (0.5) and strong (1) edges.
- eps (float): regularization number to avoid NaN during backprop. Default: 1e-6.
+ eps: regularization number to avoid NaN during backprop.
Returns:
Tuple[torch.Tensor, torch.Tensor]:
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index b215381b0..7c99a25ca 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -40,14 +40,14 @@ def filter2d(
that the output remains in the same shape.
Args:
- input (torch.Tensor): the input tensor with shape of
+ input: the input tensor with shape of
:math:`(B, C, H, W)`.
- kernel (torch.Tensor): the kernel to be convolved with the input
+ kernel: the kernel to be convolved with the input
tensor. The kernel shape must be :math:`(1, kH, kW)` or :math:`(B, kH, kW)`.
- border_type (str): the padding mode to be applied before convolving.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
- ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
- normalized (bool): If True, kernel will be L1 normalized.
+ ``'replicate'`` or ``'circular'``.
+ normalized: If True, kernel will be L1 normalized.
Return:
torch.Tensor: the convolved tensor of same size and numbers of channels
@@ -118,17 +118,17 @@ def filter3d(
that the output remains in the same shape.
Args:
- input (torch.Tensor): the input tensor with shape of
+ input: the input tensor with shape of
:math:`(B, C, D, H, W)`.
- kernel (torch.Tensor): the kernel to be convolved with the input
+ kernel: the kernel to be convolved with the input
tensor. The kernel shape must be :math:`(1, kD, kH, kW)` or :math:`(B, kD, kH, kW)`.
- border_type (str): the padding mode to be applied before convolving.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``,
- ``'replicate'`` or ``'circular'``. Default: ``'replicate'``.
- normalized (bool): If True, kernel will be L1 normalized.
+ ``'replicate'`` or ``'circular'``.
+ normalized: If True, kernel will be L1 normalized.
Return:
- torch.Tensor: the convolved tensor of same size and numbers of channels
+ the convolved tensor of same size and numbers of channels
as the input with shape :math:`(B, C, D, H, W)`.
Example:
diff --git a/kornia/filters/gaussian.py b/kornia/filters/gaussian.py
index 41e7ce52a..ca70f9d19 100644
--- a/kornia/filters/gaussian.py
+++ b/kornia/filters/gaussian.py
@@ -12,19 +12,21 @@ def gaussian_blur2d(
) -> torch.Tensor:
r"""Creates an operator that blurs a tensor using a Gaussian filter.
+ .. image:: _static/img/gaussian_blur2d.png
+
The operator smooths the given tensor with a gaussian kernel by convolving
it to each channel. It supports batched operation.
Arguments:
- input (torch.Tensor): the input tensor with shape :math:`(B,C,H,W)`.
- kernel_size (Tuple[int, int]): the size of the kernel.
- sigma (Tuple[float, float]): the standard deviation of the kernel.
- border_type (str): the padding mode to be applied before convolving.
+ input: the input tensor with shape :math:`(B,C,H,W)`.
+ kernel_size: the size of the kernel.
+ sigma: the standard deviation of the kernel.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
Returns:
- torch.Tensor: the blurred tensor with shape :math:`(B, C, H, W)`.
+ the blurred tensor with shape :math:`(B, C, H, W)`.
Examples:
>>> input = torch.rand(2, 4, 5, 5)
@@ -44,14 +46,14 @@ class GaussianBlur2d(nn.Module):
it to each channel. It supports batched operation.
Arguments:
- kernel_size (Tuple[int, int]): the size of the kernel.
- sigma (Tuple[float, float]): the standard deviation of the kernel.
- border_type (str): the padding mode to be applied before convolving.
+ kernel_size: the size of the kernel.
+ sigma: the standard deviation of the kernel.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
Returns:
- Tensor: the blurred tensor.
+ the blurred tensor.
Shape:
- Input: :math:`(B, C, H, W)`
diff --git a/kornia/filters/kernels.py b/kornia/filters/kernels.py
index 004a89a20..06d70b6aa 100644
--- a/kornia/filters/kernels.py
+++ b/kornia/filters/kernels.py
@@ -337,12 +337,12 @@ def get_gaussian_kernel1d(kernel_size: int, sigma: float, force_even: bool = Fal
r"""Function that returns Gaussian filter coefficients.
Args:
- kernel_size (int): filter size. It should be odd and positive.
- sigma (float): gaussian standard deviation.
- force_even (bool): overrides requirement for odd kernel size.
+ kernel_size: filter size. It should be odd and positive.
+ sigma: gaussian standard deviation.
+ force_even: overrides requirement for odd kernel size.
Returns:
- Tensor: 1D tensor with gaussian filter coefficients.
+ 1D tensor with gaussian filter coefficients.
Shape:
- Output: :math:`(\text{kernel_size})`
@@ -367,12 +367,12 @@ def get_gaussian_discrete_kernel1d(kernel_size: int, sigma: float, force_even: b
https://github.com/Project-MONAI/MONAI/blob/master/monai/networks/layers/convutils.py
Args:
- kernel_size (int): filter size. It should be odd and positive.
- sigma (float): gaussian standard deviation.
- force_even (bool): overrides requirement for odd kernel size.
+ kernel_size: filter size. It should be odd and positive.
+ sigma: gaussian standard deviation.
+ force_even: overrides requirement for odd kernel size.
Returns:
- Tensor: 1D tensor with gaussian filter coefficients.
+ 1D tensor with gaussian filter coefficients.
Shape:
- Output: :math:`(\text{kernel_size})`
@@ -397,12 +397,12 @@ def get_gaussian_erf_kernel1d(kernel_size: int, sigma: float, force_even: bool =
https://github.com/Project-MONAI/MONAI/blob/master/monai/networks/layers/convutils.py
Args:
- kernel_size (int): filter size. It should be odd and positive.
- sigma (float): gaussian standard deviation.
- force_even (bool): overrides requirement for odd kernel size.
+ kernel_size: filter size. It should be odd and positive.
+ sigma: gaussian standard deviation.
+ force_even: overrides requirement for odd kernel size.
Returns:
- Tensor: 1D tensor with gaussian filter coefficients.
+ 1D tensor with gaussian filter coefficients.
Shape:
- Output: :math:`(\text{kernel_size})`
@@ -427,14 +427,14 @@ def get_gaussian_kernel2d(
r"""Function that returns Gaussian filter matrix coefficients.
Args:
- kernel_size (Tuple[int, int]): filter sizes in the x and y direction.
+ kernel_size: 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
+ sigma: gaussian standard deviation in the x and y
direction.
- force_even (bool): overrides requirement for odd kernel size.
+ force_even: overrides requirement for odd kernel size.
Returns:
- Tensor: 2D tensor with gaussian filter matrix coefficients.
+ 2D tensor with gaussian filter matrix coefficients.
Shape:
- Output: :math:`(\text{kernel_size}_x, \text{kernel_size}_y)`
@@ -465,10 +465,10 @@ def get_laplacian_kernel1d(kernel_size: int) -> torch.Tensor:
r"""Function that returns the coefficients of a 1D Laplacian filter.
Args:
- kernel_size (int): filter size. It should be odd and positive.
+ kernel_size: filter size. It should be odd and positive.
Returns:
- Tensor (float): 1D tensor with laplacian filter coefficients.
+ 1D tensor with laplacian filter coefficients.
Shape:
- Output: math:`(\text{kernel_size})`
@@ -490,10 +490,10 @@ def get_laplacian_kernel2d(kernel_size: int) -> torch.Tensor:
r"""Function that returns Gaussian filter matrix coefficients.
Args:
- kernel_size (int): filter size should be odd.
+ kernel_size: filter size should be odd.
Returns:
- Tensor: 2D tensor with laplacian filter matrix coefficients.
+ 2D tensor with laplacian filter matrix coefficients.
Shape:
- Output: :math:`(\text{kernel_size}_x, \text{kernel_size}_y)`
@@ -524,11 +524,11 @@ def get_pascal_kernel_2d(kernel_size: int, norm: bool = True) -> torch.Tensor:
"""Generate pascal filter kernel by kernel size.
Args:
- kernel_size (int): height and width of the kernel.
- norm (bool): if to normalize the kernel or not. Default: True.
+ kernel_size: height and width of the kernel.
+ norm: if to normalize the kernel or not. Default: True.
Returns:
- torch.Tensor: kernel shaped as :math:`(kernel_size, kernel_size)`
+ kernel shaped as :math:`(kernel_size, kernel_size)`
Examples:
>>> get_pascal_kernel_2d(1)
@@ -556,11 +556,11 @@ def get_pascal_kernel_1d(kernel_size: int, norm: bool = False) -> torch.Tensor:
"""Generate Yang Hui triangle (Pascal's triangle) by a given number.
Args:
- kernel_size (int): height and width of the kernel.
- norm (bool): if to normalize the kernel or not. Default: False.
+ kernel_size: height and width of the kernel.
+ norm: if to normalize the kernel or not. Default: False.
Returns:
- torch.Tensor: kernel shaped as :math:`(kernel_size,)`
+ kernel shaped as :math:`(kernel_size,)`
Examples:
>>> get_pascal_kernel_1d(1)
diff --git a/kornia/filters/laplacian.py b/kornia/filters/laplacian.py
index f1560f041..b6150e1a6 100644
--- a/kornia/filters/laplacian.py
+++ b/kornia/filters/laplacian.py
@@ -10,19 +10,21 @@ def laplacian(
) -> torch.Tensor:
r"""Creates an operator that returns a tensor using a Laplacian filter.
+ .. image:: _static/img/laplacian.png
+
The operator smooths the given tensor with a laplacian kernel by convolving
it to each channel. It supports batched operation.
- Arguments:
- input (torch.Tensor): the input image tensor with shape :math:`(B, C, H, W)`.
- kernel_size (int): the size of the kernel.
- border_type (str): the padding mode to be applied before convolving.
+ Args:
+ input: the input image tensor with shape :math:`(B, C, H, W)`.
+ kernel_size: the size of the kernel.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
- ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
- normalized (bool): if True, L1 norm of the kernel is set to 1.
+ ``'replicate'`` or ``'circular'``.
+ normalized: if True, L1 norm of the kernel is set to 1.
Return:
- torch.Tensor: the blurred image with shape :math:`(B, C, H, W)`.
+ the blurred image with shape :math:`(B, C, H, W)`.
Examples:
>>> input = torch.rand(2, 4, 5, 5)
@@ -44,12 +46,12 @@ class Laplacian(nn.Module):
The operator smooths the given tensor with a laplacian kernel by convolving
it to each channel. It supports batched operation.
- Arguments:
- kernel_size (int): the size of the kernel.
- border_type (str): the padding mode to be applied before convolving.
+ Args:
+ kernel_size: the size of the kernel.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
- ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
- normalized (bool): if True, L1 norm of the kernel is set to 1.
+ ``'replicate'`` or ``'circular'``.
+ normalized: if True, L1 norm of the kernel is set to 1.
Shape:
- Input: :math:`(B, C, H, W)`
diff --git a/kornia/filters/median.py b/kornia/filters/median.py
index 8a7e26be6..ede4e9637 100644
--- a/kornia/filters/median.py
+++ b/kornia/filters/median.py
@@ -16,12 +16,14 @@ def _compute_zero_padding(kernel_size: Tuple[int, int]) -> Tuple[int, int]:
def median_blur(input: torch.Tensor, kernel_size: Tuple[int, int]) -> torch.Tensor:
r"""Blurs an image using the median filter.
+ .. image:: _static/img/median_blur.png
+
Args:
- input (torch.Tensor): the input image with shape :math:`(B,C,H,W)`.
- kernel_size (Tuple[int, int]): the blurring kernel size.
+ input: the input image with shape :math:`(B,C,H,W)`.
+ kernel_size: the blurring kernel size.
Returns:
- torch.Tensor: the blurred input tensor with shape :math:`(B,C,H,W)`.
+ the blurred input tensor with shape :math:`(B,C,H,W)`.
Example:
>>> input = torch.rand(2, 4, 5, 7)
@@ -55,10 +57,10 @@ class MedianBlur(nn.Module):
r"""Blurs an image using the median filter.
Args:
- kernel_size (Tuple[int, int]): the blurring kernel size.
+ kernel_size: the blurring kernel size.
Returns:
- torch.Tensor: the blurred input tensor.
+ the blurred input tensor.
Shape:
- Input: :math:`(B, C, H, W)`
diff --git a/kornia/filters/motion.py b/kornia/filters/motion.py
index 1c431e267..17fecdf02 100644
--- a/kornia/filters/motion.py
+++ b/kornia/filters/motion.py
@@ -11,17 +11,17 @@ class MotionBlur(nn.Module):
r"""Blur 2D images (4D tensor) using the motion filter.
Args:
- kernel_size (int): motion kernel width and height. It should be odd and positive.
- angle (float): angle of the motion blur in degrees (anti-clockwise rotation).
- direction (float): forward/backward direction of the motion blur.
+ kernel_size: motion kernel width and height. It should be odd and positive.
+ angle: angle of the motion blur in degrees (anti-clockwise rotation).
+ direction: 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.
- border_type (str): the padding mode to be applied before convolving. The expected modes are:
- ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
+ border_type: the padding mode to be applied before convolving. The expected modes are:
+ ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
Returns:
- torch.Tensor: the blurred input tensor.
+ the blurred input tensor.
Shape:
- Input: :math:`(B, C, H, W)`
@@ -54,17 +54,17 @@ class MotionBlur3D(nn.Module):
r"""Blur 3D volumes (5D tensor) using the motion filter.
Args:
- kernel_size (int): motion kernel width and height. It should be odd and positive.
- angle (float or tuple): Range of yaw (x-axis), pitch (y-axis), roll (z-axis) to select from.
- direction (float): forward/backward direction of the motion blur.
+ kernel_size: motion kernel width and height. It should be odd and positive.
+ angle: Range of yaw (x-axis), pitch (y-axis), roll (z-axis) to select from.
+ direction: 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.
- border_type (str): the padding mode to be applied before convolving. The expected modes are:
- ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
+ border_type: the padding mode to be applied before convolving. The expected modes are:
+ ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``.
Returns:
- torch.Tensor: the blurred input tensor.
+ the blurred input tensor.
Shape:
- Input: :math:`(B, C, D, H, W)`
@@ -113,25 +113,26 @@ def motion_blur(
border_type: str = 'constant',
mode: str = 'nearest',
) -> torch.Tensor:
- r"""Perform motion blur on 2D images (4D tensor).
+ r"""Perform motion blur on tensor images.
+
+ .. image:: _static/img/motion_blur.png
Args:
- input (torch.Tensor): the input tensor with shape :math:`(B, C, H, W)`.
- kernel_size (int): motion kernel width and height. It should be odd and positive.
+ input: the input tensor with shape :math:`(B, C, H, W)`.
+ kernel_size: motion kernel width and height. It should be odd and positive.
angle (Union[torch.Tensor, float]): angle of the motion blur in degrees (anti-clockwise rotation).
If tensor, it must be :math:`(B,)`.
- direction (tensor or float): forward/backward direction of the motion blur.
+ direction : 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.
If tensor, it must be :math:`(B,)`.
- border_type (str): the padding mode to be applied before convolving. The expected modes are:
+ border_type: the padding mode to be applied before convolving. The expected modes are:
``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
- mode (str): interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
- Default: ``'nearest'``
+ mode: interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
Return:
- torch.Tensor: the blurred image with shape :math:`(B, C, H, W)`.
+ the blurred image with shape :math:`(B, C, H, W)`.
Example:
>>> input = torch.randn(1, 3, 80, 90).repeat(2, 1, 1, 1)
@@ -160,22 +161,21 @@ def motion_blur3d(
r"""Perform motion blur on 3D volumes (5D tensor).
Args:
- input (torch.Tensor): the input tensor with shape :math:`(B, C, D, H, W)`.
- kernel_size (int): motion kernel width, height and depth. It should be odd and positive.
- angle (torch.Tensor or tuple): Range of yaw (x-axis), pitch (y-axis), roll (z-axis) to select from.
+ input: the input tensor with shape :math:`(B, C, D, H, W)`.
+ kernel_size: motion kernel width, height and depth. It should be odd and positive.
+ angle: Range of yaw (x-axis), pitch (y-axis), roll (z-axis) to select from.
If tensor, it must be :math:`(B, 3)`.
- direction (tensor or float): forward/backward direction of the motion blur.
+ direction: 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.
If tensor, it must be :math:`(B,)`.
- border_type (str): the padding mode to be applied before convolving. The expected modes are:
+ border_type: the padding mode to be applied before convolving. The expected modes are:
``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'constant'``.
- mode (str): interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
- Default: ``'nearest'``
+ mode: interpolation mode for rotating the kernel. ``'bilinear'`` or ``'nearest'``.
Return:
- torch.Tensor: the blurred image with shape :math:`(B, C, D, H, W)`.
+ the blurred image with shape :math:`(B, C, D, H, W)`.
Example:
>>> input = torch.randn(1, 3, 120, 80, 90).repeat(2, 1, 1, 1, 1)
diff --git a/kornia/filters/sobel.py b/kornia/filters/sobel.py
index 37b2b485b..0384c7e52 100644
--- a/kornia/filters/sobel.py
+++ b/kornia/filters/sobel.py
@@ -9,14 +9,16 @@ def spatial_gradient(input: torch.Tensor, mode: str = 'sobel', order: int = 1, n
r"""Computes the first order image derivative in both x and y using a Sobel
operator.
+ .. image:: _static/img/spatial_gradient.png
+
Args:
- input (torch.Tensor): input image tensor with shape :math:`(B, C, H, W)`.
- mode (str): derivatives modality, can be: `sobel` or `diff`. Default: `sobel`.
- order (int): the order of the derivatives. Default: 1.
- normalized (bool): whether the output is normalized. Default: True.
+ input: input image tensor with shape :math:`(B, C, H, W)`.
+ mode: derivatives modality, can be: `sobel` or `diff`.
+ order: the order of the derivatives.
+ normalized: whether the output is normalized.
Return:
- torch.Tensor: the derivatives of the input feature map. with shape :math:`(B, C, 2, H, W)`.
+ the derivatives of the input feature map. with shape :math:`(B, C, 2, H, W)`.
Examples:
>>> input = torch.rand(1, 3, 4, 4)
@@ -55,12 +57,12 @@ def spatial_gradient3d(input: torch.Tensor, mode: str = 'diff', order: int = 1)
operator.
Args:
- input (torch.Tensor): input features tensor with shape :math:`(B, C, D, H, W)`.
- mode (str): derivatives modality, can be: `sobel` or `diff`. Default: `diff`.
- order (int): the order of the derivatives. Default: 1.
+ input: input features tensor with shape :math:`(B, C, D, H, W)`.
+ mode: derivatives modality, can be: `sobel` or `diff`.
+ order: the order of the derivatives.
Return:
- torch.Tensor: the spatial gradients of the input feature map.
+ the spatial gradients of the input feature map.
Shape:
- Input: :math:`(B, C, D, H, W)`. D, H, W are spatial dimensions, gradient is calculated w.r.t to them.
@@ -106,13 +108,15 @@ def spatial_gradient3d(input: torch.Tensor, mode: str = 'diff', order: int = 1)
def sobel(input: torch.Tensor, normalized: bool = True, eps: float = 1e-6) -> torch.Tensor:
r"""Computes the Sobel operator and returns the magnitude per channel.
+ .. image:: _static/img/sobel.png
+
Args:
- input (torch.Tensor): the input image with shape :math:`(B,C,H,W)`.
- normalized (bool): if True, L1 norm of the kernel is set to 1.
- eps (float): regularization number to avoid NaN during backprop. Default: 1e-6.
+ input: the input image with shape :math:`(B,C,H,W)`.
+ normalized: if True, L1 norm of the kernel is set to 1.
+ eps: regularization number to avoid NaN during backprop.
Return:
- torch.Tensor: the sobel edge gradient magnitudes map with shape :math:`(B,C,H,W)`.
+ the sobel edge gradient magnitudes map with shape :math:`(B,C,H,W)`.
Example:
>>> input = torch.rand(1, 3, 4, 4)
@@ -144,12 +148,12 @@ class SpatialGradient(nn.Module):
operator.
Args:
- mode (str): derivatives modality, can be: `sobel` or `diff`. Default: `sobel`.
- order (int): the order of the derivatives. Default: 1.
- normalized (bool): whether the output is normalized. Default: True.
+ mode: derivatives modality, can be: `sobel` or `diff`.
+ order: the order of the derivatives.
+ normalized: whether the output is normalized.
Return:
- torch.Tensor: the sobel edges of the input feature map.
+ the sobel edges of the input feature map.
Shape:
- Input: :math:`(B, C, H, W)`
@@ -181,11 +185,11 @@ class SpatialGradient3d(nn.Module):
operator.
Args:
- mode (str): derivatives modality, can be: `sobel` or `diff`. Default: `sobel`.
- order (int): the order of the derivatives. Default: 1.
+ mode: derivatives modality, can be: `sobel` or `diff`.
+ order: the order of the derivatives.
Return:
- torch.Tensor: the spatial gradients of the input feature map.
+ the spatial gradients of the input feature map.
Shape:
- Input: :math:`(B, C, D, H, W)`. D, H, W are spatial dimensions, gradient is calculated w.r.t to them.
@@ -216,11 +220,11 @@ class Sobel(nn.Module):
r"""Computes the Sobel operator and returns the magnitude per channel.
Args:
- normalized (bool): if True, L1 norm of the kernel is set to 1.
- eps (float): regularization number to avoid NaN during backprop. Default: 1e-6.
+ normalized: if True, L1 norm of the kernel is set to 1.
+ eps: regularization number to avoid NaN during backprop.
Return:
- torch.Tensor: the sobel edge gradient magnitudes map.
+ the sobel edge gradient magnitudes map.
Shape:
- Input: :math:`(B, C, H, W)`
diff --git a/kornia/filters/unsharp.py b/kornia/filters/unsharp.py
index f63a5073c..deee5456e 100644
--- a/kornia/filters/unsharp.py
+++ b/kornia/filters/unsharp.py
@@ -11,16 +11,18 @@ def unsharp_mask(
) -> torch.Tensor:
r"""Creates an operator that blurs a tensor using the existing Gaussian filter available with the Kornia library.
- Arguments:
- input (torch.Tensor): the input tensor with shape :math:`(B,C,H,W)`.
- kernel_size (Tuple[int, int]): the size of the kernel.
- sigma (Tuple[float, float]): the standard deviation of the kernel.
- border_type (str): the padding mode to be applied before convolving.
+ .. image:: _static/img/unsharp_mask.png
+
+ Args:
+ input: the input tensor with shape :math:`(B,C,H,W)`.
+ kernel_size: the size of the kernel.
+ sigma: the standard deviation of the kernel.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
- ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
+ ``'replicate'`` or ``'circular'``.
Returns:
- torch.Tensor: the blurred tensor with shape :math:`(B,C,H,W)`.
+ the blurred tensor with shape :math:`(B,C,H,W)`.
Examples:
>>> input = torch.rand(2, 4, 5, 5)
@@ -36,15 +38,15 @@ def unsharp_mask(
class UnsharpMask(nn.Module):
r"""Creates an operator that sharpens image using the existing Gaussian filter available with the Kornia library..
- Arguments:
- kernel_size (Tuple[int, int]): the size of the kernel.
- sigma (Tuple[float, float]): the standard deviation of the kernel.
- border_type (str): the padding mode to be applied before convolving.
+ Args:
+ kernel_size: the size of the kernel.
+ sigma: the standard deviation of the kernel.
+ border_type: the padding mode to be applied before convolving.
The expected modes are: ``'constant'``, ``'reflect'``,
- ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
+ ``'replicate'`` or ``'circular'``.
Returns:
- Tensor: the sharpened tensor with shape :math:`(B,C,H,W)`.
+ the sharpened tensor with shape :math:`(B,C,H,W)`.
Shape:
- Input: :math:`(B, C, H, W)`