Smooth the image with a Gaussian kernel (weighted average; reduces noise and fine detail). Kernel size and sigma are sampled randomly per call.
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.
sigma_rangeInclusive range for the Gaussian kernel standard deviation (sigma). Both ends must be >= 0. Default: (0.5, 3.0)
blur_rangeInclusive range of the Gaussian kernel size.
Both ends must be 0 or odd and >= 0. If set to (0, 0) (default), the kernel size
is computed from sigma as int(sigma * 3.5) * 2 + 1 to exactly match PIL's
implementation.
Default: (0, 0)
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 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_range=(0.2, 0.5), # Small sigma for subtle blur
... blur_range=(0, 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_range=(3.0, 7.0), # Larger sigma for stronger blur
... blur_range=(0, 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_range=(0.5, 2.0),
... blur_range=(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_range=(1.0, 2.0),
... blur_range=(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_range=(-0.2, 0.2), contrast_range=(-0.2, 0.2), p=0.5),
... A.GaussianBlur(sigma_range=(0.5, 1.5), p=0.3), # 30% chance of applying
... A.RGBShift(r_shift_range=(-10, 10), g_shift_range=(-10, 10), b_shift_range=(-10, 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