Normalization

Abstract base classes

Normalizer abstract base classes

class staintools.normalization.normalizer_abc.FancyNormalizer(**kwargs)[source]

Abstract class for a ‘fancy’ normalizer (inherits from Normalizer). Adds methods for stain matrix and source concentration estimation.

fetch_target_stains()[source]

Fetch the target stain matrix and convert from OD to RGB. Must call fit first (this builds the stain matrix).

Returns:
fit(target)[source]

Fit to a target image.

Parameters:target – Target image RGB uint8.
Returns:
static get_concentrations(I, stain_matrix, lamda=0.01)[source]

Get the concentration matrix. Suppose the input image is H x W x 3 (uint8). Define Npix = H * W. Then the concentration matrix is Npix x 2 (or we could reshape to H x W x 2). The first element of each row is the Hematoxylin concentration. The second element of each row is the Eosin concentration.

We do this by ‘solving’ OD = C*S (Matrix product) where OD is optical density (Npix x 3), C is concentration (Npix x 2) and S is stain matrix (2 x 3). See docs for spams.lasso.

We restrict the concentrations to be positive and penalise very large concentration values, so that background pixels (which can not easily be expressed in the Hematoxylin-Eosin basis) have low concentration and thus appear white.

Parameters:
  • I – Image. A np array HxWx3 of type uint8.
  • stain_matrix – a 2x3 stain matrix. First row is Hematoxylin stain vector, second row is Eosin stain vector.
Returns:

The Nx2 concentration matrix, where N=H*W is number of pixels.

get_stain_matrix(I, *args)[source]

Estimate stain matrix given an image and relevant method parameters

hematoxylin(I)[source]

Hematoxylin channel extraction.

Parameters:I – Image RGB uint8.
Returns:
transform(I)[source]

Transform an image.

Parameters:I – Image RGB uint8.
Returns:
class staintools.normalization.normalizer_abc.Normaliser(**kwargs)[source]

Abstract base class for normalizers. Defines some necessary methods to be considered a normalizer.

fit(target)[source]

Fit the normalizer to an target image

transform(I)[source]

Transform an image to the target stain

Reinhard method

class staintools.normalization.reinhard.ReinhardNormalizer(**kwargs)[source]

Normalize a patch stain to the target image using the method of: 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.

fit(target)[source]

Fit to a target image

Parameters:target – Image RGB uint8.
Returns:
get_mean_std(I)[source]

Get mean and standard deviation of each channel.

Parameters:I – Image RGB uint8.
Returns:
static lab_split(I)[source]

Convert from RGB uint8 to LAB and split into channels.

Parameters:I – Image RGB uint8.
Returns:
static merge_back(I1, I2, I3)[source]

Take seperate LAB channels and merge back to give RGB uint8.

Parameters:
  • I1 – L
  • I2 – A
  • I3 – B
Returns:

Image RGB uint8.

transform(I)[source]

Transform an image.

Parameters:I – Image RGB uint8.
Returns:

Macenko method

class staintools.normalization.macenko.MacenkoNormalizer(**kwargs)[source]

Stain normalization based on the method of: 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.

fit(target)[source]

Fit to a target image.

Parameters:target – Image RGB uint8.
Returns:
static get_stain_matrix(I, beta=0.15, alpha=1)[source]

Get the stain matrix (2x3). First row H and second row E. See the original paper for details.

Parameters:
  • I – Image RGB uint8.
  • beta
  • alpha
Returns:

transform(I)[source]

Transform an image.

Parameters:I – Image RGB uint8.
Returns:

Vahadane method

class staintools.normalization.vahadane.VahadaneNormalizer(**kwargs)[source]

Stain normalization inspired by method of: 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.

static get_stain_matrix(I, threshold=0.8, lamda=0.1)[source]

Get 2x3 stain matrix. First row H and second row E. See the original paper for details. Also see spams docs.

Parameters:
  • I – Image RGB uint8.
  • threshold
  • lamda
Returns: