GaussianBlur

Targets:
image
volume
Image Types:uint8, float32

Apply Gaussian blur to the input image using a randomly sized kernel.

This transform blurs the input image using a Gaussian filter with a random kernel size and sigma value. Gaussian blur is a widely used image processing technique that reduces image noise and detail, creating a smoothing effect.

Arguments
sigma_limit
tuple[float, float] | float
[0.5,3]

Range for the Gaussian kernel standard deviation (sigma). Must be more or equal than 0.

  • If a single float is provided, sigma will be randomly chosen between 0 and that value.
  • If a tuple of two floats is provided, it defines the inclusive range of possible sigma values. Default: (0.5, 3.0)
blur_limit
tuple[int, int] | int
0

Controls the range of the Gaussian kernel size.

  • If a single int is provided, the kernel size will be randomly chosen between 0 and that value.
  • If a tuple of two ints is provided, it defines the inclusive range of possible kernel sizes. Must be zero or odd and in range [0, inf). If set to 0 (default), the kernel size will be computed from sigma as int(sigma * 3.5) * 2 + 1 to exactly match PIL's implementation. Default: 0
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, (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, "Sample Text", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
>>>
>>> # Example 1: Default Gaussian blur (automatic kernel size)
>>> default_blur = A.Compose([
...     A.GaussianBlur(p=1.0)  # Using default parameters
... ])
>>>
>>> default_result = default_blur(image=image)
>>> default_blurred = default_result["image"]
>>> # The image will have a medium Gaussian blur with sigma between 0.5 and 3.0
>>>
>>> # Example 2: Light Gaussian blur
>>> light_blur = A.Compose([
...     A.GaussianBlur(
...         sigma_limit=(0.2, 0.5),  # Small sigma for subtle blur
...         blur_limit=0,            # Auto-compute kernel size
...         p=1.0
...     )
... ])
>>>
>>> light_result = light_blur(image=image)
>>> light_blurred = light_result["image"]
>>> # The image will have a subtle Gaussian blur effect
>>>
>>> # Example 3: Strong Gaussian blur
>>> strong_blur = A.Compose([
...     A.GaussianBlur(
...         sigma_limit=(3.0, 7.0),  # Larger sigma for stronger blur
...         blur_limit=0,            # Auto-compute kernel size
...         p=1.0
...     )
... ])
>>>
>>> strong_result = strong_blur(image=image)
>>> strong_blurred = strong_result["image"]
>>> # The image will have a strong Gaussian blur effect
>>>
>>> # Example 4: Fixed kernel size
>>> fixed_kernel = A.Compose([
...     A.GaussianBlur(
...         sigma_limit=(0.5, 2.0),
...         blur_limit=(9, 9),       # Fixed 9x9 kernel size
...         p=1.0
...     )
... ])
>>>
>>> fixed_result = fixed_kernel(image=image)
>>> fixed_kernel_blur = fixed_result["image"]
>>> # The image will have Gaussian blur with a fixed 9x9 kernel
>>>
>>> # Example 5: Random kernel size range
>>> random_kernel = A.Compose([
...     A.GaussianBlur(
...         sigma_limit=(1.0, 2.0),
...         blur_limit=(5, 9),       # Kernel size between 5x5 and 9x9
...         p=1.0
...     )
... ])
>>>
>>> random_result = random_kernel(image=image)
>>> random_kernel_blur = random_result["image"]
>>> # The image will have Gaussian blur with a kernel size between 5x5 and 9x9
>>>
>>> # Example 6: In an augmentation pipeline
>>> pipeline = A.Compose([
...     A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.5),
...     A.GaussianBlur(sigma_limit=(0.5, 1.5), p=0.3),  # 30% chance of applying
...     A.RGBShift(r_shift_limit=10, g_shift_limit=10, b_shift_limit=10, p=0.3)
... ])
>>>
>>> pipeline_result = pipeline(image=image)
>>> transformed_image = pipeline_result["image"]
>>> # The image may have Gaussian blur applied with 30% probability along with other effects
Notes
  • When blur_limit=0 (default), this implementation exactly matches PIL's