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 void process_overlapping_edges();
41
52 inline uint8_t getPixel(const std::vector<uint8_t>& img, int w, int h, int x, int y) {
53 if (x < 0 || x >= w || y < 0 || y >= h)
54 return 0; // Boundary check
55 return img[y * w + x];
56 }
57
70 std::vector<uint8_t> analyzeJunctions(const std::vector<uint8_t>& skel, int w, int h);
71
72 public:
73 inline Graph(std::unique_ptr<std::vector<Node_ptr>>& nodes, int width, int height)
74 : m_nodes(std::move(nodes))
75 , m_width(width)
76 , m_height(height) {
77 hash_node_ids();
78 }
79
80 inline ~Graph() {
81 // Break the circular references so the shared_ptrs can reach 0
82 for (auto& node : *m_nodes) {
83 node->clear_all();
84 }
85 }
86
87 bool add_edge(int32_t node_id1, int32_t node_id2);
88 bool merge_nodes(const Node_ptr& node_to_keep, const Node_ptr& node_to_remove);
89
90 void clear_unconnected_nodes();
91
92 inline const std::vector<Node_ptr>& get_nodes() const {
93 return *m_nodes;
94 }
95
96 bool all_areas_bigger_than(int32_t min_area);
97 inline const size_t size() {
98 return m_nodes->size();
99 }
100
101 void discover_edges(
102 const std::vector<int32_t>& region_labels, const int32_t width, const int32_t height
103 );
104 void merge_small_area_nodes(const int32_t min_area, const int32_t min_thickness = 0);
105 void compute_contours();
106};
107
108#endif
Definition graph.h:33
uint8_t getPixel(const std::vector< uint8_t > &img, int w, int h, int x, int y)
Definition graph.h:52
std::vector< uint8_t > analyzeJunctions(const std::vector< uint8_t > &skel, int w, int h)
Definition graph.cpp:221