Beispiel #1
0
    def test_tree_fusion3(self):
        im1 = np.asarray(
            (0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 2, 1, 1, 1, 3, 3, 3, 2, 1, 1, 1, 3,
             3, 3, 2, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0),
            dtype=np.float32)

        im2 = np.asarray(
            (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 2, 0, 0,
             1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
            dtype=np.float32)

        g = hg.get_4_adjacency_implicit_graph((6, 7))

        t1, _ = hg.component_tree_max_tree(g, im1)
        t2, _ = hg.component_tree_max_tree(g, im2)

        res = hg.tree_fusion_depth_map((t1, t2))

        expected = np.asarray(
            (0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 2, 1, 1, 1, 3, 4, 3, 2, 2, 3, 1, 3,
             3, 3, 2, 2, 3, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0),
            dtype=np.float32)

        diff = expected - res
        self.assertTrue(np.all(diff == diff[0]))
Beispiel #2
0
    def test_reconstruct_leaf_data_component_tree_default(self):
        g = hg.get_4_adjacency_implicit_graph((1, 6))
        vertex_values = np.asarray((1, 5, 4, 3, 3, 6), dtype=np.int32)
        tree, altitudes = hg.component_tree_max_tree(g, vertex_values)

        area = hg.attribute_area(tree)
        output = hg.reconstruct_leaf_data(tree, area)
        ref = np.asarray((6, 1, 2, 5, 5, 1), dtype=np.int32)

        self.assertTrue(np.all(ref == output))
Beispiel #3
0
    def test_reconstruct_leaf_data_component_tree(self):
        g = hg.get_4_adjacency_implicit_graph((1, 6))
        vertex_values = np.asarray((1, 5, 4, 3, 3, 6), dtype=np.int32)
        tree, altitudes = hg.component_tree_max_tree(g, vertex_values)

        condition = np.asarray((True, False, True, False, True, True, False, True, False, True, False), np.bool_)

        output = hg.reconstruct_leaf_data(tree, altitudes, condition)
        ref = np.asarray((1, 4, 4, 1, 1, 6), dtype=np.int32)

        self.assertTrue(np.all(ref == output))
Beispiel #4
0
    def test_tree_attribute_extrema2(self):
        graph = hg.get_4_adjacency_implicit_graph((4, 4))
        vertex_weights = np.asarray(
            (0, 1, 4, 4, 7, 5, 6, 8, 2, 3, 4, 1, 9, 8, 6, 7))

        tree, altitudes = hg.component_tree_max_tree(graph, vertex_weights)

        extrema = hg.attribute_extrema(tree, altitudes)
        expected_extrema = np.asarray(
            (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0,
             0, 0, 0, 0, 0, 0, 0))
        self.assertTrue(np.all(expected_extrema == extrema))
Beispiel #5
0
    def test_component_tree_max_tree(self):
        graph = hg.get_4_adjacency_implicit_graph((4, 4))
        vertex_weights = np.asarray(
            ((0, 1, 4, 4), (7, 5, 6, 8), (2, 3, 4, 1), (9, 8, 6, 7)),
            dtype=np.float64)
        tree, altitudes = hg.component_tree_max_tree(graph, vertex_weights)

        expected_parents = np.asarray(
            (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), np.int64)
        expected_altitudes = np.asarray(
            (0., 1., 4., 4., 7., 5., 6., 8., 2., 3., 4., 1., 9., 8., 6., 7.,
             9., 8., 8., 7., 7., 6., 6., 5., 4., 3., 2., 1., 0.), np.float64)

        self.assertTrue(np.all(expected_parents == tree.parents()))
        self.assertTrue(np.allclose(expected_altitudes, altitudes))
Beispiel #6
0
    def test_simplify_tree_propagate_category(self):
        g = hg.get_4_adjacency_implicit_graph((1, 6))
        vertex_values = np.asarray((1, 5, 4, 3, 3, 6), dtype=np.int32)
        tree, altitudes = hg.component_tree_max_tree(g, vertex_values)

        condition = np.asarray((False, False, False, False, False, False,
                                False, True, False, True, False), np.bool)

        new_tree, node_map = hg.simplify_tree(tree, condition)

        self.assertTrue(
            np.all(new_tree.parents() == (8, 7, 7, 8, 8, 6, 8, 8, 8)))
        self.assertTrue(np.all(node_map == (0, 1, 2, 3, 4, 5, 6, 8, 10)))
        self.assertTrue(new_tree.category() == hg.TreeCategory.ComponentTree)

        rec = hg.reconstruct_leaf_data(new_tree, altitudes[node_map])
        self.assertTrue(np.all(rec == (1, 4, 4, 1, 1, 6)))
Beispiel #7
0
    def test_area_filter_max_tree(self):
        graph = hg.get_4_adjacency_implicit_graph((5, 5))
        vertex_weights = np.asarray(
            ((-5, 2, 2, 5, 5), (-4, 2, 2, 6, 5), (3, 3, 3, 3, 3),
             (-2, -2, -2, 9, 7), (-1, 0, -2, 8, 9)),
            dtype=np.float64)
        tree, altitudes = hg.component_tree_max_tree(graph, vertex_weights)
        area = hg.attribute_area(tree)

        filtered_weights = hg.reconstruct_leaf_data(tree, altitudes, area <= 4)

        expected_filtered_weights = \
            np.asarray(((-5, 2, 2, 3, 3),
                        (-4, 2, 2, 3, 3),
                        (3, 3, 3, 3, 3),
                        (-2, -2, -2, 3, 3),
                        (-2, -2, -2, 3, 3)), dtype=np.float64)

        self.assertTrue(np.all(filtered_weights == expected_filtered_weights))
Beispiel #8
0
    def test_attribute_extinction_value2(self):
        graph = hg.get_4_adjacency_implicit_graph((4, 4))
        vertex_weights = np.asarray(
            (0, 1, 4, 4, 7, 5, 6, 8, 2, 3, 4, 1, 9, 8, 6, 7))

        tree, altitudes = hg.component_tree_max_tree(graph, vertex_weights)
        area = hg.attribute_area(tree)

        expected_ext = np.asarray(
            (0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0, 1, 16, 16, 4, 1, 1,
             16, 4, 4, 16, 16, 16, 16, 16))

        ext = hg.attribute_extinction_value(tree, altitudes, area)
        self.assertTrue(np.all(expected_ext == ext))

        ext = hg.attribute_extinction_value(tree, altitudes, area, False)
        self.assertTrue(np.all(expected_ext == ext))

        ext = hg.attribute_extinction_value(tree, altitudes, area,
                                            "decreasing")
        self.assertTrue(np.all(expected_ext == ext))
Beispiel #9
0
    def test_moment_of_inertia(self):
        """
        The test image is a binary image of 5x15 pixels:
        
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        0 0 1 0 0 1 0 0 0 0 0 1 1 1 0
        0 1 1 1 0 1 0 1 1 1 0 1 1 1 0
        0 0 1 0 0 1 0 0 0 0 0 1 1 1 0
        0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
        
        The max-tree of this image is composed of five non-leaf nodes: 
        the root and the four maxima (a cross, a vertical line, a horizontal line and a square)
        Using the formula given in https://en.wikipedia.org/wiki/Image_moment, the moment of inertia of each shape is:
          - Cross: 0.16
          - Square: 0.1481
          - Vertical and horizontal lines: 0.2222
          - Rectangle (root): 0.2756
        The moment of inertia of the leaf nodes are set to 0.0
        
       
        """
        graph = hg.get_4_adjacency_implicit_graph((5, 15))
        vertex_weights = np.asarray(
            [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0],
             [0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0],
             [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

        tree, altitudes = hg.component_tree_max_tree(graph, vertex_weights)
        res = hg.attribute_moment_of_inertia(tree)
        ref = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1481, 0.2222, 0.16,
               0.2222, 0.2756)
        self.assertTrue(np.allclose(res, ref, atol=0.0001))