StainAugmentor¶
- class StainAugmentor(method='vahadane', stain_matrix=None, sigma1=0.4, sigma2=0.2, p=1.0, *, augment_background=False)[source]¶
Stain augmentation using predefined stain matrices or stain extraction methods.
This class performs stain augmentation on RGB histology images by modifying their stain concentration values. It supports two stain extraction methods: Vahadane and Macenko. The augmentation can operate either by extracting a stain matrix from the input image or by using a user-provided stain matrix.
Providing a pre-extracted stain_matrix allows faster augmentation and enables the augmentor to behave like a stain normalizer when the augmentation parameters (sigma1, sigma2) are set to zero. This avoids the need for dictionary learning during stain matrix extraction and is suitable for efficient on-the-fly stain augmentation or normalization.
- Parameters:
method (str) – Stain extraction method to use. Supported values are “vahadane” (default) and “macenko”.
stain_matrix (numpy.ndarray) – Optional pre-extracted stain matrix for a target image. When supplied, the augmentor uses this matrix directly for stain normalization or faster augmentation. If None, the stain matrix is extracted from the input image using the selected method.
sigma1 (float) – Controls the scaling of stain concentrations. The scale factor alpha is sampled from the range [1 - sigma1, 1 + sigma1]. Default is 0.4.
sigma2 (float) – Controls the additive shift of stain concentrations. The shift beta is sampled from the range [-sigma2, sigma2]. Default is 0.2.
augment_background (bool) – Whether to apply stain augmentation to background pixels. If False (default), augmentation is applied only to tissue regions determined using a luminosity-based tissue mask.
p (float) – Probability of applying stain augmentation. If a random draw exceeds p, the input image is returned unchanged. This provides a simple way to control how often augmentation occurs during training.
- stain_normalizer¶
Internal stain normalization object used for matrix and concentration extraction.
- stain_matrix¶
Extracted or user-provided stain matrix.
- Type:
- source_concentrations¶
Stain concentration values extracted from the input image.
- Type:
- n_stains¶
Number of stain channels in the concentration matrix. Typically 2 for H&E images.
- Type:
- tissue_mask¶
Boolean mask indicating tissue regions when background augmentation is disabled.
- Type:
Examples
>>> from tiatoolbox.tools.stainaugment import StainAugmentor >>> import numpy as np >>> >>> stain_matrix = np.array([ ... [0.91633014, -0.20408072, -0.34451435], ... [0.17669817, 0.92528011, 0.33561059], ... ]) >>> >>> augmentor = StainAugmentor(stain_matrix=stain_matrix, p=0.5) >>> augmentor.fit(img) >>> img_aug = augmentor.augment()
Initialize StainAugmentor object.
Methods
Generate a stain-augmented image, applied with probability p.
Extract stain matrix and concentrations from the input image.