Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
node.h
1#ifndef NODE_H
2#define NODE_H
3
4#include "internal/contours.h"
5#include "internal/RGBPixel.h"
6
7#include <array>
8#include <cstdint>
9#include <memory>
10#include <set>
11#include <vector>
12
13/*
14 Graph and Node classes support conversion of a region divided image into a
15graph structure. Each Node represents a region of pixels which tracks the color
16and coordinates (RGBXY) of belonging pixels, and its neighbors as edges.
17
18usage:
19
20std::vector<Node_ptr> nodes; // list of all nodes to be tracked
21
22std::unique_ptr<std::vector<RGBXY>> p_ptr =
23std::make_unique<std::vector<RGBXY>>(); // vector of pixels belonging to this
24region Node_ptr n_ptr = std::make_shared<Node>(<node id>, p_ptr);
25nodes.push_back(n_ptr);
26
27Graph manages a collection of nodes. It will take ownership of `nodes`.
28A graph is initialized as a list of disconnected nodes.
29
30std::unique_ptr<std::vector<Node_ptr>> node_ptr =
31std::make_unique<std::vector<Node_ptr>>(std::move(nodes)); Graph G(node_ptr);
32
33Given a region image, edges are discovered and recorded:
34discover_edges(G, region_labels, width, height);
35
36*/
37
38struct XY {
39 int32_t x, y;
40 std::pair<int32_t, int32_t> xy;
41 XY(int32_t x_, int32_t y_)
42 : x(x_)
43 , y(y_) {
44 xy = std::make_pair(x, y);
45 };
46 bool operator<(const XY& rhs) const {
47 return xy < rhs.xy;
48 };
49};
50
51struct RGBXY {
53 XY position;
54
55 RGBXY(uint8_t r, uint8_t g, uint8_t b, int32_t x, int32_t y)
56 : color(r, g, b)
57 , position {x, y} {
58 }
59};
60
61class Node;
62typedef std::shared_ptr<Node> Node_ptr;
63
64// Orders nodes by id instead of heap address so neighbour iteration is deterministic.
65// Defined after class Node, because Node is incomplete here.
66struct NodeIdLess {
67 bool operator()(const Node_ptr& a, const Node_ptr& b) const;
68};
69
70typedef std::set<Node_ptr, NodeIdLess> Node_set;
71
72/*
73 *Node represents a region - collection of pixels, and their neighboring regions
74 *(edges)
75 */
76
77class Node {
78 protected:
79 int32_t m_id;
80 std::unique_ptr<std::vector<RGBXY>> m_pixels;
81 Node_set m_edges {};
82
83 // pixels considered for contour tracing but not influencing other
84 // node properties such as color
85 std::set<XY> m_edge_pixels {};
86
87 public:
88 inline Node(int32_t id, std::unique_ptr<std::vector<RGBXY>>& pixels)
89 : m_id(id)
90 , m_pixels(std::move(pixels)) {
91 }
92
93 XY centroid() const;
94 ImageLib::RGBPixel<uint8_t> color() const;
95 std::array<int32_t, 4> bounding_box_xywh() const;
96 std::array<int, 4> create_binary_image(std::vector<uint8_t>& binary) const;
97
98 // keep track of its own contour points
99 // only filled in when compute_contour() is called
100 // though these are public only Graph should access them
101 ColoredContours m_contours;
102 void clear_contour();
103 void compute_contour();
104
105 /* access member variables */
106 inline int32_t id() const {
107 return m_id;
108 };
109 inline size_t area() const {
110 return m_pixels->size();
111 };
112 inline const Node_set& edges() const {
113 return m_edges;
114 }
115 inline size_t num_edges() const {
116 return m_edges.size();
117 }
118 inline const std::vector<RGBXY>& get_pixels() const {
119 return *m_pixels;
120 }
121 inline ColoredContours& get_contours() {
122 return m_contours;
123 }
124
125 /* modify member variables */
126 void add_pixels(const std::vector<RGBXY>& new_pixels);
127 void add_edge_pixel(const XY edge_pixel);
128 void clear_edge_pixels();
129
130 void clear_all();
131
132 inline void add_edge(const Node_ptr& node) {
133 m_edges.insert(node);
134 }
135 inline void remove_edge(const Node_ptr& node) {
136 m_edges.erase(node);
137 }
138 inline void remove_all_edges() {
139 m_edges.clear();
140 }
141};
142
143inline bool NodeIdLess::operator()(const Node_ptr& a, const Node_ptr& b) const {
144 return a->id() < b->id();
145}
146
147#endif
Definition node.h:77
Definition node.h:51
Definition node.h:38