Implementation:Kornia Kornia Motion Blur Kernel
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Filtering |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Generates 2D and 3D motion blur kernels parameterized by angle, direction, and kernel size, using geometric rotation of line kernels.
Description
This module is part of the Kornia library's filters subpackage (specifically kernels_geometry.py). It provides get_motion_kernel2d for creating 2D motion blur kernels and get_motion_kernel3d for creating 3D motion blur kernels. The kernels are constructed by creating a 1D directional line along the center of the kernel, controlling the forward/backward emphasis via the direction parameter, and then rotating the kernel by the specified angle(s) using Kornia's geometric rotate and rotate3d functions. The resulting kernels are normalized to sum to 1. Both functions support batched operation, producing a batch of kernels with different angles and directions.
Usage
Import these functions when you need to generate motion blur kernels for convolution, typically used by the motion_blur and motion_blur3d functions in Kornia, or for custom motion blur effects in image processing or data augmentation pipelines.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/filters/kernels_geometry.py
- Lines: 1-213
Signature
def get_motion_kernel2d(
kernel_size: int,
angle: torch.Tensor | float,
direction: torch.Tensor | float = 0.0,
mode: str = "nearest",
) -> torch.Tensor: ...
def get_motion_kernel3d(
kernel_size: int,
angle: torch.Tensor | tuple[float, float, float],
direction: torch.Tensor | float = 0.0,
mode: str = "nearest",
) -> torch.Tensor: ...
Import
from kornia.filters.kernels_geometry import get_motion_kernel2d, get_motion_kernel3d
I/O Contract
Inputs (get_motion_kernel2d)
| Name | Type | Required | Description |
|---|---|---|---|
| kernel_size | int | Yes | Width and height of the motion kernel. Must be odd and positive. |
| angle | torch.Tensor (B,) or float | Yes | Angle of the motion blur in degrees (anti-clockwise rotation). |
| direction | torch.Tensor (B,) or float | No (default 0.0) | Forward/backward direction in [-1.0, 1.0]. 0.0 gives uniform blur; -1.0 points backward; 1.0 points forward. |
| mode | str | No (default "nearest") | Interpolation mode for rotating the kernel: bilinear or nearest. |
Inputs (get_motion_kernel3d)
| Name | Type | Required | Description |
|---|---|---|---|
| kernel_size | int | Yes | Width, height, and depth of the motion kernel. Must be odd and positive. |
| angle | torch.Tensor (B, 3) or tuple[float, float, float] | Yes | Yaw (x-axis), pitch (y-axis), roll (z-axis) angles in degrees. |
| direction | torch.Tensor (B,) or float | No (default 0.0) | Forward/backward direction in [-1.0, 1.0]. |
| mode | str | No (default "nearest") | Interpolation mode for rotating the kernel. |
Outputs
| Name | Type | Description |
|---|---|---|
| kernel (2D) | torch.Tensor (B, kernel_size, kernel_size) | The 2D motion blur kernel, normalized to sum to 1. |
| kernel (3D) | torch.Tensor (B, kernel_size, kernel_size, kernel_size) | The 3D motion blur kernel, normalized to sum to 1. |
Usage Examples
import torch
from kornia.filters.kernels_geometry import get_motion_kernel2d, get_motion_kernel3d
# 2D motion blur kernel at 0 degrees (horizontal)
kernel_2d = get_motion_kernel2d(5, angle=0., direction=0.)
print(kernel_2d.shape) # torch.Size([1, 5, 5])
# Batched 2D kernels with different angles
kernel_batch = get_motion_kernel2d(5, angle=torch.tensor([0., 45., 90.]), direction=0.)
print(kernel_batch.shape) # torch.Size([3, 5, 5])
# 3D motion blur kernel
kernel_3d = get_motion_kernel3d(3, angle=(0., 0., 0.), direction=0.)
print(kernel_3d.shape) # torch.Size([1, 3, 3, 3])
# Directional kernel (emphasize forward direction)
kernel_fwd = get_motion_kernel2d(5, angle=45., direction=0.8)