Tools for tissue image stain normalization and augmentation in Python 3.
pip install staintools
- Install SPAMS. This is a dependency to staintools and is technically available on PyPI (see here). However, personally I have had some issues with the PyPI install and would instead recommend using conda (see here).
Original images:
Stain normalized images:
# Read data
target = staintools.read_image("./data/my_target_image.png")
to_transform = staintools.read_image("./data/my_image_to_transform.png")
# Standardize brightness (optional, can improve the tissue mask calculation)
target = staintools.LuminosityStandardizer.standardize(target)
to_transform = staintools.LuminosityStandardizer.standardize(to_transform)
# Stain normalize
normalizer = staintools.StainNormalizer(method='vahadane')
normalizer.fit(target)
transformed = normalizer.transform(to_transform)
# Read data
to_augment = staintools.read_image("./data/my_image_to_augment.png")
# Standardize brightness (optional, can improve the tissue mask calculation)
to_augment = staintools.LuminosityStandardizer.standardize(to_augment)
# Stain augment
augmentor = staintools.StainAugmentor(method='vahadane', sigma1=0.2, sigma2=0.2)
augmentor.fit(to_augment)
augmented_images = []
for _ in range(100):
augmented_image = augmentor.pop()
augmented_images.append(augmented_image)
For more examples see files inside of the examples
directory.