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

Public Member Functions

 Graph (std::unique_ptr< std::vector< Node_ptr > > &nodes, int width, int height)
 
bool add_edge (int32_t node_id1, int32_t node_id2)
 
bool merge_nodes (const Node_ptr &node_to_keep, const Node_ptr &node_to_remove)
 
void clear_unconnected_nodes ()
 
const std::vector< Node_ptr > & get_nodes () const
 
const size_t size ()
 
void discover_edges (const std::vector< int32_t > &region_labels, const int32_t width, const int32_t height)
 
void merge_small_area_nodes (const int32_t min_area, const int32_t min_thickness=0)
 
void compute_contours ()
 

Protected Member Functions

void hash_node_ids (void)
 

Protected Attributes

int m_width
 
int m_height
 
std::unique_ptr< std::vector< Node_ptr > > m_nodes
 
std::unordered_map< int32_t, int32_t > m_node_ids
 

Detailed Description

Definition at line 33 of file graph.h.

Constructor & Destructor Documentation

◆ Graph()

Graph::Graph ( std::unique_ptr< std::vector< Node_ptr > > &  nodes,
int  width,
int  height 
)
inline

@brief Safely retrieves a pixel value from a binary image with bounds checking.

@param img Binary image buffer @param w Image width @param h Image height @param x Pixel x-coordinate @param y Pixel y-coordinate @return Pixel value at (x, y), or 0 if out of bounds @brief Analyzes a skeleton image to detect junction points using 8-neighbor crossing-number.

Scans the skeleton image and marks pixels as junctions where three or more branches meet, using the crossing-number method (counts 0→1 transitions in the 8-neighbor ring).

@param skel Binary skeleton image (nonzero = skeleton pixel) @param w Image width @param h Image height @return Junction mask with nonzero entries marking junction pixels

Definition at line 66 of file graph.h.

67 : m_nodes(std::move(nodes))
68 , m_width(width)
69 , m_height(height) {
70 hash_node_ids();
71 }

◆ ~Graph()

Graph::~Graph ( )
inline

Definition at line 73 of file graph.h.

73 {
74 // Break the circular references so the shared_ptrs can reach 0
75 for (auto& node : *m_nodes) {
76 node->clear_all();
77 }
78 }

Member Function Documentation

◆ add_edge()

bool Graph::add_edge ( int32_t  node_id1,
int32_t  node_id2 
)

Definition at line 30 of file graph.cpp.

30 {
31 auto end_node_ids {m_node_ids.end()};
32 auto node1_it {m_node_ids.find(node_id1)};
33 auto node2_it {m_node_ids.find(node_id2)};
34
35 if (node1_it == end_node_ids || node2_it == end_node_ids) {
36 return false;
37 }
38
39 const int32_t idx1 {node1_it->second};
40 const int32_t idx2 {node2_it->second};
41
42 m_nodes->at(idx1)->add_edge(m_nodes->at(idx2));
43 m_nodes->at(idx2)->add_edge(m_nodes->at(idx1));
44 return true;
45}

◆ clear_unconnected_nodes()

void Graph::clear_unconnected_nodes ( )

Definition at line 76 of file graph.cpp.

76 {
77 std::vector<Node_ptr>& nodes {*m_nodes};
78
79 nodes.erase(
80 std::remove_if(
81 nodes.begin(), nodes.end(), [](const Node_ptr& n) { return n->area() == 0; }
82 ),
83 nodes.end()
84 );
85
86 hash_node_ids();
87}

◆ compute_contours()

void Graph::compute_contours ( )

Definition at line 123 of file graph.cpp.

123 {
124
125 /*
126 Shared-edge mode: build region boundaries on the crack grid so
127 neighbouring contours are exactly coincident along shared edges -- no
128 overlap band, no gaps
129 */
130
131 float eps = 0.25f;
132
133 std::vector<int32_t> labels(static_cast<size_t>(m_width) * m_height, -1);
134 for (const Node_ptr& n : get_nodes()) {
135 if (n->area() == 0)
136 continue;
137 for (auto& p : n->get_pixels())
138 labels[static_cast<size_t>(p.position.y) * m_width + p.position.x] = n->id();
139 }
140
141 auto loops = build_shared_loops(labels, m_width, m_height, eps);
142
143 for (const Node_ptr& n : get_nodes()) {
144 if (n->area() == 0)
145 continue;
146 n->clear_contour();
147 auto it = loops.find(n->id());
148 if (it == loops.end())
149 continue;
150 ImageLib::RGBPixel<uint8_t> c = n->color();
151 ImageLib::RGBAPixel<uint8_t> col {c.red, c.green, c.blue, 255};
152 for (std::vector<QuadBezier>& curve : it->second) {
153 std::vector<Point> anchors; // keep contours[] parallel to curves[]
154 anchors.reserve(curve.size() + 1);
155 for (const QuadBezier& q : curve)
156 anchors.push_back(q.p0);
157 if (!curve.empty())
158 anchors.push_back(curve.back().p2);
159 n->m_contours.contours.push_back(std::move(anchors));
160 n->m_contours.curves.push_back(std::move(curve));
161 n->m_contours.colors.push_back(col);
162 n->m_contours.hierarchy.push_back({-1, -1, -1, -1});
163 n->m_contours.is_hole.push_back(false);
164 }
165 }
166}

◆ discover_edges()

void Graph::discover_edges ( const std::vector< int32_t > &  region_labels,
const int32_t  width,
const int32_t  height 
)

Definition at line 89 of file graph.cpp.

91 {
92 // Moore 8-connected neighbourhood
93 constexpr int8_t dirs[8][2] {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
94 {1, 1}, {-1, -1}, {-1, 1}, {1, -1}};
95
96 int32_t rneigh[8];
97
98 for (int32_t y {0}; y < height; ++y) {
99 for (int32_t x {0}; x < width; ++x) {
100 const int32_t idx {y * width + x};
101 const int32_t rid {region_labels[idx]};
102
103 for (int32_t k {0}; k < 8; ++k) {
104 const int32_t nx {x + dirs[k][0]};
105 const int32_t ny {y + dirs[k][1]};
106
107 if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
108 rneigh[k] = region_labels[ny * width + nx];
109 } else {
110 rneigh[k] = rid; // ignore out-of-bounds
111 }
112 }
113
114 for (int32_t r : rneigh) {
115 if (r != rid) {
116 add_edge(rid, r);
117 }
118 }
119 }
120 }
121}

◆ get_nodes()

const std::vector< Node_ptr > & Graph::get_nodes ( ) const
inline

Definition at line 85 of file graph.h.

85 {
86 return *m_nodes;
87 }

◆ hash_node_ids()

void Graph::hash_node_ids ( void  )
protected

Definition at line 23 of file graph.cpp.

23 {
24 for (int32_t i {0}; i < m_nodes->size(); i++) {
25 const int32_t key {m_nodes->at(i)->id()};
26 m_node_ids[key] = i;
27 }
28}

◆ merge_nodes()

bool Graph::merge_nodes ( const Node_ptr &  node_to_keep,
const Node_ptr &  node_to_remove 
)

Definition at line 47 of file graph.cpp.

47 {
48 auto end_node_ids {m_node_ids.end()};
49 auto node1_it {m_node_ids.find(node_to_keep->id())};
50 auto node2_it {m_node_ids.find(node_to_remove->id())};
51
52 if (node1_it == end_node_ids || node2_it == end_node_ids) {
53 return false;
54 }
55
56 const int32_t idx_k {node1_it->second};
57 const int32_t idx_r {node2_it->second};
58
59 // transfer edges from node_to_remove to node_to_keep
60 for (Node_ptr n : m_nodes->at(idx_r)->edges()) {
61 if (n->id() != node_to_keep->id()) {
62 // prevents self referencing
63 n->remove_edge(node_to_remove);
64 n->add_edge(node_to_keep);
65 node_to_keep->add_edge(n);
66 }
67 }
68
69 node_to_keep->add_pixels(node_to_remove->get_pixels());
70
71 node_to_remove->clear_all();
72
73 return true;
74}

◆ merge_small_area_nodes()

void Graph::merge_small_area_nodes ( const int32_t  min_area,
const int32_t  min_thickness = 0 
)

Definition at line 258 of file graph.cpp.

258 {
259 // Keep merging while any pass still makes progress. Using "did this pass
260 // merge anything?" as the loop guard (instead of re-testing every node)
261 // also avoids spinning forever on a node that is too small/thin but has no
262 // valid neighbour to merge into.
263 bool merged_any = true;
264 while (merged_any) {
265 merged_any = false;
266
267 for (const Node_ptr& n : get_nodes()) {
268 if (n->area() == 0)
269 continue;
270
271 bool needs_merge = n->area() < static_cast<size_t>(min_area);
272 if (!needs_merge && min_thickness > 0) {
273 // too thin == no inscribed disk of radius min_thickness/2 fits.
274 needs_merge = 2.0f * max_inscribed_radius(n) < static_cast<float>(min_thickness);
275 }
276 if (!needs_merge)
277 continue;
278
279 ImageLib::RGBPixel<uint8_t> col = n->color();
280
281 Node_ptr best_neighbor = nullptr;
282 float best_score = std::numeric_limits<float>::max();
283 for (const Node_ptr& ne : n->edges()) {
284 if (ne->area() > 0) {
285 float cdist = ImageLib::RGBPixel<uint8_t>::colorDistance(ne->color(), col);
286 float score = static_cast<float>(ne->area()) + 10.f * cdist;
287 if (score < best_score) {
288 best_score = score;
289 best_neighbor = ne;
290 }
291 }
292 }
293
294 // no valid neighbor found, skip this node
295 if (!best_neighbor) {
296 continue;
297 }
298
299 if (best_neighbor->area() >= n->area()) {
300 merge_nodes(best_neighbor, n);
301 } else {
302 merge_nodes(n, best_neighbor);
303 }
304 merged_any = true;
305 }
306
307 clear_unconnected_nodes();
308 }
309}

◆ size()

const size_t Graph::size ( )
inline

Definition at line 89 of file graph.h.

89 {
90 return m_nodes->size();
91 }

Member Data Documentation

◆ m_height

int Graph::m_height
protected

Definition at line 35 of file graph.h.

◆ m_node_ids

std::unordered_map<int32_t, int32_t> Graph::m_node_ids
protected

Definition at line 37 of file graph.h.

◆ m_nodes

std::unique_ptr<std::vector<Node_ptr> > Graph::m_nodes
protected

Definition at line 36 of file graph.h.

◆ m_width

int Graph::m_width
protected

Definition at line 35 of file graph.h.


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