• AlbumentationsAlbumentations
All TransformsGet LicenseDocumentationNews & Insights
Report IssueJoin Discord...

Blur

Targets:
image
Image Types:uint8, float32

Average pixels over a random square kernel (box filter). Fast, soft blur; kernel size from blur_range. Good for mild smoothing or augmentation variety.

This transform uses OpenCV's cv2.blur function, which performs a simple box filter blur. The size of the blur kernel is randomly selected for each application, allowing for varying degrees of blur intensity.

Arguments
blur_range
tuple[int, int]
[3,7]

Inclusive range of the blur kernel size. Both ends must be odd and greater than or equal to 3. Larger kernel sizes produce stronger blur effects. Default: (3, 7)

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 blur effects
>>> cv2.rectangle(image, (50, 50), (250, 250), (255, 0, 0), -1)  # Red square
>>> cv2.circle(image, (150, 150), 60, (0, 255, 0), -1)  # Green circle
>>> cv2.line(image, (50, 150), (250, 150), (0, 0, 255), 5)  # Blue line
>>>
>>> # Example 1: Basic usage with default parameters
>>> transform = A.Compose([
...     A.Blur(p=1.0)  # Always apply with default blur_range=(3, 7)
... ])
>>>
>>> result = transform(image=image)
>>> blurred_image = result["image"]
>>> # The image will have a random blur with kernel size between 3 and 7
>>>
>>> # Example 2: Using a fixed blur kernel size
>>> fixed_transform = A.Compose([
...     A.Blur(blur_range=(5, 5), p=1.0)  # Always use kernel size 5x5
... ])
>>>
>>> fixed_result = fixed_transform(image=image)
>>> fixed_blurred_image = fixed_result["image"]
>>> # The image will have a consistent 5x5 kernel blur
>>>
>>> # Example 3: Using a custom range for blur kernel sizes
>>> strong_transform = A.Compose([
...     A.Blur(blur_range=(7, 13), p=1.0)  # Use larger kernel for stronger blur
... ])
>>>
>>> strong_result = strong_transform(image=image)
>>> strong_blurred = strong_result["image"]
>>> # The image will have a stronger blur with kernel size between 7 and 13
>>>
>>> # Example 4: As part of a pipeline with other transforms
>>> pipeline = A.Compose([
...     A.RandomBrightnessContrast(brightness_range=(-0.2, 0.2), contrast_range=(-0.2, 0.2), p=0.7),
...     A.Blur(blur_range=(3, 5), p=0.5),  # 50% chance of applying blur
...     A.HorizontalFlip(p=0.5)
... ])
>>>
>>> pipeline_result = pipeline(image=image)
>>> transformed_image = pipeline_result["image"]
>>> # The image may or may not be blurred depending on the random probability
Notes
  • The blur kernel is always square (same width and height).
  • Only odd kernel sizes are used to ensure the blur has a clear center pixel.
  • Box blur is faster than Gaussian blur but may produce less natural results.
  • This blur method averages all pixels under the kernel area, which can reduce noise but also reduce image detail.