Defocus

Targets:
image
volume
Image Types:uint8, float32

Apply defocus blur to the input image.

This transform simulates the effect of an out-of-focus camera by applying a defocus blur to the image. It uses a combination of disc kernels and Gaussian blur to create a realistic defocus effect.

Arguments
radius
tuple[int, int] | int
[3,10]

Range for the radius of the defocus blur. If a single int is provided, the range will be [1, radius]. Larger values create a stronger blur effect. Default: (3, 10)

alias_blur
tuple[float, float] | float
[0.1,0.5]

Range for the standard deviation of the Gaussian blur applied after the main defocus blur. This helps to reduce aliasing artifacts. If a single float is provided, the range will be (0, alias_blur). Larger values create a smoother, more aliased effect. Default: (0.1, 0.5)

p
float
0.5

Probability of applying the transform. Should be in the range [0, 1]. 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 defocus 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, "Sharp Text", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
>>>
>>> # Example 1: Subtle defocus effect (small aperture)
>>> subtle_transform = A.Compose([
...     A.Defocus(
...         radius=(2, 3),           # Small defocus radius
...         alias_blur=(0.1, 0.2),   # Minimal aliasing
...         p=1.0                    # Always apply
...     )
... ])
>>>
>>> subtle_result = subtle_transform(image=image)
>>> subtle_defocus = subtle_result["image"]
>>> # The image will have a subtle defocus effect, with just slight blurring
>>>
>>> # Example 2: Moderate defocus effect (medium aperture)
>>> moderate_transform = A.Compose([
...     A.Defocus(
...         radius=(4, 6),           # Medium defocus radius
...         alias_blur=(0.2, 0.3),   # Moderate aliasing
...         p=1.0
...     )
... ])
>>>
>>> moderate_result = moderate_transform(image=image)
>>> moderate_defocus = moderate_result["image"]
>>> # The image will have a noticeable defocus effect, similar to a poorly focused camera
>>>
>>> # Example 3: Strong defocus effect (large aperture)
>>> strong_transform = A.Compose([
...     A.Defocus(
...         radius=(8, 12),          # Large defocus radius
...         alias_blur=(0.4, 0.6),   # Strong aliasing
...         p=1.0
...     )
... ])
>>>
>>> strong_result = strong_transform(image=image)
>>> strong_defocus = strong_result["image"]
>>> # The image will have a strong defocus effect, heavily blurring the details
>>>
>>> # Example 4: Using in a pipeline with other transforms
>>> pipeline = A.Compose([
...     A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.7),
...     A.Defocus(radius=(3, 8), alias_blur=0.3, p=0.5),  # 50% chance of applying defocus
...     A.GaussNoise(var_limit=(10, 30), p=0.3)           # Possible noise after defocus
... ])
>>>
>>> pipeline_result = pipeline(image=image)
>>> transformed_image = pipeline_result["image"]
>>> # The image may have defocus blur applied with 50% probability
Notes
  • The defocus effect is created using a disc kernel, which simulates the shape of a camera's aperture.
  • The additional Gaussian blur (alias_blur) helps to soften the edges of the disc kernel, creating a more natural-looking defocus effect.
  • Larger radius values will create a stronger, more noticeable defocus effect.
  • The alias_blur parameter can be used to fine-tune the appearance of the defocus, with larger values creating a smoother, potentially more realistic effect.