AdvancedBlur
Applies a Generalized Gaussian blur to the input image with randomized parameters for advanced data augmentation.
This transform creates a custom blur kernel based on the Generalized Gaussian distribution, which allows for a wide range of blur effects beyond standard Gaussian blur. It then applies this kernel to the input image through convolution. The transform also incorporates noise into the kernel, resulting in a unique combination of blurring and noise injection.
Key features of this augmentation:
-
Generalized Gaussian Kernel: Uses a generalized normal distribution to create kernels that can range from box-like blurs to very peaked blurs, controlled by the beta parameter.
-
Anisotropic Blurring: Allows for different blur strengths in horizontal and vertical directions (controlled by sigma_x and sigma_y), and rotation of the kernel.
-
Kernel Noise: Adds multiplicative noise to the kernel before applying it to the image, creating more diverse and realistic blur effects.
Implementation Details: The kernel is generated using a 2D Generalized Gaussian function. The process involves: 1. Creating a 2D grid based on the kernel size 2. Applying rotation to this grid 3. Calculating the kernel values using the Generalized Gaussian formula 4. Adding multiplicative noise to the kernel 5. Normalizing the kernel
The resulting kernel is then applied to the image using convolution.
blur_limitControls the size of the blur kernel. If a single int is provided, the kernel size will be randomly chosen between 3 and that value. Must be odd and ≥ 3. Larger values create stronger blur effects. Default: (3, 7)
sigma_x_limitControls the spread of the blur in the x direction. Higher values increase blur strength. If a single float is provided, the range will be (0, limit). Default: (0.2, 1.0)
sigma_y_limitControls the spread of the blur in the y direction. Higher values increase blur strength. If a single float is provided, the range will be (0, limit). Default: (0.2, 1.0)
rotate_limitRange of angles (in degrees) for rotating the kernel. This rotation allows for diagonal blur directions. If limit is a single int, an angle is picked from (-rotate_limit, rotate_limit). Default: (-90, 90)
beta_limitShape parameter of the Generalized Gaussian distribution.
- beta = 1 gives a standard Gaussian distribution
- beta < 1 creates heavier tails, resulting in more uniform, box-like blur
- beta > 1 creates lighter tails, resulting in more peaked, focused blur Default: (0.5, 8.0)
noise_limitControls the strength of multiplicative noise applied to the kernel. Values around 1.0 keep the original kernel mostly intact, while values further from 1.0 introduce more variation. Default: (0.75, 1.25)
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, "Text Example", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
>>> cv2.line(image, (50, 250), (250, 250), (0, 0, 255), 3) # Blue line
>>>
>>> # Example 1: Gaussian-like blur (beta = 1)
>>> gaussian_like = A.Compose([
... A.AdvancedBlur(
... blur_limit=5,
... sigma_x_limit=(0.5, 0.5),
... sigma_y_limit=(0.5, 0.5),
... rotate_limit=0,
... beta_limit=(1.0, 1.0), # Standard Gaussian (beta = 1)
... noise_limit=(1.0, 1.0), # No noise
... p=1.0
... )
... ])
>>>
>>> gaussian_result = gaussian_like(image=image)
>>> gaussian_image = gaussian_result["image"]
>>> # The image will have a standard Gaussian blur applied
>>>
>>> # Example 2: Box-like blur (beta < 1)
>>> box_like = A.Compose([
... A.AdvancedBlur(
... blur_limit=(7, 9),
... sigma_x_limit=(0.6, 0.8),
... sigma_y_limit=(0.6, 0.8),
... rotate_limit=0,
... beta_limit=(0.5, 0.7), # Box-like blur (beta < 1)
... noise_limit=(0.9, 1.1), # Slight noise
... p=1.0
... )
... ])
>>>
>>> box_result = box_like(image=image)
>>> box_image = box_result["image"]
>>> # The image will have a more box-like blur with heavier tails
>>>
>>> # Example 3: Peaked blur (beta > 1)
>>> peaked = A.Compose([
... A.AdvancedBlur(
... blur_limit=(7, 9),
... sigma_x_limit=(0.6, 0.8),
... sigma_y_limit=(0.6, 0.8),
... rotate_limit=0,
... beta_limit=(3.0, 6.0), # Peaked blur (beta > 1)
... noise_limit=(0.9, 1.1), # Slight noise
... p=1.0
... )
... ])
>>>
>>> peaked_result = peaked(image=image)
>>> peaked_image = peaked_result["image"]
>>> # The image will have a more focused, peaked blur with lighter tails
>>>
>>> # Example 4: Anisotropic blur (directional)
>>> directional = A.Compose([
... A.AdvancedBlur(
... blur_limit=(9, 11),
... sigma_x_limit=(0.8, 1.0), # Stronger x blur
... sigma_y_limit=(0.2, 0.3), # Weaker y blur
... rotate_limit=(0, 0), # No rotation
... beta_limit=(1.0, 2.0),
... noise_limit=(0.9, 1.1),
... p=1.0
... )
... ])
>>>
>>> directional_result = directional(image=image)
>>> directional_image = directional_result["image"]
>>> # The image will have a horizontal directional blur
>>>
>>> # Example 5: Rotated directional blur
>>> rotated = A.Compose([
... A.AdvancedBlur(
... blur_limit=(9, 11),
... sigma_x_limit=(0.8, 1.0), # Stronger x blur
... sigma_y_limit=(0.2, 0.3), # Weaker y blur
... rotate_limit=(45, 45), # 45 degree rotation
... beta_limit=(1.0, 2.0),
... noise_limit=(0.9, 1.1),
... p=1.0
... )
... ])
>>>
>>> rotated_result = rotated(image=image)
>>> rotated_image = rotated_result["image"]
>>> # The image will have a diagonal directional blur
>>>
>>> # Example 6: Noisy blur
>>> noisy = A.Compose([
... A.AdvancedBlur(
... blur_limit=(5, 7),
... sigma_x_limit=(0.4, 0.6),
... sigma_y_limit=(0.4, 0.6),
... rotate_limit=(-30, 30),
... beta_limit=(0.8, 1.2),
... noise_limit=(0.7, 1.3), # Strong noise variation
... p=1.0
... )
... ])
>>>
>>> noisy_result = noisy(image=image)
>>> noisy_image = noisy_result["image"]
>>> # The image will have blur with significant noise in the kernel
>>>
>>> # Example 7: Random parameters (for general augmentation)
>>> random_blur = A.Compose([
... A.AdvancedBlur(p=0.5) # Using default parameter ranges
... ])
>>>
>>> random_result = random_blur(image=image)
>>> random_image = random_result["image"]
>>> # The image may have a random advanced blur applied with 50% probability- This transform is particularly useful for simulating complex, real-world blur effects that go beyond simple Gaussian blur.
- The combination of blur and noise can help in creating more robust models by simulating a wider range of image degradations.
- Extreme values, especially for beta and noise, may result in unrealistic effects and should be used cautiously.