def test_contour_length_partition_tree(self): tree, altitudes = TestAttributes.get_test_tree() ref_attribute = np.asarray( [4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 8, 10, 16, 12], dtype=np.float64) attribute = hg.attribute_contour_length(tree) self.assertTrue(np.allclose(ref_attribute, attribute))
def hierarchy_to_optimal_MumfordShah_energy_cut_hierarchy(tree, vertex_weights, leaf_graph, approximation_piecewise_linear_function=10): """ Transform the given hierarchy into an optimal energy cut hierarchy using the piecewise constant Mumford-Shah energy (see function :func:`~higra.hierarchy_to_optimal_energy_cut_hierarchy`). In this context: - the data fidelity energy assumes a piecewise constant model in each node and is given by the variance of the vertex values inside the node (see function :func:`~higra.attribute_gaussian_region_weights_model`) multiplied by its area, - the regularity energy is given by the length of the contour of the node (see function :func:`~higra.attribute_contour_length`). :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param vertex_weights: vertex weights of the leaf graph of the input tree :param leaf_graph: leaf graph of the input tree (deduced from :class:`~higra.CptHierarchy`) :param approximation_piecewise_linear_function: Maximum number of pieces used in the approximated piecewise linear model for the energy function (default 10). :return: a tree (Concept :class:`~higra.CptHierarchy`) and its node altitudes """ area = hg.attribute_area(tree, leaf_graph=leaf_graph) _, variance = hg.attribute_gaussian_region_weights_model(tree, vertex_weights, leaf_graph) perimeter = hg.attribute_contour_length(tree, leaf_graph=leaf_graph) if variance.ndim > 1: variance = np.trace(variance, axis1=1, axis2=2) return hierarchy_to_optimal_energy_cut_hierarchy(tree, variance * area, perimeter, int(approximation_piecewise_linear_function))
def attribute_compactness(tree, area=None, contour_length=None, normalize=True, leaf_graph=None): """ The compactness of a node is defined as its area divided by the square of its perimeter length. :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param area: node area of the input tree (provided by :func:`~higra.attribute_area` on `tree`) :param contour_length: node contour length of the input tree (provided by :func:`~higra.attribute_perimeter_length` on `tree`) :param normalize: if True the result is divided by the maximal compactness value in the tree :param leaf_graph: (deduced from :class:`~higra.CptHierarchy`) :return: a 1d array """ if area is None: area = hg.attribute_area(tree) if contour_length is None: contour_length = hg.attribute_contour_length(tree, leaf_graph=leaf_graph) compactness = area / (contour_length * contour_length) if normalize: max_compactness = np.nanmax(compactness) compactness = compactness / max_compactness return compactness
def attribute_piecewise_constant_Mumford_Shah_energy(tree, vertex_weights, gamma, leaf_graph): """ Piecewise constant Mumford-Shah energy of each node of the input tree. The energy of a node is equal to its data fidelity energy plus gamma times its regularization energy. For the piecewise constant Mumford-Shah model: - the data fidelity energy assumes a piecewise constant model in each node and is given by the variance of the vertex values inside the node (see function :func:`~higra.attribute_gaussian_region_weights_model`) multiplied by its area, - the regularity energy is given by the length of the contour of the node (see function :func:`~higra.attribute_contour_length`). :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param vertex_weights: vertex weights of the leaf graph of the input tree :param gamma: weighting of the regularization term (should be a positive value) :param leaf_graph: leaf graph of the input tree (deduced from :class:`~higra.CptHierarchy`) :return: a 1d array measuring the energy of each node the input tree """ area = hg.attribute_area(tree, leaf_graph=leaf_graph) _, variance = hg.attribute_gaussian_region_weights_model( tree, vertex_weights, leaf_graph) perimeter = hg.attribute_contour_length(tree, leaf_graph=leaf_graph) if variance.ndim > 1: variance = np.trace(variance, axis1=1, axis2=2) return variance * area + gamma * perimeter
def test_contour_length_partition_tree2(self): tree, altitudes = TestAttributes.get_test_tree() hg.set_attribute(hg.CptHierarchy.get_leaf_graph(tree), "no_border_vertex_out_degree", None) ref_attribute = np.asarray( [2, 3, 2, 3, 4, 3, 2, 3, 2, 3, 3, 5, 3, 3, 4, 5, 0], dtype=np.float64) attribute = hg.attribute_contour_length(tree) self.assertTrue(np.allclose(ref_attribute, attribute))
def test_contour_length_rag_partition_tree(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([3, 3, 6, 2, 2, 4, 4, 4, 0], dtype=np.float64) attribute = hg.attribute_contour_length(tree) self.assertTrue(np.allclose(ref_attribute, attribute))
def test_contour_length_tree_of_shapes(self): image = np.asarray( ((0, 0, 0, 0), (0, -2, 2, 0), (0, -1, 1, 0), (0, 0, 0, 0))) tree, altitudes = hg.component_tree_tree_of_shapes_image2d(image) res = hg.attribute_contour_length(tree) ref = np.asarray( (4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 16), dtype=np.float64) self.assertTrue(np.all(res == ref))
def test_contour_length_component_tree(self): g = hg.get_4_adjacency_graph((4, 4)) # for reference, tree is a max tree on the following image # 0, 1, 4, 4, # 7, 5, 6, 8, # 2, 3, 4, 1, # 9, 8, 6, 7 t = hg.Tree( (28, 27, 24, 24, 20, 23, 22, 18, 26, 25, 24, 27, 16, 17, 21, 19, 17, 21, 22, 21, 23, 24, 23, 24, 25, 26, 27, 28, 28), hg.TreeCategory.ComponentTree) res = hg.attribute_contour_length(t, leaf_graph=g) ref = np.asarray((4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 10, 6, 10, 22, 20, 18, 16, 16), dtype=np.float64) self.assertTrue(np.all(res == ref))