LFWriteCache.cpp 3.3 KB

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