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:
- HMB.ImagesNormalization.LAB2RGB(I1, I2, I3, isNorm=True)[source]¶
Convert normalized LAB channels back to the RGB color space.
- Parameters:
I1 (numpy.ndarray) – Normalized L channel.
I2 (numpy.ndarray) – Normalized A channel.
I3 (numpy.ndarray) – Normalized B channel.
- Returns:
RGB image.
- Return type:
- HMB.ImagesNormalization.LABSplit2RGB(I1, I2, I3)[source]¶
Convert LAB channels back to RGB color space.
- Parameters:
I1 (numpy.ndarray) – L channel.
I2 (numpy.ndarray) – A channel.
I3 (numpy.ndarray) – B channel.
- Returns:
RGB image.
- Return type:
- 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:
- 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:
- class HMB.ImagesNormalization.VahadaneColorNormalization(beta=0.15, lambda1=0.01)[source]¶
Bases:
objectClass 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.
- 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:
- class HMB.ImagesNormalization.MacenkoColorNormalization(beta=0.15, alpha=1.0)[source]¶
Bases:
objectClass 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.
- 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:
- class HMB.ImagesNormalization.ReinhardColorNormalization[source]¶
Bases:
objectClass for performing Reinhard color normalization on histopathology images.
- Variables:
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:
- HMB.ImagesNormalization.GetFitNormalizer(slide, normSize=(8192, 8192), method='reinhard')[source]¶
Create and fit a normalizer for a given slide.
- HMB.ImagesNormalization.CreateAverageHistogram(refImages, noChannels=4)[source]¶
Create an average histogram from a list of reference images.
- HMB.ImagesNormalization.CreateLUTFromHistogram(avgHist)[source]¶
Create a Look-Up Table (LUT) from an average histogram.
- 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:
- 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:
- 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:
- 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:
- HMB.ImagesNormalization.NormalizeRows(A)[source]¶
Normalize the rows of a matrix.
- Parameters:
A (numpy.ndarray) – Input matrix.
- Returns:
Matrix with normalized rows.
- Return type:
- 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:
- 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:
- 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:
- class HMB.ImagesNormalization.HistogramColorNormalization[source]¶
Bases:
objectClass for performing Histogram Matching (Specification) color normalization.
- Variables:
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: