ImagesNormalization Module

Contains utilities for normalizing images for machine learning and computer vision tasks.

HMB.ImagesNormalization.RGB2LAB(img, isNorm=True)[source]

Convert an RGB image to the LAB color space and normalize the channels.

Parameters:
  • img (numpy.ndarray) – Input RGB image.

  • isNorm (bool) – Normalize the LAB channels.

Returns:

Normalized LAB channels (L, A, B).

Return type:

tuple

HMB.ImagesNormalization.LAB2RGB(I1, I2, I3, isNorm=True)[source]

Convert normalized LAB channels back to the RGB color space.

Parameters:
Returns:

RGB image.

Return type:

numpy.ndarray

HMB.ImagesNormalization.LABSplit2RGB(I1, I2, I3)[source]

Convert LAB channels back to RGB color space.

Parameters:
Returns:

RGB image.

Return type:

numpy.ndarray

HMB.ImagesNormalization.LabSplitMeanStd(img)[source]

Compute the mean and standard deviation of the LAB channels of an RGB image.

Parameters:

img (numpy.ndarray) – Input RGB image.

Returns:

Means and standard deviations of the LAB channels.

Return type:

tuple

HMB.ImagesNormalization.FindStainMatrixVahadane(img, beta=0.15, lambda1=0.01)[source]

Find the stain matrix using Vahadane’s method (Sparse Non-negative Matrix Factorization). It has multiple safeguards to ensure robustness, including fallbacks to a default stain matrix if the input image is mostly blank or if SPAMS fails. First converts the image to Optical Density (OD) space, filters out low OD values, and then applies SPAMS to learn the stain matrix. The resulting matrix is normalized and ordered to ensure Hematoxylin is the first row.

Parameters:
  • img (numpy.ndarray) – Input RGB image (range [0, 255]).

  • beta (float) – Threshold for OD values.

  • lambda1 (float) – Sparsity penalty for dictionary learning.

Returns:

Stain matrix (2x3).

Return type:

numpy.ndarray

class HMB.ImagesNormalization.VahadaneColorNormalization(beta=0.15, lambda1=0.01)[source]

Bases: object

Class for performing Vahadane color normalization on histopathology images. Uses Sparse Non-negative Matrix Factorization (SNMF) to estimate the stain matrix.

Variables:
  • targetStainMatrix (numpy.ndarray) – Target stain matrix.

  • targetConcentrationsMax (numpy.ndarray) – Target maximum concentrations for each stain.

  • isFit (bool) – Flag to indicate if the model has been fitted.

  • beta (float) – Threshold for OD values.

  • lambda1 (float) – Sparsity penalty for dictionary learning.

Initialize the VahadaneColorNormalization object.

__init__(beta=0.15, lambda1=0.01)[source]

Initialize the VahadaneColorNormalization object.

Fit(img)[source]

Fit the model to a target image by calculating its stain matrix and max concentrations.

Parameters:

img (numpy.ndarray) – Target RGB image.

Normalize(img)[source]

Normalize an input image using the fitted target stain matrix and concentrations.

Parameters:

img (numpy.ndarray) – Input RGB image.

Returns:

Normalized RGB image.

Return type:

numpy.ndarray

class HMB.ImagesNormalization.MacenkoColorNormalization(beta=0.15, alpha=1.0)[source]

Bases: object

Class for performing Macenko color normalization on histopathology images.

Variables:
  • targetStainMatrix (numpy.ndarray) – Target stain matrix.

  • targetConcentrationsMax (numpy.ndarray) – Target maximum concentrations for each stain.

  • isFit (bool) – Flag to indicate if the model has been fitted.

  • beta (float) – Threshold for OD values.

  • alpha (float) – Percentile for stain orientation.

Initialize the MacenkoColorNormalization object.

__init__(beta=0.15, alpha=1.0)[source]

Initialize the MacenkoColorNormalization object.

Fit(img)[source]

Fit the model to a target image by calculating its stain matrix and max concentrations.

Parameters:

img (numpy.ndarray) – Target RGB image.

Normalize(img)[source]

Normalize an input image using the fitted target stain matrix and concentrations.

Parameters:

img (numpy.ndarray) – Input RGB image.

Returns:

Normalized RGB image.

Return type:

numpy.ndarray

class HMB.ImagesNormalization.ReinhardColorNormalization[source]

Bases: object

Class for performing Reinhard color normalization on histopathology images.

Variables:
  • targetMeans (list) – Target means for LAB channels.

  • targetStds (list) – Target standard deviations for LAB channels.

  • isFit (bool) – Flag to indicate if the model has been fitted.

  • eps (float) – Small epsilon to avoid division by zero.

Initialize the ReinhardColorNormalization object.

__init__()[source]

Initialize the ReinhardColorNormalization object.

Fit(img)[source]

Fit the model to a target image by calculating its LAB means and standard deviations.

Parameters:

img (numpy.ndarray) – Target RGB image.

Normalize(img)[source]

Normalize an input image using the fitted target means and standard deviations.

Parameters:

img (numpy.ndarray) – Input RGB image.

Returns:

Normalized RGB image.

Return type:

numpy.ndarray

HMB.ImagesNormalization.GetFitNormalizer(slide, normSize=(8192, 8192), method='reinhard')[source]

Create and fit a normalizer for a given slide.

Parameters:
  • slide (OpenSlide) – The OpenSlide object for the slide.

  • normSize (tuple) – The size of the region to fit the normalizer.

  • method (str) – The normalization method to use (“reinhard”, “macenko”, “vahadane”, or “histogram”).

Returns:

The fitted normalizer.

Return type:

object

HMB.ImagesNormalization.CreateAverageHistogram(refImages, noChannels=4)[source]

Create an average histogram from a list of reference images.

Parameters:
  • refImages (list) – List of reference images.

  • noChannels (int) – Number of channels in the images.

Returns:

Average histogram for each channel.

Return type:

list

HMB.ImagesNormalization.CreateLUTFromHistogram(avgHist)[source]

Create a Look-Up Table (LUT) from an average histogram.

Parameters:

avgHist (list) – Average histogram for each channel.

Returns:

LUT for each channel.

Return type:

list

HMB.ImagesNormalization.ApplyLUT(image, lut, noChannels=4)[source]

Apply a Look-Up Table (LUT) to an image.

Parameters:
  • image (numpy.ndarray) – Input image.

  • lut (list) – LUT for each channel.

  • noChannels (int) – Number of channels in the image.

Returns:

Image with LUT applied.

Return type:

numpy.ndarray

HMB.ImagesNormalization.RGB2OD(img)[source]

Convert an RGB image to Optical Density (OD) space.

Parameters:

img (numpy.ndarray) – Input RGB image (range [0, 255]).

Returns:

Image in OD space.

Return type:

numpy.ndarray

HMB.ImagesNormalization.OD2RGB(img)[source]

Convert an image from Optical Density (OD) space back to RGB.

Parameters:

img (numpy.ndarray) – Image in OD space.

Returns:

RGB image (range [0, 255]).

Return type:

numpy.ndarray

HMB.ImagesNormalization.StandarizeBrightness(img)[source]

Standardize the brightness of an image using the 90th percentile.

Parameters:

img (numpy.ndarray) – Input image (assumed to be in range [0, 255]).

Returns:

Brightness-standardized image.

Return type:

numpy.ndarray

HMB.ImagesNormalization.NormalizeRows(A)[source]

Normalize the rows of a matrix.

Parameters:

A (numpy.ndarray) – Input matrix.

Returns:

Matrix with normalized rows.

Return type:

numpy.ndarray

HMB.ImagesNormalization.FindStainMatrixMacenko(img, beta=0.15, alpha=1.0)[source]

Find the stain matrix using Macenko’s method.

Parameters:
  • img (numpy.ndarray) – Input RGB image (range [0, 255]).

  • beta (float) – Threshold for OD values.

  • alpha (float) – Percentile for stain orientation.

Returns:

Stain matrix (2x3).

Return type:

numpy.ndarray

HMB.ImagesNormalization.ConcentrateStainMatrixMacenko(img, stainMatrix, lambdaVal=0.01)[source]

Concentrate the stain matrix using LASSO.

Parameters:
  • img (numpy.ndarray) – Input RGB image (range [0, 255]).

  • stainMatrix (numpy.ndarray) – Stain matrix (2x3).

  • lambdaVal (float) – L1 penalty for LASSO.

Returns:

Concentrated stain matrix (2x3).

Return type:

numpy.ndarray

HMB.ImagesNormalization.ApplyReinhard(img, targetMeans, targetStds, eps=1e-06)[source]

Apply Reinhard color normalization to an image.

Parameters:
  • img (numpy.ndarray) – Input image.

  • targetMeans (list) – Target means for LAB channels.

  • targetStds (list) – Target standard deviations for LAB channels.

  • eps (float) – Small epsilon to avoid division by zero.

Returns:

Normalized image.

Return type:

numpy.ndarray

class HMB.ImagesNormalization.HistogramColorNormalization[source]

Bases: object

Class for performing Histogram Matching (Specification) color normalization.

Variables:
  • targetHist (list) – Target histograms for each channel.

  • isFit (bool) – Flag to indicate if the model has been fitted.

Initialize the HistogramColorNormalization object.

__init__()[source]

Initialize the HistogramColorNormalization object.

Fit(img)[source]

Fit the model to a target image by calculating its channel histograms.

Parameters:

img (numpy.ndarray) – Target RGB image.

Normalize(img)[source]

Normalize an input image using the fitted target histograms.

Parameters:

img (numpy.ndarray) – Input RGB image.

Returns:

Normalized RGB image.

Return type:

numpy.ndarray