A method to normalize H and E images for Image Processing and Quantitative Testing, this is referenced from a paper - https://www.cs.unc.edu/~mn/sites/default/files/macenko2009.pdf
Setting up the tool:
- Cloning the repository:
git clone https://github.com/unikill066/Normalize-H-and-E-images.git
- Install all the necessary libraries:
pip install -r requirements.txt
to run the tool in 2 ways; from the terminal/any python IDE or from Jupyter Notebook/Lab(.ipynb)
2.1 Running the tool from terminal:
- Windows:
python h_and_e_nrom_script.py C:\path\to\neuron\images C:\path\to\output\directory
- Linux and MacOS:
python h_and_e_nrom_script.py /path/to/neuron/images /path/to/output/directory
2.2 Running the tool from Jupyter Notebook/Lab:
- Spin a jpyter notebook instance from the conda/miniconda terminal:
jupyter notebook
- Run the code cell(s) to generate the normalized images.
Output:
The output is put in 3 folders in /path/to/output/directory
:
- h - hematoxylin separated images
- e - eosin separated images
- norm - normalized images
Foot notes on the theory behind this tool:
- H and E stains are applied to a Neuronal Image, where H-Hematoxylin stains for nucleic acids (blue-purple) and eosin stains for proteins (bright pink).
- All color values are converted to their corresponding optical density (OD) values using the formula
OD = -log10(I)
, where I is the RGB color vector, and each component is normalized in the range of[0, 1]
. This transformation results in a linear combination of OD values.OD-hat
represents values that pass a pre-defined filter withbeta=0.15
. - Color deconvolution is performed to transform color values into quantitative values of interest:
OD = V · S S = V^(-1) · OD
where V and S are stain vectors and saturation of each stain, respectively
- The single value decomposition (SVD) is applied to the OD tuples, creating a plane from SVD directions corresponding to the two largest singular values. This plane is then projected and normalized.
- NMF-based algorithms attempt to factor the OD matrix into V and S using an algorithm that automatically finds the correct stain vectors for the image and performs color deconvolution.
- The angle at each point is calculated with respect to the SVD direction, and the data is normalized between the 1st and 99th percentiles. A value of 0 corresponds to a white pixel, and 1 corresponds to a black pixel, maintaining stability for low OD values with no stains.
- Finally, the values are converted back to OD space to reconstruct an image.