Img2Num C++ (Internal Developer Docs)  dev
API Documentation
graph.h
1 #ifndef GRAPH_H
2 #define GRAPH_H
3 
4 #include <unordered_map>
5 
6 #include "internal/node.h"
7 
8 /*
9  Graph and Node classes support conversion of a region divided image into a
10 graph structure. Each Node represents a region of pixels which tracks the color
11 and coordinates (RGBXY) of belonging pixels, and its neighbors as edges.
12 
13 usage:
14 
15 std::vector<Node_ptr> nodes; // list of all nodes to be tracked
16 
17 std::unique_ptr<std::vector<RGBXY>> p_ptr =
18 std::make_unique<std::vector<RGBXY>>(); // vector of pixels belonging to this
19 region Node_ptr n_ptr = std::make_shared<Node>(<node id>, p_ptr);
20 nodes.push_back(n_ptr);
21 
22 Graph manages a collection of nodes. It will take ownership of `nodes`.
23 A graph is initialized as a list of disconnected nodes.
24 
25 std::unique_ptr<std::vector<Node_ptr>> node_ptr =
26 std::make_unique<std::vector<Node_ptr>>(std::move(nodes)); Graph G(node_ptr);
27 
28 Given a region image, edges are discovered and recorded:
29 discover_edges(G, region_labels, width, height);
30 
31 */
32 
33 class Graph {
34  protected:
35  int m_width, m_height;
36  std::unique_ptr<std::vector<Node_ptr>> m_nodes;
37  std::unordered_map<int32_t, int32_t> m_node_ids;
38 
39  void hash_node_ids(void);
40 
41  public:
42  inline Graph(std::unique_ptr<std::vector<Node_ptr>> &nodes, int width, int height)
43  : m_nodes(std::move(nodes)), m_width(width), m_height(height) {
44  hash_node_ids();
45  }
46 
47  bool add_edge(int32_t node_id1, int32_t node_id2);
48  bool merge_nodes(const Node_ptr &node_to_keep, const Node_ptr &node_to_remove);
49 
50  void clear_unconnected_nodes();
51 
52  inline const std::vector<Node_ptr> &get_nodes() const {
53  return *m_nodes;
54  }
55 
56  bool all_areas_bigger_than(int32_t min_area);
57  inline const size_t size() {
58  return m_nodes->size();
59  }
60 
61  void discover_edges(const std::vector<int32_t> &region_labels, const int32_t width,
62  const int32_t height);
63  void merge_small_area_nodes(const int32_t min_area);
64  void compute_contours();
65 };
66 
67 #endif
Definition: graph.h:33