How to extract stain matrix from image using Color Deconvolution code?

Hello, I’m a student currently learning about color deconvolution methods. I was wondering how I can extract the stain matrix from an image using the Ruifrok et al. colors separation script found at this link?

My goal is to obtain the stain matrix of an H&E image and use this stain matrix in non-negative matrix factorization (NMF) to separate the stains of the image. I think I’m having some trouble understanding at which point in the Ruifrok et al. script is the stain matrix obtained from the image? From my understanding, the stain matrix is a 3x3 matrix with rows corresponding to each stain (hematoxylin, eosin, and DAB) and columns corresponding to each RGB channel.

This section of the Ruifrok et al. paper explains more about the stain matrix:
Link to image

Are these values obtained in the script or through some other procedure in the lab?

Thank you!

You have to determine them yourself using background-corrected images stained with a single dye at a time.
To make the determination, you can try the ImageJ version:
and follow the section called “Determining new vectors”
You will then have to use the vectors that you determine either recompiling the plugin or via the “User Values” inputting the 9 values that you see in the Log window where it shows the ROI Java code (MODx[0] = … and so on).


You can also take a look at this approach - There is a link to the associated paper. It does some additions on top of NMF, it gave me pretty good results for separation and normalization.

Please note that colour normalisation and background correction are not the same thing.

I think the best method to correct the background is via transmittance (the a priori method) (because it is based on the features of your microscope illumination):

Yes, the original question was on stain separation and the paper pointed to is on that. Additionally background correction could be done as you suggest.


The original question was on how to extract the stain matrix and for that, background correction is necessary if the expectation is for the procedure to work. Not “could be done”, but “must be done”.

Hi Gabriel,
The stain separation approach outlined leads to the stain matrix for HE images. The paper caters to WSI images and includes a heuristic for a better estimation of background (and hence I0 of the beer lambert).