GenericLFSelection.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @file GenericLFSelection.h
  3. * @brief This class provides a generic chooser function for different descriptors, which are derived from LocalFeatureRepresentation.
  4. * @date 26.10.2011
  5. */
  6. #ifndef _NICE_OBJREC_LF_SELECTION_INCLUDE
  7. #define _NICE_OBJREC_LF_SELECTION_INCLUDE
  8. #include <core/basics/Exception.h>
  9. #include "vislearning/features/localfeatures/LocalFeatureRepresentation.h"
  10. #include "vislearning/features/localfeatures/LFMikolajczyk.h"
  11. #include "vislearning/features/localfeatures/LFPatches.h"
  12. #include "vislearning/features/localfeatures/LFGenericLocal.h"
  13. #include "vislearning/features/localfeatures/LFSiftPP.h"
  14. // #include "vislearning/features/localfeatures/LFSegmentation.h" //located in objrec, please move this to ensure its functionality
  15. #include "vislearning/features/localfeatures/LFWriteCache.h"
  16. #include "vislearning/features/localfeatures/LFReadCache.h"
  17. // #include "vislearning/features/localfeatures/LFSegContour.h" //located in objrec, please move this to ensure its functionality
  18. #include "vislearning/features/localfeatures/LFColorSande.h"
  19. #include "vislearning/features/localfeatures/LFonHSG.h"
  20. namespace OBJREC {
  21. /** @class GenericLFSelection
  22. * @brief Select a specific LF-Type (local feature representation). LFs compute Descriptors, which DO NOT need given positions, but calculate them ON THEIR OWN!
  23. * All Descriptor-Methods calculate there own positions previously, and on DIFFERENT ways.
  24. */
  25. class GenericLFSelection
  26. {
  27. public:
  28. //! enum specifying for which step the feature extractor shall be used (this influences the config section which will be used)
  29. enum UsageForTrainOrTest { NOTSPECIFIED = 0,
  30. TRAINING,
  31. TESTING
  32. };
  33. /** LocalFeature Selector
  34. * @brief This methode switches between the different LocalFeature-Types. One has to set the "localfeature_type" on a valid value and the methode returns a LocalFeatureRepresentation derived Object.
  35. * @param[in] conf - A pointer to the given configfile, which must contain "section" - "localfeature_type"
  36. * @param[in] section - This string defines the value for "section" in the configfile.
  37. * @param[in] useForTrainOrTest - Specify whether we use the LFRep for Training, Testing, or for both - this influences the choice of the config section that is handed over to the LFRep-method
  38. * @return LocalFeatureRepresentation* - The LocalFeatureRepresentation which contains the selected LocalFeature-Type.
  39. */
  40. static
  41. LocalFeatureRepresentation *selectLocalFeatureRep ( const NICE::Config *conf, std::string section = "features", const UsageForTrainOrTest & useForTrainOrTest = GenericLFSelection::NOTSPECIFIED )
  42. {
  43. // return Value
  44. OBJREC::LocalFeatureRepresentation *lfrep = NULL;
  45. // string which defines the localfeature_type (for Ex. Sande, ...)
  46. std::string localfeature_type = conf->gS(section, "localfeature_type", "");
  47. if ( ( localfeature_type == "mikolajczyk" )|| ( localfeature_type == "LFMikolajczyk" ) )
  48. {
  49. lfrep = new OBJREC::LFMikolajczyk ( conf );
  50. }
  51. else if ( ( localfeature_type == "VANDESANDE" ) || ( localfeature_type == "LFColorSande" ) )//previously: "color"
  52. {
  53. if ( useForTrainOrTest == TRAINING )
  54. {
  55. lfrep = new OBJREC::LFColorSande ( conf, "LFColorSandeTrain" );
  56. }
  57. else if ( useForTrainOrTest == TESTING )
  58. {
  59. lfrep = new OBJREC::LFColorSande ( conf, "LFColorSandeTest" );
  60. }
  61. else //not specified whether for training or testing, so we do not care about
  62. {
  63. lfrep = new OBJREC::LFColorSande ( conf );
  64. }
  65. }
  66. else if ( ( localfeature_type == "sift" ) || ( localfeature_type == "siftpp" ) || ( localfeature_type == "LFSiftPP" ) )
  67. {
  68. lfrep = new OBJREC::LFSiftPP ( conf );
  69. }
  70. else if ( ( localfeature_type == "generic_local" ) || ( localfeature_type == "generic" ) || ( localfeature_type == "LFGenericLocal" ) )
  71. {
  72. int numFeatures = conf->gI(section, "localfeature_count");
  73. lfrep = new OBJREC::LFGenericLocal ( conf, numFeatures );
  74. }
  75. else if ( ( localfeature_type == "grey" ) || ( localfeature_type == "patches" ) || ( localfeature_type == "LFPatches" ) )
  76. {
  77. int numFeatures = conf->gI(section, "localfeature_count");
  78. lfrep = new OBJREC::LFPatches ( conf, numFeatures );
  79. }
  80. else if ( ( localfeature_type == "onHSG" ) || ( localfeature_type == "LFonHSG" ) )
  81. {
  82. if ( useForTrainOrTest == TRAINING )
  83. {
  84. lfrep = new OBJREC::LFonHSG ( conf, "HSGTrain" );
  85. }
  86. else if ( useForTrainOrTest == TESTING )
  87. {
  88. lfrep = new OBJREC::LFonHSG ( conf, "HSGTest" );
  89. }
  90. else //not specified whether for training or testing, so we do not care about
  91. {
  92. lfrep = new OBJREC::LFonHSG( conf );
  93. }
  94. }
  95. else
  96. {
  97. lfrep = NULL;
  98. }
  99. // read features?
  100. bool readfeat;
  101. readfeat = conf->gB ( section, "localfeature_cache_read", false );
  102. // write features?
  103. bool writefeat;
  104. writefeat = conf->gB ( section, "localfeature_cache_write", false );
  105. LocalFeatureRepresentation *writeFeats = NULL;
  106. LocalFeatureRepresentation *readFeats = NULL;
  107. if ( writefeat )
  108. {
  109. // write the features to a file, if there isn't any to read
  110. if ( useForTrainOrTest == TRAINING )
  111. {
  112. writeFeats = new OBJREC::LFWriteCache ( conf, lfrep, "cacheTrain" );
  113. lfrep = writeFeats;
  114. }
  115. else if ( useForTrainOrTest == TESTING )
  116. {
  117. writeFeats = new OBJREC::LFWriteCache ( conf, lfrep, "cacheTest" );
  118. lfrep = writeFeats;
  119. }
  120. else //not specified whether for training or testing, so we do not care about
  121. {
  122. writeFeats = new OBJREC::LFWriteCache ( conf, lfrep );
  123. lfrep = writeFeats;
  124. }
  125. }
  126. if ( readfeat )
  127. {
  128. int numFeatures = conf->gI(section, "localfeature_count", -1);
  129. // read the features from a file
  130. if ( writefeat )
  131. {
  132. if ( useForTrainOrTest == TRAINING )
  133. {
  134. readFeats = new OBJREC::LFReadCache ( conf, writeFeats, numFeatures, "cacheTrain" );
  135. }
  136. else if ( useForTrainOrTest == TESTING )
  137. {
  138. readFeats = new OBJREC::LFReadCache ( conf, writeFeats, numFeatures, "cacheTest" );
  139. }
  140. else //not specified whether for training or testing, so we do not care about
  141. {
  142. readFeats = new OBJREC::LFReadCache ( conf, writeFeats, numFeatures, "cache" );
  143. }
  144. }
  145. else
  146. {
  147. if ( useForTrainOrTest == TRAINING )
  148. {
  149. readFeats = new OBJREC::LFReadCache (conf, lfrep, numFeatures, "cacheTrain" );
  150. }
  151. else if ( useForTrainOrTest == TESTING )
  152. {
  153. readFeats = new OBJREC::LFReadCache (conf, lfrep, numFeatures, "cacheTest" );
  154. }
  155. else //not specified whether for training or testing, so we do not care about
  156. {
  157. readFeats = new OBJREC::LFReadCache (conf, lfrep, numFeatures, "cache" );
  158. }
  159. }
  160. lfrep = readFeats;
  161. }
  162. return lfrep;
  163. };
  164. static
  165. void restoreLocalFeatureRep ( LocalFeatureRepresentation * _lfrep, std::istream & is, int format = 0 )
  166. {
  167. if ( is.good() )
  168. {
  169. if ( _lfrep != NULL )
  170. delete _lfrep;
  171. std::string className;
  172. is >> className; //class name
  173. if ( className == "<LFMikolajczyk>" )
  174. {
  175. _lfrep = new OBJREC::LFMikolajczyk();
  176. }
  177. else if ( className == "<LFColorSande>" )
  178. {
  179. _lfrep = new OBJREC::LFColorSande();
  180. }
  181. else if ( className == "<LFSiftPP>" )
  182. {
  183. _lfrep = new OBJREC::LFSiftPP();
  184. }
  185. else if ( className == "<LFGenericLocal>" )
  186. {
  187. _lfrep = new OBJREC::LFGenericLocal();
  188. }
  189. else if ( className == "<LFPatches>" )
  190. {
  191. _lfrep = new OBJREC::LFPatches();
  192. }
  193. else if ( className == "<LFonHSG>" )
  194. {
  195. _lfrep = new OBJREC::LFonHSG();
  196. }
  197. else
  198. {
  199. fthrow ( NICE::Exception, "GenericLFSelection::restoreLocalFeatureRep -- class name " << className << "unknown. Aborting." );
  200. }
  201. //undo reading of class name
  202. for ( uint i = 0; i < className.size(); i++)
  203. {
  204. is.unget();
  205. }
  206. //now, call the restore method of the underlying object
  207. //NOTE this could be also done externally, leaving only the actual instantiation of the derived objects here
  208. _lfrep->restore ( is );
  209. }
  210. else
  211. {
  212. fthrow ( NICE::Exception, "GenericLFSelection::restoreLocalFeatureRep -- InStream not initialized - restoring not possible!" );
  213. }
  214. };
  215. };
  216. }
  217. #endif