BowVector.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * File: BowVector.cpp
  3. * Date: March 2011
  4. * Author: Dorian Galvez-Lopez
  5. * Description: bag of words vector
  6. * License: see the LICENSE.txt file
  7. *
  8. */
  9. #include <iostream>
  10. #include <fstream>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <cmath>
  14. #include "BowVector.h"
  15. namespace DBoW2 {
  16. // --------------------------------------------------------------------------
  17. BowVector::BowVector(void)
  18. {
  19. }
  20. // --------------------------------------------------------------------------
  21. BowVector::~BowVector(void)
  22. {
  23. }
  24. // --------------------------------------------------------------------------
  25. void BowVector::addWeight(WordId id, WordValue v)
  26. {
  27. BowVector::iterator vit = this->lower_bound(id);
  28. if(vit != this->end() && !(this->key_comp()(id, vit->first)))
  29. {
  30. vit->second += v;
  31. }
  32. else
  33. {
  34. this->insert(vit, BowVector::value_type(id, v));
  35. }
  36. }
  37. // --------------------------------------------------------------------------
  38. void BowVector::addIfNotExist(WordId id, WordValue v)
  39. {
  40. BowVector::iterator vit = this->lower_bound(id);
  41. if(vit == this->end() || (this->key_comp()(id, vit->first)))
  42. {
  43. this->insert(vit, BowVector::value_type(id, v));
  44. }
  45. }
  46. // --------------------------------------------------------------------------
  47. void BowVector::normalize(LNorm norm_type)
  48. {
  49. double norm = 0.0;
  50. BowVector::iterator it;
  51. if(norm_type == DBoW2::L1)
  52. {
  53. for(it = begin(); it != end(); ++it)
  54. norm += fabs(it->second);
  55. }
  56. else
  57. {
  58. for(it = begin(); it != end(); ++it)
  59. norm += it->second * it->second;
  60. norm = sqrt(norm);
  61. }
  62. if(norm > 0.0)
  63. {
  64. for(it = begin(); it != end(); ++it)
  65. it->second /= norm;
  66. }
  67. }
  68. // --------------------------------------------------------------------------
  69. std::ostream& operator<< (std::ostream &out, const BowVector &v)
  70. {
  71. BowVector::const_iterator vit;
  72. std::vector<unsigned int>::const_iterator iit;
  73. unsigned int i = 0;
  74. const unsigned int N = v.size();
  75. for(vit = v.begin(); vit != v.end(); ++vit, ++i)
  76. {
  77. out << "<" << vit->first << ", " << vit->second << ">";
  78. if(i < N-1) out << ", ";
  79. }
  80. return out;
  81. }
  82. // --------------------------------------------------------------------------
  83. void BowVector::saveM(const std::string &filename, size_t W) const
  84. {
  85. std::fstream f(filename.c_str(), std::ios::out);
  86. WordId last = 0;
  87. BowVector::const_iterator bit;
  88. for(bit = this->begin(); bit != this->end(); ++bit)
  89. {
  90. for(; last < bit->first; ++last)
  91. {
  92. f << "0 ";
  93. }
  94. f << bit->second << " ";
  95. last = bit->first + 1;
  96. }
  97. for(; last < (WordId)W; ++last)
  98. f << "0 ";
  99. f.close();
  100. }
  101. // --------------------------------------------------------------------------
  102. } // namespace DBoW2