ScoringObject.cpp
Go to the documentation of this file.
1 
10 #include <cfloat>
11 #include "TemplatedVocabulary.h"
12 #include "BowVector.h"
13 
14 using namespace DBoW2;
15 
16 // If you change the type of WordValue, make sure you change also the
17 // epsilon value (this is needed by the KL method)
18 const double GeneralScoring::LOG_EPS = log(DBL_EPSILON); // FLT_EPSILON
19 
20 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
22 
23 double L1Scoring::score(const BowVector &v1, const BowVector &v2) const
24 {
25  BowVector::const_iterator v1_it, v2_it;
26  const BowVector::const_iterator v1_end = v1.end();
27  const BowVector::const_iterator v2_end = v2.end();
28 
29  v1_it = v1.begin();
30  v2_it = v2.begin();
31 
32  double score = 0;
33 
34  while(v1_it != v1_end && v2_it != v2_end)
35  {
36  const WordValue& vi = v1_it->second;
37  const WordValue& wi = v2_it->second;
38 
39  if(v1_it->first == v2_it->first)
40  {
41  score += fabs(vi - wi) - fabs(vi) - fabs(wi);
42 
43  // move v1 and v2 forward
44  ++v1_it;
45  ++v2_it;
46  }
47  else if(v1_it->first < v2_it->first)
48  {
49  // move v1 forward
50  v1_it = v1.lower_bound(v2_it->first);
51  // v1_it = (first element >= v2_it.id)
52  }
53  else
54  {
55  // move v2 forward
56  v2_it = v2.lower_bound(v1_it->first);
57  // v2_it = (first element >= v1_it.id)
58  }
59  }
60 
61  // ||v - w||_{L1} = 2 + Sum(|v_i - w_i| - |v_i| - |w_i|)
62  // for all i | v_i != 0 and w_i != 0
63  // (Nister, 2006)
64  // scaled_||v - w||_{L1} = 1 - 0.5 * ||v - w||_{L1}
65  score = -score/2.0;
66 
67  return score; // [0..1]
68 }
69 
70 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
72 
73 double L2Scoring::score(const BowVector &v1, const BowVector &v2) const
74 {
75  BowVector::const_iterator v1_it, v2_it;
76  const BowVector::const_iterator v1_end = v1.end();
77  const BowVector::const_iterator v2_end = v2.end();
78 
79  v1_it = v1.begin();
80  v2_it = v2.begin();
81 
82  double score = 0;
83 
84  while(v1_it != v1_end && v2_it != v2_end)
85  {
86  const WordValue& vi = v1_it->second;
87  const WordValue& wi = v2_it->second;
88 
89  if(v1_it->first == v2_it->first)
90  {
91  score += vi * wi;
92 
93  // move v1 and v2 forward
94  ++v1_it;
95  ++v2_it;
96  }
97  else if(v1_it->first < v2_it->first)
98  {
99  // move v1 forward
100  v1_it = v1.lower_bound(v2_it->first);
101  // v1_it = (first element >= v2_it.id)
102  }
103  else
104  {
105  // move v2 forward
106  v2_it = v2.lower_bound(v1_it->first);
107  // v2_it = (first element >= v1_it.id)
108  }
109  }
110 
111  // ||v - w||_{L2} = sqrt( 2 - 2 * Sum(v_i * w_i) )
112  // for all i | v_i != 0 and w_i != 0 )
113  // (Nister, 2006)
114  if(score >= 1) // rounding errors
115  score = 1.0;
116  else
117  score = 1.0 - sqrt(1.0 - score); // [0..1]
118 
119  return score;
120 }
121 
122 // ---------------------------------------------------------------------------
123 // ---------------------------------------------------------------------------
124 
125 double ChiSquareScoring::score(const BowVector &v1, const BowVector &v2)
126  const
127 {
128  BowVector::const_iterator v1_it, v2_it;
129  const BowVector::const_iterator v1_end = v1.end();
130  const BowVector::const_iterator v2_end = v2.end();
131 
132  v1_it = v1.begin();
133  v2_it = v2.begin();
134 
135  double score = 0;
136 
137  // all the items are taken into account
138 
139  while(v1_it != v1_end && v2_it != v2_end)
140  {
141  const WordValue& vi = v1_it->second;
142  const WordValue& wi = v2_it->second;
143 
144  if(v1_it->first == v2_it->first)
145  {
146  // (v-w)^2/(v+w) - v - w = -4 vw/(v+w)
147  // we move the -4 out
148  if(vi + wi != 0.0) score += vi * wi / (vi + wi);
149 
150  // move v1 and v2 forward
151  ++v1_it;
152  ++v2_it;
153  }
154  else if(v1_it->first < v2_it->first)
155  {
156  // move v1 forward
157  v1_it = v1.lower_bound(v2_it->first);
158  }
159  else
160  {
161  // move v2 forward
162  v2_it = v2.lower_bound(v1_it->first);
163  }
164  }
165 
166  // this takes the -4 into account
167  score = 2. * score; // [0..1]
168 
169  return score;
170 }
171 
172 // ---------------------------------------------------------------------------
173 // ---------------------------------------------------------------------------
174 
175 double KLScoring::score(const BowVector &v1, const BowVector &v2) const
176 {
177  BowVector::const_iterator v1_it, v2_it;
178  const BowVector::const_iterator v1_end = v1.end();
179  const BowVector::const_iterator v2_end = v2.end();
180 
181  v1_it = v1.begin();
182  v2_it = v2.begin();
183 
184  double score = 0;
185 
186  // all the items or v are taken into account
187 
188  while(v1_it != v1_end && v2_it != v2_end)
189  {
190  const WordValue& vi = v1_it->second;
191  const WordValue& wi = v2_it->second;
192 
193  if(v1_it->first == v2_it->first)
194  {
195  if(vi != 0 && wi != 0) score += vi * log(vi/wi);
196 
197  // move v1 and v2 forward
198  ++v1_it;
199  ++v2_it;
200  }
201  else if(v1_it->first < v2_it->first)
202  {
203  // move v1 forward
204  score += vi * (log(vi) - LOG_EPS);
205  ++v1_it;
206  }
207  else
208  {
209  // move v2_it forward, do not add any score
210  v2_it = v2.lower_bound(v1_it->first);
211  // v2_it = (first element >= v1_it.id)
212  }
213  }
214 
215  // sum rest of items of v
216  for(; v1_it != v1_end; ++v1_it)
217  if(v1_it->second != 0)
218  score += v1_it->second * (log(v1_it->second) - LOG_EPS);
219 
220  return score; // cannot be scaled
221 }
222 
223 // ---------------------------------------------------------------------------
224 // ---------------------------------------------------------------------------
225 
226 double BhattacharyyaScoring::score(const BowVector &v1,
227  const BowVector &v2) const
228 {
229  BowVector::const_iterator v1_it, v2_it;
230  const BowVector::const_iterator v1_end = v1.end();
231  const BowVector::const_iterator v2_end = v2.end();
232 
233  v1_it = v1.begin();
234  v2_it = v2.begin();
235 
236  double score = 0;
237 
238  while(v1_it != v1_end && v2_it != v2_end)
239  {
240  const WordValue& vi = v1_it->second;
241  const WordValue& wi = v2_it->second;
242 
243  if(v1_it->first == v2_it->first)
244  {
245  score += sqrt(vi * wi);
246 
247  // move v1 and v2 forward
248  ++v1_it;
249  ++v2_it;
250  }
251  else if(v1_it->first < v2_it->first)
252  {
253  // move v1 forward
254  v1_it = v1.lower_bound(v2_it->first);
255  // v1_it = (first element >= v2_it.id)
256  }
257  else
258  {
259  // move v2 forward
260  v2_it = v2.lower_bound(v1_it->first);
261  // v2_it = (first element >= v1_it.id)
262  }
263  }
264 
265  return score; // already scaled
266 }
267 
268 // ---------------------------------------------------------------------------
269 // ---------------------------------------------------------------------------
270 
271 double DotProductScoring::score(const BowVector &v1,
272  const BowVector &v2) const
273 {
274  BowVector::const_iterator v1_it, v2_it;
275  const BowVector::const_iterator v1_end = v1.end();
276  const BowVector::const_iterator v2_end = v2.end();
277 
278  v1_it = v1.begin();
279  v2_it = v2.begin();
280 
281  double score = 0;
282 
283  while(v1_it != v1_end && v2_it != v2_end)
284  {
285  const WordValue& vi = v1_it->second;
286  const WordValue& wi = v2_it->second;
287 
288  if(v1_it->first == v2_it->first)
289  {
290  score += vi * wi;
291 
292  // move v1 and v2 forward
293  ++v1_it;
294  ++v2_it;
295  }
296  else if(v1_it->first < v2_it->first)
297  {
298  // move v1 forward
299  v1_it = v1.lower_bound(v2_it->first);
300  // v1_it = (first element >= v2_it.id)
301  }
302  else
303  {
304  // move v2 forward
305  v2_it = v2.lower_bound(v1_it->first);
306  // v2_it = (first element >= v1_it.id)
307  }
308  }
309 
310  return score; // cannot scale
311 }
312 
313 // ---------------------------------------------------------------------------
314 // ---------------------------------------------------------------------------
315 
Vector of words to represent images.
Definition: BowVector.h:56
double WordValue
Value of a word.
Definition: BowVector.h:23
static const double LOG_EPS
Log of epsilon.
Definition: ScoringObject.h:39


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05