Beispiel #1
0
 def __prepare_multi_hyperedge(self):
     width, height = self.image.size
     P1(self.graph,
        x_max_idx=width - 1,
        y_max_idx=height - 1,
        image=self.image)
     i_hyperedges_ids = self.__hyperedges_ids(HyperEdge.I)
     P5(self.graph, i_hyperedges_ids[0], self.image)
     P2(self.graph, i_hyperedges_ids[0], self.image)
Beispiel #2
0
 def setUp(self):
     self.graph = nx.Graph()
     self.image = Image.open(IMAGE_PATH)
     width, height = self.image.size
     P1(self.graph,
        x_max_idx=width - 1,
        y_max_idx=height - 1,
        image=self.image)
     self.sorted_nodes_with_data = sorted(self.graph.nodes(data=True),
                                          key=lambda x:
                                          (x[1]['x'], x[1]['y']))
Beispiel #3
0
    def test_p6(self):
        width, height = self.image.size

        P1(self.graph,
           x_max_idx=width - 1,
           y_max_idx=height - 1,
           image=self.image)
        i_hyperedges_ids = self.__hyperedges_ids(HyperEdge.I)
        P5(self.graph, i_hyperedges_ids[0], self.image)
        P2(self.graph, i_hyperedges_ids[0], self.image)

        i_hyperedges_ids = self.__hyperedges_ids(HyperEdge.I)
        print(i_hyperedges_ids)
Beispiel #4
0
    def setUp(self):
        self.graph = nx.Graph()
        self.image = Image.open(IMAGE_PATH)
        width, height = self.image.size
        P1(self.graph,
           x_max_idx=width - 1,
           y_max_idx=height - 1,
           image=self.image)
        hyperedge = [(x, y) for x, y in self.graph.nodes(data=True)
                     if 'label' in y.keys() and y['label'] == 'I'][0]
        hyperedge[1]['should_break'] = 1
        self.graph.add_node(hyperedge[0], **hyperedge[1])
        P2(self.graph, hyperedge_id=hyperedge[0], image=self.image)

        # plot(self.graph)

        self.hyp_fs = [(x, y) for x, y in self.graph.nodes(data=True)
                       if 'label' in y.keys()
                       and y['label'] in [d.name for d in Direction]]
        self.hyp_bs = [(x, y) for x, y in self.graph.nodes(data=True)
                       if 'label' in y.keys() and y['label'] == 'B']
        self.hyp_is = [(x, y) for x, y in self.graph.nodes(data=True)
                       if 'label' in y.keys() and y['label'] == 'I']
        self.hyperedges = {
            Direction.N: {},
            Direction.S: {},
            Direction.E: {},
            Direction.W: {},
        }
        for direction, edges in self.hyperedges.items():
            edges['f'] = [
                x for x in self.hyp_fs if x[1]['label'] == direction.name
            ][0]
            edges['b'] = [
                (x, y) for x, y in self.hyp_bs
                if B_DIRECTION_EDGE_LAMBDAS[direction](y, edges['f'][1])
            ][0]

            f_neighbour = list(self.graph.neighbors(edges['f'][0]))[0]
            b_neighbours = list(self.graph.neighbors(edges['b'][0]))
            edges['is'] = []
            for x, y in self.graph.nodes(data=True):
                if 'label' in y and y['label'] == 'I':
                    i_neighbours = list(self.graph.neighbors(x))

                    if f_neighbour in i_neighbours and (
                            b_neighbours[0] in i_neighbours
                            or b_neighbours[1] in i_neighbours):
                        edges['is'].append((x, y))
Beispiel #5
0
 def prepare_graph_and_get_central_hyperedge(self):
     width, height = self.image.size
     P1(self.graph,
        x_max_idx=width - 1,
        y_max_idx=height - 1,
        image=self.image)
     hyperedges_to_remove = [
         x for x, y in self.graph.nodes(data=True)
         if 'label' in y.keys() and y['label'] == 'B'
     ]
     for id in hyperedges_to_remove:
         self.graph.remove_node(id)
     hyperedge = [(x, y) for x, y in self.graph.nodes(data=True)
                  if 'label' in y.keys() and y['label'] == 'I'][0]
     hyperedge[1]['should_break'] = 1
     self.graph.add_node(hyperedge[0], **hyperedge[1])
     return hyperedge
Beispiel #6
0
    def __prepare_i_hyperedge(self):
        width, height = self.image.size
        P1(self.graph,
           x_max_idx=width - 1,
           y_max_idx=height - 1,
           image=self.image)

        hyperedges_to_remove = [
            x for x, y in self.graph.nodes(data=True)
            if 'label' in y.keys() and y['label'] == HyperEdge.B.name
        ]
        for id in hyperedges_to_remove:
            self.graph.remove_node(id)

        hyperedge = [(x, y) for x, y in self.graph.nodes(data=True)
                     if 'label' in y.keys() and y['label'] == HyperEdge.I.name
                     ][0]
        self.graph.add_node(hyperedge[0], **hyperedge[1])
        return hyperedge
def prepare_graph(image: Image):
    graph = Graph()
    P1(graph, image.width // 2 - 1, image.height - 1, image)

    upper_right_node = image.width - 1, image.height - 1
    mid_right_node = image.width - 1, image.height // 2 - 1
    lower_right_node = image.width - 1, 0
    add_nodes(graph, image,
              [lower_right_node, mid_right_node, upper_right_node])

    upper_hyperedge = uuid4(), 3 * image.width // 4, 3 * image.height // 4
    lower_hyperedge = uuid4(), 3 * image.width // 4, image.height // 4
    add_hyperedges_nodes(graph, [upper_hyperedge, lower_hyperedge])

    upper_hyperedge_neighbors = [(image.width // 2 - 1, image.height - 1),
                                 upper_right_node, mid_right_node]
    lower_hyperedge_neighbors = [
        mid_right_node, lower_right_node, (image.width // 2 - 1, 0)
    ]
    add_hyperedge_edges(graph, upper_hyperedge[0], upper_hyperedge_neighbors)
    add_hyperedge_edges(graph, lower_hyperedge[0], lower_hyperedge_neighbors)

    upper_b_nodes = [(uuid4(), 3 * image.width // 4, image.height - 1),
                     (uuid4(), image.width - 1, 3 * image.height // 4)]
    lower_b_nodes = [(uuid4(), image.width - 1, image.height // 4),
                     (uuid4(), 3 * image.width // 4, 0)]
    add_b_nodes(upper_b_nodes, graph)
    add_b_nodes(lower_b_nodes, graph)

    add_hyperedge_edges(graph, upper_b_nodes[0][0],
                        upper_hyperedge_neighbors[:-1])
    add_hyperedge_edges(graph, upper_b_nodes[1][0],
                        upper_hyperedge_neighbors[1:])
    add_hyperedge_edges(graph, lower_b_nodes[0][0],
                        lower_hyperedge_neighbors[:-1])
    add_hyperedge_edges(graph, lower_b_nodes[1][0],
                        lower_hyperedge_neighbors[1:])

    plot(graph)
    return graph