Implementation:Kornia Kornia Bilateral Filter
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Filtering |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Implements bilateral and joint bilateral filtering for edge-preserving image smoothing, where pixel weights depend on both spatial distance and intensity/color similarity.
Description
This module is part of the Kornia library's filters subpackage. It provides the bilateral_blur function and BilateralBlur nn.Module class for standard bilateral filtering, as well as joint_bilateral_blur and JointBilateralBlur for joint (cross) bilateral filtering where the color kernel is computed from a separate guidance image. Both share a common internal implementation _bilateral_blur that unfolds the input into local neighborhoods, computes spatial Gaussian weights and color distance weights, and combines them to produce edge-preserving smoothing. The color distance can use either L1 (matching OpenCV) or L2 (matching MATLAB) norms.
Usage
Import these functions or classes when you need edge-preserving smoothing that reduces noise while maintaining sharp edges, such as in image denoising, HDR tonemapping, or texture removal tasks.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/filters/bilateral.py
- Lines: 1-295
Signature
def bilateral_blur(
input: torch.Tensor,
kernel_size: tuple[int, int] | int,
sigma_color: float | torch.Tensor,
sigma_space: tuple[float, float] | torch.Tensor,
border_type: str = "reflect",
color_distance_type: str = "l1",
) -> torch.Tensor: ...
def joint_bilateral_blur(
input: torch.Tensor,
guidance: torch.Tensor,
kernel_size: tuple[int, int] | int,
sigma_color: float | torch.Tensor,
sigma_space: tuple[float, float] | torch.Tensor,
border_type: str = "reflect",
color_distance_type: str = "l1",
) -> torch.Tensor: ...
class BilateralBlur(nn.Module):
def __init__(self, kernel_size: tuple[int, int] | int,
sigma_color: float | torch.Tensor,
sigma_space: tuple[float, float] | torch.Tensor,
border_type: str = "reflect",
color_distance_type: str = "l1") -> None: ...
def forward(self, input: torch.Tensor) -> torch.Tensor: ...
class JointBilateralBlur(nn.Module):
def __init__(self, kernel_size: tuple[int, int] | int,
sigma_color: float | torch.Tensor,
sigma_space: tuple[float, float] | torch.Tensor,
border_type: str = "reflect",
color_distance_type: str = "l1") -> None: ...
def forward(self, input: torch.Tensor, guidance: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.filters import bilateral_blur, joint_bilateral_blur
from kornia.filters import BilateralBlur, JointBilateralBlur
I/O Contract
Inputs (bilateral_blur)
| 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 bilateral filter kernel. |
| sigma_color | float or torch.Tensor (B,) | Yes | Standard deviation for the intensity/color Gaussian kernel. Smaller values preserve more edges. |
| sigma_space | tuple[float, float] or torch.Tensor | Yes | Standard deviation for the spatial Gaussian kernel, similar to sigma in gaussian_blur2d. |
| border_type | str | No (default "reflect") | Padding mode: constant, reflect, replicate, or circular. |
| color_distance_type | str | No (default "l1") | Type of color distance: l1 (OpenCV-compatible) or l2 (MATLAB-compatible). |
Inputs (joint_bilateral_blur -- additional)
| Name | Type | Required | Description |
|---|---|---|---|
| guidance | torch.Tensor (B, C, H, W) | Yes | The guidance image used to compute the color Gaussian kernel. |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor (B, C, H, W) | The edge-preserving blurred image, same shape as input. |
Usage Examples
import torch
from kornia.filters import bilateral_blur, joint_bilateral_blur, BilateralBlur
# Functional bilateral blur
input = torch.rand(2, 4, 5, 5)
output = bilateral_blur(input, (3, 3), sigma_color=0.1, sigma_space=(1.5, 1.5))
print(output.shape) # torch.Size([2, 4, 5, 5])
# Module-based bilateral blur
blur = BilateralBlur((3, 3), 0.1, (1.5, 1.5))
output = blur(input)
# Joint bilateral blur with a guidance image
guidance = torch.rand(2, 4, 5, 5)
output = joint_bilateral_blur(input, guidance, (3, 3), 0.1, (1.5, 1.5))