PICCANTE  0.4
The hottest HDR imaging library!
hash_table_lsh.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_FEATURES_MATCHING_HASH_TABLE_LSH_HPP
19 #define PIC_FEATURES_MATCHING_HASH_TABLE_LSH_HPP
20 
21 #include <vector>
22 #include <math.h>
23 #include <set>
24 
25 #include "../features_matching/brief_descriptor.hpp"
26 
27 namespace pic {
28 
29 #ifndef PIC_DISABLE_EIGEN
30 
35 {
36 public:
37  unsigned int *g_f;
38 
39  std::vector< unsigned int *> *descs;
40  std::vector< unsigned int > *table;
41  unsigned int nTable;
42  unsigned int hash_size, desc_size, size_ui;
43 
44  HashTableLSH(unsigned int hash_size, unsigned int *g_f, std::vector< unsigned int *> *descs, unsigned int desc_size)
45  {
46  if(hash_size == 0) {
47  hash_size = 8;
48  }
49 
50  this->hash_size = hash_size;
51 
52  nTable = 1 << hash_size;
53  table = new std::vector< unsigned int >[nTable];
54 
55  //hash function
56  this->g_f = g_f;
57 
58  //insert descriptors
59  this->descs = descs;
60  this->desc_size = desc_size;
61  size_ui = sizeof(unsigned int) * 8;
62 
63  for(unsigned int i = 0; i < descs->size(); i++) {
64  unsigned int address = getAddress(descs->at(i));
65  table[address].push_back(i);
66  }
67  }
68 
74  unsigned int getAddress(unsigned int *desc)
75  {
76  unsigned int address = 0;
77  for(unsigned int i=0; i<hash_size; i++) {
78  unsigned int pos = g_f[i];
79 
80  unsigned int block = pos / size_ui;
81  unsigned int pos_block = pos % size_ui;
82 
83  unsigned int bit = (desc[block] >> pos_block) & 0x1;
84 
85  if(bit == 1) {
86  address += (1 << i);
87  }
88  }
89 
90  return address;
91  }
92 
100  void getNearest(unsigned int * desc, int &matched_j, unsigned int &dist_1, unsigned int &dist_2)
101  {
102  unsigned int address = getAddress(desc);
103 
104  for(unsigned int i=0; i<table[address].size(); i++) {
105  unsigned int j = table[address].at(i);
106  unsigned int dist = BRIEFDescriptor::match(desc, descs->at(j), desc_size);
107 
108  if(dist > dist_1) {
109  dist_2 = dist_1;
110  dist_1 = dist;
111  matched_j = j;
112  } else {
113  if(dist > dist_2) {
114  dist_2 = dist;
115  }
116  }
117  }
118  }
119 };
120 
121 #endif
122 
123 } // end namespace pic
124 
125 #endif /* PIC_FEATURES_MATCHING_HASH_TABLE_LSH_HPP */
126 
std::vector< unsigned int * > * descs
Definition: hash_table_lsh.hpp:39
The Hash class.
Definition: hash_table_lsh.hpp:34
unsigned int nTable
Definition: hash_table_lsh.hpp:41
unsigned int size_ui
Definition: hash_table_lsh.hpp:42
void getNearest(unsigned int *desc, int &matched_j, unsigned int &dist_1, unsigned int &dist_2)
getNearest
Definition: hash_table_lsh.hpp:100
unsigned int getAddress(unsigned int *desc)
getAddress
Definition: hash_table_lsh.hpp:74
unsigned int desc_size
Definition: hash_table_lsh.hpp:42
HashTableLSH(unsigned int hash_size, unsigned int *g_f, std::vector< unsigned int *> *descs, unsigned int desc_size)
Definition: hash_table_lsh.hpp:44
static uint match(uint *fv0, uint *fv1, uint nfv)
match matches two descriptors. Note: Higher scores means better matching.
Definition: brief_descriptor.hpp:255
unsigned int * g_f
Definition: hash_table_lsh.hpp:37
Definition: bilateral_separation.hpp:25
unsigned int hash_size
Definition: hash_table_lsh.hpp:42
std::vector< unsigned int > * table
Definition: hash_table_lsh.hpp:40