Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
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
10graph structure. Each Node represents a region of pixels which tracks the color
11and coordinates (RGBXY) of belonging pixels, and its neighbors as edges.
12
13usage:
14
15std::vector<Node_ptr> nodes; // list of all nodes to be tracked
16
17std::unique_ptr<std::vector<RGBXY>> p_ptr =
18std::make_unique<std::vector<RGBXY>>(); // vector of pixels belonging to this
19region Node_ptr n_ptr = std::make_shared<Node>(<node id>, p_ptr);
20nodes.push_back(n_ptr);
21
22Graph manages a collection of nodes. It will take ownership of `nodes`.
23A graph is initialized as a list of disconnected nodes.
24
25std::unique_ptr<std::vector<Node_ptr>> node_ptr =
26std::make_unique<std::vector<Node_ptr>>(std::move(nodes)); Graph G(node_ptr);
27
28Given a region image, edges are discovered and recorded:
29discover_edges(G, region_labels, width, height);
30
31*/
32
33class 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 void process_overlapping_edges();
41
42 public:
43 inline Graph(std::unique_ptr<std::vector<Node_ptr>> &nodes, int width, int height)
44 : m_nodes(std::move(nodes)), m_width(width), m_height(height) {
45 hash_node_ids();
46 }
47
48 inline ~Graph() {
49 // Break the circular references so the shared_ptrs can reach 0
50 for (auto &node : *m_nodes) {
51 node->clear_all();
52 }
53 }
54
55 bool add_edge(int32_t node_id1, int32_t node_id2);
56 bool merge_nodes(const Node_ptr &node_to_keep, const Node_ptr &node_to_remove);
57
58 void clear_unconnected_nodes();
59
60 inline const std::vector<Node_ptr> &get_nodes() const {
61 return *m_nodes;
62 }
63
64 bool all_areas_bigger_than(int32_t min_area);
65 inline const size_t size() {
66 return m_nodes->size();
67 }
68
69 void discover_edges(const std::vector<int32_t> &region_labels, const int32_t width,
70 const int32_t height);
71 void merge_small_area_nodes(const int32_t min_area);
72 void compute_contours();
73};
74
75#endif
Definition graph.h:33