Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnNNGraph.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnNNGraph.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #if defined(_WIN32)||defined(WIN32)||defined(__WIN32__)||defined(__VISUALC__)
17 #include <cstdint>
18 #endif
19 #include <stdint.h>
20 #include <cassert>
21 #include <list>
22 
23 #include "Eigen/Core"
24 
25 #include "cv.h"
26 
27 #include "drwnBase.h"
28 #include "drwnIO.h"
29 #include "drwnVision.h"
30 
31 using namespace std;
32 using namespace Eigen;
33 
34 // drwnNNGraphEdgeStatus -----------------------------------------------------
37 
38 typedef enum { DRWN_NNG_DIRTY,
39  DRWN_NNG_PROCESSED_ONCE,
40  DRWN_NNG_PROCESSED_TWICE
41 } drwnNNGraphEdgeStatus;
42 
43 // drwnNNGraphNodeIndex ------------------------------------------------------
44 
46  public:
47  uint16_t imgIndx;
48  uint16_t segId;
49 
50  public:
52  drwnNNGraphNodeIndex() : imgIndx(0), segId(0) { /* do nothing */ }
54  drwnNNGraphNodeIndex(uint16_t i, uint16_t s) : imgIndx(i), segId(s) { /* do nothing */ }
55 
57  bool operator<(const drwnNNGraphNodeIndex& n) const {
58  if (imgIndx < n.imgIndx) return true;
59  if (imgIndx > n.imgIndx) return false;
60  return (segId < n.segId);
61  }
62 
64  bool operator==(const drwnNNGraphNodeIndex& n) const {
65  return ((imgIndx == n.imgIndx) && (segId == n.segId));
66  }
67 
69  bool operator!=(const drwnNNGraphNodeIndex& n) const {
70  return ((imgIndx != n.imgIndx) || (segId != n.segId));
71  }
72 
74  friend ostream& operator<<(ostream& os, const drwnNNGraphNodeIndex& n) {
75  os << "(" << n.imgIndx << ", " << n.segId << ")"; return os;
76  }
77 };
78 
79 // drwnNNGraphEdge -----------------------------------------------------------
81 
83  public:
85  float weight;
86 
87  drwnNNGraphEdgeStatus status;
88 
89  public:
92  weight(DRWN_FLT_MAX), status(DRWN_NNG_DIRTY) { /* do nothing */ }
94  drwnNNGraphEdge(const drwnNNGraphNodeIndex& tgtIndx, float w) :
95  targetNode(tgtIndx), weight(w), status(DRWN_NNG_DIRTY) { /* do nothing */ }
96 
98  bool operator<(const drwnNNGraphEdge& e) const {
99  if (weight < e.weight) return true;
100  if (weight > e.weight) return false;
101  return (targetNode < e.targetNode);
102  }
103 };
104 
105 // drwnNNGraphEdge sorting functions -----------------------------------------
106 
107 class drwnNNGraphSortByScore : public std::binary_function<drwnNNGraphEdge, drwnNNGraphEdge, bool> {
108  public:
109  bool operator()(const drwnNNGraphEdge& a, const drwnNNGraphEdge& b) const {
110  return (a.weight < b.weight);
111  }
112 };
113 
114 class drwnNNGraphSortByImage : public std::binary_function<drwnNNGraphEdge, drwnNNGraphEdge, bool> {
115  public:
116  bool operator()(const drwnNNGraphEdge& a, const drwnNNGraphEdge& b) const {
117  return (a.targetNode.imgIndx < b.targetNode.imgIndx) ||
118  ((a.targetNode.imgIndx == b.targetNode.imgIndx) && (a.weight < b.weight));
119  }
120 };
121 
122 // drwnNNGraphEdgeList -------------------------------------------------------
124 
125 typedef list<drwnNNGraphEdge> drwnNNGraphEdgeList;
126 
127 // drwnNNGraphNode -----------------------------------------------------------
129 
131  public:
132  VectorXf features;
133  int32_t label;
134 
135  drwnNNGraphEdgeList edges;
136  set<uint16_t> spatialNeighbours;
137 
138  public:
140  drwnNNGraphNode() : label(-1) { /* do nothing */ }
142  drwnNNGraphNode(const VectorXf& x, int32_t y = -1) :
143  features(x), label(-1) { /* do nothing */ }
144 
146  void clear();
147 
149  bool insert(const drwnNNGraphEdge& e);
150 
152  size_t numBytesOnDisk() const;
154  bool read(istream& is);
156  bool write(ostream& os) const;
157 };
158 
159 // drwnNNGraphImageData ------------------------------------------------------
161 
163  public:
164  static string imgDir;
165  static string imgExt;
166  static string lblDir;
167  static string lblExt;
168  static string segDir;
169  static string segExt;
170 
171  protected:
172  string _name;
173  cv::Mat _img;
174  MatrixXi _labels;
176  vector<unsigned> _colours;
177  vector<cv::Point> _centroids;
178 
179  public:
181  drwnNNGraphImageData(const string &name);
183  drwnNNGraphImageData(const cv::Mat& img, const drwnSuperpixelContainer& segments);
184 
186  const string& name() const { return _name; }
188  const cv::Mat& image() const { return _img; }
190  const MatrixXi& labels() const { return _labels; }
192  const drwnSuperpixelContainer& segments() const { return _segments; }
194  drwnSuperpixelContainer& segments() { return _segments; }
195 
197  size_t numSegments() const { return _segments.size(); }
199  size_t height() const { return _img.rows; }
201  size_t width() const { return _img.cols; }
202 
204  unsigned colour(unsigned segId) const { return _colours[segId]; }
206  cv::Scalar rgbColour(unsigned segId) const { return CV_RGB(_colours[segId] & 0x0000ff,
207  (_colours[segId] >> 8) & 0x0000ff, (_colours[segId] >> 16) & 0x0000ff); }
208 
210  cv::Point centroid(unsigned segId) const { return _centroids[segId]; }
211 
213  void setLabels(const MatrixXi& labels);
214 
216  vector<VectorXd> getSegmentLabelMarginals(int numLabels = -1) const;
217 
218  protected:
220  void cacheSegmentData();
221 };
222 
223 // drwnNNGraphImage ----------------------------------------------------------
225 
227  public:
231 
237 
241  int32_t eqvClass;
242 
243  protected:
244  string _name;
245  vector<drwnNNGraphNode> _nodes;
246 
247  public:
249  drwnNNGraphImage() : drwnPersistentRecord(), bSourceMatchable(true), bTargetMatchable(true),
250  eqvClass(-1), _name("") { /* do nothing */ }
252  drwnNNGraphImage(const string& name, unsigned n = 0);
256  ~drwnNNGraphImage() { /* do nothing */ }
257 
259  const string& name() const { return _name; }
261  size_t numNodes() const { return _nodes.size(); }
262 
264  void clearNodes();
266  void clearEdges();
267 
269  void initialize(const string& name, unsigned n = 0);
271  virtual void initialize(const drwnNNGraphImageData& image);
272 
274  void transformNodeFeatures(const drwnFeatureTransform& xform);
276  void appendNodeFeatures(const drwnNNGraphImageData& image, const cv::Mat& features);
278  void appendNodeFeatures(const drwnNNGraphImageData& image, const vector<cv::Mat>& features);
279 
281  size_t numBytesOnDisk() const;
283  bool read(istream& is);
285  bool write(ostream& os) const;
286 
288  drwnNNGraphImage clone(bool bWithFeatures = true) const;
289 
291  inline const drwnNNGraphNode& operator[](unsigned segId) const { return _nodes[segId]; }
293  inline drwnNNGraphNode& operator[](unsigned segId) { return _nodes[segId]; }
294 
295  protected:
297  virtual void cacheNodeNeighbourhoods(const drwnNNGraphImageData& image);
299  virtual void cacheNodeFeatures(const drwnNNGraphImageData& image);
301  virtual void cacheNodeLabels(const drwnNNGraphImageData& image);
302 };
303 
304 // drwnNNGraph ---------------------------------------------------------------
309 class drwnNNGraph {
310  public:
311  static unsigned int K;
312  static bool DO_PROPAGATE;
313  static bool DO_LOCAL;
314  static bool DO_SEARCH;
315  static int DO_RANDPROJ;
316  static bool DO_ENRICHMENT;
317  static int DO_EXHAUSTIVE;
318 
319  protected:
320  vector<drwnNNGraphImage> _images;
321  map<string, unsigned> _names;
322 
323  public:
325  drwnNNGraph() { /* do nothing */ }
327  virtual ~drwnNNGraph() { /* do nothing */ }
328 
330  bool write(const char *filestem) const;
332  bool read(const char *filestem);
333 
335  void clear() { _images.clear(); }
337  void reserve(size_t n) { _images.reserve(_images.size() + n); }
338 
340  drwnNNGraph clone(bool bWithFeatures = true) const;
341 
343  size_t numImages() const { return _images.size(); }
345  size_t numNodes() const;
347  size_t numEdges() const;
348 
350  size_t numNodesWithLabel(int label) const;
351 
353  int findImage(const string& baseName) const;
355  int appendImage(const string& baseName, unsigned numNodes = 0);
357  int appendImage(const drwnNNGraphImage& image);
361  int removeImage(unsigned imgIndx);
362 
364  pair<double, double> energy() const;
365 
367  inline bool inSameEqvClass(unsigned imgIndxA, unsigned imgIndxB) const {
368  if (imgIndxA == imgIndxB) return true;
369  if (_images[imgIndxA].eqvClass < 0) return false;
370  return (_images[imgIndxA].eqvClass == _images[imgIndxB].eqvClass);
371  }
372 
374  inline const drwnNNGraphImage& operator[](size_t indx) const { return _images[indx]; }
376  inline drwnNNGraphImage& operator[](size_t indx) { return _images[indx]; }
377 
379  inline const drwnNNGraphImage& operator[](const string& baseName) const {
380  const map<string, unsigned>::const_iterator it = _names.find(baseName);
381  return _images[it->second];
382  }
384  inline drwnNNGraphImage& operator[](const string& baseName) {
385  const map<string, unsigned>::const_iterator it = _names.find(baseName);
386  return _images[it->second];
387  }
388 
390  inline const drwnNNGraphNode& operator[](const drwnNNGraphNodeIndex& indx) const {
391  return _images[indx.imgIndx][indx.segId];
392  }
395  return _images[indx.imgIndx][indx.segId];
396  }
397 };
398 
399 // drwnNNGraphNodeAnnotation -------------------------------------------------
403 template <typename T>
405  protected:
406  vector<vector<T> > _tags;
407 
408  public:
410  drwnNNGraphNodeAnnotation() { /* do nothing */ }
412  drwnNNGraphNodeAnnotation(const drwnNNGraph& graph, const T& initValue) {
413  initialize(graph, initValue);
414  }
416  ~drwnNNGraphNodeAnnotation() { /* do nothing */ }
417 
419  void initialize(const drwnNNGraph& graph, const T& initValue) {
420  _tags.clear();
421  _tags.resize(graph.numImages());
422  for (size_t imgIndx = 0; imgIndx < graph.numImages(); imgIndx++) {
423  _tags[imgIndx].resize(graph[imgIndx].numNodes(), initValue);
424  }
425  }
426 
428  void reset(const T& initValue) {
429  for (size_t imgIndx = 0; imgIndx < _tags.size(); imgIndx++) {
430  for (size_t segId = 0; segId < _tags[imgIndx].size(); segId++) {
431  _tags[imgIndx][segId] = initValue;
432  }
433  }
434  }
435 
437  inline const vector<T>& operator[](unsigned imgIndx) const {
438  return _tags[imgIndx];
439  }
441  inline vector<T>& operator[](unsigned imgIndx) {
442  return _tags[imgIndx];
443  }
444 
446  inline const T& operator[](const drwnNNGraphNodeIndex& indx) const {
447  return _tags[indx.imgIndx][indx.segId];
448  }
450  inline T& operator[](const drwnNNGraphNodeIndex& indx) {
451  return _tags[indx.imgIndx][indx.segId];
452  }
453 };
static int DO_RANDPROJ
execute random projection move to given horizon
Definition: drwnNNGraph.h:315
drwnNNGraphImage & operator[](const string &baseName)
returns reference an image (and its nodes)
Definition: drwnNNGraph.h:384
MatrixXi _labels
pixel labels
Definition: drwnNNGraph.h:174
bool bSourceMatchable
Include this image during update (initialize, propagate, local, search, and enrichment). If false the outgoing edges from all nodes in this image are fixed.
Definition: drwnNNGraph.h:230
size_t width() const
return image width
Definition: drwnNNGraph.h:201
string _name
basename for this image
Definition: drwnNNGraph.h:244
drwnNNGraphNode & operator[](const drwnNNGraphNodeIndex &indx)
returns reference a node
Definition: drwnNNGraph.h:394
const drwnNNGraphNode & operator[](const drwnNNGraphNodeIndex &indx) const
returns const reference to a node
Definition: drwnNNGraph.h:390
drwnNNGraphNodeIndex targetNode
index of the target node
Definition: drwnNNGraph.h:84
drwnNNGraphImage & operator[](size_t indx)
returns reference an image (and its nodes)
Definition: drwnNNGraph.h:376
cv::Scalar rgbColour(unsigned segId) const
return average colour (as 8-bit rgb) for a given superpixle
Definition: drwnNNGraph.h:206
uint16_t imgIndx
image index for this node
Definition: drwnNNGraph.h:47
drwnNNGraph()
default constructor
Definition: drwnNNGraph.h:325
size_t numSegments() const
return the number of superpixels
Definition: drwnNNGraph.h:197
Holds image, segments and other housekeeping information for an image.
Definition: drwnNNGraph.h:162
uint16_t segId
superpixel identifier this node
Definition: drwnNNGraph.h:48
bool operator==(const drwnNNGraphNodeIndex &n) const
comparison operator
Definition: drwnNNGraph.h:64
bool operator<(const drwnNNGraphNodeIndex &n) const
default comparison for sorting
Definition: drwnNNGraph.h:57
drwnNNGraphEdge()
default constructor
Definition: drwnNNGraph.h:91
cv::Point centroid(unsigned segId) const
return centroid for a given superpixel
Definition: drwnNNGraph.h:210
Implements the interface for a generic feature transforms possibly with learned parameters, e.g., PCA (unsupervised) or LDA (supervised).
Definition: drwnFeatureTransform.h:31
drwnNNGraphEdgeList edges
sorted outgoing edges (match neighbours)
Definition: drwnNNGraph.h:135
Definition: drwnNNGraph.h:45
drwnNNGraphNodeAnnotation(const drwnNNGraph &graph, const T &initValue)
construct for a given graph
Definition: drwnNNGraph.h:412
const vector< T > & operator[](unsigned imgIndx) const
returns const reference to annotations for an entire image
Definition: drwnNNGraph.h:437
const drwnNNGraphNode & operator[](unsigned segId) const
returns const reference to the node for superpixel segId
Definition: drwnNNGraph.h:291
static string imgDir
directory to prepend to instance basename for images
Definition: drwnNNGraph.h:164
size_t numNodes() const
return the number of nodes (superpixels)
Definition: drwnNNGraph.h:261
map< string, unsigned > _names
image name lookup
Definition: drwnNNGraph.h:321
void reserve(size_t n)
reserve a given number of images (reduced memory allocation)
Definition: drwnNNGraph.h:337
Holds multiple oversegmentations for a given image.
Definition: drwnSuperpixelContainer.h:91
static string segDir
directory to prepend to instance basename for segments
Definition: drwnNNGraph.h:168
T & operator[](const drwnNNGraphNodeIndex &indx)
returns reference an annotation for an individual node
Definition: drwnNNGraph.h:450
int32_t label
label for this node
Definition: drwnNNGraph.h:133
const drwnSuperpixelContainer & segments() const
return the superpixel container
Definition: drwnNNGraph.h:192
static bool DO_PROPAGATE
execute propagate move
Definition: drwnNNGraph.h:312
static string lblExt
extension to append to instance basename for labels
Definition: drwnNNGraph.h:167
const string & name() const
return the name of the image
Definition: drwnNNGraph.h:186
static bool DO_SEARCH
execute random search move
Definition: drwnNNGraph.h:314
friend ostream & operator<<(ostream &os, const drwnNNGraphNodeIndex &n)
stream output operator
Definition: drwnNNGraph.h:74
Holds nodes (superpixels) for a single image.
Definition: drwnNNGraph.h:226
drwnNNGraphNode & operator[](unsigned segId)
returns reference to the node for superpixel segId
Definition: drwnNNGraph.h:293
Templated utility class for holding annotations for every node in a graph. See learning code for exam...
Definition: drwnNNGraph.h:404
static string lblDir
directory to prepend to instance basename for labels
Definition: drwnNNGraph.h:166
cv::Mat _img
the RGB image
Definition: drwnNNGraph.h:173
drwnNNGraphNodeAnnotation()
default constructor (use initialize after constructing)
Definition: drwnNNGraph.h:410
int32_t eqvClass
Equivalence class for this image. If negative (which is the default) the image is considered to be in...
Definition: drwnNNGraph.h:241
const MatrixXi & labels() const
return the labels
Definition: drwnNNGraph.h:190
static unsigned int K
default number of matches per node
Definition: drwnNNGraph.h:311
vector< drwnNNGraphImage > _images
graph images containing nodes
Definition: drwnNNGraph.h:320
drwnNNGraphNodeIndex()
default constructor
Definition: drwnNNGraph.h:52
vector< T > & operator[](unsigned imgIndx)
returns reference an annotation for an entire image
Definition: drwnNNGraph.h:441
bool inSameEqvClass(unsigned imgIndxA, unsigned imgIndxB) const
return true if two images are in the same equivalence class
Definition: drwnNNGraph.h:367
const drwnNNGraphImage & operator[](size_t indx) const
returns const reference to an image (and its nodes)
Definition: drwnNNGraph.h:374
const T & operator[](const drwnNNGraphNodeIndex &indx) const
returns const reference to an annotation for an individual node
Definition: drwnNNGraph.h:446
~drwnNNGraphNodeAnnotation()
destructor
Definition: drwnNNGraph.h:416
bool operator<(const drwnNNGraphEdge &e) const
default comparison for sorting
Definition: drwnNNGraph.h:98
VectorXf features
features for this node
Definition: drwnNNGraph.h:132
Encapsulates a superpixel node in a drwnNNGraph.
Definition: drwnNNGraph.h:130
size_t height() const
return image height
Definition: drwnNNGraph.h:199
Definition: drwnNNGraph.h:114
Definition: drwnNNGraph.h:107
string _name
basename for this image
Definition: drwnNNGraph.h:172
vector< unsigned > _colours
cached 24-bit colour for visualization
Definition: drwnNNGraph.h:176
size_t numImages() const
number of images in the graph
Definition: drwnNNGraph.h:343
const string & name() const
return the name of the image
Definition: drwnNNGraph.h:259
float weight
weight ot score for this edge
Definition: drwnNNGraph.h:85
unsigned colour(unsigned segId) const
return average colour (as 24-bit unsigned) for a given superpixel
Definition: drwnNNGraph.h:204
drwnSuperpixelContainer _segments
superpixel maps
Definition: drwnNNGraph.h:175
Encapsulates an outgoing edge in a drwnNNGraph.
Definition: drwnNNGraph.h:82
static int DO_EXHAUSTIVE
execute n exhaustive search moves per iteration
Definition: drwnNNGraph.h:317
static bool DO_LOCAL
execute local move
Definition: drwnNNGraph.h:313
drwnNNGraphNode()
default constructor
Definition: drwnNNGraph.h:140
vector< cv::Point > _centroids
superpixel centroids for visualization
Definition: drwnNNGraph.h:177
drwnSuperpixelContainer & segments()
return the superpixel container
Definition: drwnNNGraph.h:194
drwnNNGraphEdgeStatus status
status of this edge
Definition: drwnNNGraph.h:87
Interface class for drwnPersistentStorage.
Definition: drwnPersistentStorage.h:25
drwnNNGraphImage()
default constructor
Definition: drwnNNGraph.h:249
static bool DO_ENRICHMENT
execute enrichment moves
Definition: drwnNNGraph.h:316
void reset(const T &initValue)
reset all tags to initValue.
Definition: drwnNNGraph.h:428
drwnNNGraphEdge(const drwnNNGraphNodeIndex &tgtIndx, float w)
constructor
Definition: drwnNNGraph.h:94
Class for maintaining a nearest neighbour graph over superpixel images. Search moves are implemented ...
Definition: drwnNNGraph.h:309
bool bTargetMatchable
Allow other image to match to this one. If false no new edges will be created that end at any node in...
Definition: drwnNNGraph.h:236
drwnNNGraphNodeIndex(uint16_t i, uint16_t s)
constructor
Definition: drwnNNGraph.h:54
~drwnNNGraphImage()
destructor
Definition: drwnNNGraph.h:256
void initialize(const drwnNNGraph &graph, const T &initValue)
Initialize annotation for a given graph. Sets all tags to initValue.
Definition: drwnNNGraph.h:419
virtual ~drwnNNGraph()
destructor
Definition: drwnNNGraph.h:327
vector< drwnNNGraphNode > _nodes
nodes associated with this image
Definition: drwnNNGraph.h:245
bool operator!=(const drwnNNGraphNodeIndex &n) const
not-equal-to operator
Definition: drwnNNGraph.h:69
void clear()
clear the entire graph
Definition: drwnNNGraph.h:335
static string segExt
extension to append to instance basename for segments
Definition: drwnNNGraph.h:169
set< uint16_t > spatialNeighbours
set of spatial neighbours (for search)
Definition: drwnNNGraph.h:136
drwnNNGraphNode(const VectorXf &x, int32_t y=-1)
constructor
Definition: drwnNNGraph.h:142
const drwnNNGraphImage & operator[](const string &baseName) const
returns const reference to an image (and its nodes)
Definition: drwnNNGraph.h:379
static string imgExt
extension to append to instance basename for images
Definition: drwnNNGraph.h:165
const cv::Mat & image() const
return the image
Definition: drwnNNGraph.h:188