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 "internal/node.h"
5
6#include <unordered_map>
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
65 public:
66 inline Graph(std::unique_ptr<std::vector<Node_ptr>>& nodes, int width, int height)
67 : m_nodes(std::move(nodes))
68 , m_width(width)
69 , m_height(height) {
70 hash_node_ids();
71 }
72
73 inline ~Graph() {
74 // Break the circular references so the shared_ptrs can reach 0
75 for (auto& node : *m_nodes) {
76 node->clear_all();
77 }
78 }
79
80 bool add_edge(int32_t node_id1, int32_t node_id2);
81 bool merge_nodes(const Node_ptr& node_to_keep, const Node_ptr& node_to_remove);
82
83 void clear_unconnected_nodes();
84
85 inline const std::vector<Node_ptr>& get_nodes() const {
86 return *m_nodes;
87 }
88
89 inline const size_t size() {
90 return m_nodes->size();
91 }
92
93 void discover_edges(
94 const std::vector<int32_t>& region_labels, const int32_t width, const int32_t height
95 );
96 void merge_small_area_nodes(const int32_t min_area, const int32_t min_thickness = 0);
97 void compute_contours();
98};
99
100#endif
Definition graph.h:33
Graph(std::unique_ptr< std::vector< Node_ptr > > &nodes, int width, int height)
Definition graph.h:66