LFWriteCache.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 ) : lfrep ( _lfrep )
  22. {
  23. cachedir = conf->gS ( "cache", "root" );
  24. cachemode = Globals::getCacheMode ( conf->gS ( "cache", "mode", "cat" ) );
  25. std::string descFormat_s = conf->gS ( "cache", "descriptor_format", "binary_double" );
  26. if ( descFormat_s == "binary_double" )
  27. descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
  28. else if ( descFormat_s == "binary_uchar" )
  29. descFormat = VVector::FILEFORMAT_BINARY_CHAR;
  30. else if ( descFormat_s == "text_line" )
  31. descFormat = VVector::FILEFORMAT_LINE;
  32. else if ( descFormat_s == "text_ice" )
  33. descFormat = VVector::FILEFORMAT_NICE;
  34. }
  35. LFWriteCache::~LFWriteCache()
  36. {
  37. }
  38. int LFWriteCache::getDescSize () const
  39. {
  40. return lfrep->getDescSize();
  41. }
  42. int LFWriteCache::extractFeatures ( const NICE::Image & img,
  43. VVector & features,
  44. VVector & positions ) const
  45. {
  46. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  47. int ret = lfrep->extractFeatures ( img, features, positions );
  48. std::string filename_desc = filename + ".desc";
  49. std::string filename_pos = filename + ".key";
  50. struct stat dummy;
  51. if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  52. {
  53. fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
  54. ( int ) positions.size() );
  55. if ( features.size() > 0 )
  56. fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
  57. features.save ( filename_desc, descFormat );
  58. positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  59. } else {
  60. fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  61. }
  62. return ret;
  63. }
  64. int LFWriteCache::extractFeatures ( const NICE::ColorImage & img,
  65. VVector & features,
  66. VVector & positions ) const
  67. {
  68. std::string filename = Globals::getCacheFilename ( cachedir, cachemode );
  69. int ret = lfrep->extractFeatures ( img, features, positions );
  70. std::string filename_desc = filename + ".desc";
  71. std::string filename_pos = filename + ".key";
  72. struct stat dummy;
  73. if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  74. {
  75. fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
  76. ( int ) positions.size() );
  77. if ( features.size() > 0 )
  78. fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
  79. features.save ( filename_desc, descFormat );
  80. positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  81. }
  82. else
  83. {
  84. fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  85. }
  86. return ret;
  87. }
  88. void LFWriteCache::visualize ( NICE::Image & img,
  89. const NICE::Vector & feature ) const
  90. {
  91. lfrep->visualize ( img, feature );
  92. }
  93. void LFWriteCache::visualizeFeatures ( NICE::Image & mark,
  94. const VVector & positions,
  95. size_t color ) const
  96. {
  97. lfrep->visualizeFeatures ( mark, positions, color );
  98. }