Example.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef EXAMPLEINCLUDE
  2. #define EXAMPLEINCLUDE
  3. /**
  4. * @file Example.h
  5. * @brief data caching of several feature images and many more
  6. * @author Erik Rodner, Johannes Ruehle, Sven Sickert
  7. * @date 04/21/2008
  8. */
  9. #include "CachedExample.h"
  10. namespace OBJREC {
  11. /** Cached example with window information */
  12. class Example
  13. {
  14. public:
  15. /** weight of example */
  16. double weight;
  17. /** complete image example */
  18. CachedExample *ce;
  19. /** x position of window */
  20. long long int x;
  21. /** y position of window */
  22. long long int y;
  23. /** z position of window */
  24. long long int z;
  25. /** width of window */
  26. int width;
  27. /** height of window */
  28. int height;
  29. /** depth of window */
  30. int depth;
  31. //! if some examples are related, they have the same position
  32. int position;
  33. //! scale of the feature
  34. double scale;
  35. /** evil workaround: store simple vector example */
  36. NICE::Vector *vec;
  37. /** evil workaround: store sparse vector example */
  38. NICE::SparseVector *svec;
  39. /** simple constructor, initialize everything with 0
  40. */
  41. Example();
  42. /** constructor using a window covering the whole image
  43. @param ce associated image data
  44. */
  45. Example ( CachedExample *_ce );
  46. /** constructor
  47. @param ce associated image data
  48. @param x x position of window
  49. @param y y position of window
  50. @param weight weight of example
  51. */
  52. Example ( CachedExample *ce, int x, int y, double weight = 1.0 );
  53. /** constructor 3d
  54. @param ce associated image data
  55. @param x x position of window
  56. @param y y position of window
  57. @param z z position of window
  58. @param weight weight of example
  59. */
  60. Example ( CachedExample *ce, int x, int y, int z, double weight = 1.0 );
  61. /** constructor
  62. @param ce associated image data
  63. @param x x position of window
  64. @param y y position of window
  65. @param weight weight of example
  66. */
  67. Example ( CachedExample *ce, int x, int y, int width, int height, double weight = 1.0 );
  68. /** constructor 3d
  69. @param ce associated image data
  70. @param x x position of window
  71. @param y y position of window
  72. @param z z position of window
  73. @param width width of window
  74. @param height height of window
  75. @param depth depth of window
  76. @param weight weight of example
  77. */
  78. Example ( CachedExample *ce, int x, int y, int z,
  79. int width, int height, int depth, double weight = 1.0 );
  80. /** evil workaround: simple constructors for std::vector examples
  81. @param vec simple feature vector
  82. @param weight optional weight
  83. */
  84. Example ( NICE::Vector *vec, double weight = 1.0 );
  85. /**
  86. * copy constructor
  87. * @param ex input Example
  88. */
  89. Example ( const Example &ex );
  90. /**
  91. * copies the values of ex in this example
  92. * @param ex input Example
  93. */
  94. void copy ( const Example &ex );
  95. /** destructor */
  96. ~Example ();
  97. /** delete all data associated with this example
  98. (sparse vector, vector, cached example, etc.) */
  99. void clean ();
  100. /**
  101. * load from file (warning: no cachedexamples supported yet
  102. * @param is file
  103. * @param format
  104. */
  105. void restore ( std::istream & is, int format = 0 );
  106. /**
  107. * write to file (warning: no cachedexamples supported yet
  108. * @param is file
  109. * @param format
  110. */
  111. void store ( std::ostream & os, int format = 0 ) const;
  112. };
  113. /** labeled pair of Example
  114. @see Example */
  115. class Examples : public std::vector< std::pair<int, Example> >
  116. {
  117. public:
  118. std::string filename;
  119. inline int getMaxClassNo () const
  120. {
  121. int maxClassno = -1;
  122. for ( const_iterator i = begin(); i != end(); i++ )
  123. if ( i->first > maxClassno )
  124. maxClassno = i->first;
  125. return maxClassno;
  126. }
  127. /** delete all data associated with all examples
  128. (sparse vector, vector, cached example, etc.) */
  129. void clean ();
  130. /**
  131. * @brief Create an Examples object from a given full matrix of features and a vector of sample labels.
  132. *
  133. * The Examples object consists of individual Example objects containing the label and a pointer to the provided raw feature data (stored in variable Example::vec).
  134. * Note: No feature data is copied - an Example only contains a pointer to the raw double data.
  135. * An NICE::Vector is created as an wrapper around this raw double pointer using it, but not copying it.
  136. * You need to take care to delete these wrapper vectors once you're finished working with the Examples object, otherwise you generate a memory leak.
  137. * Calling Examples::clean() handles this.
  138. *
  139. * Note: memory layout needs to be transposed into rows x column: features x samples
  140. * features must lay next to each other in memory, so that each feature vector can
  141. * be adressed by a starting pointer and the number of feature dimensions to come.
  142. *
  143. * @param p_MatFeaturesColumWiseSamples matrix containing the features (dimension M) of N samples ( M x N matrix )
  144. * @param p_VecLabels matrix containing the labels for N samples (1xN)
  145. * @param p_Examples created Examples object (vector of N Example object with each Example containing a valid vec-ptr to the feature data [uncopied] )
  146. * @return true for successful Examples creation
  147. * @author Johannes Ruehle
  148. */
  149. static bool wrapExamplesAroundFeatureMatrix(const NICE::Matrix &p_MatFeaturesColumWiseSamples, const NICE::Vector &p_VecLabels, Examples &p_Examples);
  150. static bool wrapExamplesAroundFeatureMatrix(const NICE::Matrix &p_MatFeaturesColumWiseSamples, const NICE::VectorT<int> &p_VecLabels, Examples &p_Examples);
  151. };
  152. } // namespace
  153. #endif