Implementation:Kornia Kornia Box Blur
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Filtering |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Applies box (mean) filtering to blur images using a uniform kernel where all weights are equal.
Description
This module is part of the Kornia library's filters subpackage. It provides the box_blur function and BoxBlur nn.Module class for smoothing images with a uniform averaging kernel. The box filter replaces each pixel with the average of its neighbors within the kernel window. The implementation supports both standard 2D convolution via filter2d and a more efficient separable decomposition via filter2d_separable, where the 2D box kernel is decomposed into two 1D passes.
Usage
Import this function or class when you need a simple, fast averaging blur for image smoothing, noise reduction, or as a building block in more complex filters such as guided filtering.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/filters/blur.py
- Lines: 1-149
Signature
def box_blur(
input: torch.Tensor,
kernel_size: tuple[int, int] | int,
border_type: str = "reflect",
separable: bool = False,
) -> torch.Tensor: ...
class BoxBlur(nn.Module):
def __init__(self, kernel_size: tuple[int, int] | int,
border_type: str = "reflect",
separable: bool = False) -> None: ...
def forward(self, input: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.filters import box_blur, BoxBlur
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor (B, C, H, W) | Yes | The input image tensor to blur. |
| kernel_size | tuple[int, int] or int | Yes | The size of the box blurring kernel. |
| border_type | str | No (default "reflect") | Padding mode: constant, reflect, replicate, or circular. |
| separable | bool | No (default False) | If True, runs as a composition of two 1D convolutions for better performance. |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor (B, C, H, W) | The blurred image, same shape as input. |
Usage Examples
import torch
from kornia.filters import box_blur, BoxBlur
# Functional box blur
input = torch.rand(2, 4, 5, 7)
output = box_blur(input, (3, 3))
print(output.shape) # torch.Size([2, 4, 5, 7])
# Separable box blur (more efficient for large kernels)
output = box_blur(input, (5, 5), separable=True)
# Module-based box blur
blur = BoxBlur((3, 3), border_type='reflect')
output = blur(input)
print(output.shape) # torch.Size([2, 4, 5, 7])