• AlbumentationsAlbumentations
All TransformsGet LicenseDocumentationNews & Insights
Report IssueJoin Discord...

Defocus

Targets:
image
Image Types:uint8, float32

Simulate out-of-focus lens via a disc-shaped kernel plus optional Gaussian alias blur. Strength and edge softness via radius_range and alias_blur_range.

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

Range for the radius of the defocus blur. Larger values create a stronger blur effect. Default: (3, 10)

alias_blur_range
tuple[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. 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_range=(2, 3),           # Small defocus radius
...         alias_blur_range=(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_range=(4, 6),           # Medium defocus radius
...         alias_blur_range=(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_range=(8, 12),          # Large defocus radius
...         alias_blur_range=(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_range=(-0.1, 0.1), contrast_range=(-0.1, 0.1), p=0.7),
...     A.Defocus(radius_range=(3, 8), alias_blur_range=(0.3, 0.3), p=0.5),  # 50% chance of applying defocus
...     A.GaussNoise(std_range=(0.04, 0.15), 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_range) helps to soften the edges of the disc kernel, creating a more natural-looking defocus effect.
  • Larger radius_range values will create a stronger, more noticeable defocus effect.
  • The alias_blur_range parameter can be used to fine-tune the appearance of the defocus, with larger values creating a smoother, potentially more realistic effect.
References
  • Defocus aberrationhttps://en.wikipedia.org/wiki/Defocus_aberration