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 Motion Blur

From Leeroopedia


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

Overview

Applies motion blur effects to 2D images and 3D volumes by convolving with directional motion kernels parameterized by angle, direction, and kernel size.

Description

This module is part of the Kornia library's filters subpackage. It provides motion_blur and motion_blur3d functions along with their corresponding nn.Module classes MotionBlur and MotionBlur3D. The motion blur effect is achieved by first generating a motion kernel (via get_motion_kernel2d or get_motion_kernel3d from kernels_geometry) and then convolving the input with it using filter2d or filter3d. The angle parameter controls the direction of the blur (anti-clockwise rotation), while the direction parameter controls the forward/backward emphasis of the blur. Both functions support batched operation with per-element angle and direction tensors for element-wise motion blur across a batch.

Usage

Import these functions or classes when you need to simulate camera or object motion blur in images or 3D volumes, useful for data augmentation in training CNNs, realistic rendering effects, or image degradation modeling.

Code Reference

Source Location

Signature

def motion_blur(
    input: torch.Tensor,
    kernel_size: int,
    angle: float | torch.Tensor,
    direction: float | torch.Tensor,
    border_type: str = "constant",
    mode: str = "nearest",
) -> torch.Tensor: ...

def motion_blur3d(
    input: torch.Tensor,
    kernel_size: int,
    angle: tuple[float, float, float] | torch.Tensor,
    direction: float | torch.Tensor,
    border_type: str = "constant",
    mode: str = "nearest",
) -> torch.Tensor: ...

class MotionBlur(nn.Module):
    def __init__(self, kernel_size: int, angle: float, direction: float,
                 border_type: str = "constant", mode: str = "nearest") -> None: ...
    def forward(self, x: torch.Tensor) -> torch.Tensor: ...

class MotionBlur3D(nn.Module):
    def __init__(self, kernel_size: int,
                 angle: float | tuple[float, float, float] | torch.Tensor,
                 direction: float | torch.Tensor,
                 border_type: str = "constant", mode: str = "nearest") -> None: ...
    def forward(self, x: torch.Tensor) -> torch.Tensor: ...

Import

from kornia.filters import motion_blur, motion_blur3d, MotionBlur, MotionBlur3D

I/O Contract

Inputs (motion_blur)

Name Type Required Description
input torch.Tensor (B, C, H, W) Yes The input image tensor.
kernel_size int Yes Motion kernel width and height. Must be odd and positive.
angle float or torch.Tensor (B,) Yes Angle of motion blur in degrees (anti-clockwise).
direction float or torch.Tensor (B,) Yes Forward/backward direction in [-1.0, 1.0].
border_type str No (default "constant") Padding mode: constant, reflect, replicate, or circular.
mode str No (default "nearest") Interpolation mode for kernel rotation.

Inputs (motion_blur3d)

Name Type Required Description
input torch.Tensor (B, C, D, H, W) Yes The input 3D volume tensor.
kernel_size int Yes Motion kernel size. Must be odd and positive.
angle tuple[float, float, float] or torch.Tensor (B, 3) Yes Yaw, pitch, roll angles in degrees.
direction float or torch.Tensor (B,) Yes Forward/backward direction in [-1.0, 1.0].
border_type str No (default "constant") Padding mode.
mode str No (default "nearest") Interpolation mode for kernel rotation.

Outputs

Name Type Description
output (2D) torch.Tensor (B, C, H, W) The motion-blurred image.
output (3D) torch.Tensor (B, C, D, H, W) The motion-blurred 3D volume.

Usage Examples

import torch
from kornia.filters import motion_blur, motion_blur3d, MotionBlur

# Functional motion blur at 90 degrees
input = torch.randn(1, 3, 80, 90)
output = motion_blur(input, kernel_size=5, angle=90., direction=0.)
print(output.shape)  # torch.Size([1, 3, 80, 90])

# Batched motion blur with different angles per element
input_batch = torch.randn(2, 3, 80, 90)
output = motion_blur(input_batch, 5,
                     angle=torch.tensor([90., 180.]),
                     direction=torch.tensor([1., -1.]))

# Module-based motion blur
mb = MotionBlur(kernel_size=3, angle=35., direction=0.5)
output = mb(input)

# 3D motion blur on volumetric data
input_3d = torch.randn(1, 3, 120, 80, 90)
output_3d = motion_blur3d(input_3d, 5, (0., 90., 90.), 1.)

Related Pages

Page Connections

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