segmentation_functions.py
def watershed(mask, img, plot_image=False, kernel_size=2):
    """Do watershed segmentation for input mask and image.
    
    Args:
        mask: A 0/255 matrix with 255 indicating objects.
        img: An 8UC3 matrix for watershed segmentation.
        plot_image: Set True if want to real-time display results. Default is False.
        kernel_size: Kernal size for inner marker erosion. Default is 2.
        
    Returns:
        A uint8 mask same size as input image, with -1 indicating boundary, 1 indicating background, 
        and numbers>1 indicating objects.
    """
    img_copy = img.copy()
    mask_copy = np.array(mask.copy(), dtype=np.uint8)

    # Sure foreground area (inner marker)
    mask_closed = closing(np.array(mask_copy, dtype=np.uint8))
    mask_closed = closing(np.array(mask_closed, dtype=np.uint8))
    kernel = np.ones((kernel_size, kernel_size), np.uint8)
    sure_fg = cv2.erode(mask_closed, kernel, iterations=2)
    sure_fg = skimage.morphology.closing(np.array(sure_fg, dtype=np.uint8))
    
    # Sure background area (outer marker)
    sure_fg_bool = 1 - img_as_bool(sure_fg)
    sure_bg = np.uint8(1 - morphology.skeletonize(sure_fg_bool))

    # Unknown region (the region other than inner or outer marker)
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    
    # Marker for cv2.watershed
    _, markers = cv2.connectedComponents(sure_fg)
    markers = markers + 1  # Set background to 1
    markers[unknown==1] = 0
    
    # Watershed
    # TODO(shidan.wang@utsouthwestern.edu): Replace cv2.watershed with skimage.morphology.watershed
    marker = cv2.watershed(img_copy, markers.copy())

    if plot_image:
        plt.figure()
        plt.imshow(sure_fg)
        plt.title("Inner Marker")
        plt.figure()
        plt.imshow(sure_bg)
        plt.title("Outer Marker")
        plt.figure()
        plt.imshow(unknown)
        plt.title("Unknown")
        plt.figure()
        plt.imshow(markers, cmap='jet')
        plt.title("Markers")
        plt.figure()
        plt.imshow(marker, cmap='jet')
        plt.title("Mask")
        plt.figure()
        plt.imshow(img)
        plt.title("Original Image")
        plt.figure()
        img_copy[marker == -1] = [0, 255 ,0]
        plt.imshow(img_copy)
        plt.title("Marked Image")
        plt.show()

    return marker
Do watershed segmentation for input mask and image.
    
    Args:
        mask: A 0/255 matrix with 255 indicating objects.
        img: An 8UC3 matrix for watershed segmentation.
        plot_image: Set True if want to real-time display results. Default is False.
        kernel_size: Kernal size for inner marker erosion. Default is 2.
        
    Returns:
        A uint8 mask same size as input image, with -1 indicating boundary, 1 indicating background, 
        and numbers>1 indicating objects.
    
Do watershed segmentation for input mask and image.
Do watershed segmentation for input mask and image .
def watershed(mask, img, plot_image=False, kernel_size=2):
    img_copy = img.copy()
    mask_copy = np.array(mask.copy(), dtype=np.uint8)
    mask_closed = closing(np.array(mask_copy, dtype=np.uint8))
    mask_closed = closing(np.array(mask_closed, dtype=np.uint8))
    kernel = np.ones((kernel_size, kernel_size), np.uint8)
    sure_fg = cv2.erode(mask_closed, kernel, iterations=2)
    sure_fg = skimage.morphology.closing(np.array(sure_fg, dtype=np.uint8))
    sure_fg_bool = 1 - img_as_bool(sure_fg)
    sure_bg = np.uint8(1 - morphology.skeletonize(sure_fg_bool))
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    _, markers = cv2.connectedComponents(sure_fg)
    markers = markers + 1  
    markers[unknown==1] = 0
    marker = cv2.watershed(img_copy, markers.copy())
    if plot_image:
        plt.figure()
        plt.imshow(sure_fg)
        plt.title("Inner Marker")
        plt.figure()
        plt.imshow(sure_bg)
        plt.title("Outer Marker")
        plt.figure()
        plt.imshow(unknown)
        plt.title("Unknown")
        plt.figure()
        plt.imshow(markers, cmap='jet')
        plt.title("Markers")
        plt.figure()
        plt.imshow(marker, cmap='jet')
        plt.title("Mask")
        plt.figure()
        plt.imshow(img)
        plt.title("Original Image")
        plt.figure()
        img_copy[marker == -1] = [0, 255 ,0]
        plt.imshow(img_copy)
        plt.title("Marked Image")
        plt.show()
    return marker
def watershed ( mask , img , plot_image = False , kernel_size = 2 ) : img_copy = img . copy ( ) mask_copy = np . array ( mask . copy ( ) , dtype = np . uint8 ) mask_closed = closing ( np . array ( mask_copy , dtype = np . uint8 ) ) mask_closed = closing ( np . array ( mask_closed , dtype = np . uint8 ) ) kernel = np . ones ( ( kernel_size , kernel_size ) , np . uint8 ) sure_fg = cv2 . erode ( mask_closed , kernel , iterations = 2 ) sure_fg = skimage . morphology . closing ( np . array ( sure_fg , dtype = np . uint8 ) ) sure_fg_bool = 1 - img_as_bool ( sure_fg ) sure_bg = np . uint8 ( 1 - morphology . skeletonize ( sure_fg_bool ) ) sure_fg = np . uint8 ( sure_fg ) unknown = cv2 . subtract ( sure_bg , sure_fg ) _ , markers = cv2 . connectedComponents ( sure_fg ) markers = markers + 1 markers [ unknown == 1 ] = 0 marker = cv2 . watershed ( img_copy , markers . copy ( ) ) if plot_image : plt . figure ( ) plt . imshow ( sure_fg ) plt . title ( "Inner Marker" ) plt . figure ( ) plt . imshow ( sure_bg ) plt . title ( "Outer Marker" ) plt . figure ( ) plt . imshow ( unknown ) plt . title ( "Unknown" ) plt . figure ( ) plt . imshow ( markers , cmap = 'jet' ) plt . title ( "Markers" ) plt . figure ( ) plt . imshow ( marker , cmap = 'jet' ) plt . title ( "Mask" ) plt . figure ( ) plt . imshow ( img ) plt . title ( "Original Image" ) plt . figure ( ) img_copy [ marker == - 1 ] = [ 0 , 255 , 0 ] plt . imshow ( img_copy ) plt . title ( "Marked Image" ) plt . show ( ) return marker
Do watershed segmentation for input mask and image.
Do watershed segmentation for input mask and image .
"""Do watershed segmentation for input mask and image.
    
    Args:
        mask: A 0/255 matrix with 255 indicating objects.
        img: An 8UC3 matrix for watershed segmentation.
        plot_image: Set True if want to real-time display results. Default is False.
        kernel_size: Kernal size for inner marker erosion. Default is 2.
        
    Returns:
        A uint8 mask same size as input image, with -1 indicating boundary, 1 indicating background, 
        and numbers>1 indicating objects.
    """ # Sure foreground area (inner marker) # Sure background area (outer marker) # Unknown region (the region other than inner or outer marker) # Marker for cv2.watershed # Set background to 1 # Watershed # TODO(shidan.wang@utsouthwestern.edu): Replace cv2.watershed with skimage.morphology.watershed
A uint8 mask same size as input image, with -1 indicating boundary, 1 indicating background,
and numbers>1 indicating objects.
A uint8 mask same size as input image with - 1 indicating boundary 1 indicating background and numbers > 1 indicating objects .
mask
A 0/255 matrix with 255 indicating objects.
A 0 / 255 matrix with 255 indicating objects .
img
An 8UC3 matrix for watershed segmentation.
An 8UC3 matrix for watershed segmentation .
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 .
kernel_size
Kernal size for inner marker erosion. Default is 2.
Kernal size for inner marker erosion . Default is 2 .