PICCANTE  0.4
The hottest HDR imaging library!
brief_descriptor.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_BRIEF_DESCRIPTOR_HPP
19 #define PIC_FEATURES_MATCHING_BRIEF_DESCRIPTOR_HPP
20 
21 #include <random>
22 #include <chrono>
23 #include <vector>
24 
25 #include "../base.hpp"
26 #include "../util/std_util.hpp"
27 #include "../util/math.hpp"
28 #include "../image.hpp"
29 
30 #ifndef PIC_DISABLE_EIGEN
31 
32 #ifndef PIC_EIGEN_NOT_BUNDLED
33  #include "../externals/Eigen/Dense"
34 #else
35  #include <Eigen/Dense>
36 #endif
37 
38 #endif
39 
40 namespace pic {
41 
42 #ifndef PIC_DISABLE_EIGEN
43 
48 {
49 protected:
50  int S;
51  unsigned int n;
53  std::mt19937 *m;
54 
55  //samples coordinates
56  int *x, *y;
57 
62  void generateSample(int *sample)
63  {
64  float theta = C_PI_2 * getRandom((*m)());
65 
66  float u = getRandom((*m)());
67  float r = sqrtf(MAX(-logf(u) * sigma_sq_2, 0.0f));
68 
69  sample[0] = int(r * cosf(theta));
70  sample[1] = int(r * sinf(theta));
71  }
72 
77  void generateSamples(unsigned int n)
78  {
79  this->n = n;
80  unsigned int n2 = n * 2;
81 
82  x = new int [n2];
83  y = new int [n2];
84 
85  for(unsigned int i = 0; i < n2; i += 2) {
86  generateSample(&x[i]);
87  generateSample(&y[i]);
88  }
89  }
90 
96  static unsigned int countZeros(unsigned int x)
97  {
98  unsigned int n = sizeof(unsigned int) * 8;
99 
100  unsigned int ret = 0;
101 
102  for(unsigned int i = 0; i < n; i++) {
103  if((x & (1 << i)) == 0) {
104  ret++;
105  }
106  }
107 
108  return ret;
109  }
110 
119  uint* getAux(Image *img, int x0, int y0, int *x, int *y, uint *desc = NULL)
120  {
121  unsigned int bits = sizeof(unsigned int) * 8;
122  unsigned int subBlock = n / bits;
123 
124  if(desc == NULL) {
125  desc = new unsigned int[subBlock];
126  }
127 
128  int c = 0;
129 
130  for(unsigned int i = 0; i < subBlock; i++) {
131  unsigned int value = 0;
132 
133  for(unsigned int j = 0; j < bits; j++) {
134  int cShifted = c * 2;
135 
136  float *p_x_val = (*img)(x0 + x[cShifted], y0 + x[cShifted + 1]);
137  float *p_y_val = (*img)(x0 + y[cShifted], y0 + y[cShifted + 1]);
138 
139  float p_x = 0.0f;
140  float p_y = 0.0f;
141 
142  for(int k = 0; k < img->channels; k++) {
143  p_x += p_x_val[k];
144  p_y += p_y_val[k];
145  }
146 
147  unsigned int ret = (p_x < p_y) ? 1 : 0;
148 
149  value += (ret << j);
150 
151  c++;
152  }
153 
154  desc[i] = value;
155  }
156 
157  return desc;
158  }
159 
160 public:
161 
167  BRIEFDescriptor(int S = 32, int n = 256, int seed = 1)
168  {
169  if(seed >= 0) {
170  m = new std::mt19937(seed);
171  } else {
172  auto seed_time = std::chrono::system_clock::now().time_since_epoch().count();
173  m = new std::mt19937(int (seed_time));
174  }
175 
176  this->S = S;
177  this->sigma_sq = float(S * S) / 25.0f;
178  this->sigma_sq_2 = 2.0f * this->sigma_sq;
179 
180  generateSamples(n);
181  }
182 
184  {
185  release();
186  }
187 
191  void release()
192  {
193  m = delete_s(m);
194  x = delete_s(x);
195  y = delete_s(y);
196  }
197 
206  uint *get(Image *img, int x0, int y0, uint *desc = NULL)
207  {
208  if(img == NULL) {
209  return NULL;
210  }
211 
212  if(!img->checkCoordinates(x0, y0)) {
213  return NULL;
214  }
215 
216  return getAux(img, x0, y0, x, y, desc);
217  }
218 
219  #ifndef PIC_DISABLE_EIGEN
220 
226  void getAll(Image *img,
227  std::vector< Eigen::Vector2f > &corners,
228  std::vector< uint* > &descs)
229  {
230  descs.clear();
231 
232  for(unsigned int i = 0; i < corners.size(); i++) {
233  int x0 = int(corners[i][0]);
234  int y0 = int(corners[i][1]);
235  descs.push_back(get(img, x0, y0, NULL));
236  }
237  }
238  #endif
239 
245  return n / (sizeof(unsigned int) * 8);
246  }
247 
255  static uint match(uint *fv0, uint *fv1, uint nfv)
256  {
257  if((fv0 == NULL) || (fv1 == NULL)) {
258  return 0;
259  }
260 
261  uint ret = 0;
262  for(uint i = 0; i < nfv; i++) {
263  ret += countZeros(fv0[i] ^ fv1[i]);
264  }
265 
266  return ret;
267  }
268 };
269 
270 #endif
271 
272 } // end namespace pic
273 
274 #endif /* PIC_FEATURES_MATCHING_BRIEF_DESCRIPTOR_HPP */
275 
unsigned int uint
Definition: base.hpp:23
BRIEFDescriptor(int S=32, int n=256, int seed=1)
BRIEFDescriptor.
Definition: brief_descriptor.hpp:167
int channels
Definition: image.hpp:80
T * delete_s(T *data)
delete_s
Definition: std_util.hpp:123
int S
Definition: brief_descriptor.hpp:50
std::mt19937 * m
Definition: brief_descriptor.hpp:53
The BRIEFDescriptor class.
Definition: brief_descriptor.hpp:47
float sigma_sq
Definition: brief_descriptor.hpp:52
const float C_PI_2
Definition: math.hpp:52
void generateSamples(unsigned int n)
generateSamples
Definition: brief_descriptor.hpp:77
void release()
Release deallocates memory.
Definition: brief_descriptor.hpp:191
int * y
Definition: brief_descriptor.hpp:56
uint * getAux(Image *img, int x0, int y0, int *x, int *y, uint *desc=NULL)
getAux computes a descriptor at position (x0,y0) with size n.
Definition: brief_descriptor.hpp:119
~BRIEFDescriptor()
Definition: brief_descriptor.hpp:183
int * x
Definition: brief_descriptor.hpp:56
static uint match(uint *fv0, uint *fv1, uint nfv)
match matches two descriptors. Note: Higher scores means better matching.
Definition: brief_descriptor.hpp:255
The Image class stores an image as buffer of float.
Definition: image.hpp:60
void getAll(Image *img, std::vector< Eigen::Vector2f > &corners, std::vector< uint * > &descs)
getAll
Definition: brief_descriptor.hpp:226
float sigma_sq_2
Definition: brief_descriptor.hpp:52
Definition: bilateral_separation.hpp:25
unsigned int n
Definition: brief_descriptor.hpp:51
PIC_INLINE float getRandom(unsigned int n)
Random returns a number in [0, 2^32 - 1] to a float in [0, 1].
Definition: math.hpp:138
#define MAX(a, b)
Definition: math.hpp:73
int getDescriptorSize()
getDescriptorSize returns the descriptor size.
Definition: brief_descriptor.hpp:244
static unsigned int countZeros(unsigned int x)
countZeros
Definition: brief_descriptor.hpp:96
void generateSample(int *sample)
generateSample
Definition: brief_descriptor.hpp:62