libvot  0.1.3
A C++11 multithread library for image retrieval
data_types.h
Go to the documentation of this file.
1 
5 #ifndef DATA_TYPES_H
6 #define DATA_TYPES_H
7 
8 #include <cstdlib>
9 #include <vector>
10 #include <iostream>
11 #include <fstream>
12 #include <cstring>
13 #include <cmath>
14 #include <cassert>
15 
16 #include "global_params.h"
17 #include "openmvg_descriptor.hpp"
18 
19 namespace tw
20 {
22 class SiftData
23 {
24 public:
26  {
27  name_ = 'S' + ('I' << 8) + ('F' << 16) + ('T' << 24);
28  version_ = 'V' + ('5' << 8) + ('.' << 16) + ('0' << 24);
29  npoint_ = 0;
30  nLocDim_ = 5;
31  nDesDim_ = 128;
32  dp_ = nullptr; lp_ = nullptr;
33  }
34 
35  // copy constructor
36  SiftData(const SiftData & obj):
37  name_(obj.name_), version_(obj.version_), npoint_(obj.npoint_),
38  nLocDim_(obj.nLocDim_), nDesDim_(obj.nDesDim_)
39  {
40  //copy feature descriptors
41  dp_ = new DTYPE [obj.npoint_ * nDesDim_];
42  memcpy(dp_, obj.dp_, sizeof(DTYPE) * obj.npoint_ * nDesDim_);
43 
44  //copy location descriptors
45  lp_ = new LTYPE [obj.npoint_ * nLocDim_];
46  memcpy(lp_, obj.lp_, sizeof(LTYPE) * obj.npoint_ * nLocDim_);
47  }
48 
49  // copy assignment operator
50  SiftData & operator=(const SiftData &rhs)
51  {
52  name_ = rhs.name_;
53  version_ = rhs.version_;
54  npoint_ = rhs.npoint_;
55  nLocDim_ = rhs.nLocDim_;
56  nDesDim_ = rhs.nDesDim_;
57 
58  //copy feature descriptors
59  dp_ = new DTYPE [rhs.npoint_ * nDesDim_];
60  memcpy(dp_, rhs.dp_, sizeof(DTYPE) * rhs.npoint_ * nDesDim_);
61 
62  //copy location descriptors
63  lp_ = new LTYPE [rhs.npoint_ * nLocDim_];
64  memcpy(lp_, rhs.lp_, sizeof(LTYPE) * rhs.npoint_ * nLocDim_);
65 
66  return *this;
67  }
68 
69  // destructor
71  {
72  if (nullptr != dp_) delete [] dp_;
73  if (nullptr != lp_) delete [] lp_;
74  }
75 
76  //TODO(tianwei): add serialization library for I/O
82  bool ReadSiftFile(std::string const &szFileName)
83  {
84  name_ = 'S' + ('I' << 8) + ('F' << 16) + ('T' << 24);
85  version_ = 'V' + ('5' << 8) + ('.' << 16) + ('0' << 24);
86  FILE *fd;
87  if((fd = fopen(szFileName.c_str(), "rb")) == nullptr)
88  {
89  std::cerr << "[ReadSiftFile] Can't read sift file " << szFileName << '\n';
90  return false;
91  }
92  int f = fread(&name_, sizeof(int), 1, fd);
93  int g = fread(&version_, sizeof(int), 1, fd);
94  if(f != 1 || g != 1)
95  {
96  std::cerr << "[ReadSiftFile] Reading error\n";
97  return false;
98  }
99 
100  if(name_ == ('S'+ ('I'<<8)+('F'<<16)+('T'<<24)))
101  {
102  int a, b, c;
103  a = fread(&npoint_, sizeof(int), 1, fd);
104  b = fread(&nLocDim_, sizeof(int), 1, fd);
105  c = fread(&nDesDim_, sizeof(int), 1, fd);
106  if(a != 1 || b != 1 || c!= 1)
107  {
108  std::cerr << "[ReadSiftFile] Reading error\n";
109  return false;
110  }
111 
112  lp_ = new LTYPE [npoint_ * nLocDim_]; //restoring location data
113  dp_ = new DTYPE [npoint_ * nDesDim_]; //restoring descriptor data
114  if(npoint_ >= 0 && nLocDim_ > 0 && nDesDim_ == FDIM)
115  {
116  int d, e;
117  d = fread(lp_, sizeof(LTYPE), nLocDim_ * npoint_, fd);
118  e = fread(dp_, sizeof(DTYPE), nDesDim_ * npoint_, fd);
119  if(d != nLocDim_ * npoint_ || e != nDesDim_ * npoint_)
120  {
121  std::cerr << "[ReadSiftFile] Reading error\n";
122  return false;
123  }
124  fclose(fd);
125  }
126  else
127  {
128  fclose(fd);
129  return false;
130  }
131  }
132  else
133  {
134  fclose(fd);
135  return false;
136  }
137  return true;
138  }
139 
146  bool ReadChar2DTYPE(std::string const &szFileName)
147  {
148  name_ = 'S' + ('I' << 8) + ('F' << 16) + ('T' << 24);
149  version_ = 'V' + ('5' << 8) + ('.' << 16) + ('0' << 24);
150  FILE *fd;
151  if((fd = fopen(szFileName.c_str(), "rb")) == nullptr)
152  {
153  std::cout << "[ReadSiftFile] Can't read sift file " << szFileName << '\n';
154  return false;
155  }
156  int f = fread(&name_, sizeof(int), 1, fd);
157  int g = fread(&version_, sizeof(int), 1, fd);
158  if(f != 1 || g != 1)
159  {
160  std::cerr << "[ReadSiftFile] Reading error\n";
161  return false;
162  }
163 
164  if(name_ == ('S'+ ('I'<<8)+('F'<<16)+('T'<<24)))
165  {
166  int a, b, c;
167  a = fread(&npoint_, sizeof(int), 1, fd);
168  b = fread(&nLocDim_, sizeof(int), 1, fd);
169  c = fread(&nDesDim_, sizeof(int), 1, fd);
170  if(a != 1 || b != 1 || c!= 1)
171  {
172  std::cerr << "[ReadSiftFile] Reading error\n";
173  return false;
174  }
175 
176  lp_ = new LTYPE [npoint_ * nLocDim_]; //restoring location data
177  dp_ = new DTYPE [npoint_ * nDesDim_]; //restoring descriptor data
178  unsigned char *temp_dp = new unsigned char [npoint_ * nDesDim_]; // restoring the temporary descriptor data
179  if(npoint_ >= 0 && nLocDim_ > 0 && nDesDim_ == FDIM)
180  {
181  int d, e;
182  d = fread(lp_, sizeof(LTYPE), nLocDim_ * npoint_, fd);
183  e = fread(temp_dp, sizeof(unsigned char), nDesDim_ * npoint_, fd);
184  if(d != nLocDim_ * npoint_ || e != nDesDim_ * npoint_)
185  {
186  std::cerr << "[ReadSiftFile] Reading error\n";
187  return false;
188  }
189  // convert SIFT to RootSIFT
190  for(int i = 0; i < npoint_; i++)
191  {
192  DTYPE magnitude = 0.0;
193  for(int j = 0; j < nDesDim_; j++)
194  {
195  magnitude += (DTYPE) temp_dp[i*nDesDim_+j];
196  }
197  for(int j = 0; j < nDesDim_; j++)
198  {
199  dp_[i*nDesDim_+j] = sqrt((double) temp_dp[i*nDesDim_+j] / magnitude);
200  }
201  }
202  delete [] temp_dp;
203  fclose(fd);
204  }
205  else
206  {
207  fclose(fd);
208  return false;
209  }
210  }
211  else
212  {
213  fclose(fd);
214  return false;
215  }
216  return true;
217  }
218 
224  template <typename T, std::size_t N>
225  bool ReadOpenmvgDesc(std::string const &szFileName)
226  {
227  assert(sizeof(T) == sizeof(DTYPE) && N == FDIM &&
228  "Internal and external feature parameters are not consistent\n");
229  typedef std::vector<openMVG::Descriptor<T, N>> DescriptorsT;
230  typedef typename DescriptorsT::value_type VALUE;
231  DescriptorsT vec_desc;
232 
233  vec_desc.clear();
234  std::ifstream fileIn(szFileName.c_str(), std::ios::in | std::ios::binary);
235  if(!fileIn.is_open())
236  {
237  std::cerr << "[ReadOpenmvgDesc] Fail to load openmvg desc file.\n";
238  return false;
239  }
240  //Read the number of descriptor in the file
241  std::size_t cardDesc = 0;
242  fileIn.read((char*) &cardDesc, sizeof(std::size_t));
243  npoint_ = cardDesc;
244 
245  //TODO(tianwei): restoring location data (since it's useless for now, fill it with 0)
246  lp_ = new LTYPE [npoint_ * nLocDim_];
247  for(size_t i = 0; i < npoint_ * nLocDim_; i++)
248  lp_[i] = 0;
249 
250  //restoring descriptor data
251  dp_ = new DTYPE [npoint_ * nDesDim_];
252  fileIn.read((char*)dp_, VALUE::static_size*sizeof(typename VALUE::bin_type)*npoint_);
253  bool bOk = !fileIn.bad();
254  fileIn.close();
255 
256  return bOk;
257  }
258 
259  bool SaveSiftFile(std::string const &szFileName)
260  {
261  FILE *fd;
262  if((fd = fopen(szFileName.c_str(), "wb")) == nullptr)
263  {
264  std::cerr << "[SaveSiftFile] Can't open file " << szFileName << " for saving\n";
265  return false;
266  }
267  int f = fwrite(&name_, sizeof(int), 1, fd);
268  int g = fwrite(&version_, sizeof(int), 1, fd);
269  if(f != 1 || g != 1)
270  {
271  std::cerr << "[SaveSiftFile] Writing error\n";
272  return false;
273  }
274 
275  if(name_ == ('S'+ ('I'<<8)+('F'<<16)+('T'<<24)))
276  {
277  int a, b, c;
278  a = fwrite(&npoint_, sizeof(int), 1, fd);
279  b = fwrite(&nLocDim_, sizeof(int), 1, fd);
280  c = fwrite(&nDesDim_, sizeof(int), 1, fd);
281  if(a != 1 || b != 1 || c!= 1)
282  {
283  std::cerr << "[SaveSiftFile] Write error\n";
284  return false;
285  }
286 
287  if(npoint_ >= 0 && nLocDim_ > 0 && nDesDim_ == FDIM)
288  {
289  int d, e;
290  d = fwrite((void*) lp_, sizeof(LTYPE), nLocDim_ * npoint_, fd);
291  e = fwrite((void*) dp_, sizeof(DTYPE), nDesDim_ * npoint_, fd);
292  if(d != nLocDim_ * npoint_ || e != nDesDim_ * npoint_)
293  {
294  std::cerr << "[SaveSiftFile] Writing error\n";
295  return false;
296  }
297  fclose(fd);
298  }
299  else
300  {
301  fclose(fd);
302  return false;
303  }
304  }
305  else
306  {
307  fclose(fd);
308  return false;
309  }
310  return true;
311  }
312 
313  const int getFeatureNum() const {return npoint_;}
314  void setFeatureNum(int feat_num) {npoint_ = feat_num;}
315  const int getLocDim() const {return nLocDim_;}
316  void setLocDim(int loc_dim) {nLocDim_ = loc_dim;}
317  const int getDesDim() const {return nDesDim_;}
318  void setDesDim(int des_dim) {nDesDim_ = des_dim;}
319  const int getVersion() const {return version_;}
320  void setVersion(int version) {version_ = version;}
321 
322  void clear()
323  {
324  if (nullptr != dp_) delete [] dp_;
325  if (nullptr != lp_) delete [] lp_;
326  dp_ = nullptr; lp_ = nullptr;
327  }
328 
329  // To manipulate data in SiftData
330  LTYPE* &getLocPointer() {return lp_;}
331  DTYPE* &getDesPointer() {return dp_;}
332 
333 private:
334  int name_;
335  int version_;
336  int npoint_;
337  int nLocDim_;
338  int nDesDim_;
339  DTYPE *dp_;
340  LTYPE *lp_;
341 };
342 
343 } // end of namespace tw
344 
345 #endif
DTYPE *& getDesPointer()
Definition: data_types.h:331
#define LTYPE
Definition: global_params.h:42
#define DTYPE
Definition: global_params.h:41
void setLocDim(int loc_dim)
Definition: data_types.h:316
SiftData & operator=(const SiftData &rhs)
Definition: data_types.h:50
#define FDIM
Definition: global_params.h:43
void setDesDim(int des_dim)
Definition: data_types.h:318
const int getLocDim() const
Definition: data_types.h:315
void setVersion(int version)
Definition: data_types.h:320
bool ReadOpenmvgDesc(std::string const &szFileName)
ReadOpenmvgDesc: read desc file of openmvg.
Definition: data_types.h:225
void clear()
Definition: data_types.h:322
bool ReadSiftFile(std::string const &szFileName)
ReadSiftFile: read sift file, this function is compatible with the sfm version of sift file...
Definition: data_types.h:82
~SiftData()
Definition: data_types.h:70
LTYPE *& getLocPointer()
Definition: data_types.h:330
Sift data structure used in libvot.
Definition: data_types.h:22
const int getFeatureNum() const
Definition: data_types.h:313
global parameters and utility functions
namespace tw is mostly related to some utility functions developed by the repository owner...
Definition: opencv_libvot_api.cpp:12
SiftData()
Definition: data_types.h:25
void setFeatureNum(int feat_num)
Definition: data_types.h:314
bool SaveSiftFile(std::string const &szFileName)
Definition: data_types.h:259
const int getDesDim() const
Definition: data_types.h:317
SiftData(const SiftData &obj)
Definition: data_types.h:36
const int getVersion() const
Definition: data_types.h:319
bool ReadChar2DTYPE(std::string const &szFileName)
ConvertChar2Float: Read a char sift file and convert it to DTYPE(float/double) sift. The descriptors are L1 normalized and then square-rooted (RootSift)
Definition: data_types.h:146