示例#1
0
def segment_graph_cut_general(segments,
                              proba,
                              image=None,
                              features=None,
                              gc_regul=1.,
                              edge_type='model',
                              edge_cost=1.,
                              debug_visual=None):
    """ segment the image segmented via superpixels and estimated features

    :param ndarray features: features sor each instance
    :param ndarray segments: segmentation mapping each pixel into a class
    :param ndarray image: image
    :param ndarray proba: probabilities that each feature belongs to each class
    :param str edge_type:
    :param str edge_cost:
    :param gc_regul: regularisation for GrphCut
    :param {} debug_visual:
    :return [int]: labelling by resulting classes

    >>> np.random.seed(0)
    >>> segments = np.array([[0] * 3 + [2] * 3 + [4] * 3 + [6] * 3 + [8] * 3,
    ...                      [1] * 3 + [3] * 3 + [5] * 3 + [7] * 3 + [9] * 3])
    >>> proba = np.array([[0.1] * 6 + [0.9] * 4,
    ...                   [0.9] * 6 + [0.1] * 4], dtype=float).T
    >>> proba += (0.5 - np.random.random(proba.shape)) * 0.2
    >>> compute_unary_cost(proba)
    array([[ 2.40531242,  0.15436155],
           [ 2.53266106,  0.11538463],
           [ 2.1604864 ,  0.13831863],
           [ 2.18495711,  0.19644636],
           [ 4.60517019,  0.0797884 ],
           [ 3.17833405,  0.11180231],
           [ 0.12059702,  4.20769207],
           [ 0.0143091 ,  1.70059894],
           [ 0.01005034,  3.39692559],
           [ 0.16916609,  3.64975219]])
    >>> segment_graph_cut_general(segments, proba, gc_regul=0., edge_type='')
    array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0], dtype=int32)
    >>> labels = segment_graph_cut_general(segments, proba, gc_regul=1.,
    ...                                    edge_type='spatial')
    >>> labels[segments]
    array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]], dtype=int32)
    >>> slic = np.array([[0] * 4 + [1] * 6 + [2] * 4,
    ...                  [3] * 5 + [4] * 4 + [5] * 5])
    >>> proba = np.array([[1] * 3 + [0] * 3, [0] * 3 + [1] * 3], dtype=float).T
    >>> proba += np.random.random(proba.shape) / 2.
    >>> np.argmax(proba, axis=1)
    array([0, 0, 0, 1, 1, 1])
    >>> debug_visual = dict()
    >>> segment_graph_cut_general(slic, proba, gc_regul=0., edge_type='',
    ...                           debug_visual=debug_visual)
    array([0, 0, 0, 1, 1, 1], dtype=int32)
    >>> sorted(debug_visual.keys())  #doctest: +NORMALIZE_WHITESPACE
    ['edge_weights', 'edges', 'img_graph_edges', 'img_graph_segm',
     'imgs_unary_cost', 'segments']
    """
    logging.debug('convert variables and run GraphCut on created graph.')

    edges, edge_weights = compute_edge_weights(segments, image, features,
                                               proba, edge_type)
    edge_weights *= edge_cost
    logging.debug('graph edges weights %r', edge_weights.shape)

    unary_cost = compute_unary_cost(proba)
    logging.debug('graph unaries potentials: %r', unary_cost.shape)
    pairwise_cost = compute_pairwise_cost(gc_regul, proba.shape)
    logging.debug('graph pairwise coefs: \n%r', pairwise_cost)

    if gc_regul <= 0:
        logging.debug('gc_regul=%f so we use just argmax()', gc_regul)
        graph_labels = np.argmin(unary_cost, axis=-1).astype(np.int32)
    else:
        # run GraphCut
        logging.debug('perform GraphCut')
        graph_labels = cut_general_graph(
            edges,
            edge_weights,
            unary_cost,
            pairwise_cost,
            algorithm='expansion',
            # down_weight_factor=np.abs(unary_cost).max(),
            # init_labels=np.argmax(proba, axis=1),
            n_iter=-1)

    insert_gc_debug_images(debug_visual, segments, graph_labels,
                           compute_unary_cost(proba), edges, edge_weights)
    return graph_labels
示例#2
0
def segment_graph_cut_general(segments,
                              proba,
                              image=None,
                              features=None,
                              gc_regul=1.,
                              edge_type='model',
                              edge_cost=1.,
                              dict_debug_imgs=None):
    """ segment the image segmented via superpixels and estimated features

    :param ndarray features: features sor each instance
    :param ndarray segments: segmentation mapping each pixel into a class
    :param ndarray proba: probabilities that each feature belongs to each class
    :param gc_regul: regularisation for GrphCut
    :param {} dict_debug_imgs:
    :return [int]: labelling by resulting classes

    >>> np.random.seed(0)
    >>> segments = np.array([[0] * 3 + [1] * 3 + [2] * 3 + [3] * 3 + [4] * 3,
    ...                      [5] * 3 + [6] * 3 + [7] * 3 + [8] * 3 + [9] * 3])
    >>> proba = np.array([[0] * 6 + [1] * 4, [1] * 6 + [0] * 4], dtype=float).T
    >>> proba += np.random.random(proba.shape) / 2.
    >>> compute_unary_cost(proba)
    array([[ 1.29314378,  0.30571452],
           [ 1.19937775,  0.24093757],
           [ 1.55198349,  0.27986187],
           [ 1.51962643,  0.36872263],
           [ 0.73016106,  0.17539828],
           [ 0.9266883 ,  0.23463524],
           [ 0.24999756,  0.77046392],
           [ 0.03490181,  3.13350924],
           [ 0.01005844,  0.87632529],
           [ 0.32864049,  0.83239528]])
    >>> segment_graph_cut_general(segments, proba, gc_regul=0., edge_type='')
    array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0], dtype=int32)
    >>> labels = segment_graph_cut_general(segments, proba, gc_regul=1.,
    ...                                    edge_type='spatial')
    >>> labels[segments]
    array([[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)
    >>> slic = np.array([[0] * 4 + [1] * 6 + [2] * 4,
    ...                  [3] * 5 + [4] * 4 + [5] * 5])
    >>> proba = np.array([[1] * 3 + [0] * 3, [0] * 3 + [1] * 3], dtype=float).T
    >>> proba += np.random.random(proba.shape) / 2.
    >>> np.argmax(proba, axis=1)
    array([0, 0, 0, 1, 1, 1])
    >>> dict_debug_imgs = dict()
    >>> segment_graph_cut_general(slic, proba, gc_regul=0., edge_type='',
    ...                           dict_debug_imgs=dict_debug_imgs)
    array([0, 0, 0, 1, 1, 1], dtype=int32)
    >>> sorted(dict_debug_imgs.keys())  #doctest: +NORMALIZE_WHITESPACE
    ['edge_weights', 'edges', 'img_graph_edges', 'img_graph_segm',
     'imgs_unary_cost', 'segments']
    """
    logging.debug('convert variables and run GraphCut on created graph.')

    edges, edge_weights = compute_edge_weights(segments, image, features,
                                               proba, edge_type)
    edge_weights *= edge_cost
    logging.debug('graph edges weights %s', repr(edge_weights.shape))

    unary_cost = compute_unary_cost(proba)
    logging.debug('graph unaries potentials: %s', repr(unary_cost.shape))
    pairwise_cost = compute_pairwise_cost(gc_regul, proba.shape)
    logging.debug('graph pairwise coefs: \n%s', repr(pairwise_cost))

    labels = np.argmax(proba, axis=1)
    # run GraphCut
    logging.debug('perform GraphCut')
    graph_labels = cut_general_graph(
        edges,
        edge_weights,
        unary_cost,
        pairwise_cost,
        algorithm='expansion',
        # down_weight_factor=np.abs(unary_cost).max()
        init_labels=labels,
        n_iter=9999)

    insert_gc_debug_images(dict_debug_imgs, segments, graph_labels,
                           compute_unary_cost(proba), edges, edge_weights)
    return graph_labels