jgamper / Stain_Normalization

Some stain normalization code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overview

Implementation of a few common stain normalization techniques (Reinhard, Macenko, Vahadane) in Python (tested on 3.5). Please refer to the linked papers for details on the methods.

For example usage see the notebook demo.ipynb

In short do something like (all techniques have the same API, where we create a stain normalization object or Normalizer. The fit and transform methods are then the most important).

n = normalizers.ReinhardNormalizer()
n.fit(target_image)
out = n.transform(source_image)

If you want Hematoxylin do something like (Macenko or Vahadane methods only)

n = normalizers.VahadaneNormalizer()
out = n.hematoxylin(source_image)

We can also view the stains separated by e.g. (Macenko or Vahadane methods only)

n = normalizers.VahadaneNormalizer()
n.fit(target_image)
stain_utils.show_colors(n.target_stains())

We use the SPAMS (SPArse Modeling Software) package. Use with Python via e.g https://anaconda.org/conda-forge/python-spams

Below we show the application of the techniques to a few images (in data folder). We normalize to the first image and for Macenko and Vahadane also show the extracted Hematoxylin channel. Below that are a few more challenging images (also in data folder). All images are taken from the ICIAR 2018 challenge.

One change to the vanilla methods is used. With all images we first apply a brightness standardizing step (below). This is especially useful in handling the more challenging images (which are typically too dim) and does not damage performance for the other images.

def standardize_brightness(I):
    """
    An image is a numpy array of size HxWx3 (RGB) with integer values in the range 0-255 (uint8).
    We standardize so 10% of the elements of this array take the value 255.
    For already bright images this makes little change. For dark images this makes a significant difference,
    :param I:
    :return:
    """
    p = np.percentile(I, 90)
    return np.clip(I * 255.0 / p, 0, 255).astype(np.uint8)

Some simple examples

Original images

Reinhard

E. Reinhard, M. Adhikhmin, B. Gooch, and P. Shirley, ‘Color transfer between images’, IEEE Computer Graphics and Applications, vol. 21, no. 5, pp. 34–41, Sep. 2001.

Macenko

M. Macenko et al., ‘A method for normalizing histology slides for quantitative analysis’, in 2009 IEEE International Symposium on Biomedical Imaging: From Nano to Macro, 2009, pp. 1107–1110.

Vahadane

A. Vahadane et al., ‘Structure-Preserving Color Normalization and Sparse Stain Separation for Histological Images’, IEEE Transactions on Medical Imaging, vol. 35, no. 8, pp. 1962–1971, Aug. 2016.

More challenging images

Original images

Reinhard

Macenko

Vahadane

About

Some stain normalization code

License:MIT License


Languages

Language:Jupyter Notebook 99.8%Language:Python 0.2%