GenericLFSelection.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "vislearning/features/localfeatures/LocalFeatureRepresentation.h"
  9. #include "vislearning/features/localfeatures/LFMikolajczyk.h"
  10. #include "vislearning/features/localfeatures/LFPatches.h"
  11. #include "vislearning/features/localfeatures/LFGenericLocal.h"
  12. #include "vislearning/features/localfeatures/LFSiftPP.h"
  13. // #include "vislearning/features/localfeatures/LFSegmentation.h" //located in objrec, please move this to ensure its functionality
  14. #include "vislearning/features/localfeatures/LFWriteCache.h"
  15. #include "vislearning/features/localfeatures/LFReadCache.h"
  16. // #include "vislearning/features/localfeatures/LFSegContour.h" //located in objrec, please move this to ensure its functionality
  17. #include "vislearning/features/localfeatures/LFColorSande.h"
  18. #include "vislearning/features/localfeatures/LFonHSG.h"
  19. namespace OBJREC {
  20. /** @class GenericLFSelection
  21. * @brief Select a specific LF-Type (local feature representation). LFs compute Descriptors, which DO NOT need given positions, but calculate them ON THEIR OWN!
  22. * All Descriptor-Methods calculate there own positions previously, and on DIFFERENT ways.
  23. */
  24. class GenericLFSelection
  25. {
  26. public:
  27. //! enum specifying for which step the feature extractor shall be used (this influences the config section which will be used)
  28. enum UsageForTrainOrTest { NOTSPECIFIED = 0,
  29. TRAINING,
  30. TESTING
  31. };
  32. /** LocalFeature Selector
  33. * @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.
  34. * @param[in] conf - A pointer to the given configfile, which must contain "section" - "localfeature_type"
  35. * @param[in] section - This string defines the value for "section" in the configfile.
  36. * @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
  37. * @return LocalFeatureRepresentation* - The LocalFeatureRepresentation which contains the selected LocalFeature-Type.
  38. */
  39. static
  40. LocalFeatureRepresentation *selectLocalFeatureRep ( const NICE::Config *conf, std::string section = "features", const UsageForTrainOrTest & useForTrainOrTest = NOTSPECIFIED )
  41. {
  42. // return Value
  43. LocalFeatureRepresentation *lfrep = NULL;
  44. // string which defines the localfeature_type (for Ex. Sande, ...)
  45. std::string localfeature_type = conf->gS(section, "localfeature_type", "");
  46. if ( localfeature_type == "mikolajczyk" )
  47. {
  48. lfrep = new LFMikolajczyk ( conf );
  49. }
  50. else if ( localfeature_type == "VANDESANDE" ) //previously: "color"
  51. {
  52. if ( useForTrainOrTest == TRAINING )
  53. {
  54. lfrep = new LFColorSande ( conf, "LFColorSandeTrain" );
  55. }
  56. else if ( useForTrainOrTest == TESTING )
  57. {
  58. lfrep = new LFColorSande ( conf, "LFColorSandeTest" );
  59. }
  60. else //not specified whether for training or testing, so we do not care about
  61. {
  62. lfrep = new LFColorSande ( conf );
  63. }
  64. }
  65. else if ( ( localfeature_type == "sift" ) || ( localfeature_type == "siftpp" ) )
  66. {
  67. lfrep = new LFSiftPP ( conf );
  68. }
  69. else if ( ( localfeature_type == "generic_local" ) || ( localfeature_type == "generic" ) )
  70. {
  71. int numFeatures = conf->gI(section, "localfeature_count");
  72. lfrep = new LFGenericLocal ( conf, numFeatures );
  73. }
  74. else if ( ( localfeature_type == "grey" ) || ( localfeature_type == "patches" ) )
  75. {
  76. int numFeatures = conf->gI(section, "localfeature_count");
  77. lfrep = new LFPatches ( conf, numFeatures );
  78. }
  79. else if( ( localfeature_type == "onHSG" ) )
  80. {
  81. if ( useForTrainOrTest == TRAINING )
  82. {
  83. lfrep = new OBJREC::LFonHSG ( conf, "HSGTrain" );
  84. }
  85. else if ( useForTrainOrTest == TESTING )
  86. {
  87. lfrep = new OBJREC::LFonHSG ( conf, "HSGTest" );
  88. }
  89. else //not specified whether for training or testing, so we do not care about
  90. {
  91. lfrep = new OBJREC::LFonHSG( conf);
  92. }
  93. }
  94. else
  95. {
  96. lfrep = NULL;
  97. }
  98. // if ( conf->gB(section, "localfeature_cache_read", false) )
  99. // {
  100. // int numFeatures = conf->gI(section, "localfeature_count", -1);
  101. // LocalFeatureRepresentation *lfrep_read = new LFReadCache ( conf, lfrep, numFeatures );
  102. // lfrep = lfrep_read;
  103. // }
  104. //
  105. // // no correct localfeature_type was given
  106. // if ( lfrep == NULL )
  107. // fthrow(NICE::Exception, "Local feature type not found: " << localfeature_type );
  108. //
  109. // if ( conf->gB(section, "localfeature_cache_write", false) )
  110. // {
  111. // LocalFeatureRepresentation *lfrep_write = new LFWriteCache ( conf, lfrep );
  112. // lfrep = lfrep_write;
  113. // }
  114. // read features?
  115. bool readfeat;
  116. readfeat = conf->gB ( section, "localfeature_cache_read", true );
  117. // write features?
  118. bool writefeat;
  119. writefeat = conf->gB ( section, "localfeature_cache_write", true );
  120. LocalFeatureRepresentation *writeFeats = NULL;
  121. LocalFeatureRepresentation *readFeats = NULL;
  122. if ( writefeat )
  123. {
  124. // write the features to a file, if there isn't any to read
  125. if ( useForTrainOrTest == TRAINING )
  126. {
  127. writeFeats = new LFWriteCache ( conf, lfrep, "cacheTrain" );
  128. lfrep = writeFeats;
  129. }
  130. else if ( useForTrainOrTest == TESTING )
  131. {
  132. writeFeats = new LFWriteCache ( conf, lfrep, "cacheTest" );
  133. lfrep = writeFeats;
  134. }
  135. else //not specified whether for training or testing, so we do not care about
  136. {
  137. writeFeats = new LFWriteCache ( conf, lfrep );
  138. lfrep = writeFeats;
  139. }
  140. }
  141. //TODO add the section into constructor for readCache
  142. if ( readfeat )
  143. {
  144. int numFeatures = conf->gI(section, "localfeature_count", -1);
  145. // read the features from a file
  146. if ( writefeat )
  147. {
  148. if ( useForTrainOrTest == TRAINING )
  149. {
  150. readFeats = new LFReadCache ( conf, writeFeats, numFeatures );
  151. }
  152. else if ( useForTrainOrTest == TESTING )
  153. {
  154. readFeats = new LFReadCache ( conf, writeFeats, numFeatures );
  155. }
  156. else //not specified whether for training or testing, so we do not care about
  157. {
  158. readFeats = new LFReadCache ( conf, writeFeats, numFeatures );
  159. }
  160. }
  161. else
  162. {
  163. if ( useForTrainOrTest == TRAINING )
  164. {
  165. readFeats = new LFReadCache (conf, lfrep, numFeatures );
  166. }
  167. else if ( useForTrainOrTest == TESTING )
  168. {
  169. readFeats = new LFReadCache (conf, lfrep, numFeatures );
  170. }
  171. else //not specified whether for training or testing, so we do not care about
  172. {
  173. readFeats = new LFReadCache (conf, lfrep, numFeatures );
  174. }
  175. }
  176. lfrep = readFeats;
  177. }
  178. return lfrep;
  179. }
  180. };
  181. }
  182. #endif