Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Filter2D Filter3D

From Leeroopedia
Revision as of 15:21, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Kornia_Kornia_Filter2D_Filter3D.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Vision, Image_Filtering
Last Updated 2026-02-09 15:00 GMT

Overview

Provides generic 2D and 3D convolution filtering functions that convolve tensors with arbitrary kernels, supporting multiple padding modes, separable filtering, and both correlation and convolution behaviours.

Description

This module is part of the Kornia library's filters subpackage. It implements three core filtering functions: filter2d for convolving a 4D tensor (B, C, H, W) with a 2D kernel, filter2d_separable for applying two 1D kernels sequentially in x and y directions, and filter3d for convolving a 5D tensor (B, C, D, H, W) with a 3D kernel. These functions serve as the foundation for many higher-level filtering operations in Kornia such as Gaussian blur, box blur, motion blur, and unsharp masking.

Usage

Import these functions when you need to apply custom convolution kernels to image tensors. They are typically used internally by other Kornia filters but can also be used directly for custom filtering operations.

Code Reference

Source Location

Signature

def filter2d(
    input: torch.Tensor,
    kernel: torch.Tensor,
    border_type: str = "reflect",
    normalized: bool = False,
    padding: str = "same",
    behaviour: str = "corr",
) -> torch.Tensor: ...

def filter2d_separable(
    input: torch.Tensor,
    kernel_x: torch.Tensor,
    kernel_y: torch.Tensor,
    border_type: str = "reflect",
    normalized: bool = False,
    padding: str = "same",
) -> torch.Tensor: ...

def filter3d(
    input: torch.Tensor,
    kernel: torch.Tensor,
    border_type: str = "replicate",
    normalized: bool = False,
) -> torch.Tensor: ...

Import

from kornia.filters import filter2d, filter2d_separable, filter3d

I/O Contract

Inputs (filter2d)

Name Type Required Description
input torch.Tensor (B, C, H, W) Yes The input image tensor.
kernel torch.Tensor (1, kH, kW) or (B, kH, kW) Yes The 2D convolution kernel.
border_type str No (default "reflect") Padding mode: constant, reflect, replicate, or circular.
normalized bool No (default False) If True, the kernel is L1-normalized before convolution.
padding str No (default "same") Padding type: same (output same size) or valid (no padding).
behaviour str No (default "corr") corr for correlation (default PyTorch conv2d) or conv for true convolution (kernel is flipped).

Inputs (filter2d_separable)

Name Type Required Description
input torch.Tensor (B, C, H, W) Yes The input image tensor.
kernel_x torch.Tensor (1, kW) or (B, kW) Yes The 1D horizontal kernel.
kernel_y torch.Tensor (1, kH) or (B, kH) Yes The 1D vertical kernel.
border_type str No (default "reflect") Padding mode.
normalized bool No (default False) If True, kernel is L1-normalized.
padding str No (default "same") Padding type: same or valid.

Inputs (filter3d)

Name Type Required Description
input torch.Tensor (B, C, D, H, W) Yes The input 3D volume tensor.
kernel torch.Tensor (1, kD, kH, kW) or (B, kD, kH, kW) Yes The 3D convolution kernel.
border_type str No (default "replicate") Padding mode: constant, replicate, or circular.
normalized bool No (default False) If True, kernel is L1-normalized.

Outputs

Name Type Description
output torch.Tensor The convolved tensor. Same shape as input when padding is same; reduced spatial dimensions when padding is valid.

Usage Examples

import torch
from kornia.filters import filter2d, filter2d_separable, filter3d

# 2D filtering with a custom kernel
input = torch.rand(1, 3, 64, 64)
kernel = torch.ones(1, 3, 3) / 9.0  # mean filter
output = filter2d(input, kernel, border_type='reflect', padding='same')
print(output.shape)  # torch.Size([1, 3, 64, 64])

# Separable filtering (more efficient for separable kernels)
kernel_x = torch.ones(1, 3) / 3.0
kernel_y = torch.ones(1, 3) / 3.0
output = filter2d_separable(input, kernel_x, kernel_y, padding='same')

# 3D filtering on volumetric data
input_3d = torch.rand(1, 1, 16, 64, 64)
kernel_3d = torch.ones(1, 3, 3, 3) / 27.0
output_3d = filter3d(input_3d, kernel_3d)
print(output_3d.shape)  # torch.Size([1, 1, 16, 64, 64])

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment