MotionBlur

Targets:
image
volume

Apply motion blur to the input image using a directional kernel.

This transform simulates motion blur effects that occur during image capture, such as camera shake or object movement. It creates a directional blur using a line-shaped kernel with controllable angle, direction, and position.

Arguments
blur_limit
tuple[int, int] | int
[3,7]

Maximum kernel size for blurring. Should be in range [3, inf).

  • If int: kernel size will be randomly chosen from [3, blur_limit]
  • If tuple: kernel size will be randomly chosen from [min, max] Larger values create stronger blur effects. Default: (3, 7)
angle_range
tuple[float, float]
[0,360]

Range of possible angles in degrees. Controls the rotation of the motion blur line:

  • 0°: Horizontal motion blur →
  • 45°: Diagonal motion blur ↗
  • 90°: Vertical motion blur ↑
  • 135°: Diagonal motion blur ↖ Default: (0, 360)
direction_range
tuple[float, float]
[-1,1]

Range for motion bias. Controls how the blur extends from the center:

  • -1.0: Blur extends only backward (←)
  • 0.0: Blur extends equally in both directions (←→)
  • 1.0: Blur extends only forward (→) For example, with angle=0:
  • direction=-1.0: ←•
  • direction=0.0: ←•→
  • direction=1.0: •→ Default: (-1.0, 1.0)
allow_shifted
bool
true

Allow random kernel position shifts.

  • If True: Kernel can be randomly offset from center
  • If False: Kernel will always be centered Default: True
p
float
0.5

Probability of applying the transform. Default: 0.5

Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Create a sample image for demonstration
>>> image = np.zeros((300, 300, 3), dtype=np.uint8)
>>> # Add some shapes to visualize motion blur effects
>>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1)  # Red square
>>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1)  # Green circle
>>> cv2.putText(image, "Motion Blur", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
>>>
>>> # Example 1: Horizontal camera shake (symmetric)
>>> horizontal_shake = A.Compose([
...     A.MotionBlur(
...         blur_limit=(10, 12),     # Strong blur
...         angle_range=(-5, 5),     # Near-horizontal motion (±5°)
...         direction_range=(0, 0),  # Symmetric blur (equally in both directions)
...         p=1.0                    # Always apply
...     )
... ])
>>>
>>> horizontal_result = horizontal_shake(image=image)
>>> horizontal_blur = horizontal_result["image"]
>>> # The image will have a horizontal camera shake effect, blurring equally in both directions
>>>
>>> # Example 2: Object moving right (directional motion)
>>> rightward_motion = A.Compose([
...     A.MotionBlur(
...         blur_limit=(7, 9),         # Medium blur
...         angle_range=(0, 0),        # Exactly horizontal motion (0°)
...         direction_range=(0.8, 1.0), # Strong forward bias (mostly rightward)
...         p=1.0
...     )
... ])
>>>
>>> rightward_result = rightward_motion(image=image)
>>> rightward_blur = rightward_result["image"]
>>> # The image will simulate an object moving rightward, with blur mostly to the right
>>>
>>> # Example 3: Object moving diagonally down-right
>>> diagonal_motion = A.Compose([
...     A.MotionBlur(
...         blur_limit=(9, 11),       # Stronger blur
...         angle_range=(135, 135),   # 135° motion (down-right diagonal)
...         direction_range=(0.7, 0.9), # Forward bias
...         p=1.0
...     )
... ])
>>>
>>> diagonal_result = diagonal_motion(image=image)
>>> diagonal_blur = diagonal_result["image"]
>>> # The image will simulate diagonal motion down and to the right
>>>
>>> # Example 4: Vertical motion (up-down)
>>> vertical_motion = A.Compose([
...     A.MotionBlur(
...         blur_limit=9,             # Fixed kernel size
...         angle_range=(90, 90),     # Vertical motion (90°)
...         direction_range=(-0.2, 0.2), # Near-symmetric (slight bias)
...         p=1.0
...     )
... ])
>>>
>>> vertical_result = vertical_motion(image=image)
>>> vertical_blur = vertical_result["image"]
>>> # The image will simulate vertical motion blur
>>>
>>> # Example 5: Random motion blur (can be in any direction)
>>> random_motion = A.Compose([
...     A.MotionBlur(
...         blur_limit=(5, 12),       # Variable strength
...         angle_range=(0, 360),     # Any angle
...         direction_range=(-1.0, 1.0), # Any direction bias
...         allow_shifted=True,       # Allow kernel to be shifted from center
...         p=1.0
...     )
... ])
>>>
>>> random_result = random_motion(image=image)
>>> random_blur = random_result["image"]
>>> # The image will have a random motion blur in any direction
>>>
>>> # Example 6: Multiple random parameters with kernel centered (not shifted)
>>> centered_motion = A.Compose([
...     A.MotionBlur(
...         blur_limit=(5, 9),
...         angle_range=(0, 360),
...         direction_range=(-1.0, 1.0),
...         allow_shifted=False,      # Kernel will always be centered
...         p=1.0
...     )
... ])
>>>
>>> centered_result = centered_motion(image=image)
>>> centered_blur = centered_result["image"]
>>> # The image will have motion blur with the kernel centered (not shifted)
>>>
>>> # Example 7: In a composition with other transforms
>>> pipeline = A.Compose([
...     A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.5),
...     A.MotionBlur(                                   # 30% chance of applying motion blur
...         blur_limit=(3, 7),
...         angle_range=(0, 180),                       # Only horizontal to vertical
...         direction_range=(-0.5, 0.5),                # Moderate direction bias
...         p=0.3
...     ),
...     A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=15, val_shift_limit=10, p=0.3)
... ])
>>>
>>> pipeline_result = pipeline(image=image)
>>> transformed_image = pipeline_result["image"]
>>> # The image may have motion blur applied with 30% probability along with other effects
Notes
  • angle controls the orientation of the motion line
  • direction controls the distribution of the blur along that line
  • Together they can simulate various motion effects:
    • Camera shake: Small angle range + direction near 0
    • Object motion: Specific angle + direction=1.0
    • Complex motion: Random angle + random direction
See Also
  • GaussianBlur: For uniform blur effects
  • MedianBlur: For noise reduction while preserving edges
  • RandomRain: Another motion-based effect
  • Perspective: For geometric motion-like distortions
References