示例#1
0
def graphcut_stawiaski(regions, gradient = False, foreground = False, background = False):
    """
    Executes a Stawiaski label graph cut.
    
    @param regions the regions image
    @type regions scipy.ndarray (dtype=scipy.int?)
    @param gradient the gradient image
    @type gradient scipy.ndarray (dtype=scipy.float?)
    @param foreground the foreground markers
    @type foreground scipy.ndarray (dtype=scipy.bool_)
    @param background the background markers
    @type background scipy.ndarray (dtype=scipy.bool_)
    
    @return the cut image as binary array
    @rtype scipy.ndarray (dtype=scipy.bool_)
    
    @raise ArgumentError when the supplied data is erroneous
    """
    # initialize logger
    logger = Logger.getInstance()
    
    # unpack images if required
    # !TODO: This is an ugly hack, especially since it can be seen inside the function definition
    # How to overcome this, since I can not use a wrapper function as the whole thing must be pickable
    if not gradient and not foreground and not background: 
        regions, gradient, foreground, background = regions
    
    # ensure that input images are scipy arrays
    img_region = scipy.asarray(regions)
    img_gradient = scipy.asarray(gradient)
    img_fg = scipy.asarray(foreground, dtype=scipy.bool_)
    img_bg = scipy.asarray(background, dtype=scipy.bool_)
    
    # ensure correctness of supplied images
    if not (img_region.shape == img_gradient.shape == img_fg.shape == img_bg.shape): raise ArgumentError('All supplied images must be of the same shape.')

    # recompute the label ids to start from id = 1
    img_region = relabel(img_region)
    
    # generate graph
    gcgraph = graph_from_labels(img_region, img_fg, img_bg, boundary_term = boundary_stawiaski, boundary_term_args = (img_gradient))
    
    # execute min-cut
    maxflow = gcgraph.maxflow() # executes the cut and returns the maxflow value
    
    logger.debug('Graph-cut terminated successfully with maxflow of {}.'.format(maxflow))
    
    # apply results to the region image
    mapping = [0] # no regions with id 1 exists in mapping, entry used as padding
    mapping.extend(map(lambda x: 0 if gcgraph.termtype.SINK == gcgraph.what_segment(int(x) - 1) else 1,
                       scipy.unique(img_region)))
    img_results = relabel_map(img_region, mapping)
    
    return img_results.astype(scipy.bool_)
示例#2
0
 def fun(arr, start):
     arr = relabel(arr, start)
     return arr, arr.max() + 1
 def fun(arr, start):
     arr = relabel(arr, start)
     return arr, arr.max() + 1