Blur

Targets:
image
volume
Image Types:uint8, float32

Apply uniform box blur to the input image using a randomly sized square kernel.

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_limit
tuple[int, int] | int
[3,7]

Controls the range of the blur kernel size.

  • If a single int is provided, the kernel size will be randomly chosen between 3 and that value.
  • If a tuple of two ints is provided, it defines the inclusive range of possible kernel sizes. The kernel size 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_limit=(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_limit=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_limit=(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_limit=0.2, contrast_limit=0.2, p=0.7),
...     A.Blur(blur_limit=(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.