segmentation_functions.py
def generate_mask(channel, original_img=None, overlap_color=(0, 1, 0), 
                  plot_process=False, plot_result=False, title="",
                  fill_hole=False, thresh=None,
                  use_watershed=True, watershed_kernel_size=2,
                  save_img=False, save_path=None):
    """Generate mask for a gray-value image.
    
    Args:
        channel: Channel returned by function 'channel_deconvolution'. A gray-value image is also accepted.
        original_img: A image used for plotting overlapped segmentation result, optional.
        overlap_color: A 3-value tuple setting the color used to mark segmentation boundaries on original
            image. Default is green (0, 1, 0).
        plot_process: Set True if want to display the whole mask generation process. Default is False.
        plot_result: Set True if want to display the final result. Default is False.
        title: The title used for plot_result, optional.
        fill_hole: Set True if want to fill mask holes. Default is False.
        thresh: Provide this value to do binary thresholding instead of default otsu thresholding.
        use_watershed: Set False if want to skip the watershed segmentation step. Default is True.
        watershed_kernel_size: Kernel size of inner marker erosion. Default is 2.
        save_img: Set True if want to save the mask image. Default is False.
        save_path: The path to save the mask image, optional. Prefer *.png or *.pdf.
        
    Returns: 
        A binary mask with 1 indicating an object and 0 indicating background.
        
    Raises:
        IOError: An error occured writing image to save_path.
    """  
    if not check_channel(channel):
        # If there is not any signal
        print("No signals detected for this channel")
        return np.zeros(channel.shape)
    else:
        channel = normalize(channel)
        if use_watershed:
            mask_threshold = otsu_thresholding(make_8UC(channel),
                                               plot_image=plot_process, fill_hole=fill_hole, thresh=thresh)
            marker = watershed(mask_threshold, make_8UC3(channel),
                               plot_image = plot_process, kernel_size = watershed_kernel_size)     
            # create mask
            mask = np.zeros(marker.shape)
            mask[marker == 1] = 1
            mask = 1 - mask
            # Set boundary as mask from Otsu_thresholding, since cv2.watershed automatically set boundary as -1
            mask[0, :] = mask_threshold[0, :] == 255
            mask[-1, :] = mask_threshold[-1, :] == 255
            mask[:, 0] = mask_threshold[:, 0] == 255
            mask[:, -1] = mask_threshold[:, -1] == 255
        else:
            mask = otsu_thresholding(make8UC(channel), 
                                     plot_image=plot_process, fill_hole=fill_hole, thresh=thresh)
        
        if plot_result or save_img:
            if original_img is None:
                # If original image is not provided, plot mask only
                plt.figure()
                plt.imshow(mask, cmap = "gray")
            else:
                # If original image is provided
                overlapped_img = segmentation.mark_boundaries(original_img, skimage.measure.label(mask), 
                                                             overlap_color, mode = "thick")
                fig, axes = plt.subplots(1, 2, figsize=(15, 15), sharex=True, sharey=True,
                                     subplot_kw={'adjustable': 'box-forced'})
                ax = axes.ravel()
                ax[0].imshow(mask, cmap="gray")
                ax[0].set_title(str(title)+" Mask")
                ax[1].imshow(overlapped_img)
                ax[1].set_title("Overlapped with Original Image")
            if save_img:
                try: 
                    plt.savefig(save_path)
                except:
                    raise IOError("Error saving image to {}".format(save_path))
            if plot_result:
                plt.show()
            plt.close()
    return mask
Generate mask for a gray-value image.
    
    Args:
        channel: Channel returned by function 'channel_deconvolution'. A gray-value image is also accepted.
        original_img: A image used for plotting overlapped segmentation result, optional.
        overlap_color: A 3-value tuple setting the color used to mark segmentation boundaries on original
            image. Default is green (0, 1, 0).
        plot_process: Set True if want to display the whole mask generation process. Default is False.
        plot_result: Set True if want to display the final result. Default is False.
        title: The title used for plot_result, optional.
        fill_hole: Set True if want to fill mask holes. Default is False.
        thresh: Provide this value to do binary thresholding instead of default otsu thresholding.
        use_watershed: Set False if want to skip the watershed segmentation step. Default is True.
        watershed_kernel_size: Kernel size of inner marker erosion. Default is 2.
        save_img: Set True if want to save the mask image. Default is False.
        save_path: The path to save the mask image, optional. Prefer *.png or *.pdf.
        
    Returns: 
        A binary mask with 1 indicating an object and 0 indicating background.
        
    Raises:
        IOError: An error occured writing image to save_path.
    
Generate mask for a gray-value image.
Generate mask for a gray - value image .
def generate_mask(channel, original_img=None, overlap_color=(0, 1, 0), 
                  plot_process=False, plot_result=False, title="",
                  fill_hole=False, thresh=None,
                  use_watershed=True, watershed_kernel_size=2,
                  save_img=False, save_path=None):
    if not check_channel(channel):
        print("No signals detected for this channel")
        return np.zeros(channel.shape)
    else:
        channel = normalize(channel)
        if use_watershed:
            mask_threshold = otsu_thresholding(make_8UC(channel),
                                               plot_image=plot_process, fill_hole=fill_hole, thresh=thresh)
            marker = watershed(mask_threshold, make_8UC3(channel),
                               plot_image = plot_process, kernel_size = watershed_kernel_size)     
            mask = np.zeros(marker.shape)
            mask[marker == 1] = 1
            mask = 1 - mask
            mask[0, :] = mask_threshold[0, :] == 255
            mask[-1, :] = mask_threshold[-1, :] == 255
            mask[:, 0] = mask_threshold[:, 0] == 255
            mask[:, -1] = mask_threshold[:, -1] == 255
        else:
            mask = otsu_thresholding(make8UC(channel), 
                                     plot_image=plot_process, fill_hole=fill_hole, thresh=thresh)
        if plot_result or save_img:
            if original_img is None:
                plt.figure()
                plt.imshow(mask, cmap = "gray")
            else:
                overlapped_img = segmentation.mark_boundaries(original_img, skimage.measure.label(mask), 
                                                             overlap_color, mode = "thick")
                fig, axes = plt.subplots(1, 2, figsize=(15, 15), sharex=True, sharey=True,
                                     subplot_kw={'adjustable': 'box-forced'})
                ax = axes.ravel()
                ax[0].imshow(mask, cmap="gray")
                ax[0].set_title(str(title)+" Mask")
                ax[1].imshow(overlapped_img)
                ax[1].set_title("Overlapped with Original Image")
            if save_img:
                try: 
                    plt.savefig(save_path)
                except:
                    raise IOError("Error saving image to {}".format(save_path))
            if plot_result:
                plt.show()
            plt.close()
    return mask
def generate_mask ( channel , original_img = None , overlap_color = ( 0 , 1 , 0 ) , plot_process = False , plot_result = False , title = "" , fill_hole = False , thresh = None , use_watershed = True , watershed_kernel_size = 2 , save_img = False , save_path = None ) : if not check_channel ( channel ) : print ( "No signals detected for this channel" ) return np . zeros ( channel . shape ) else : channel = normalize ( channel ) if use_watershed : mask_threshold = otsu_thresholding ( make_8UC ( channel ) , plot_image = plot_process , fill_hole = fill_hole , thresh = thresh ) marker = watershed ( mask_threshold , make_8UC3 ( channel ) , plot_image = plot_process , kernel_size = watershed_kernel_size ) mask = np . zeros ( marker . shape ) mask [ marker == 1 ] = 1 mask = 1 - mask mask [ 0 , : ] = mask_threshold [ 0 , : ] == 255 mask [ - 1 , : ] = mask_threshold [ - 1 , : ] == 255 mask [ : , 0 ] = mask_threshold [ : , 0 ] == 255 mask [ : , - 1 ] = mask_threshold [ : , - 1 ] == 255 else : mask = otsu_thresholding ( make8UC ( channel ) , plot_image = plot_process , fill_hole = fill_hole , thresh = thresh ) if plot_result or save_img : if original_img is None : plt . figure ( ) plt . imshow ( mask , cmap = "gray" ) else : overlapped_img = segmentation . mark_boundaries ( original_img , skimage . measure . label ( mask ) , overlap_color , mode = "thick" ) fig , axes = plt . subplots ( 1 , 2 , figsize = ( 15 , 15 ) , sharex = True , sharey = True , subplot_kw = { 'adjustable' : 'box-forced' } ) ax = axes . ravel ( ) ax [ 0 ] . imshow ( mask , cmap = "gray" ) ax [ 0 ] . set_title ( str ( title ) + " Mask" ) ax [ 1 ] . imshow ( overlapped_img ) ax [ 1 ] . set_title ( "Overlapped with Original Image" ) if save_img : try : plt . savefig ( save_path ) except : raise IOError ( "Error saving image to {}" . format ( save_path ) ) if plot_result : plt . show ( ) plt . close ( ) return mask
Generate mask for a gray-value image.
Generate mask for a gray - value image .
"""Generate mask for a gray-value image.
    
    Args:
        channel: Channel returned by function 'channel_deconvolution'. A gray-value image is also accepted.
        original_img: A image used for plotting overlapped segmentation result, optional.
        overlap_color: A 3-value tuple setting the color used to mark segmentation boundaries on original
            image. Default is green (0, 1, 0).
        plot_process: Set True if want to display the whole mask generation process. Default is False.
        plot_result: Set True if want to display the final result. Default is False.
        title: The title used for plot_result, optional.
        fill_hole: Set True if want to fill mask holes. Default is False.
        thresh: Provide this value to do binary thresholding instead of default otsu thresholding.
        use_watershed: Set False if want to skip the watershed segmentation step. Default is True.
        watershed_kernel_size: Kernel size of inner marker erosion. Default is 2.
        save_img: Set True if want to save the mask image. Default is False.
        save_path: The path to save the mask image, optional. Prefer *.png or *.pdf.
        
    Returns: 
        A binary mask with 1 indicating an object and 0 indicating background.
        
    Raises:
        IOError: An error occured writing image to save_path.
    """ # If there is not any signal # create mask # Set boundary as mask from Otsu_thresholding, since cv2.watershed automatically set boundary as -1 # If original image is not provided, plot mask only # If original image is provided
A binary mask with 1 indicating an object and 0 indicating background.
A binary mask with 1 indicating an object and 0 indicating background .
An error occured writing image to save_path.
An error occured writing image to save_path .
IOError
channel
Channel returned by function 'channel_deconvolution'. A gray-value image is also accepted.
Channel returned by function ' channel_deconvolution ' . A gray - value image is also accepted .
original_img
A image used for plotting overlapped segmentation result, optional.
A image used for plotting overlapped segmentation result optional .
overlap_color
A 3-value tuple setting the color used to mark segmentation boundaries on original
image.
A 3 - value tuple setting the color used to mark segmentation boundaries on original image .
plot_process
Set True if want to display the whole mask generation process. Default is False.
Set True if want to display the whole mask generation process . Default is False .
plot_result
Set True if want to display the final result. Default is False.
Set True if want to display the final result . Default is False .
title
The title used for plot_result, optional.
The title used for plot_result optional .
fill_hole
Set True if want to fill mask holes. Default is False.
Set True if want to fill mask holes . Default is False .
thresh
Provide this value to do binary thresholding instead of default otsu thresholding.
Provide this value to do binary thresholding instead of default otsu thresholding .
use_watershed
Set False if want to skip the watershed segmentation step. Default is True.
Set False if want to skip the watershed segmentation step . Default is True .
watershed_kernel_size
Kernel size of inner marker erosion. Default is 2.
Kernel size of inner marker erosion . Default is 2 .
save_img
Set True if want to save the mask image. Default is False.
Set True if want to save the mask image . Default is False .
save_path
The path to save the mask image, optional. Prefer *.png or *.pdf.
The path to save the mask image optional . Prefer * . png or * . pdf .