AlbumentationsExplore
DocumentationExploreAdoptionCommercial LicenseBlog
GitHub

RandomGamma

Targets:
image
volume
Image Types:uint8, float16, float32

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.

Arguments
gamma_range
tuple[float, float]
[80, 120]

Lower 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).

p
float
0.5

Probability of applying the transform. Default: 0.5.

Examples
>>> 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"]
Notes
  • For normalized intensities in [0, 1], gamma > 1 darkens the image, while 0 < gamma < 1 brightens it. Gamma = 1 leaves intensities unchanged.
  • This transform is particularly useful for:
    • Simulating different lighting conditions
    • Correcting for non-linear display characteristics
    • Enhancing contrast in certain regions of the image
    • Data augmentation in computer vision tasks
References
  • Gamma correctionhttps://en.wikipedia.org/wiki/Gamma_correction
  • Power law (Gamma) encodinghttps://www.cambridgeincolour.com/tutorials/gamma-correction.htm