Implementation:Kornia Kornia Gaussian Blur
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Filtering |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Applies Gaussian blur to 2D image tensors by convolving with a Gaussian kernel, with support for batched per-element sigma values and separable filtering.
Description
This module is part of the Kornia library's filters subpackage. It provides the gaussian_blur2d function and GaussianBlur2d nn.Module class for smoothing images using a Gaussian kernel. The Gaussian kernel is parameterized by kernel_size and sigma (standard deviation), and is applied independently to each channel. The implementation supports both separable filtering (default, more efficient -- two sequential 1D convolutions) and non-separable 2D convolution. The sigma parameter can be a tuple (applied uniformly to all batch elements) or a torch.Tensor of shape (B, 2) for per-batch-element sigma values. A deprecated alias gaussian_blur2d_t is also provided.
Usage
Import this function or class whenever you need to apply Gaussian smoothing to images, such as for noise reduction, scale-space construction, or as a preprocessing step before edge detection.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/filters/gaussian.py
- Lines: 1-178
Signature
def gaussian_blur2d(
input: torch.Tensor,
kernel_size: tuple[int, int] | int,
sigma: tuple[float, float] | torch.Tensor,
border_type: str = "reflect",
separable: bool = True,
) -> torch.Tensor: ...
class GaussianBlur2d(nn.Module):
def __init__(self, kernel_size: tuple[int, int] | int,
sigma: tuple[float, float] | torch.Tensor,
border_type: str = "reflect",
separable: bool = True) -> None: ...
def forward(self, input: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.filters import gaussian_blur2d, GaussianBlur2d
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor (B, C, H, W) | Yes | The input image tensor. |
| kernel_size | tuple[int, int] or int | Yes | Size of the Gaussian kernel. Must be positive odd integers. |
| sigma | tuple[float, float] or torch.Tensor (B, 2) | Yes | Standard deviation of the Gaussian kernel for (height, width). Must be positive. |
| border_type | str | No (default "reflect") | Padding mode: constant, reflect, replicate, or circular. |
| separable | bool | No (default True) | If True, uses separable 1D convolutions for efficiency. |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor (B, C, H, W) | The Gaussian-blurred image, same shape as input. |
Usage Examples
import torch
from kornia.filters import gaussian_blur2d, GaussianBlur2d
# Functional Gaussian blur
input = torch.rand(2, 4, 5, 5)
output = gaussian_blur2d(input, (3, 3), (1.5, 1.5))
print(output.shape) # torch.Size([2, 4, 5, 5])
# With per-batch sigma using a tensor
sigma_batch = torch.tensor([[1.5, 1.5], [2.0, 2.0]])
output = gaussian_blur2d(input, (3, 3), sigma_batch)
# Module-based Gaussian blur
gauss = GaussianBlur2d((5, 5), (2.0, 2.0), border_type='reflect')
output = gauss(input)
print(output.shape) # torch.Size([2, 4, 5, 5])