RegionGraph.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @file Graph.h
  3. * @brief a simple graph interface for superregion growing
  4. * @author Björn Fröhlich
  5. * @date 08/19/2009
  6. */
  7. #ifndef GRAPHINCLUDE
  8. #define GRAPHINCLUDE
  9. #include "vislearning/cbaselib/Example.h"
  10. #include <set>
  11. namespace OBJREC {
  12. class Node
  13. {
  14. protected:
  15. //! list of the neighbor nodes
  16. std::vector<Node*> neighbors;
  17. //! is it a borderregion?
  18. bool atborder;
  19. //! number of the region for lookup in Examples
  20. int number;
  21. //! actual class label of the region
  22. int label;
  23. //! size of the region in pixel
  24. int size;
  25. //! used to compute the centroid of the region
  26. int amountx, amounty;
  27. //! saving the bounding box
  28. int minx, miny, maxx, maxy;
  29. //! probabilities for each class
  30. std::vector<double> probs;
  31. public:
  32. /**
  33. * simple constructor
  34. */
  35. Node(int n);
  36. /**
  37. * adds a neighbor to the current node
  38. * @param nb pointer to a neighbor
  39. */
  40. void addNeighbor(Node *nb);
  41. /**
  42. * set atborder to true if the region is a border region
  43. * @param nblabel changed label of the neighbor
  44. */
  45. void change(int nblabel);
  46. /**
  47. * returns the label of the node
  48. * @return
  49. */
  50. int getLabel() const;
  51. /**
  52. * return size of region
  53. * @return
  54. */
  55. int getSize() const;
  56. /**
  57. * set a new label
  58. * @param l
  59. */
  60. void setLabel(int l);
  61. /**
  62. * returns true if region is a borderregion
  63. * @return
  64. */
  65. bool isAtBorder();
  66. /**
  67. * returns the neighbors of the node
  68. * @param nb
  69. */
  70. void getNeighbors(std::vector<Node*> &nb) const;
  71. /**
  72. * returns the number of the region for lookup in Examples
  73. * @return
  74. */
  75. int getRegion() const;
  76. /**
  77. * returns the number of the region for lookup in Examples
  78. * @return
  79. */
  80. int getNumber() const {
  81. return number;
  82. };
  83. /**
  84. * increments the size of the actual region
  85. * @param s
  86. */
  87. void incSize(int s = 1);
  88. /**
  89. * add the position of a pixel of the region
  90. * used to compute the centroid of the region
  91. * @param x
  92. * @param y
  93. */
  94. void addPos(int x, int y);
  95. /**
  96. * returns the postion of the centroid of the region
  97. * @param x
  98. * @param y
  99. */
  100. void getCentroid(int &x, int &y) const;
  101. /**
  102. * set probabilities for each class
  103. * @param _probs vector of probabilities
  104. */
  105. void setProbs(std::vector<double> _probs);
  106. /**
  107. * get the max of width and height
  108. * @return max of width and height of the region
  109. */
  110. int getSpan() const;
  111. /**
  112. * return the bounding box
  113. * @param x0
  114. * @param y0
  115. * @param x1
  116. * @param y1
  117. */
  118. void getRect(int &x0, int &y0, int &x1, int &y1);
  119. /**
  120. * save node information in filestream
  121. * @param fout outputfilestream
  122. */
  123. int store(std::ofstream & fout);
  124. };
  125. class RegionGraph
  126. {
  127. protected:
  128. //! the nodes of the graph
  129. std::vector<Node*> nodes;
  130. public:
  131. /**
  132. * simple constructor
  133. */
  134. RegionGraph() {};
  135. ~RegionGraph();
  136. void get(std::vector<Node*> &n) const;
  137. /**
  138. * creates the graph out of a region matrix
  139. * @param mask region label for each pixel
  140. * @param rgcount number of different regions
  141. */
  142. void computeGraph(const NICE::Matrix &mask, const int &rgcount);
  143. /**
  144. * creates the graph ot of a region matrix mask
  145. * @param regions
  146. * @param mask
  147. */
  148. void computeGraph(const Examples &regions, const NICE::Matrix &mask);
  149. /**
  150. * returns the number of nodes
  151. * @return number of nodes
  152. */
  153. int size() const;
  154. /**
  155. * get pointer to the i-th node
  156. * @param i number of node
  157. * @return node
  158. */
  159. Node* operator[](int i) const;
  160. void write(std::string file);
  161. };
  162. } //namespace
  163. #endif