StainTools¶
Tools for stain normalization and augmentation in Python (tested on 3.5). GitHub repository here.
Latest build:
Install¶
pip install staintools
NOTE: StainTools requires the SPAMS (SPArse Modeling Software) package. Please find out about this here. This may be installed via conda. For example, see here.
Contents¶
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:
-
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.
-
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.
-
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:
-
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.
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.
Utils¶
Visualization Utils¶
Visualization utilities.
-
staintools.utils.visual.
build_stack
(images)[source]¶ Build a stack of images from a tuple/list of images.
Parameters: images – A tuple/list of images. Returns:
-
staintools.utils.visual.
patch_grid
(ims, width=5, sub_sample=False, rand=False, save_name=None)[source]¶ Display a grid of patches.
Parameters: - ims – A patch ‘stack’
- width – Images per row.
- sub_sample – Should we take a subsample?
- rand – Randomize subsample?
Returns:
-
staintools.utils.visual.
read_image
(path)[source]¶ Read an image to RGB uint8. Read with opencv (cv) and covert from BGR colorspace to RGB.
Parameters: path – The path to the image. Returns: RGB uint8 image.
Misc Utils¶
Other utilities.
-
staintools.utils.misc.
OD_to_RGB
(OD)[source]¶ Convert from optical density (OD_RGB) to RGB RGB = 255 * exp(-1*OD_RGB)
Parameters: OD – Optical denisty RGB image. Returns: Image RGB uint8.
-
staintools.utils.misc.
RGB_to_OD
(I)[source]¶ Convert from RGB to optical density (OD_RGB) space. RGB = 255 * exp(-1*OD_RGB).
Parameters: I – Image RGB uint8. Returns: Optical denisty RGB image.
-
staintools.utils.misc.
array_equal
(A, B, eps=1e-09)[source]¶ Are arrays A and B equal?
Parameters: - A – Array.
- B – Array.
- eps – Tolerance.
Returns: True/False.
-
staintools.utils.misc.
check_image
(x)[source]¶ Check if is an image. If gray make sure it is ‘squeezed’ correctly.
Parameters: x – Input. Returns: True/False.
-
staintools.utils.misc.
is_gray_image
(x)[source]¶ Is x a gray image?
Parameters: x – Input. Returns: True/False.
-
staintools.utils.misc.
is_image
(x)[source]¶ Is x an image? i.e. numpy array of 2 or 3 dimensions.
Parameters: x – Input. Returns: True/False.
-
staintools.utils.misc.
is_uint8_image
(x)[source]¶ Is x a uint8 image?
Parameters: x – Input. Returns: True/False.
-
staintools.utils.misc.
normalize_rows
(A)[source]¶ Normalize the rows of an array.
Parameters: A – An array. Returns: Array with rows normalized.
-
staintools.utils.misc.
notwhite_mask
(I, thresh=0.8)[source]¶ Get a binary mask where true denotes ‘not white’. Specifically, a pixel is not white if its luminance (in LAB color space) is less than the specified threshold.
Parameters: - I – RGB uint 8 image.
- thresh – Luminosity threshold.
Returns: Binary mask where true denotes ‘not white’.
-
staintools.utils.misc.
remove_zeros
(I)[source]¶ Remove zeros in an image, replace with 1’s.
Parameters: I – An Array. Returns: New array where 0’s have been replaced with 1’s.