Randomly adjust brightness and contrast with separate ranges. Simple and fast; good baseline color augmentation for classification and detection.
This transform adjusts the brightness and contrast of an image simultaneously, allowing for a wide range of lighting and contrast variations. It's particularly useful for data augmentation in computer vision tasks, helping models become more robust to different lighting conditions.
brightness_rangeFactor range for changing brightness, sampled per image. Values should typically be in the range [-1.0, 1.0], where 0 means no change, 1.0 means maximum brightness, and -1.0 means minimum brightness. Default: (-0.2, 0.2).
contrast_rangeFactor range for changing contrast, sampled per image. Values should typically be in the range [-1.0, 1.0], where 0 means no change, 1.0 means maximum increase in contrast, and -1.0 means maximum decrease in contrast. Default: (-0.2, 0.2).
brightness_by_maxIf True, adjusts brightness by scaling pixel values up to the maximum value of the image's dtype. If False, uses the mean pixel value for adjustment. Default: True.
ensure_safe_outputIf True, adjusts alpha and beta to prevent overflow/underflow. This keeps output values inside the valid range for the image dtype without clipping. Default: False.
pProbability of applying the transform. Default: 0.5.
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, [100, 100, 3], dtype=np.uint8)
# Default usage
>>> transform = A.RandomBrightnessContrast(p=1.0)
>>> augmented_image = transform(image=image)["image"]
# Custom brightness and contrast limits
>>> transform = A.RandomBrightnessContrast(
... brightness_range=(-0.3, 0.3),
... contrast_range=(-0.3, 0.3),
... p=1.0,
... )
>>> augmented_image = transform(image=image)["image"]
# Adjust brightness based on mean value
>>> transform = A.RandomBrightnessContrast(
... brightness_range=(-0.2, 0.2),
... contrast_range=(-0.2, 0.2),
... brightness_by_max=False,
... p=1.0,
... )
>>> augmented_image = transform(image=image)["image"]brightness_by_max parameter affects how brightness is adjusted: