LFonHSG.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @file LFonHSG.cpp
  3. * @brief Implementation of the LocalFeatureHSG.h. See description there.
  4. * @author Eric Bach, Alexander Freytag
  5. * @date 26.10.2011
  6. */
  7. /* LocalFeatureHSG Include */
  8. #include "vislearning/features/localfeatures/LFonHSG.h"
  9. using namespace std;
  10. using namespace NICE;
  11. using namespace OBJREC;
  12. #ifdef NICE_USELIB_BOOST
  13. using namespace boost;
  14. #endif
  15. ///////////////////// ///////////////////// /////////////////////
  16. // PROTECTED METHODS
  17. ///////////////////// ///////////////////// /////////////////
  18. void LFonHSG::convertScalesStringToScaleList ( )
  19. {
  20. if ( this->debug )
  21. {
  22. clog << "[log] LocalFeatureHSG::LocalFeatureHSG" << std::endl;
  23. clog << "[log] try to parse the 'scales-string': " << this->scales << " -> ";
  24. }
  25. // the scales are seperated by '+', like in the Van de Sande implementation
  26. char separator = '+';
  27. #ifdef NICE_USELIB_BOOST
  28. typedef tokenizer<boost::char_separator<char> > tokenizer;
  29. char_separator<char> sep (&separator);
  30. tokenizer tokens (scales, sep); // parse the string into tokens
  31. for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
  32. {
  33. if ( this->debug )
  34. clog << *tok_iter << " ";
  35. this->scalesV.push_back(StringTools::convert<float>(*tok_iter));
  36. }
  37. #else // standard
  38. std::vector<std::string> temp;
  39. StringTools::split (scales, separator, temp);
  40. for ( std::vector<std::string>::const_iterator it = temp.begin(); it != temp.end(); ++it)
  41. {
  42. if ( this->debug )
  43. clog << *it << " ";
  44. this->scalesV.push_back(StringTools::convert<float>(*it));
  45. }
  46. #endif
  47. if ( this->debug )
  48. {
  49. clog << std::endl;
  50. }
  51. }
  52. ///////////////////// ///////////////////// /////////////////////
  53. // CONSTRUCTORS / DESTRUCTORS
  54. ///////////////////// ///////////////////// /////////////////
  55. LFonHSG::LFonHSG () : LocalFeatureRepresentation ()
  56. {
  57. this->debug = false;
  58. this->sampleScaling = 50;
  59. this->scales = "1";
  60. /** parse scales string **/
  61. this->convertScalesStringToScaleList();
  62. this->lf = NULL;
  63. }
  64. LFonHSG::LFonHSG ( const NICE::Config * _conf, const std::string _confSection)
  65. {
  66. this->initFromConfig( _conf, _confSection );
  67. }
  68. LFonHSG::~LFonHSG()
  69. {
  70. /** free memory of descriptors **/
  71. // don't waste memory
  72. if ( this->lf != NULL )
  73. {
  74. delete this->lf;
  75. this->lf = NULL;
  76. }
  77. }
  78. void OBJREC::LFonHSG::initFromConfig(const NICE::Config * _conf, const std::string & _confSection)
  79. {
  80. this->debug = _conf->gB (_confSection, "debug", false);
  81. this->sampleScaling = _conf->gI (_confSection, "sample_scaling", 50);
  82. this->scales = _conf->gS (_confSection, "scales" , "1");
  83. this->lf = NULL;
  84. this->lf = OBJREC::GenericLocalFeatureSelection::selectLocalFeature (_conf, _confSection /*TODO check whether it is useful to hand over the confSection string here*/);
  85. /** parse scales string **/
  86. this->convertScalesStringToScaleList();
  87. }
  88. ///////////////////// ///////////////////// /////////////////////
  89. // FEATURE STUFF
  90. ///////////////////// ///////////////////// /////////////////
  91. int LFonHSG::getDescSize() const
  92. {
  93. return lf->getDescSize();
  94. }
  95. void LFonHSG::getPositionsOnHSG (const unsigned int imageWidth, const unsigned int imageHeight, VVector& positions) const
  96. {
  97. if (sampleScaling < 1)
  98. {
  99. std::cerr << "[err] sample-scaling (" << sampleScaling << ") musst be larger the 0!" << std::endl;
  100. return;
  101. }
  102. if ( this->debug )
  103. clog << "[log] LocalFeatureHSG::getPositionsOnHSG calculate ";
  104. bool oddRow = true;
  105. NICE::Vector pos (4);
  106. /** we have to calculate the koo. for every different scale **/
  107. for ( std::vector<float>::const_iterator it = this->scalesV.begin(); it != this->scalesV.end(); ++it)
  108. {
  109. oddRow = true;
  110. for (unsigned int j = sampleScaling; j <= (imageHeight - sampleScaling); j += sampleScaling)
  111. {
  112. for ( unsigned int i = (oddRow ? sampleScaling + sampleScaling / 2 : sampleScaling);
  113. i <= (imageWidth - sampleScaling);
  114. i += sampleScaling
  115. )
  116. {
  117. pos[ 0 ] = i;
  118. pos[ 1 ] = j;
  119. pos[ 2 ] = *it;
  120. pos[ 3 ] = 0;
  121. positions.push_back (pos);
  122. }
  123. oddRow = !oddRow;
  124. }
  125. }
  126. }
  127. int LFonHSG::extractFeatures (const NICE::ColorImage & cimg, VVector & features, VVector & positions) const
  128. {
  129. /** To get the keypoint descriptor, we need the positions of the keypoints. **/
  130. this->getPositionsOnHSG (cimg.width(), cimg.height(), positions);
  131. /** calulate the descriptor-values **/
  132. return lf->getDescriptors (cimg, positions, features);
  133. }
  134. int LFonHSG::extractFeatures (const NICE::Image & img, VVector & features, VVector & positions) const
  135. {
  136. /** To get the keypoint descriptor, we need the positions of the keypoints. **/
  137. this->getPositionsOnHSG (img.width(), img.height(), positions);
  138. /** calculate the descriptor-values **/
  139. return lf->getDescriptors (img, positions, features);
  140. }
  141. void LFonHSG::visualizeFeatures (NICE::Image & mark, const VVector & positions, size_t color) const
  142. {
  143. // TODO: Implementierung des gewaehlten Descriptortyps aufrufen.
  144. throw NICE::Exception( "LFonHSG::visualizeFeatures currently not implemented." );
  145. }
  146. ///////////////////// INTERFACE PERSISTENT /////////////////////
  147. // interface specific methods for store and restore
  148. ///////////////////// INTERFACE PERSISTENT /////////////////////
  149. void LFonHSG::restore ( std::istream & is, int format )
  150. {
  151. //delete everything we knew so far...
  152. this->clear();
  153. if ( is.good() )
  154. {
  155. std::string tmp;
  156. is >> tmp; //class name
  157. if ( ! this->isStartTag( tmp, "LFonHSG" ) )
  158. {
  159. std::cerr << " WARNING - attempt to restore LFonHSG, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  160. throw;
  161. }
  162. bool b_endOfBlock ( false ) ;
  163. while ( !b_endOfBlock )
  164. {
  165. is >> tmp; // start of block
  166. if ( this->isEndTag( tmp, "LFonHSG" ) )
  167. {
  168. b_endOfBlock = true;
  169. continue;
  170. }
  171. tmp = this->removeStartTag ( tmp );
  172. if ( tmp.compare("debug") == 0 )
  173. {
  174. is >> this->debug;
  175. is >> tmp; // end of block
  176. tmp = this->removeEndTag ( tmp );
  177. }
  178. else if ( tmp.compare("sampleScaling") == 0 )
  179. {
  180. is >> this->sampleScaling;
  181. is >> tmp; // end of block
  182. tmp = this->removeEndTag ( tmp );
  183. }
  184. else if ( tmp.compare("scales") == 0 )
  185. {
  186. is >> this->scales;
  187. is >> tmp; // end of block
  188. tmp = this->removeEndTag ( tmp );
  189. }
  190. else if ( tmp.compare("scalesV") == 0 )
  191. {
  192. is >> tmp; //size:
  193. unsigned int ui_scalesVSize;
  194. is >> ui_scalesVSize;
  195. this->scalesV.clear();
  196. //allocate enough memory
  197. this->scalesV.resize( ui_scalesVSize );
  198. std::vector< float >::iterator itScalesV = this->scalesV.begin();
  199. for ( uint tmpIdx = 0; tmpIdx < ui_scalesVSize; tmpIdx++, itScalesV++ )
  200. {
  201. is >> *itScalesV;
  202. }
  203. is >> tmp; // end of block
  204. tmp = this->removeEndTag ( tmp );
  205. }
  206. else if ( tmp.compare("lf") == 0 )
  207. {
  208. OBJREC::GenericLocalFeatureSelection::restoreLocalFeature( this->lf, is );
  209. is >> tmp; // end of block
  210. tmp = this->removeEndTag ( tmp );
  211. }
  212. else
  213. {
  214. std::cerr << "WARNING -- unexpected LFonHSG object -- " << tmp << " -- for restoration... aborting" << std::endl;
  215. throw;
  216. }
  217. }
  218. }
  219. else
  220. {
  221. std::cerr << "LFonHSG::restore -- InStream not initialized - restoring not possible!" << std::endl;
  222. throw;
  223. }
  224. }
  225. void LFonHSG::store ( std::ostream & os, int format ) const
  226. {
  227. if (os.good())
  228. {
  229. // show starting point
  230. os << this->createStartTag( "LFonHSG" ) << std::endl;
  231. os << this->createStartTag( "debug" ) << std::endl;
  232. os << this->debug << std::endl;
  233. os << this->createEndTag( "debug" ) << std::endl;
  234. os << this->createStartTag( "sampleScaling" ) << std::endl;
  235. os << this->sampleScaling << std::endl;
  236. os << this->createEndTag( "sampleScaling" ) << std::endl;
  237. os << this->createStartTag( "scalesV" ) << std::endl;
  238. os << "size: " << this->scalesV.size() << std::endl;
  239. for ( std::vector< float >::const_iterator itScalesV = this->scalesV.begin();
  240. itScalesV != this->scalesV.end();
  241. itScalesV++
  242. )
  243. {
  244. os << *itScalesV << " ";
  245. }
  246. os << std::endl;
  247. os << this->createEndTag( "scalesV" ) << std::endl;
  248. os << this->createStartTag( "lf" ) << std::endl;
  249. if ( this->lf != NULL )
  250. {
  251. //TODO this might be tricky if lf was not intantiated properly.
  252. this->lf->store ( os );
  253. }
  254. os << this->createEndTag( "lf" ) << std::endl;
  255. // done
  256. os << this->createEndTag( "LFonHSG" ) << std::endl;
  257. }
  258. else
  259. {
  260. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  261. }
  262. }
  263. void LFonHSG::clear ()
  264. {
  265. if ( this->lf != NULL )
  266. {
  267. delete this->lf;
  268. this->lf = NULL;
  269. }
  270. }