BowVector.cpp
Go to the documentation of this file.
1 
10 #include <iostream>
11 #include <fstream>
12 #include <vector>
13 #include <algorithm>
14 #include <cmath>
15 
16 #include "BowVector.h"
17 
18 namespace DBoW2 {
19 
20 // --------------------------------------------------------------------------
21 
23 {
24 }
25 
26 // --------------------------------------------------------------------------
27 
29 {
30 }
31 
32 // --------------------------------------------------------------------------
33 
35 {
36  BowVector::iterator vit = this->lower_bound(id);
37 
38  if(vit != this->end() && !(this->key_comp()(id, vit->first)))
39  {
40  vit->second += v;
41  }
42  else
43  {
44  this->insert(vit, BowVector::value_type(id, v));
45  }
46 }
47 
48 // --------------------------------------------------------------------------
49 
51 {
52  BowVector::iterator vit = this->lower_bound(id);
53 
54  if(vit == this->end() || (this->key_comp()(id, vit->first)))
55  {
56  this->insert(vit, BowVector::value_type(id, v));
57  }
58 }
59 
60 // --------------------------------------------------------------------------
61 
62 void BowVector::normalize(LNorm norm_type)
63 {
64  double norm = 0.0;
65  BowVector::iterator it;
66 
67  if(norm_type == DBoW2::L1)
68  {
69  for(it = begin(); it != end(); ++it)
70  norm += fabs(it->second);
71  }
72  else
73  {
74  for(it = begin(); it != end(); ++it)
75  norm += it->second * it->second;
76  norm = sqrt(norm);
77  }
78 
79  if(norm > 0.0)
80  {
81  for(it = begin(); it != end(); ++it)
82  it->second /= norm;
83  }
84 }
85 
86 // --------------------------------------------------------------------------
87 
88 std::ostream& operator<< (std::ostream &out, const BowVector &v)
89 {
90  BowVector::const_iterator vit;
91  std::vector<unsigned int>::const_iterator iit;
92  unsigned int i = 0;
93  const unsigned int N = v.size();
94  for(vit = v.begin(); vit != v.end(); ++vit, ++i)
95  {
96  out << "<" << vit->first << ", " << vit->second << ">";
97 
98  if(i < N-1) out << ", ";
99  }
100  return out;
101 }
102 
103 // --------------------------------------------------------------------------
104 
105 void BowVector::saveM(const std::string &filename, size_t W) const
106 {
107  std::fstream f(filename.c_str(), std::ios::out);
108 
109  WordId last = 0;
110  BowVector::const_iterator bit;
111  for(bit = this->begin(); bit != this->end(); ++bit)
112  {
113  for(; last < bit->first; ++last)
114  {
115  f << "0 ";
116  }
117  f << bit->second << " ";
118 
119  last = bit->first + 1;
120  }
121  for(; last < (WordId)W; ++last)
122  f << "0 ";
123 
124  f.close();
125 }
126 
127 // --------------------------------------------------------------------------
128 
129 } // namespace DBoW2
130 
f
void addWeight(WordId id, WordValue v)
Definition: BowVector.cpp:34
void saveM(const std::string &filename, size_t W) const
Definition: BowVector.cpp:105
friend std::ostream & operator<<(std::ostream &out, const BowVector &v)
Definition: BowVector.cpp:88
void normalize(LNorm norm_type)
Definition: BowVector.cpp:62
LNorm
L-norms for normalization.
Definition: BowVector.h:29
Vector of words to represent images.
Definition: BowVector.h:56
double WordValue
Value of a word.
Definition: BowVector.h:23
unsigned int WordId
Id of words.
Definition: BowVector.h:20
void addIfNotExist(WordId id, WordValue v)
Definition: BowVector.cpp:50


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47