Skip to main content

A package for tissue image stain normalization, augmentation and more.

Project description

StainTools

Tools for tissue image stain normalization and augmentation in Python 3.

https://i.imgur.com/sS8AKaV.png https://i.imgur.com/PzY0fE7.png

Install

pip install staintools + install SPAMS (see below)

StainTools requires the SPAMS (SPArse Modeling Software) package. Please find out about this here. This may be installed via conda. For example, see here. Alternatively, a version is currently available from the PyPI testing site - run pip install --index-url https://test.pypi.org/simple/ spams. Hopefully, SPAMS will soon be available on the main PyPI.

Docs

Histology images are often stained with the Hematoxylin & Eosin (H&E) stains. These two chemicals typically stain: the nuclei a dark purple (Hematoxylin) and the cytoplasm a light pink (Eosin). Thus all pixels in a histology image are principally composed of two colors. These stain colors vary from image to image and may be summarised in a stain matrix:

\begin{equation*} S = \left( \begin{array}{ccc} H_R & H_G & H_B \\ E_R & E_G & E_B \end{array} \right) \end{equation*}

The first row of this matrix shows the Hematoxylin stain color in RGB. The second row of the matrix shows the Eosin stain color in RGB. Strictly speaking these RGB values should be interpreted in optical density space. We transform a normal RGB image to a RGB optical density image via the Beer-Lambert Law:

\begin{equation*} I = 255 \times \exp(-OD) \end{equation*}

If we flatten the OD image so that it is Npix x 3, with Npix = h x w and h and w the original image height and width, then we can relate the OD array and the stain matrix via the pixel concentration matrix C (a Npix x 2 array where the columns give the pixel concentration of H and E respectively):

\begin{equation*} OD_{flat} = C S \end{equation*}

A StainExtractor provides methods for estimating a stain matrix and a concentration matrix given an image. We implement:

  • MacenkoStainExtractor. Stain matrix estimation via method of M. Macenko et al.,“A method for normalizing histology slides for quantitative analysis,”. This method considers the projection of pixels onto the 2D plane defined by the two principle eigenvectors of the optical density covariance matrix. It then considers the extreme directions (in terms of angular polar coordinate) in this plane. See the paper for details.

  • VahadaneStainExtractor. Stain matrix estimation via method of A. Vahadane et al., “Structure-Preserving Color Normalization and Sparse Stain Separation for Histological Images,”. This method takes a dictionary learning based approach to find the two basis stains that best fit the image. See the paper for details.

In both cases the first step is to attempt to remove background pixels - pixels of the image where no tissue was present. In principle these should be just white light therefore we isolate tissue by a thresholding on the pixel luminosity. For some images that are dimly lit however the background is not bright enough and therefore it is recommended to standardize the brightness of any image first. For this we implement a BrightnessStandardizer, which enforces an image to have at least 5% of pixels being luminosity saturated. To understand this better it is recommended to see the demo notebook demo_brightness_standardizer_and_luminosity_mask.

Once we have the stain and concentration matrices we are able to easily carry out.

  • Stain Normalization. This basically involves casting one image in the stain colors of a target image. For this we basically decompose the images into the stain matrix S and the concentration matrix C, then replace the stain matrix of the image to be transformed with that of the target image. We then recombine to give the final stain normalized image. This is implemented in the class StainNormalizer. See the demo notebook demo_stain_normalizer for an example.

  • Stain Augmentation. For this we simply augment a single image by decomposing it into the stain matrix S and the concentration matrix C, perturbing the concentrations somewhat and then recombining to be get an augmented image. This is implemented in the class StainAugmentor. See the demo notebook demo_stain_augmentor for an example.

We also implement a simpler color normalizer, the ReinhardColorNormalizer, which normalizes images according to the method of E. Reinhard, M. Adhikhmin, B. Gooch, and P. Shirley, ‘Color transfer between images’. This method does not consider the details of stain matrices etc. Instead it simply maps the color distribution mean and standard deviation to match that of another target image. See the demo notebook demo_reinhard_color_normalizer for an example.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

staintools-1.2.4.tar.gz (11.5 kB view details)

Uploaded Source

File details

Details for the file staintools-1.2.4.tar.gz.

File metadata

  • Download URL: staintools-1.2.4.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.5.5

File hashes

Hashes for staintools-1.2.4.tar.gz
Algorithm Hash digest
SHA256 07c3aea245a12f625213c4b22e01120634d3b12514ce27f89de6ec33957d9a7e
MD5 240a450b0d94a1e4ad326944668521df
BLAKE2b-256 84b197f90faf17785556322222e8f9e842bc8cab8ee0acc7c60d45107aacb403

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page