@@ -0,0 +1,2457 @@
+import numpy%0A%0Adef ColorDeconvolution ( I, W ):%0A    '''%0A    Implements color deconvolution. The input image %22I%22 consisting of RGB values is%0A    first transformed into optical density space, and then projected onto the stain%0A    vectors in the columns of %22W%22.%0A    example H&E matrix%0A    W =array(%5B%5B0.650, 0.072, 0%5D,%0A              %5B0.704, 0.990, 0%5D,%0A              %5B0.286, 0.105, 0%5D%5D)%0A    *Inputs:%0A        I (rgbimage) - an RGB image of type unsigned char.%0A        W (matrix) - a 3x3 matrix containing the color vectors in columns. %0A                     For two stain images the third column is zero and will be %0A                     complemented using cross-product. Minumum two nonzero %0A                     columns required.%0A    *Outputs:%0A        Stains (rgbimage) - an rgb image with deconvolved stain intensities in %0A                            each channel, values ranging from %5B0, 255%5D, suitable for display.%0A        StainsFloat (matrix) - an intensity image of deconvolved stains that is %0A                            unbounded, suitable for reconstructing color images %0A                            of deconvolved stains with ColorConvolution.%0A        Wc (matrix) - a 3x3 complemented stain matrix. Useful for color image%0A                      reconstruction with ColorConvolution.%0A    *Related functions:%0A        ComplementStainMatrix, OpticalDensityFwd, OpticalDensityInv, ColorConvolution%0A    '''%0A%0A    #complement stain matrix if needed%0A    if(numpy.linalg.norm(W%5B:,2%5D) %3C= 1e-16):%0A        Wc = ComplementStainMatrix(W)    %0A%0A    #normalize stains to unit-norm%0A    for i in range(Wc.shape%5B1%5D):%0A        Norm = numpy.linalg.norm(Wc%5B:,i%5D)%0A        if(Norm %3E= 1e-16):%0A            Wc%5B:,i%5D /= Norm%0A%0A    #invert stain matrix%0A    Q = numpy.linalg.inv(Wc)%0A%0A    #transform 3D input image to 2D RGB matrix format%0A    m = I.shape%5B0%5D%0A    n = I.shape%5B1%5D%0A    if(I.shape%5B2%5D == 4):%0A        I = I%5B:,:,(0,1,2)%5D%0A    I = numpy.reshape(I, (m*n,3))%0A%0A    #transform input RGB to optical density values and deconvolve, tfm back to RGB%0A    I = I.astype(dtype=numpy.float32)%0A    I%5BI == 0%5D = 1e-16%0A    ODfwd = OpticalDensityFwd(I)%0A    ODdeconv = numpy.dot(ODfwd, numpy.transpose(Q))%0A    ODinv = OpticalDensityInv(ODdeconv)%0A%0A    #reshape output%0A    StainsFloat = numpy.reshape(ODinv, (m,n,3))%0A    %0A    #transform type  %0A    Stains = numpy.copy(StainsFloat)%0A    Stains%5BStains %3E 255%5D = 255%0A    Stains = Stains.astype(numpy.uint8)%0A    %0A    #return %0A    return (Stains, StainsFloat, Wc)%0A

Create ColorDeconvolution.py