LFonHSG.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file LFonHSG.cpp
  3. * @brief Implementation of the LocalFeatureHSG.h. See description there.
  4. * @author Eric Bach
  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. LFonHSG::LFonHSG (const Config *conf, const string section)
  16. {
  17. /** initialization **/
  18. this->lf = NULL;
  19. /** get parameters for the grid **/
  20. this->sampleScaling = conf->gI (section, "sample_scaling", 50);
  21. this->scales = conf->gS (section, "scales" , "1");
  22. // the scales are seperated by '+', like in the Van de Sande implementation
  23. char separator = '+';
  24. /** get debuginformation **/
  25. this->debug = conf->gB ("debug", "show_log_HSG", false);
  26. /** generate the descriptor-instanz **/
  27. lf = GenericLocalFeatureSelection::selectLocalFeature (conf, section);
  28. /** parse scales string **/
  29. if ( this->debug )
  30. {
  31. clog << "[log] LocalFeatureHSG::LocalFeatureHSG" << std::endl;
  32. clog << "[log] try to parse the 'scales-string': " << this->scales << " -> ";
  33. }
  34. #ifdef NICE_USELIB_BOOST
  35. typedef tokenizer<boost::char_separator<char> > tokenizer;
  36. char_separator<char> sep (separator);
  37. tokenizer tokens (scales, sep); // parse the string into tokens
  38. for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
  39. {
  40. if ( this->debug )
  41. clog << *tok_iter << " ";
  42. scalesV.push_back(StringTools::convert<float>(*tok_iter));
  43. }
  44. #else // standard
  45. std::vector<std::string> temp;
  46. StringTools::split (scales, separator, temp);
  47. for ( std::vector<std::string>::const_iterator it = temp.begin(); it != temp.end(); ++it)
  48. {
  49. if ( this->debug )
  50. clog << *it << " ";
  51. scalesV.push_back(StringTools::convert<float>(*it));
  52. }
  53. #endif
  54. if ( this->debug )
  55. clog << std::endl;
  56. }
  57. LFonHSG::~LFonHSG()
  58. {
  59. /** free memory of descriptors **/
  60. // don't waste memory
  61. if ( this->lf != NULL )
  62. {
  63. delete this->lf;
  64. this->lf = NULL;
  65. }
  66. }
  67. int LFonHSG::getDescSize() const
  68. {
  69. return lf->getDescSize();
  70. }
  71. void LFonHSG::getPositionsOnHSG (const unsigned int imageWidth, const unsigned int imageHeight, VVector& positions) const
  72. {
  73. if (sampleScaling < 1)
  74. {
  75. std::cerr << "[err] sample-scaling (" << sampleScaling << ") musst be larger the 0!" << std::endl;
  76. return;
  77. }
  78. if ( this->debug )
  79. clog << "[log] LocalFeatureHSG::getPositionsOnHSG calculate ";
  80. bool oddRow = true;
  81. NICE::Vector pos (4);
  82. /** we have to calculate the koo. for every different scale **/
  83. for ( std::vector<float>::const_iterator it = this->scalesV.begin(); it != this->scalesV.end(); ++it)
  84. {
  85. oddRow = true;
  86. for (unsigned int j = sampleScaling; j <= (imageHeight - sampleScaling); j += sampleScaling)
  87. {
  88. for ( unsigned int i = (oddRow ? sampleScaling + sampleScaling / 2 : sampleScaling);
  89. i <= (imageWidth - sampleScaling);
  90. i += sampleScaling
  91. )
  92. {
  93. pos[ 0 ] = i;
  94. pos[ 1 ] = j;
  95. pos[ 2 ] = *it;
  96. pos[ 3 ] = 0;
  97. positions.push_back (pos);
  98. }
  99. oddRow = !oddRow;
  100. }
  101. }
  102. }
  103. int LFonHSG::extractFeatures (const NICE::ColorImage & cimg, VVector & features, VVector & positions) const
  104. {
  105. /** To get the keypoint descriptor, we need the positions of the keypoints. **/
  106. this->getPositionsOnHSG (cimg.width(), cimg.height(), positions);
  107. /** calulate the descriptor-values **/
  108. return lf->getDescriptors (cimg, positions, features);
  109. }
  110. int LFonHSG::extractFeatures (const NICE::Image & img, VVector & features, VVector & positions) const
  111. {
  112. /** To get the keypoint descriptor, we need the positions of the keypoints. **/
  113. this->getPositionsOnHSG (img.width(), img.height(), positions);
  114. /** calculate the descriptor-values **/
  115. return lf->getDescriptors (img, positions, features);
  116. }
  117. void LFonHSG::visualizeFeatures (NICE::Image & mark, const VVector & positions, size_t color) const
  118. {
  119. // TODO: Implementierung des gewaehlten Descriptortyps aufrufen.
  120. throw NICE::Exception( "LFonHSG::visualizeFeatures currently not implemented." );
  121. }