Img2Num C++ (Internal Developer Docs) dev
API Documentation
Loading...
Searching...
No Matches
Node Class Reference
+ Collaboration diagram for Node:

Public Member Functions

 Node (int32_t id, std::unique_ptr< std::vector< RGBXY > > &pixels)
 
XY centroid () const
 
ImageLib::RGBPixel< uint8_t > color () const
 
std::array< int32_t, 4 > bounding_box_xywh () const
 
std::array< int, 4 > create_binary_image (std::vector< uint8_t > &binary) const
 
void clear_contour ()
 
void compute_contour ()
 
int32_t id () const
 
size_t area () const
 
const std::set< Node_ptr > & edges () const
 
size_t num_edges () const
 
const std::vector< RGBXY > & get_pixels () const
 
ColoredContoursget_contours ()
 
void add_pixels (const std::vector< RGBXY > &new_pixels)
 
void add_edge_pixel (const XY edge_pixel)
 
void clear_edge_pixels ()
 
void clear_all ()
 
void add_edge (const Node_ptr &node)
 
void remove_edge (const Node_ptr &node)
 
void remove_all_edges ()
 

Public Attributes

ColoredContours m_contours
 

Protected Attributes

int32_t m_id
 
std::unique_ptr< std::vector< RGBXY > > m_pixels
 
std::set< Node_ptr > m_edges {}
 
std::set< XYm_edge_pixels {}
 

Detailed Description

Definition at line 65 of file node.h.

Constructor & Destructor Documentation

◆ Node()

Node::Node ( int32_t  id,
std::unique_ptr< std::vector< RGBXY > > &  pixels 
)
inline

Definition at line 76 of file node.h.

77 : m_id(id), m_pixels(std::move(pixels)) {
78 }

Member Function Documentation

◆ add_edge()

void Node::add_edge ( const Node_ptr &  node)
inline

Definition at line 119 of file node.h.

119 {
120 m_edges.insert(node);
121 }

◆ add_edge_pixel()

void Node::add_edge_pixel ( const XY  edge_pixel)

Definition at line 170 of file node.cpp.

170 {
171 m_edge_pixels.insert(edge_pixel);
172}

◆ add_pixels()

void Node::add_pixels ( const std::vector< RGBXY > &  new_pixels)

Definition at line 164 of file node.cpp.

164 {
165 for (auto &c : new_pixels) {
166 m_pixels->push_back(c);
167 }
168}

◆ area()

size_t Node::area ( ) const
inline

Definition at line 96 of file node.h.

96 {
97 return m_pixels->size();
98 };

◆ bounding_box_xywh()

std::array< int32_t, 4 > Node::bounding_box_xywh ( ) const

Definition at line 54 of file node.cpp.

54 {
55 if (m_pixels->empty()) {
56 return {0, 0, 0, 0};
57 }
58
59 int32_t x_min{INT_MAX};
60 int32_t y_min{INT_MAX};
61 int32_t x_max{0};
62 int32_t y_max{0};
63 for (auto &[_, p] : *m_pixels) {
64 if (p.x < x_min) {
65 x_min = p.x;
66 }
67 if (p.x > x_max) {
68 x_max = p.x;
69 }
70 if (p.y < y_min) {
71 y_min = p.y;
72 }
73 if (p.y > y_max) {
74 y_max = p.y;
75 }
76 }
77
78 for (auto &p : m_edge_pixels) {
79 if (p.x < x_min) {
80 x_min = p.x;
81 }
82 if (p.x > x_max) {
83 x_max = p.x;
84 }
85 if (p.y < y_min) {
86 y_min = p.y;
87 }
88 if (p.y > y_max) {
89 y_max = p.y;
90 }
91 }
92
93 const int32_t w{x_max - x_min + 1};
94 const int32_t h{y_max - y_min + 1};
95
96 return std::array<int32_t, 4>{x_min, y_min, w, h};
97}

◆ centroid()

XY Node::centroid ( ) const

Definition at line 9 of file node.cpp.

9 {
10 XY centroid{0, 0};
11 const int32_t m_pixels_size{static_cast<int32_t>(m_pixels->size())};
12
13 // Guard against division by zero after loop
14 if (m_pixels_size == 0) {
15 return centroid;
16 }
17
18 for (auto &[_, pos] : *m_pixels) {
19 centroid.x += pos.x;
20 centroid.y += pos.y;
21 }
22
23 centroid.x /= m_pixels_size;
24 centroid.y /= m_pixels_size;
25
26 return centroid;
27}
Definition node.h:38

◆ clear_all()

void Node::clear_all ( )

Definition at line 178 of file node.cpp.

178 {
179 m_edges.clear();
180 m_pixels->clear();
181 m_edge_pixels.clear();
182}

◆ clear_contour()

void Node::clear_contour ( void  )

Definition at line 120 of file node.cpp.

120 {
121 m_contours.contours.clear();
122 m_contours.hierarchy.clear();
123 m_contours.is_hole.clear();
124 m_contours.colors.clear();
125 m_contours.curves.clear();
126}

◆ clear_edge_pixels()

void Node::clear_edge_pixels ( )

Definition at line 174 of file node.cpp.

174 {
175 m_edge_pixels.clear();
176}

◆ color()

ImageLib::RGBPixel< uint8_t > Node::color ( ) const

Definition at line 29 of file node.cpp.

29 {
30 const int32_t m_pixels_size{static_cast<int32_t>(m_pixels->size())};
31
32 // Guard against division by zero after loop
33 if (m_pixels_size == 0) {
34 return {0, 0, 0};
35 }
36
37 float r{0};
38 float g{0};
39 float b{0};
40 for (auto &[color, _] : *m_pixels) {
41 r += color.red;
42 g += color.green;
43 b += color.blue;
44 }
45
46 r /= m_pixels_size;
47 g /= m_pixels_size;
48 b /= m_pixels_size;
49
50 // Accept lossy conversion - the difference is very minimal
51 return {static_cast<uint8_t>(r), static_cast<uint8_t>(g), static_cast<uint8_t>(b)};
52}

◆ compute_contour()

void Node::compute_contour ( void  )

Definition at line 128 of file node.cpp.

128 {
129 // return list of all contours present in Node.
130 // usually 1 sometimes more if holes are present
131
132 clear_contour();
133
134 std::vector<uint8_t> binary;
135 std::array<int, 4> xywh{create_binary_image(binary)};
136
137 int xmin = xywh[0];
138 int ymin = xywh[1];
139 int bw = xywh[2];
140 int bh = xywh[3];
141
142 ContoursResult contour_res = contours::find_contours(binary, bw, bh);
143
144 for (size_t cidx = 0; cidx < contour_res.contours.size(); ++cidx) {
145 auto &contour = contour_res.contours[cidx];
146 for (auto &p : contour) {
147 p.x += xmin;
148 p.y += ymin;
149 }
150
151 // if (contour_res.is_hole[cidx]) { continue; }
152
153 ImageLib::RGBPixel<uint8_t> _col = color();
154 ImageLib::RGBAPixel<uint8_t> col{_col.red, _col.green, _col.blue, 255};
155 m_contours.contours.push_back(contour);
156 m_contours.hierarchy.push_back(contour_res.hierarchy[cidx]);
157 m_contours.is_hole.push_back(contour_res.is_hole[cidx]);
158 m_contours.colors.push_back(col);
159 }
160
161 m_contours.curves.resize(m_contours.contours.size());
162}

◆ create_binary_image()

std::array< int, 4 > Node::create_binary_image ( std::vector< uint8_t > &  binary) const

Definition at line 99 of file node.cpp.

99 {
100 std::array<int, 4> xywh{bounding_box_xywh()};
101
102 binary.resize(static_cast<size_t>(xywh[2]) * static_cast<size_t>(xywh[3]), 0);
103
104 for (auto &[_, p] : *m_pixels) {
105 int32_t _x = p.x - xywh[0];
106 int32_t _y = p.y - xywh[1];
107 binary[_y * xywh[2] + _x] = 1;
108 }
109
110 // include the edge pixels to ensure contour overlap with neighbor
111 for (auto &p : m_edge_pixels) {
112 int32_t _x = p.x - xywh[0];
113 int32_t _y = p.y - xywh[1];
114 binary[_y * xywh[2] + _x] = 1;
115 }
116
117 return xywh;
118}

◆ edges()

const std::set< Node_ptr > & Node::edges ( ) const
inline

Definition at line 99 of file node.h.

99 {
100 return m_edges;
101 }

◆ get_contours()

ColoredContours & Node::get_contours ( )
inline

Definition at line 108 of file node.h.

108 {
109 return m_contours;
110 }

◆ get_pixels()

const std::vector< RGBXY > & Node::get_pixels ( ) const
inline

Definition at line 105 of file node.h.

105 {
106 return *m_pixels;
107 }

◆ id()

int32_t Node::id ( ) const
inline

Definition at line 93 of file node.h.

93 {
94 return m_id;
95 };

◆ num_edges()

size_t Node::num_edges ( ) const
inline

Definition at line 102 of file node.h.

102 {
103 return m_edges.size();
104 }

◆ remove_all_edges()

void Node::remove_all_edges ( )
inline

Definition at line 125 of file node.h.

125 {
126 m_edges.clear();
127 }

◆ remove_edge()

void Node::remove_edge ( const Node_ptr &  node)
inline

Definition at line 122 of file node.h.

122 {
123 m_edges.erase(node);
124 }

Member Data Documentation

◆ m_contours

ColoredContours Node::m_contours

Definition at line 88 of file node.h.

◆ m_edge_pixels

std::set<XY> Node::m_edge_pixels {}
protected

Definition at line 73 of file node.h.

73{};

◆ m_edges

std::set<Node_ptr> Node::m_edges {}
protected

Definition at line 69 of file node.h.

69{};

◆ m_id

int32_t Node::m_id
protected

Definition at line 67 of file node.h.

◆ m_pixels

std::unique_ptr<std::vector<RGBXY> > Node::m_pixels
protected

Definition at line 68 of file node.h.


The documentation for this class was generated from the following files: