-
Notifications
You must be signed in to change notification settings - Fork 142
/
dbow2fbow.cpp
67 lines (61 loc) · 2.52 KB
/
dbow2fbow.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
#include "dbow2/TemplatedVocabulary.h"
#include "dbow2/FORB.h"
#include <opencv2/core/core.hpp>
#include "fbow.h"
#include <set>
using namespace std;
using ORBVocabulary=DBoW2::TemplatedVocabulary<DBoW2::FORB::TDescriptor, DBoW2::FORB> ;
namespace fbow{
class VocabularyCreator{
public:
struct ninfo{
ninfo(){}
ninfo(uint32_t Block,ORBVocabulary::Node *Node):block(Block),node(Node){}
int64_t block=-1;
ORBVocabulary::Node *node=0;
};
static void convert(ORBVocabulary &voc,fbow::Vocabulary &out_voc){
uint32_t nonLeafNodes=0;
std::map<uint32_t,ninfo> nodeid_info;
for(int i=0;i<voc.m_nodes.size();i++){
auto &node=voc.m_nodes[i];
if(!node.isLeaf()) nodeid_info.insert(std::make_pair(node.id,ninfo(nonLeafNodes++,&node)));
else nodeid_info.insert(std::make_pair(node.id,ninfo(-1,&node)));
}
out_voc.setParams(8,voc.m_k,CV_8UC1,32,nonLeafNodes,"orb");
cerr<<"creating size="<<out_voc._params._total_size/(1024*1024)<<"Mb "<<out_voc._params._total_size<<" bytes"<<endl;
for(int i=0;i<voc.m_nodes.size();i++){
ORBVocabulary::Node &node= voc.m_nodes[i];
if(!node.isLeaf()) {
auto &n_info=nodeid_info[node.id];
fbow::Vocabulary::Block binfo=out_voc.getBlock(n_info.block);
binfo.setN(node.children.size());
binfo.setParentId(node.id);
bool areAllChildrenLeaf=true;
std::sort(node.children.begin(),node.children.end());
for(int c=0;c<node.children.size();c++){
auto &child_info=nodeid_info[node.children[c]];
binfo.setFeature(c, child_info.node->descriptor);
if (child_info.node->isLeaf())
binfo.getBlockNodeInfo(c)->setLeaf(child_info.node->word_id,child_info.node->weight);
else {
areAllChildrenLeaf=false;
binfo.getBlockNodeInfo(c)->setNonLeaf(child_info.block);
}
}
binfo.setLeaf(areAllChildrenLeaf);
}
}
}
};
}
int main(int argc,char **argv){
if (argc!=3){cerr<<"Usage voc.txt out.fbow"<<endl;return -1;}
ORBVocabulary voc;
cout<<"loading dbow2 voc"<<endl;
voc.loadFromTextFile(argv[1]);
cout<<"done"<<endl;
fbow::Vocabulary fvoc;
fbow::VocabularyCreator::convert(voc,fvoc);
fvoc.saveToFile(argv[2]);
}