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/*
65 *Node represents a region - collection of pixels, and their neighboring regions
66 *(edges)
67 */
68
69class Node {
70 protected:
71 int32_t m_id;
72 std::unique_ptr<std::vector<RGBXY>> m_pixels;
73 std::set<Node_ptr> m_edges {};
74
75 // pixels considered for contour tracing but not influencing other
76 // node properties such as color
77 std::set<XY> m_edge_pixels {};
78
79 public:
80 inline Node(int32_t id, std::unique_ptr<std::vector<RGBXY>>& pixels)
81 : m_id(id)
82 , m_pixels(std::move(pixels)) {
83 }
84
85 XY centroid() const;
86 ImageLib::RGBPixel<uint8_t> color() const;
87 std::array<int32_t, 4> bounding_box_xywh() const;
88 std::array<int, 4> create_binary_image(std::vector<uint8_t>& binary) const;
89
90 // keep track of its own contour points
91 // only filled in when compute_contour() is called
92 // though these are public only Graph should access them
93 ColoredContours m_contours;
94 void clear_contour();
95 void compute_contour();
96
97 /* access member variables */
98 inline int32_t id() const {
99 return m_id;
100 };
101 inline size_t area() const {
102 return m_pixels->size();
103 };
104 inline const std::set<Node_ptr>& edges() const {
105 return m_edges;
106 }
107 inline size_t num_edges() const {
108 return m_edges.size();
109 }
110 inline const std::vector<RGBXY>& get_pixels() const {
111 return *m_pixels;
112 }
113 inline ColoredContours& get_contours() {
114 return m_contours;
115 }
116
117 /* modify member variables */
118 void add_pixels(const std::vector<RGBXY>& new_pixels);
119 void add_edge_pixel(const XY edge_pixel);
120 void clear_edge_pixels();
121
122 void clear_all();
123
124 inline void add_edge(const Node_ptr& node) {
125 m_edges.insert(node);
126 }
127 inline void remove_edge(const Node_ptr& node) {
128 m_edges.erase(node);
129 }
130 inline void remove_all_edges() {
131 m_edges.clear();
132 }
133};
134
135#endif
Definition node.h:69
Definition node.h:51
Definition node.h:38