16 #if defined(_WIN32)||defined(WIN32)||defined(__WIN32__)||defined(__VISUALC__)
29 #include "drwnVision.h"
32 using namespace Eigen;
38 typedef enum { DRWN_NNG_DIRTY,
39 DRWN_NNG_PROCESSED_ONCE,
40 DRWN_NNG_PROCESSED_TWICE
41 } drwnNNGraphEdgeStatus;
58 if (imgIndx < n.
imgIndx)
return true;
59 if (imgIndx > n.
imgIndx)
return false;
60 return (segId < n.
segId);
75 os <<
"(" << n.
imgIndx <<
", " << n.
segId <<
")";
return os;
92 weight(DRWN_FLT_MAX), status(DRWN_NNG_DIRTY) { }
95 targetNode(tgtIndx), weight(w), status(DRWN_NNG_DIRTY) { }
99 if (weight < e.
weight)
return true;
100 if (weight > e.
weight)
return false;
125 typedef list<drwnNNGraphEdge> drwnNNGraphEdgeList;
143 features(x), label(-1) { }
152 size_t numBytesOnDisk()
const;
154 bool read(istream& is);
156 bool write(ostream& os)
const;
186 const string&
name()
const {
return _name; }
188 const cv::Mat&
image()
const {
return _img; }
190 const MatrixXi&
labels()
const {
return _labels; }
199 size_t height()
const {
return _img.rows; }
201 size_t width()
const {
return _img.cols; }
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); }
210 cv::Point
centroid(
unsigned segId)
const {
return _centroids[segId]; }
213 void setLabels(
const MatrixXi& labels);
216 vector<VectorXd> getSegmentLabelMarginals(
int numLabels = -1)
const;
220 void cacheSegmentData();
250 eqvClass(-1), _name(
"") { }
259 const string&
name()
const {
return _name; }
269 void initialize(
const string& name,
unsigned n = 0);
281 size_t numBytesOnDisk()
const;
283 bool read(istream& is);
285 bool write(ostream& os)
const;
311 static unsigned int K;
330 bool write(
const char *filestem)
const;
332 bool read(
const char *filestem);
337 void reserve(
size_t n) { _images.reserve(_images.size() + n); }
340 drwnNNGraph clone(
bool bWithFeatures =
true)
const;
345 size_t numNodes()
const;
347 size_t numEdges()
const;
350 size_t numNodesWithLabel(
int label)
const;
353 int findImage(
const string& baseName)
const;
355 int appendImage(
const string& baseName,
unsigned numNodes = 0);
361 int removeImage(
unsigned imgIndx);
364 pair<double, double> energy()
const;
368 if (imgIndxA == imgIndxB)
return true;
369 if (_images[imgIndxA].eqvClass < 0)
return false;
370 return (_images[imgIndxA].eqvClass == _images[imgIndxB].eqvClass);
380 const map<string, unsigned>::const_iterator it = _names.find(baseName);
381 return _images[it->second];
385 const map<string, unsigned>::const_iterator it = _names.find(baseName);
386 return _images[it->second];
403 template <
typename T>
406 vector<vector<T> > _tags;
413 initialize(graph, initValue);
422 for (
size_t imgIndx = 0; imgIndx < graph.
numImages(); imgIndx++) {
423 _tags[imgIndx].resize(graph[imgIndx].numNodes(), 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;
438 return _tags[imgIndx];
442 return _tags[imgIndx];
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
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