Superpixel Benchmark
Superpixel benchmark, tools and algorithms.
graph_segmentation.h
Go to the documentation of this file.
1 
32 #ifndef GRAPH_SEGMENTATION_H
33 #define GRAPH_SEGMENTATION_H
34 
35 #include <opencv2/opencv.hpp>
36 #include "image_graph.h"
37 
38 #define RAND() ((float) std::rand() / (RAND_MAX))
39 
47 public:
51 
55 
60  virtual float operator()(const ImageNode & n, const ImageNode & m) = 0;
61 
62 };
63 
68 public:
72  // Normalization.
73  D = 255 + 255 + 255;
74  }
75 
80  virtual float operator()(const ImageNode & n, const ImageNode & m) {
81  float dr = std::abs(n.r - m.r);
82  float dg = std::abs(n.g - m.g);
83  float db = std::abs(n.b - m.b);
84 
85  return (dr + dg + db);
86  }
87 
88 private:
89 
91  float D;
92 
93 };
94 
99 public:
103  // Normalization.
104  D = std::sqrt(255*255 + 255*255 + 255*255);
105  }
106 
111  virtual float operator()(const ImageNode & n, const ImageNode & m) {
112  float dr = n.r - m.r;
113  float dg = n.g - m.g;
114  float db = n.b - m.b;
115 
116  return std::sqrt(dr*dr + dg*dg + db*db);
117  }
118 
119 private:
120 
122  float D;
123 
124 };
125 
132 public:
136 
144  virtual bool operator()(const ImageNode & S_n, const ImageNode & S_m,
145  const ImageEdge & e) = 0;
146 
147 };
148 
153 public:
158 
166  virtual bool operator()(const ImageNode & S_n, const ImageNode & S_m,
167  const ImageEdge & e) {
168 
169  float threshold = std::min(S_n.max_w + c/S_n.n, S_m.max_w + c/S_m.n);
170 
171  if (e.w < threshold) {
172  return true;
173  }
174 
175  return false;
176  }
177 
178 private:
179 
181  float c;
182 
183 };
184 
190 public:
194  magic(new GraphSegmentationMagicThreshold(1)) {
195 
196  };
197 
200  virtual ~GraphSegmentation() {};
201 
206  distance = _distance;
207  }
208 
213  magic = _magic;
214  }
215 
220  void buildGraph(const cv::Mat &image);
221 
224  void oversegmentGraph();
225 
229  void enforceMinimumSegmentSize(int M);
230 
234  cv::Mat deriveLabels();
235 
236 protected:
237 
239  int H;
240 
242  int W;
243 
246 
249 
252 
253 };
254 
255 #endif /* GRAPH_SEGMENTATION_H */
256 
unsigned long int n
Size of node after merging with other nodes.
Definition: image_graph.h:101
virtual float operator()(const ImageNode &n, const ImageNode &m)=0
Compute the distance given 2 nodes.
GraphSegmentationEuclideanRGB()
Constructor; sets normalization constant.
Definition: graph_segmentation.h:102
float max_w
Maximum weight.
Definition: image_graph.h:107
Represents a pixel in a video. Each pixel is represented by its color which is needed to compute the ...
Definition: image_graph.h:80
The magic part of the graph segmentation, i.e. s given two nodes decide whether to add an edge betwee...
Definition: graph_segmentation.h:131
void setMagic(GraphSegmentationMagic *_magic)
Set the magic part of graph segmentation.
Definition: graph_segmentation.h:212
unsigned char b
Blue channel.
Definition: image_graph.h:86
void setDistance(GraphSegmentationDistance *_distance)
Set the distance to use.
Definition: graph_segmentation.h:205
GraphSegmentationMagic * magic
The magic part of graph segmentation.
Definition: graph_segmentation.h:251
Manhatten (i.e. L1) distance.
Definition: graph_segmentation.h:67
GraphSegmentationMagic()
Constructor.
Definition: graph_segmentation.h:135
Euclidean RGB distance.
Definition: graph_segmentation.h:98
virtual float operator()(const ImageNode &n, const ImageNode &m)
Compute the distance given 2 nodes.
Definition: graph_segmentation.h:80
ImageGraph graph
The constructed and segmented image graph.
Definition: graph_segmentation.h:245
unsigned char r
Red channel.
Definition: image_graph.h:95
int H
Image height.
Definition: graph_segmentation.h:239
virtual float operator()(const ImageNode &n, const ImageNode &m)
Compute the distance given 2 nodes.
Definition: graph_segmentation.h:111
unsigned char g
Green channel.
Definition: image_graph.h:92
GraphSegmentationManhattenRGB()
Constructor; sets normalization constant.
Definition: graph_segmentation.h:71
Implementation of graph based image segmentation as described in the paper by Felzenswalb and Huttenl...
Definition: graph_segmentation.h:189
virtual bool operator()(const ImageNode &S_n, const ImageNode &S_m, const ImageEdge &e)
Decide whether to merge the two segments corresponding to the given nodes or not. ...
Definition: graph_segmentation.h:166
GraphSegmentationDistance()
Constructor.
Definition: graph_segmentation.h:50
Represents an image graph, consisting of one node per pixel which are 4-connected.
Definition: image_graph.h:115
Interface to be implemented by a concerete distance. The distance defines how the weights between nod...
Definition: graph_segmentation.h:46
GraphSegmentation()
Default constructor; uses the Manhatten distance.
Definition: graph_segmentation.h:193
virtual ~GraphSegmentationDistance()
Destructor.
Definition: graph_segmentation.h:54
float w
Edge weight.
Definition: image_graph.h:57
Definition: graph_segmentation.h:152
int W
Image widt.h.
Definition: graph_segmentation.h:242
Represents an edge between two pixels in an image. Each edge is characterized by a weight and the adj...
Definition: image_graph.h:44
virtual ~GraphSegmentation()
Destructor.
Definition: graph_segmentation.h:200
GraphSegmentationDistance * distance
The underlying distance to use.
Definition: graph_segmentation.h:248
GraphSegmentationMagicThreshold(float c)
Constructor; sets the threshold.
Definition: graph_segmentation.h:157