Simulate motion blur along a random direction (camera shake or moving subject). Kernel size and angle sampled per call; optional shift for off-center streaks.
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.
blur_limitMaximum kernel size for blurring. Should be in range [3, inf).
angle_rangeRange of possible angles in degrees. Controls the rotation of the motion blur line:
direction_rangeRange for motion bias. Controls how the blur extends from the center:
allow_shiftedAllow random kernel position shifts.
pProbability of applying the transform. Default: 0.5
>>> 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