HEStain

Targets:
image
volume
Image Types:uint8, float32

Applies H&E (Hematoxylin and Eosin) stain augmentation to histopathology images.

This transform simulates different H&E staining conditions using either:

  1. Predefined stain matrices (8 standard references)
  2. Vahadane method for stain extraction
  3. Macenko method for stain extraction
  4. Custom stain matrices
Arguments
method
preset | random_preset | vahadane | macenko
random_preset

Method to use for stain augmentation:

  • "preset": Use predefined stain matrices
  • "random_preset": Randomly select a preset matrix each time
  • "vahadane": Extract using Vahadane method
  • "macenko": Extract using Macenko method Default: "preset"
preset
ruifrok | macenko | standard | high_contrast | h_heavy | e_heavy | dark | light |

Preset stain matrix to use when method="preset":

  • "ruifrok": Standard reference from Ruifrok & Johnston
  • "macenko": Reference from Macenko's method
  • "standard": Typical bright-field microscopy
  • "high_contrast": Enhanced contrast
  • "h_heavy": Hematoxylin dominant
  • "e_heavy": Eosin dominant
  • "dark": Darker staining
  • "light": Lighter staining Default: "standard"
intensity_scale_range
tuple[float, float]
[0.7,1.3]

Range for multiplicative stain intensity variation. Values are multipliers between 0.5 and 1.5. For example:

  • (0.7, 1.3) means stain intensities will vary from 70% to 130%
  • (0.9, 1.1) gives subtle variations
  • (0.5, 1.5) gives dramatic variations Default: (0.7, 1.3)
intensity_shift_range
tuple[float, float]
[-0.2,0.2]

Range for additive stain intensity variation. Values between -0.3 and 0.3. For example:

  • (-0.2, 0.2) means intensities will be shifted by -20% to +20%
  • (-0.1, 0.1) gives subtle shifts
  • (-0.3, 0.3) gives dramatic shifts Default: (-0.2, 0.2)
augment_background
bool
false

Whether to apply augmentation to background regions. Default: False

Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Create a sample H&E stained histopathology image
>>> # For real use cases, load an actual H&E stained image
>>> image = np.zeros((300, 300, 3), dtype=np.uint8)
>>> # Simulate tissue regions with different staining patterns
>>> image[50:150, 50:150] = np.array([120, 140, 180], dtype=np.uint8)  # Hematoxylin-rich region
>>> image[150:250, 150:250] = np.array([140, 160, 120], dtype=np.uint8)  # Eosin-rich region
>>>
>>> # Example 1: Using a specific preset stain matrix
>>> transform = A.HEStain(
...     method="preset",
...     preset="standard",
...     intensity_scale_range=(0.8, 1.2),
...     intensity_shift_range=(-0.1, 0.1),
...     augment_background=False,
...     p=1.0
... )
>>> result = transform(image=image)
>>> transformed_image = result['image']
>>>
>>> # Example 2: Using random preset selection
>>> transform = A.HEStain(
...     method="random_preset",
...     intensity_scale_range=(0.7, 1.3),
...     intensity_shift_range=(-0.15, 0.15),
...     p=1.0
... )
>>> result = transform(image=image)
>>> transformed_image = result['image']
>>>
>>> # Example 3: Using Vahadane method (requires H&E stained input)
>>> transform = A.HEStain(
...     method="vahadane",
...     intensity_scale_range=(0.7, 1.3),
...     p=1.0
... )
>>> result = transform(image=image)
>>> transformed_image = result['image']
>>>
>>> # Example 4: Using Macenko method (requires H&E stained input)
>>> transform = A.HEStain(
...     method="macenko",
...     intensity_scale_range=(0.7, 1.3),
...     intensity_shift_range=(-0.2, 0.2),
...     p=1.0
... )
>>> result = transform(image=image)
>>> transformed_image = result['image']
>>>
>>> # Example 5: Combining with other transforms in a pipeline
>>> transform = A.Compose([
...     A.HEStain(method="preset", preset="high_contrast", p=1.0),
...     A.RandomBrightnessContrast(p=0.5),
... ])
>>> result = transform(image=image)
>>> transformed_image = result['image']
References
  • [{'description': 'A. C. Ruifrok and D. A. Johnston, "Quantification of histochemical"', 'source': 'Analytical and quantitative cytology and histology, 2001.'}, {'description': 'M. Macenko et al., "A method for normalizing histology slides for', 'source': '2009 IEEE International Symposium on quantitative analysis," 2009 IEEE International Symposium on Biomedical Imaging, 2009.'}]