-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.cpp
More file actions
241 lines (184 loc) · 6.49 KB
/
main.cpp
File metadata and controls
241 lines (184 loc) · 6.49 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/flann.hpp>
#include <sys/sysinfo.h>
#include <unistd.h>
#include "DBoW3.h"
#include "utils.h"
#include "Database.h"
#include "Vocabulary.h"
#include "Searcher.h"
#include <string>
extern "C" {
#include <vl/kmeans.h>
}
#include "siftDetector.h"
using namespace std;
using namespace cv;
void cpu(int i){
cpu_set_t mask; // cpu_set_t 代表一个cpu集合
CPU_ZERO(&mask);
CPU_SET(i,&mask);
if(pthread_setaffinity_np(pthread_self(),sizeof(mask),&mask) < 0){
cout << "Set thread affinity failed" << endl;
}
cpu_set_t get;
int num = sysconf(_SC_NPROCESSORS_CONF);
cout << "System has " << num << " processors" << endl;
CPU_ZERO(&mask); // Clear a cpu set
CPU_SET(i,&mask); // Put cpu_myid to mask set
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
printf("warning: could not set CPU affinity, continuing...\n");
}
while (true)
{
CPU_ZERO(&get);
if (sched_getaffinity(0, sizeof(get), &get) == -1)
{
printf("warning: cound not get cpu affinity, continuing...\n");
}
for (int i = 0; i < num; i++)
{
if (CPU_ISSET(i, &get))
{
printf("this process %d is running processor : %d\n",getpid(), i);
}
}
}
}
void sift_test(){
const string file = "../1.jpg";
Mat img = imread(file);
SiftDetector sift_detector;
vector<VlSiftKeypoint> kpts;
vector<vector<float>> descriptors;
sift_detector.detect_and_compute(img,kpts,descriptors);
for(int i = 0; i < kpts.size(); i ++) {
circle(img,Point(kpts[i].x,kpts[i].y),kpts[i].sigma,Scalar(0,255,0));
}
vector<KeyPoint> oc_kpts;
Mat oc_des;
Ptr<xfeatures2d::SIFT> sift = xfeatures2d::SIFT::create();
sift->detectAndCompute(img,noArray(),oc_kpts,oc_des);
for(int i = 0; i < oc_kpts.size(); i ++) {
circle(img,oc_kpts[i].pt,oc_kpts[i].size,Scalar(255,0,0));
}
cout << "vl_sift:" << descriptors.size() << endl;
cout << "opencv_sift-descriptors:" << oc_des.size() << endl;
namedWindow("SIFT");
imshow("SIFT",img);
waitKey();
}
void extract_features(vector<string> image_file_list,vector<Mat> &features){
int index = 1;
int count = 0;
for(const string &str : image_file_list){
auto img = imread(str,IMREAD_GRAYSCALE);
if(img.empty()){
cerr << "Open image #" << str << " features failed" << endl;
continue;
}
cout << "Extract feature from #" << index << "st image #" << str << endl;
auto sift = xfeatures2d::SIFT::create(0,3,0.2,10);
vector<KeyPoint> kpts;
Mat des;
sift->detectAndCompute(img,noArray(),kpts,des);
features.emplace_back(des);
count += des.rows;
index ++ ;
}
cout << "Extract #" << index << "# images features done!" << "Count of features:#" << count << endl;
}
void vocabulary(const vector<Mat> &features){
//Branching factor and depth levels
const int K = 9;
const int L = 3;
const DBoW3::WeightingType weight = DBoW3::TF_IDF;
const DBoW3::ScoringType score = DBoW3::L2_NORM;
DBoW3::Vocabulary voc(K,L,weight,score);
cout << "Creating a small " << K << "^" << L << " vocabulary..." << endl;
voc.create(features);
cout << "...done!" << endl;
cout << "Vocabulary infomation: " << endl << voc << endl << endl;
/*cout << "Matching images against themselves (0 low,1 hight): " << endl;
DBoW3::BowVector v1,v2;
int i = 0, j = 0;
for(const Mat &m : features) {
voc.transform(m,v1);
j = 0;
for(const Mat &m1 : features){
voc.transform(m1,v2);
double score = voc.score(v1,v2);
cout << "Image " << i << " vs Image " << j << ": " << score << endl;
j ++;
}
i ++;
}*/
// save the vocabulary to disk
cout << endl << "Saving vocabulary..." << endl;
voc.save("small_voc.yml.gz");
cout << "Done" << endl;
}
void database(const vector<Mat> &features,vector<string> &image_file_list){
// load the vocabulary from disk
DBoW3::Vocabulary voc("small_voc.yml.gz");
DBoW3::Database db(voc, false, 0); // false = do not use direct index
// (so ignore the last param)
// The direct index is useful if we want to retrieve the features that
// belong to some vocabulary node.
// db creates a copy of the vocabulary, we may get rid of "voc" now
// add images to the database
for(size_t i = 0; i < features.size(); i++)
db.add(features[i]);
cout << "... done!" << endl;
cout << "Database information: " << endl << db << endl;
// and query the database
cout << "Querying the database: " << endl;
DBoW3::QueryResults ret;
/*for(size_t i = 0; i < features.size(); i++)
{
db.query(features[i], ret, 4);
// ret[0] is always the same image in this case, because we added it to the
// database. ret[1] is the second best match.
cout << "Searching for Image " << i << ". " << ret << endl;
}*/
db.query(features[0],ret,4);
cout << "Searching for Image " << ret << endl;
cout << endl;
// we can save the database. The created file includes the vocabulary
// and the entries added
cout << "Saving database..." << endl;
db.save("small_db.yml.gz");
cout << "... done!" << endl;
}
int main(int argc ,char* argv[])
{
const string path = "/home/test/projects/imageRetrievalService/build";
const string identifier = "test-6k-nopca-256";
Vocabulary voc;
stringstream ss;
ss << path << "/voc-" << identifier << ".yml";
cout << "Load vocabulary from " << ss.str() << endl;
voc.load(ss.str());
cout << "Load vocabulary successful." << endl;
auto detector = make_shared<RootSiftDetector>(5,0.2,10);
auto db = make_shared<Database>(detector);
cout << "Load database from " << path << "/db-" << identifier << ".yml" << endl;
db->load1(path,identifier);
db->setVocabulary(voc);
cout << "Load database successful." << endl;
const string image_folder = "/home/test/images/sync_down_1";
const string str = "/home/test/test_images/6.jpg";
Mat img = imread(str);
vector<string> res;
vector<float> dists;
int k = 50;
Searcher s;
s.init(10);
s.setDatabase(db);
string md5;
float score;
return 0;
}