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