Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
image_graph.h
Go to the documentation of this file.
1 
32 #ifndef IMAGE_GRAPH_H
33 #define IMAGE_GRAPH_H
34 
35 #include <assert.h>
36 #include <vector>
37 #include <map>
38 #include <algorithm>
39 
44 class ImageEdge {
45 public:
48  ImageEdge() : n(0), m(0), w(0) {};
49 
51  unsigned long int n;
52 
54  unsigned long int m;
55 
57  float w;
58 
59 };
60 
65 public:
71  inline bool operator()(const ImageEdge & g, const ImageEdge h) {
72  return (h.w > g.w);
73  }
74 };
75 
80 class ImageNode {
81 public:
84  ImageNode() : b(0), g(0), r(0), l(0), n(1), id(0), max_w(0) {
85 
86  };
87 
89  unsigned char b;
90 
92  unsigned char g;
93 
95  unsigned char r;
96 
98  unsigned long int l; // label, i.e. the index of the node this node belongs to
99 
101  unsigned long int n;
102 
104  unsigned long int id;
105 
107  float max_w;
108 
109 };
110 
115 class ImageGraph {
116 public:
120  K = 0;
121  };
122 
126  ImageGraph(int N) {
127  nodes = std::vector<ImageNode>(N);
128  K = N;
129  }
130 
134  void operator=(const ImageGraph & graph) {
135  nodes = graph.nodes;
136  edges = graph.edges;
137  K = graph.K;
138  }
139 
144  void setNode(int n, ImageNode & node) {
145  nodes[n] = node;
146  }
147 
151  void addNode(ImageNode & node) {
152  nodes.push_back(node);
153  K++;
154  }
155 
159  void addEdge(ImageEdge & edge) {
160  edges.push_back(edge);
161  }
162 
167  ImageNode & getNode(int n) {
168  assert(n >= 0 && n < static_cast<int>(nodes.size()));
169  return nodes[n];
170  }
171 
175  ImageEdge & getEdge(int e) {
176  assert(e >= 0 && e < static_cast<int>(edges.size()));
177  return edges[e];
178  }
179 
183  int getNumNodes() {
184  return nodes.size();
185  }
186 
190  int getNumEdges() {
191  return edges.size();
192  }
197  return K;
198  }
199 
202  void sortEdges() {
203  std::sort(edges.begin(), edges.end(), ImageEdgeSorter());
204  }
205 
214 
215  // Get component of node n.
216  int l = n.l;
217  int id = n.id;
218 
219  while (l != id) {
220  id = nodes[l].id;
221  l = nodes[l].l;
222  }
223 
224  ImageNode & S = nodes[l];
225  assert(S.l == S.id);
226 
227  // Save latest component.
228  n.l = S.id;
229 
230  return S;
231  }
232 
242  void merge(ImageNode & S_n, ImageNode & S_m, ImageEdge & e) {
243  S_m.l = S_n.id;
244 
245  // Update cound.
246  S_n.n += S_m.n;
247 
248  // Update maximum weight.
249  S_n.max_w = std::max(std::max(S_n.max_w, S_m.max_w), e.w);
250 
251  // Update component count.
252  K--;
253  }
254 
255 private:
256 
258  int K;
259 
261  std::vector<ImageEdge> edges;
262 
264  std::vector<ImageNode> nodes;
265 
266 };
267 
268 #endif /* IMAGE_GRAPH_H */
269 
void sortEdges()
Sort the edges by weight.
Definition: image_graph.h:202
unsigned long int n
Size of node after merging with other nodes.
Definition: image_graph.h:101
int getNumComponents()
Get number of connected components.
Definition: image_graph.h:196
float max_w
Maximum weight.
Definition: image_graph.h:107
int getNumEdges()
Get the number of edges.
Definition: image_graph.h:190
Represents a pixel in a video. Each pixel is represented by its color which is needed to compute the ...
Definition: image_graph.h:80
void addNode(ImageNode &node)
Add a new node.
Definition: image_graph.h:151
ImageGraph()
Default constructor.
Definition: image_graph.h:119
unsigned char b
Blue channel.
Definition: image_graph.h:86
void operator=(const ImageGraph &graph)
Assignment operator.
Definition: image_graph.h:134
unsigned long int n
Index of first node.
Definition: image_graph.h:48
ImageNode & getNode(int n)
Definition: image_graph.h:167
ImageGraph(int N)
Constructs an image graph with the given exact number of nodes.
Definition: image_graph.h:126
Class for sorting edges according to weight.
Definition: image_graph.h:64
unsigned long int m
Index of second node.
Definition: image_graph.h:54
unsigned char r
Red channel.
Definition: image_graph.h:95
unsigned char g
Green channel.
Definition: image_graph.h:92
void setNode(int n, ImageNode &node)
Set the node of the given index.
Definition: image_graph.h:144
unsigned long int l
The label of the pixel.
Definition: image_graph.h:98
Represents an image graph, consisting of one node per pixel which are 4-connected.
Definition: image_graph.h:115
ImageNode & findNodeComponent(ImageNode &n)
When two nodes get merged, the first node is assigned the id of the second node as label...
Definition: image_graph.h:213
int getNumNodes()
Get the number of nodes.
Definition: image_graph.h:183
float w
Edge weight.
Definition: image_graph.h:57
ImageEdge & getEdge(int e)
Get the e-th edge in the current sorting.
Definition: image_graph.h:175
Represents an edge between two pixels in an image. Each edge is characterized by a weight and the adj...
Definition: image_graph.h:44
bool operator()(const ImageEdge &g, const ImageEdge h)
Compare to edges according to their weights.
Definition: image_graph.h:71
unsigned long int id
Id of the node.
Definition: image_graph.h:104
ImageNode()
Default constructor.
Definition: image_graph.h:84
ImageEdge()
Default constructor.
Definition: image_graph.h:48
void addEdge(ImageEdge &edge)
Add a new edge.
Definition: image_graph.h:159
void merge(ImageNode &S_n, ImageNode &S_m, ImageEdge &e)
Merge two pixels (that is merge two nodes).
Definition: image_graph.h:242