Map a single-channel grayscale image to a 2- or 3-color RGB gradient with per-call sampled
anchor colors (Pillow ImageOps.colorize style).
Intensity acts as a coordinate along a sampled color ramp:
0 maps to a sample from black_range255 (or 1.0 for float32) maps to a sample from white_rangemid_range is set, intensity sampled from mid_value_range maps to a sample from
mid_range and the ramp becomes piecewise linearEach anchor range is given as (low_rgb, high_rgb) and sampled per-channel uniformly on
every call. Pass identical low/high tuples to fix a color
(e.g. black_range=((0, 0, 255), (0, 0, 255))). Anchors are always specified in 0-255 RGB;
for float32 inputs they are rescaled to [0, 1] internally.
black_rangeInclusive per-channel range from which the dark anchor is sampled. Default: ((0, 0, 0), (0, 0, 0)).
white_rangeInclusive per-channel range from which the bright anchor is sampled. Default: ((255, 255, 255), (255, 255, 255)).
mid_rangeOptional inclusive
range from which the midpoint anchor is sampled. None disables 3-color mode.
Default: None.
mid_value_rangeInclusive intensity range (each in 1-254) from which
the midpoint position is sampled. Ignored when mid is None. Default: (127, 127).
pProbability of applying the transform. Default: 0.5.
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 1), dtype=np.uint8)
>>>
>>> # Fixed blue -> yellow ramp (low == high)
>>> fixed = A.Compose([A.Colorize(
... black_range=((0, 0, 255), (0, 0, 255)),
... white_range=((255, 255, 0), (255, 255, 0)),
... p=1.0,
... )])
>>> assert fixed(image=image)["image"].shape == (100, 100, 3)
>>>
>>> # Random thermal-ish ramp with random midpoint position
>>> random_thermal = A.Compose([A.Colorize(
... black_range=((0, 0, 64), (32, 0, 192)),
... mid_range=((96, 0, 96), (160, 64, 160)),
... white_range=((220, 160, 0), (255, 220, 32)),
... mid_value_range=(96, 160),
... p=1.0,
... )])
>>> assert random_thermal(image=image)["image"].shape == (100, 100, 3)cv2.LUT;
for float32 inputs np.interp is used per channel.