How to use the histomicstk.preprocessing.color_conversion function in histomicstk

To help you get started, we’ve selected a few histomicstk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_deconvolution / color_convolution.py View on Github external
histomicstk.preprocessing.color_conversion.rgb_to_od
    histomicstk.preprocessing.color_conversion.od_to_rgb
    histomicstk.preprocessing.color_conversion.rgb_to_sda
    histomicstk.preprocessing.color_conversion.sda_to_rgb

    """

    # transform 3D input stain image to 2D stain matrix format
    m = utils.convert_image_to_matrix(im_stains)

    # transform input stains to optical density values, convolve and
    # tfm back to stain
    sda_fwd = color_conversion.rgb_to_sda(m, 255 if I_0 is not None else None,
                                          allow_negatives=True)
    sda_conv = np.dot(w, sda_fwd)
    sda_inv = color_conversion.sda_to_rgb(sda_conv, I_0)

    # reshape output, transform type
    im_rgb = (utils.convert_matrix_to_image(sda_inv, im_stains.shape)
              .clip(0, 255).astype(np.uint8))

    return im_rgb
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_deconvolution / color_deconvolution.py View on Github external
wc = w

    # normalize stains to unit-norm
    wc = normalize(wc)

    # invert stain matrix
    Q = numpy.linalg.inv(wc)

    # transform 3D input image to 2D RGB matrix format
    m = utils.convert_image_to_matrix(im_rgb)[:3]

    # transform input RGB to optical density values and deconvolve,
    # tfm back to RGB
    sda_fwd = color_conversion.rgb_to_sda(m, I_0)
    sda_deconv = numpy.dot(Q, sda_fwd)
    sda_inv = color_conversion.sda_to_rgb(sda_deconv,
                                          255 if I_0 is not None else None)

    # reshape output
    StainsFloat = utils.convert_matrix_to_image(sda_inv, im_rgb.shape)

    # transform type
    Stains = StainsFloat.clip(0, 255).astype(numpy.uint8)

    # return
    Unmixed = collections.namedtuple('Unmixed',
                                     ['Stains', 'StainsFloat', 'Wc'])
    Output = Unmixed(Stains, StainsFloat, wc)

    return Output
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_normalization / ReinhardSample.py View on Github external
Returns a namedtuple.

    See Also
    --------
    histomicstk.preprocessing.color_conversion.rgb_to_lab,
    histomicstk.preprocessing.color_conversion.lab_to_rgb
    """

    # generate a sampling of RGB pixels from whole-slide image
    RGB = sample(File, Magnification, Percent, Tile)

    # reshape the 3xN pixel array into an image for rgb_to_lab
    RGB = np.reshape(RGB.transpose(), (1, RGB.shape[1], 3))

    # perform forward LAB transformation
    LAB = color_conversion.rgb_to_lab(RGB)

    # compute statistics of LAB channels
    Mu = LAB.sum(axis=0).sum(axis=0) / (LAB.size / 3)
    LAB[:, :, 0] = LAB[:, :, 0] - Mu[0]
    LAB[:, :, 1] = LAB[:, :, 1] - Mu[1]
    LAB[:, :, 2] = LAB[:, :, 2] - Mu[2]
    Sigma = ((LAB * LAB).sum(axis=0).sum(axis=0) / (LAB.size / 3 - 1)) ** 0.5

    # build named tuple for output
    OutTuple = collections.namedtuple('Statistics', ['Mu', 'Sigma'])
    Output = OutTuple(Mu, Sigma)

    return Output
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_deconvolution / color_convolution.py View on Github external
--------
    histomicstk.preprocessing.color_deconvolution.complement_stain_matrix,
    histomicstk.preprocessing.color_deconvolution.color_deconvolution
    histomicstk.preprocessing.color_conversion.rgb_to_od
    histomicstk.preprocessing.color_conversion.od_to_rgb
    histomicstk.preprocessing.color_conversion.rgb_to_sda
    histomicstk.preprocessing.color_conversion.sda_to_rgb

    """

    # transform 3D input stain image to 2D stain matrix format
    m = utils.convert_image_to_matrix(im_stains)

    # transform input stains to optical density values, convolve and
    # tfm back to stain
    sda_fwd = color_conversion.rgb_to_sda(m, 255 if I_0 is not None else None,
                                          allow_negatives=True)
    sda_conv = np.dot(w, sda_fwd)
    sda_inv = color_conversion.sda_to_rgb(sda_conv, I_0)

    # reshape output, transform type
    im_rgb = (utils.convert_matrix_to_image(sda_inv, im_stains.shape)
              .clip(0, 255).astype(np.uint8))

    return im_rgb
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_normalization / reinhard_stats.py View on Github external
See Also
    --------
    histomicstk.preprocessing.color_conversion.lab_mean_std
    histomicstk.preprocessing.color_normalization.reinhard
    """

    # generate a sampling of sample_pixels_rgb pixels from whole-slide image
    sample_pixels_rgb = sample_pixels(slide_path, sample_percent,
                                      magnification)

    # reshape the Nx3 pixel array into a 1 x N x 3 image for lab_mean_std
    sample_pixels_rgb = np.reshape(sample_pixels_rgb,
                                   (1, sample_pixels_rgb.shape[0], 3))

    # compute mean and stddev of sample pixels in Lab space
    Mu, Sigma = color_conversion.lab_mean_std(sample_pixels_rgb)

    # build named tuple for output
    ReinhardStats = collections.namedtuple('ReinhardStats', ['Mu', 'Sigma'])
    stats = ReinhardStats(Mu, Sigma)

    return stats
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_normalization / reinhard.py View on Github external
# calculate src_sigma if not provided
    if src_sigma is None:
        src_sigma = ((im_lab * im_lab).sum(axis=0).sum(axis=0) /
                     (m * n - 1)) ** 0.5

    # scale to unit variance
    for i in range(3):
        im_lab[:, :, i] = im_lab[:, :, i] / src_sigma[i]

    # rescale and recenter to match target statistics
    for i in range(3):
        im_lab[:, :, i] = im_lab[:, :, i] * target_sigma[i] + target_mu[i]

    # convert back to RGB colorspace
    im_normalized = color_conversion.lab_to_rgb(im_lab)
    im_normalized[im_normalized > 255] = 255
    im_normalized[im_normalized < 0] = 0
    im_normalized = im_normalized.astype(np.uint8)

    return im_normalized
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_deconvolution / color_deconvolution.py View on Github external
wc = complement_stain_matrix(w)
    else:
        wc = w

    # normalize stains to unit-norm
    wc = normalize(wc)

    # invert stain matrix
    Q = numpy.linalg.inv(wc)

    # transform 3D input image to 2D RGB matrix format
    m = utils.convert_image_to_matrix(im_rgb)[:3]

    # transform input RGB to optical density values and deconvolve,
    # tfm back to RGB
    sda_fwd = color_conversion.rgb_to_sda(m, I_0)
    sda_deconv = numpy.dot(Q, sda_fwd)
    sda_inv = color_conversion.sda_to_rgb(sda_deconv,
                                          255 if I_0 is not None else None)

    # reshape output
    StainsFloat = utils.convert_matrix_to_image(sda_inv, im_rgb.shape)

    # transform type
    Stains = StainsFloat.clip(0, 255).astype(numpy.uint8)

    # return
    Unmixed = collections.namedtuple('Unmixed',
                                     ['Stains', 'StainsFloat', 'Wc'])
    Output = Unmixed(Stains, StainsFloat, wc)

    return Output
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_normalization / reinhard.py View on Github external
----------
    .. [#] E. Reinhard, M. Adhikhmin, B. Gooch, P. Shirley, "Color transfer
       between images," in IEEE Computer Graphics and Applications, vol.21,
       no.5,pp.34-41, 2001.
    .. [#] D. Ruderman, T. Cronin, and C. Chiao, "Statistics of cone responses
       to natural images: implications for visual coding," J. Opt. Soc. Am. A
       vol.15, pp.2036-2045, 1998.

    """

    # get input image dimensions
    m = im_src.shape[0]
    n = im_src.shape[1]

    # convert input image to LAB color space
    im_lab = color_conversion.rgb_to_lab(im_src)

    # calculate src_mu if not provided
    if src_mu is None:
        src_mu = im_lab.sum(axis=0).sum(axis=0) / (m * n)

    # center to zero-mean
    for i in range(3):
        im_lab[:, :, i] = im_lab[:, :, i] - src_mu[i]

    # calculate src_sigma if not provided
    if src_sigma is None:
        src_sigma = ((im_lab * im_lab).sum(axis=0).sum(axis=0) /
                     (m * n - 1)) ** 0.5

    # scale to unit variance
    for i in range(3):
github DigitalSlideArchive / HistomicsTK / histomicstk / preprocessing / color_deconvolution / sparse_color_deconvolution.py View on Github external
# determine if input is RGB or pixel-matrix format
    if len(im_rgb.shape) == 3:  # RBG image provided
        if im_rgb.shape[2] == 4:  # remove alpha channel if needed
            im_rgb = im_rgb[:, :, (0, 1, 2)]
        m = im_rgb.shape[0]
        n = im_rgb.shape[1]
        im_rgb = np.reshape(im_rgb, (m * n, 3)).transpose()
    elif len(im_rgb.shape) == 2:  # pixel matrix provided
        m = -1
        n = -1

    # transform input RGB to optical density values
    im_rgb = im_rgb.astype(dtype=np.float32)
    im_rgb[im_rgb == 0] = 1e-16
    ODfwd = color_conversion.rgb_to_od(im_rgb)

    if w_init is None:

        # set number of output stains
        K = 3

        # perform NMF without initialization
        Factorization = nimfa.Snmf(V=ODfwd, seed=None, rank=K,
                                   version='r', beta=beta)
        Factorization()

    else:

        # get number of output stains
        K = w_init.shape[1]