LFWriteCache.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @file LFWriteCache.cpp
  3. * @brief read local features from file
  4. * @author Erik Rodner
  5. * @date 02/14/2008
  6. */
  7. #include "core/vector/VectorT.h"
  8. #include "core/vector/MatrixT.h"
  9. #include "core/image/ImageT.h"
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include "vislearning/baselib/Globals.h"
  15. #include "core/basics/StringTools.h"
  16. #include "vislearning/features/localfeatures/LFWriteCache.h"
  17. using namespace OBJREC;
  18. using namespace std;
  19. using namespace NICE;
  20. LFWriteCache::LFWriteCache ( const Config *conf,
  21. const LocalFeatureRepresentation *_lfrep,
  22. const std::string & _section ) : lfrep ( _lfrep )
  23. {
  24. cachedir = conf->gS ( _section, "root" );
  25. cachemode = Globals::getCacheMode ( conf->gS ( _section, "mode", "cat" ) );
  26. std::string descFormat_s = conf->gS ( _section, "descriptor_format", "binary_double" );
  27. if ( descFormat_s == "binary_double" )
  28. descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
  29. else if ( descFormat_s == "binary_uchar" )
  30. descFormat = VVector::FILEFORMAT_BINARY_CHAR;
  31. else if ( descFormat_s == "text_line" )
  32. descFormat = VVector::FILEFORMAT_LINE;
  33. else if ( descFormat_s == "text_ice" )
  34. descFormat = VVector::FILEFORMAT_NICE;
  35. }
  36. LFWriteCache::~LFWriteCache()
  37. {
  38. }
  39. int LFWriteCache::getDescSize () const
  40. {
  41. return lfrep->getDescSize();
  42. }
  43. int LFWriteCache::extractFeatures ( const NICE::Image & img,
  44. VVector & features,
  45. VVector & positions ) const
  46. {
  47. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  48. int ret = lfrep->extractFeatures ( img, features, positions );
  49. std::string filename_desc = filename + ".desc";
  50. std::string filename_pos = filename + ".key";
  51. struct stat dummy;
  52. if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  53. {
  54. fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
  55. ( int ) positions.size() );
  56. if ( features.size() > 0 )
  57. fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
  58. features.save ( filename_desc, descFormat );
  59. positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  60. } else {
  61. fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  62. }
  63. return ret;
  64. }
  65. int LFWriteCache::extractFeatures ( const NICE::ColorImage & img,
  66. VVector & features,
  67. VVector & positions ) const
  68. {
  69. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  70. int ret = lfrep->extractFeatures ( img, features, positions );
  71. std::string filename_desc = filename + ".desc";
  72. std::string filename_pos = filename + ".key";
  73. struct stat dummy;
  74. if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  75. {
  76. fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
  77. ( int ) positions.size() );
  78. if ( features.size() > 0 )
  79. fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
  80. features.save ( filename_desc, descFormat );
  81. positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  82. }
  83. else
  84. {
  85. fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  86. }
  87. return ret;
  88. }
  89. void LFWriteCache::visualize ( NICE::Image & img,
  90. const NICE::Vector & feature ) const
  91. {
  92. lfrep->visualize ( img, feature );
  93. }
  94. void LFWriteCache::visualizeFeatures ( NICE::Image & mark,
  95. const VVector & positions,
  96. size_t color ) const
  97. {
  98. lfrep->visualizeFeatures ( mark, positions, color );
  99. }