-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_simple_voc.cpp
114 lines (89 loc) · 2.75 KB
/
create_simple_voc.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*************************************************************************
> File Name: create_simple_voc.cpp
> Author:wangzheqie
> Mail:[email protected]
> Created Time: Fri 21 Jul 2017 10:13:52 AM UTC
************************************************************************/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "Thirdparty/DBoW2/DBoW2/TemplatedVocabulary.h"
#include "Thirdparty/DBoW2/DBoW2/BowVector.h"
#include "Thirdparty/DBoW2/DBoW2/FeatureVector.h"
#include "Thirdparty/DBoW2/DBoW2/DBoW2.h"
using namespace DBoW2;
using namespace cv;
using namespace std;
void changeStructure(const vector<int> &plain, vector<vector<int>> &out,
int size);
int main() {
OrbFeatureDetector detector;
vector<KeyPoint> kps;
OrbDescriptorExtractor extractor;
Mat dspts;
typedef TemplatedVocabulary<FORB::TDescriptor, FORB> ORBVocabulary;
//!!Note: 9 and 3 is the parameters you may want to change
ORBVocabulary orb(9, 3, TF_IDF, L1_NORM);
int NIMAGES;
typedef cv::Mat mat;
vector<vector<mat>> features;
//load images
//data.txt contain the images' name, left and right images have the same name
//and each name lies on a single line in data.txt
string imPathLeft = "images/left";
string imPathRight = "images/right";
string imagefile = "images/data.txt";
vector<string> imLeft;
vector<string> imRight;
vector<double> timestamp;
ifstream file;
file.open(imagefile.c_str());
double t;
string name;
stringstream ss;
while (!file.eof()) {
string s;
getline(file, s);
if (!s.empty()) {
ss << s;
imLeft.push_back(imPathLeft + "/" + ss.str() + ".png");
imRight.push_back(imPathRight + "/" + ss.str() + "png");
ss >> t;
timestamp.push_back(t / 1e9);
}
}
NIMAGES = imLeft.size();
cout << "images now is :" << NIMAGES << endl;
vector<float> des;
features.clear();
features.reserve(NIMAGES);
for (int n = 0; n < NIMAGES; n++) {
cv::Mat im = imread(imLeft.at(n));
detector.detect(im, kps);
extractor.compute(im, kps, dspts);
for (int i = 0; i < kps.size(); i++) {
cv::Mat single_des = dspts.row(i);
// cout << single_des << endl;
vector<vector<mat>>::const_iterator it1;
vector<mat>::const_iterator it2;
//must pre-initialize, to allocate memery
features.push_back(vector<mat>());
vector<mat> tmp;
tmp.push_back(single_des);
features.push_back(tmp);
}
cout << "features size now is: " << features.size() << endl;
}
cout << "creating the vocabulary..." << endl;
orb.create(features);
// cout << orb << endl;
cout << "saving the vocabulary to text file..." << endl;
orb.saveToTextFile("voc.txt");
return 0;
}