예제 #1
0
def attribute_frontier_strength(tree, edge_weights, leaf_graph):
    """
    Mean edge weight along the frontier represented by each node the given partition tree.

    In a partition tree, each node represent the merging of 2 or more regions.
    The frontier of a node is then defined as the common contour between the merged regions.
    This function compute the strength of a common contour as the sum of the weights of edges going from one of the
    merged region to the other one divided by the length of the contour.

    The result has the same dtype as the edge_weights array.

    :param tree: input tree
    :param edge_weights: weight of the edges of the leaf graph (if leaf_graph is a region adjacency graph, edge_weights might be weights on the edges of the pre-graph of the rag).
    :param leaf_graph: graph on the leaves of the input tree (deduced from :class:`~higra.CptHierarchy`)
    :return: a 1d array
    """
    # this is a rag like graph
    if hg.CptRegionAdjacencyGraph.validate(
            leaf_graph) and edge_weights.shape[0] != leaf_graph.num_edges():
        edge_weights = hg.rag_accumulate_on_edges(leaf_graph,
                                                  hg.Accumulators.sum,
                                                  edge_weights=edge_weights)

    frontier_length = hg.attribute_frontier_length(tree, leaf_graph=leaf_graph)
    frontier_strength = hg.attribute_frontier_length(tree, edge_weights,
                                                     leaf_graph)
    frontier_strength[tree.num_leaves():] = frontier_strength[tree.num_leaves(
    ):] / frontier_length[tree.num_leaves():]
    return frontier_strength
예제 #2
0
    def test_frontier_length_rag(self):
        g = hg.get_4_adjacency_graph((3, 3))
        vertex_labels = np.asarray(((0, 1, 1), (0, 2, 2), (3, 2, 4)))
        rag = hg.make_region_adjacency_graph_from_labelisation(
            g, vertex_labels)
        edge_weights = np.asarray((1, 5, 4, 3, 6, 2), dtype=np.float64)
        tree, altitudes = hg.bpt_canonical(rag, edge_weights)

        ref_attribute = np.asarray([0, 0, 0, 0, 0, 1, 2, 1, 4],
                                   dtype=np.float64)
        attribute = hg.attribute_frontier_length(tree)
        self.assertTrue(np.allclose(ref_attribute, attribute))
예제 #3
0
def attribute_contour_length(tree,
                             vertex_perimeter=None,
                             edge_length=None,
                             leaf_graph=None):
    """
    Length of the contour (perimeter) of each node of the given tree.

    :param tree: input tree (Concept :class:`~higra.CptHierarchy`)
    :param vertex_perimeter: perimeter of each vertex of the leaf graph (provided by :func:`~higra.attribute_vertex_perimeter` on `leaf_graph`)
    :param edge_length: length of each edge of the leaf graph (provided by :func:`~higra.attribute_edge_length` on `leaf_graph`)
    :param leaf_graph: (deduced from :class:`~higra.CptHierarchy`)
    :return: a 1d array
    """

    if vertex_perimeter is None:
        vertex_perimeter = hg.attribute_vertex_perimeter(leaf_graph)

    if edge_length is None:
        edge_length = hg.attribute_edge_length(leaf_graph)

    if leaf_graph is not None:
        vertex_perimeter = hg.linearize_vertex_weights(vertex_perimeter,
                                                       leaf_graph)

    frontier_length = hg.attribute_frontier_length(tree, edge_length,
                                                   leaf_graph)
    perimeter = hg.accumulate_and_add_sequential(tree, -2 * frontier_length,
                                                 vertex_perimeter,
                                                 hg.Accumulators.sum)

    # hg.cpp._attribute_contour_length_component_tree is more efficient than the partition tree
    # algorithm but it does not work for tree of shapes left in original space (the problem is that
    # two children of a node may become adjacent when the interpolated pixels are removed).

    # if tree.category() == hg.TreeCategory.PartitionTree:
    #     frontier_length = hg.attribute_frontier_length(tree, edge_length, leaf_graph)
    #     perimeter = hg.accumulate_and_add_sequential(tree, -2 * frontier_length, vertex_perimeter,
    #                                                  hg.Accumulators.sum)
    # elif tree.category() == hg.TreeCategory.ComponentTree:
    #     perimeter = hg.cpp._attribute_contour_length_component_tree(tree, leaf_graph, vertex_perimeter,
    #                                                                 edge_length)

    return perimeter
예제 #4
0
    def test_frontier_length(self):
        tree, altitudes = TestAttributes.get_test_tree()

        ref_attribute = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 5]
        attribute = hg.attribute_frontier_length(tree)
        self.assertTrue(np.allclose(ref_attribute, attribute))