Apply a random power-law correction (random gamma) to image intensities, brightening or darkening midtones to simulate exposure and display variation.
Gamma correction, or simply gamma, is a nonlinear operation used to encode and decode luminance or tristimulus values in imaging systems. This transform can adjust the brightness of an image while preserving the relative differences between darker and lighter areas, making it useful for simulating different lighting conditions or correcting for display characteristics.
gamma_rangeLower and upper bounds for a uniformly sampled value that is divided by 100 to obtain the exponent. For example, (80, 120) gives exponents from 0.8 to 1.2, while (100, 100) gives an exponent of 1. Default: (80, 120).
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.RandomGamma(p=1.0)
>>> augmented_image = transform(image=image)["image"]
# Custom gamma range
>>> transform = A.RandomGamma(gamma_range=(50, 150), p=1.0)
>>> augmented_image = transform(image=image)["image"]
# A fixed exponent of 2 darkens a normalized midtone
>>> midtone = np.full((2, 2, 3), 0.5, dtype=np.float32)
>>> transform = A.RandomGamma(gamma_range=(200, 200), p=1.0)
>>> float(transform(image=midtone)["image"][0, 0, 0])
0.25
# Applying with other transforms
>>> transform = A.Compose([
... A.RandomGamma(gamma_range=(80, 120), p=0.5),
... A.RandomBrightnessContrast(p=0.5),
... ])
>>> augmented_image = transform(image=image)["image"]