AutoContrast
Targets:
image
volume
Image Types:uint8, float32
Automatically adjust image contrast by stretching the intensity range.
This transform provides two methods for contrast enhancement:
- CDF method (default): Uses cumulative distribution function for more gradual adjustment
- PIL method: Uses linear scaling like PIL.ImageOps.autocontrast
The transform can optionally exclude extreme values from both ends of the intensity range and preserve specific intensity values (e.g., alpha channel).
Arguments
cutofffloat
0
Percentage of pixels to exclude from both ends of the histogram. Range: [0, 100]. Default: 0 (use full intensity range)
- 0 means use the minimum and maximum intensity values found
- 20 means exclude darkest and brightest 20% of pixels
ignoreint | None
Intensity value to preserve (e.g., alpha channel). Range: [0, 255]. Default: None
- If specified, this intensity value will not be modified
- Useful for images with alpha channel or special marker values
methodcdf | pil
cdf
Algorithm to use for contrast enhancement. Default: "cdf"
- "cdf": Uses cumulative distribution for smoother adjustment
- "pil": Uses linear scaling like PIL.ImageOps.autocontrast
pfloat
0.5
Probability of applying the transform. Default: 0.5
Examples
>>> import albumentations as A
>>> # Basic usage
>>> transform = A.AutoContrast(p=1.0)
>>>
>>> # Exclude extreme values
>>> transform = A.AutoContrast(cutoff=20, p=1.0)
>>>
>>> # Preserve alpha channel
>>> transform = A.AutoContrast(ignore=255, p=1.0)
>>>
>>> # Use PIL-like contrast enhancement
>>> transform = A.AutoContrast(method="pil", p=1.0)Notes
- The transform processes each color channel independently
- For grayscale images, only one channel is processed
- The output maintains the same dtype as input
- Empty or single-color channels remain unchanged