forked from rmsalinas/DBow3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_voc_step1.cpp
71 lines (55 loc) · 2.42 KB
/
create_voc_step1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Second step,creates the vocabulary from the set of features. It can be slow
#include <iostream>
#include <vector>
// DBoW3
#include "DBoW3.h"
// OpenCV
#include <opencv2/core/core.hpp>
using namespace DBoW3;
using namespace std;
//command line parser
class CmdLineParser{int argc; char **argv; public: CmdLineParser(int _argc,char **_argv):argc(_argc),argv(_argv){} bool operator[] ( string param ) {int idx=-1; for ( int i=0; i<argc && idx==-1; i++ ) if ( string ( argv[i] ) ==param ) idx=i; return ( idx!=-1 ) ; } string operator()(string param,string defvalue="-1"){int idx=-1; for ( int i=0; i<argc && idx==-1; i++ ) if ( string ( argv[i] ) ==param ) idx=i; if ( idx==-1 ) return defvalue; else return ( argv[ idx+1] ); }};
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
vector<cv::Mat> readFeaturesFromFile(string filename){
vector<cv::Mat> features;
//test it is not created
std::ifstream ifile(filename);
if (!ifile.is_open()){cerr<<"could not open input file"<<endl;exit(0);}
uint32_t size;
ifile.read((char*)&size,sizeof(size));
features.resize(size);
for(size_t i=0;i<size;i++){
uint32_t cols,rows,type;
ifile.read( (char*)&cols,sizeof(cols));
ifile.read( (char*)&rows,sizeof(rows));
ifile.read( (char*)&type,sizeof(type));
features[i].create(rows,cols,type);
ifile.read( (char*)features[i].ptr<uchar>(0),features[i].total()*features[i].elemSize());
}
return features;
}
// ----------------------------------------------------------------------------
int main(int argc,char **argv)
{
try{
CmdLineParser cml(argc,argv);
if (cml["-h"] || argc!=3){
cerr<<"Usage: features output_voc.yml[.gz]"<<endl;
return -1;
}
auto features=readFeaturesFromFile(argv[1]);
const int k = 9;
const int L = 3;
const WeightingType weight = TF_IDF;
const ScoringType score = L1_NORM;
DBoW3::Vocabulary voc (k, L, weight, score);
cout << "Creating a small " << k << "^" << L << " vocabulary..." << endl;
voc.create(features);
cerr<<"Saving "<<argv[2]<<endl;
voc.save(argv[2]);
}catch(std::exception &ex){
cerr<<ex.what()<<endl;
}
return 0;
}