segmentation_functions.py
def channel_deconvolution (img_RGB, staining_type, plot_image=False, to_normalize=True):
    """Deconvolute RGB image into different staining channels.
       Ref: https://blog.bham.ac.uk/intellimic/g-landini-software/colour-deconvolution/
    
    Args:
        img_RGB: A uint8 numpy array with RGB channels.
        staining_type: Dyes used to stain the image; choose one from ("HDB", "HRB", "HDR", "HEB").
        plot_image: Set True if want to real-time display results. Default is False.

    Returns:
        An unnormlized h*w*3 deconvoluted matrix and 3 different channels normalized to [0, 1] seperately.
        
    Raises:
        Exception: An error occured if staining_type is not defined. 
    """ 
    if staining_type == "HDB":
        channels = ("Hematoxylin", "DAB", "Background")
        stain_OD = np.asarray([[0.650,0.704,0.286],[0.268,0.570,0.776],[0.754,0.077,0.652]])
    elif staining_type == "HRB":
        channels = ("Hematoxylin", "Red", "Background")
        stain_OD = np.asarray([[0.650,0.704,0.286],[0.214,0.851,0.478],[0.754,0.077,0.652]])
    elif staining_type == "HDR":
        channels = ("Hematoxylin", "DAB", "Red")
        stain_OD = np.asarray([[0.650,0.704,0.286],[0.268,0.570,0.776],[0.214,0.851,0.478]])
    elif staining_type == "HEB":
        channels = ("Hematoxylin", "Eosin", "Background")
        # stain_OD = np.asarray([[0.550,0.758,0.351],[0.398,0.634,0.600],[0.754,0.077,0.652]])
        stain_OD = np.asarray([[0.644211,0.716556,0.266844],[0.092789,0.964111,0.283111],[0.754,0.077,0.652]])
    else:
        raise Exception("Staining type not defined. Choose one from the following: HDB, HRB, HDR, HEB.")
    
    # Stain absorbance matrix normalization
    normalized_stain_OD = []
    for r in stain_OD:
        normalized_stain_OD.append(r/np.linalg.norm(r))
    normalized_stain_OD = np.asarray(normalized_stain_OD)
    stain_OD_inverse = np.linalg.inv(normalized_stain_OD)
    
    # Calculate optical density of input image
    OD = convert_to_optical_densities(img_RGB, 255, 255, 255)

    # Deconvolution
    img_deconvoluted = np.reshape(np.dot(np.reshape(OD, (-1, 3)), stain_OD_inverse), OD.shape)
    
    # Define each channel
    if to_normalize:
        channel1 = normalize(img_deconvoluted[:, :, 0]) # First dye
        channel2 = normalize(img_deconvoluted[:, :, 1]) # Second dye
        channel3 = normalize(img_deconvoluted[:, :, 2]) # Third dye or background
    else:
        channel1 = img_deconvoluted[:, :, 0] # First dye
        channel2 = img_deconvoluted[:, :, 1] # Second dye
        channel3 = img_deconvoluted[:, :, 2] # Third dye or background
    
    if plot_image: 
        fig, axes = plt.subplots(2, 2, figsize=(15, 15), sharex=True, sharey=True,
                                 subplot_kw={'adjustable': 'box-forced'})
        ax = axes.ravel()
        ax[0].imshow(img_RGB)
        ax[0].set_title("Original image")
        ax[1].imshow(channel1, cmap="gray")
        ax[1].set_title(channels[0])
        ax[2].imshow(channel2, cmap="gray")
        ax[2].set_title(channels[1])
        ax[3].imshow(channel3, cmap="gray")
        ax[3].set_title(channels[2])        
        plt.show()

    return img_deconvoluted, channel1, channel2, channel3
Deconvolute RGB image into different staining channels.
       Ref: https://blog.bham.ac.uk/intellimic/g-landini-software/colour-deconvolution/
    
    Args:
        img_RGB: A uint8 numpy array with RGB channels.
        staining_type: Dyes used to stain the image; choose one from ("HDB", "HRB", "HDR", "HEB").
        plot_image: Set True if want to real-time display results. Default is False.

    Returns:
        An unnormlized h*w*3 deconvoluted matrix and 3 different channels normalized to [0, 1] seperately.
        
    Raises:
        Exception: An error occured if staining_type is not defined. 
    
Deconvolute RGB image into different staining channels.
Deconvolute RGB image into different staining channels .
def channel_deconvolution (img_RGB, staining_type, plot_image=False, to_normalize=True):
    if staining_type == "HDB":
        channels = ("Hematoxylin", "DAB", "Background")
        stain_OD = np.asarray([[0.650,0.704,0.286],[0.268,0.570,0.776],[0.754,0.077,0.652]])
    elif staining_type == "HRB":
        channels = ("Hematoxylin", "Red", "Background")
        stain_OD = np.asarray([[0.650,0.704,0.286],[0.214,0.851,0.478],[0.754,0.077,0.652]])
    elif staining_type == "HDR":
        channels = ("Hematoxylin", "DAB", "Red")
        stain_OD = np.asarray([[0.650,0.704,0.286],[0.268,0.570,0.776],[0.214,0.851,0.478]])
    elif staining_type == "HEB":
        channels = ("Hematoxylin", "Eosin", "Background")
        stain_OD = np.asarray([[0.644211,0.716556,0.266844],[0.092789,0.964111,0.283111],[0.754,0.077,0.652]])
    else:
        raise Exception("Staining type not defined. Choose one from the following: HDB, HRB, HDR, HEB.")
    normalized_stain_OD = []
    for r in stain_OD:
        normalized_stain_OD.append(r/np.linalg.norm(r))
    normalized_stain_OD = np.asarray(normalized_stain_OD)
    stain_OD_inverse = np.linalg.inv(normalized_stain_OD)
    OD = convert_to_optical_densities(img_RGB, 255, 255, 255)
    img_deconvoluted = np.reshape(np.dot(np.reshape(OD, (-1, 3)), stain_OD_inverse), OD.shape)
    if to_normalize:
        channel1 = normalize(img_deconvoluted[:, :, 0]) 
        channel2 = normalize(img_deconvoluted[:, :, 1]) 
        channel3 = normalize(img_deconvoluted[:, :, 2]) 
    else:
        channel1 = img_deconvoluted[:, :, 0] 
        channel2 = img_deconvoluted[:, :, 1] 
        channel3 = img_deconvoluted[:, :, 2] 
    if plot_image: 
        fig, axes = plt.subplots(2, 2, figsize=(15, 15), sharex=True, sharey=True,
                                 subplot_kw={'adjustable': 'box-forced'})
        ax = axes.ravel()
        ax[0].imshow(img_RGB)
        ax[0].set_title("Original image")
        ax[1].imshow(channel1, cmap="gray")
        ax[1].set_title(channels[0])
        ax[2].imshow(channel2, cmap="gray")
        ax[2].set_title(channels[1])
        ax[3].imshow(channel3, cmap="gray")
        ax[3].set_title(channels[2])        
        plt.show()
    return img_deconvoluted, channel1, channel2, channel3
def channel_deconvolution ( img_RGB , staining_type , plot_image = False , to_normalize = True ) : if staining_type == "HDB" : channels = ( "Hematoxylin" , "DAB" , "Background" ) stain_OD = np . asarray ( [ [ 0.650 , 0.704 , 0.286 ] , [ 0.268 , 0.570 , 0.776 ] , [ 0.754 , 0.077 , 0.652 ] ] ) elif staining_type == "HRB" : channels = ( "Hematoxylin" , "Red" , "Background" ) stain_OD = np . asarray ( [ [ 0.650 , 0.704 , 0.286 ] , [ 0.214 , 0.851 , 0.478 ] , [ 0.754 , 0.077 , 0.652 ] ] ) elif staining_type == "HDR" : channels = ( "Hematoxylin" , "DAB" , "Red" ) stain_OD = np . asarray ( [ [ 0.650 , 0.704 , 0.286 ] , [ 0.268 , 0.570 , 0.776 ] , [ 0.214 , 0.851 , 0.478 ] ] ) elif staining_type == "HEB" : channels = ( "Hematoxylin" , "Eosin" , "Background" ) stain_OD = np . asarray ( [ [ 0.644211 , 0.716556 , 0.266844 ] , [ 0.092789 , 0.964111 , 0.283111 ] , [ 0.754 , 0.077 , 0.652 ] ] ) else : raise Exception ( "Staining type not defined. Choose one from the following: HDB, HRB, HDR, HEB." ) normalized_stain_OD = [ ] for r in stain_OD : normalized_stain_OD . append ( r / np . linalg . norm ( r ) ) normalized_stain_OD = np . asarray ( normalized_stain_OD ) stain_OD_inverse = np . linalg . inv ( normalized_stain_OD ) OD = convert_to_optical_densities ( img_RGB , 255 , 255 , 255 ) img_deconvoluted = np . reshape ( np . dot ( np . reshape ( OD , ( - 1 , 3 ) ) , stain_OD_inverse ) , OD . shape ) if to_normalize : channel1 = normalize ( img_deconvoluted [ : , : , 0 ] ) channel2 = normalize ( img_deconvoluted [ : , : , 1 ] ) channel3 = normalize ( img_deconvoluted [ : , : , 2 ] ) else : channel1 = img_deconvoluted [ : , : , 0 ] channel2 = img_deconvoluted [ : , : , 1 ] channel3 = img_deconvoluted [ : , : , 2 ] if plot_image : fig , axes = plt . subplots ( 2 , 2 , figsize = ( 15 , 15 ) , sharex = True , sharey = True , subplot_kw = { 'adjustable' : 'box-forced' } ) ax = axes . ravel ( ) ax [ 0 ] . imshow ( img_RGB ) ax [ 0 ] . set_title ( "Original image" ) ax [ 1 ] . imshow ( channel1 , cmap = "gray" ) ax [ 1 ] . set_title ( channels [ 0 ] ) ax [ 2 ] . imshow ( channel2 , cmap = "gray" ) ax [ 2 ] . set_title ( channels [ 1 ] ) ax [ 3 ] . imshow ( channel3 , cmap = "gray" ) ax [ 3 ] . set_title ( channels [ 2 ] ) plt . show ( ) return img_deconvoluted , channel1 , channel2 , channel3
Deconvolute RGB image into different staining channels.
Deconvolute RGB image into different staining channels .
"""Deconvolute RGB image into different staining channels.
       Ref: https://blog.bham.ac.uk/intellimic/g-landini-software/colour-deconvolution/
    
    Args:
        img_RGB: A uint8 numpy array with RGB channels.
        staining_type: Dyes used to stain the image; choose one from ("HDB", "HRB", "HDR", "HEB").
        plot_image: Set True if want to real-time display results. Default is False.

    Returns:
        An unnormlized h*w*3 deconvoluted matrix and 3 different channels normalized to [0, 1] seperately.
        
    Raises:
        Exception: An error occured if staining_type is not defined. 
    """ # stain_OD = np.asarray([[0.550,0.758,0.351],[0.398,0.634,0.600],[0.754,0.077,0.652]]) # Stain absorbance matrix normalization # Calculate optical density of input image # Deconvolution # Define each channel # First dye # Second dye # Third dye or background # First dye # Second dye # Third dye or background
An unnormlized h*w*3 deconvoluted matrix and 3 different channels normalized to [0, 1] seperately.
An unnormlized h * w * 3 deconvoluted matrix and 3 different channels normalized to [ 0 1 ] seperately .
An error occured if staining_type is not defined.
An error occured if staining_type is not defined .
Exception
img_RGB
A uint8 numpy array with RGB channels.
A uint8 numpy array with RGB channels .
staining_type
Dyes used to stain the image; choose one from ("HDB", "HRB", "HDR", "HEB").
Dyes used to stain the image ; choose one from ( " HDB " " HRB " " HDR " " HEB " ) .
plot_image
Set True if want to real-time display results. Default is False.
Set True if want to real - time display results . Default is False .
to_normalize