-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate_sketch.cpp
339 lines (241 loc) · 8.55 KB
/
create_sketch.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
hash values restored using long long int
only 1 sketch file per image
neighborhood = 3*4*SIGMA
we keep only unique VWs in each bundle
keypoints are associated to VW using fastann library
compile: g++ -O2 `pkg-config --cflags opencv` -o create_sketch create_sketch.cpp `pkg-config --libs opencv` -lfastann
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
//#include "bundle.h"
#include "sketch.h"
#include <fastann/fastann.hpp>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include<sys/stat.h>
#include <functional>
using namespace std;
using namespace cv;
// Dictionary in txt format for simplicity
const char *dictionary_path = "../Dictionary1M_iter10.txt";
// Folder where the key files are
const char *dir = "../data/FlickrLogos-v2/keys_train/";
// Folder to store sketches of key files
string sketch_folder = string("../data/FlickrLogos-v2/sketches_train/");
# define BUNDLE_SIZE 4 // min of 4 vw in a bundle - paper mentions 3
# define NEIGHBORHOOD_RATIO 2 //ratio of radius of central feature and cutoff radius for bundling - paper mentions to use 1
# define DICTIONARY_SIZE 1000003
# define hasha1 234511
# define hasha2 462901
# define hasha3 678383
static inline uint64_t rdtsc()
{
#ifdef __i386__
uint32_t a, d;
#elif defined __x86_64__
uint64_t a, d;
#endif
asm volatile ("rdtsc" : "=a" (a), "=d" (d));
return ((uint64_t)a | (((uint64_t)d)<<32));
}
template<class Float>
vector<unsigned>
test_kdtree(unsigned D, double min_accuracy, Float* pnts, unsigned pnts_size, Float*qus, unsigned qus_size, fastann::nn_obj<Float>* nnobj_kdt )
{
// Float* pnts = fastann::gen_unit_random<Float>(N, D, 42);
// Float* qus = fastann::gen_unit_random<Float>(N, D, 43);
std::vector<Float> mins_kdt(qus_size);
std::vector<unsigned> argmins_kdt(qus_size);
// fastann::nn_obj<Float>* nnobj_exact = fastann::nn_obj_build_exact(pnts, pnts_size, D);
// fastann::nn_obj<Float>* nnobj_kdt = fastann::nn_obj_build_kdtree(pnts, pnts_size, D, 8, 768);
// nnobj_exact->search_nn(qus, qus_size, &argmins_exact[0], &mins_exact[0]);
nnobj_kdt->search_nn(qus, qus_size, &argmins_kdt[0], &mins_kdt[0]);
return argmins_kdt;
}
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
/* creates single sketch from a bundle
b is a bundle which is vector of VWs
*/
sketch* create_sketch(vector<unsigned>& b, int prime, int hasha)
{
int sketch_b=0;
int minhash=DICTIONARY_SIZE, k=0;
unsigned long long int j=0;
for(int i=1; i<b.size(); i++)
{
j = hasha*b[i];
k = j%prime;
if(k < minhash) { minhash=k; sketch_b=b[i]; }
}
// cout<< sketch_a<<"\t"<<sketch_b<<endl;
return new sketch(b[0], sketch_b);
}
/*
Reads rootSift features from key file
fills array of descriptor and vector of keypoints
size of keypoint is multiplied by factor of 4*3 as read in paper
*/
void readKeyDesFileFloat_rootsift(char *filename, float* desc, vector<KeyPoint>::iterator it,int numkeys){
ifstream in;
in.open(filename);
float x=0,y=0,sigma=0,angle=0,a=0;
int oc=-8;
unsigned char c;
int count=0;
in>>x; // ignore num_columns from first line of key file
in>>y; // ignore num_rows from first line of key file
while(!in.eof() && count<numkeys){
in>>x;
in>>y;
in>>sigma;
in>>angle;
in>>oc;
it->pt.x = x;
it->pt.y = y;
it->size = 4*3.0*sigma;
it->angle = angle*180/3.14;
it->octave = oc;
it++;
for(int i=0;i<128;i++) {in>>a; *(desc+count*128+i)=a; }
if(!in){
in.close();
}
count++;
}
in.close();
}
void compute_bow(char *filename, string dir_name, string filename_cut, float* dictionary, int file_num, string sketch_folder, fastann::nn_obj<float>* nnobj_kdt)
{
cout<<"computing BOW -> Processing file "<<file_num<<" : "<<filename<<endl;
ifstream inFile(filename);
// cout<<filename<<endl;
int no_keypoints = count(istreambuf_iterator<char>(inFile), istreambuf_iterator<char>(), '\n')-1;
vector<KeyPoint> keypoints(no_keypoints);
vector<KeyPoint> :: iterator iter = keypoints.begin();
float* desc_fl = new float[no_keypoints*128];
readKeyDesFileFloat_rootsift(filename, desc_fl, iter, no_keypoints);
vector<unsigned> keypoints_vw = test_kdtree<float>(128, 36, dictionary, DICTIONARY_SIZE, desc_fl, no_keypoints, nnobj_kdt);
vector<sketch> sketch_coll1, sketch_coll2, sketch_coll3;
for(int i=0; i<no_keypoints; i++)
{
float center_x = keypoints[i].pt.x;
float center_y = keypoints[i].pt.y;
float center_size = keypoints[i].size;
float spatial_dist = 0.0;
vector<unsigned> b;
b.push_back(keypoints_vw[i]);
for(int j=0; j<no_keypoints; j++)
{
if(j != i && keypoints[j].size>0.5*center_size && keypoints[j].size<2*center_size)
{
spatial_dist = sqrt((center_x-keypoints[j].pt.x)*(center_x-keypoints[j].pt.x) + (center_y-keypoints[j].pt.y)*(center_y-keypoints[j].pt.y));
if(NEIGHBORHOOD_RATIO*spatial_dist<center_size)
{
bool flag_vw=true;
/////////////// UNCOMMENT TO KEEP ONLY UNIQUE VISUAL WORDS IN A BUNDLE ////////////////
for(int v=0;v<b.size();v++)
if(b[v] == keypoints_vw[j])
flag_vw=false;
if(flag_vw)
b.push_back(keypoints_vw[j]);
}
}
}
if(b.size()>=BUNDLE_SIZE)
{
sketch_coll1.push_back(*create_sketch(b, DICTIONARY_SIZE, hasha1));
sketch_coll2.push_back(*create_sketch(b, DICTIONARY_SIZE, hasha2));
sketch_coll3.push_back(*create_sketch(b, DICTIONARY_SIZE, hasha3));
}
}
vector<string> dir_split = split(dir_name, '/');
vector<string> filename_split = split(filename_cut, '.');
// cout<<dir_split[dir_split.size()-1]<<endl;
mkdir((sketch_folder+dir_split[dir_split.size()-1]).c_str(),S_IRWXU);
string sketch_path1 = sketch_folder+dir_split[dir_split.size()-1]+"/"+filename_split[0]+".txt";
// cout<<sketch_path<<endl;
ofstream myfile1;
myfile1.open (sketch_path1.c_str());
for(int i=0;i<sketch_coll1.size();i++)
{
myfile1 << sketch_coll1[i].a<<"\t"<<sketch_coll1[i].b << "\t"<<sketch_coll2[i].b << "\t"<<sketch_coll3[i].b;
myfile1<<"\n";
}
myfile1.close();
}
void searchDir_create_bundles(const char * dir, float* dictionary, int count, string sketch_folder, fastann::nn_obj<float>* nnobj_kdt)
{
DIR *d;
struct dirent * entry;
struct stat buf;
d = opendir(dir);
entry = readdir(d);
while(entry != NULL)
{
if(0 != strcmp( ".", entry->d_name) && //Skip those directories
0 != strcmp( "..", entry->d_name) )
{
string t_name = string(dir) + "/" + string(entry->d_name);
char *name = new char[t_name.length() + 1];
strcpy(name, t_name.c_str());
// char * name = string(dir)+entry->d_name;
stat(name, &buf);
if(name[t_name.length()-4]=='.')
{
count++;
compute_bow(name, string(dir), string(entry->d_name), dictionary, count, sketch_folder, nnobj_kdt);
}
if(S_ISDIR(buf.st_mode)) //Check if sub directory
{
// cout << "/"; //Formatting
searchDir_create_bundles(name, dictionary, count, sketch_folder, nnobj_kdt);
}
}
entry = readdir(d); //Next file in directory
}
closedir(d);
}
void readDictionary_float(const char *filename, float* dict, int dict_size){
ifstream in;
in.open(filename);
float a=0.0;
int count=0;
while(!in.eof() && count<dict_size){
for(int i=0;i<128;i++) {in>>a; *(dict+count*128+i)=a;}
if(!in){
in.close();
}
count++;
}
in.close();
}
int main(int argc, char* argv[]){
cout<<"Reading Dictionary !!!"<<endl;
float* dict_fl = new float[DICTIONARY_SIZE*128];
readDictionary_float(dictionary_path, dict_fl, DICTIONARY_SIZE);
cout<<"Dictionary Read !!!"<<endl;
fastann::nn_obj<float>* nnobj_kdt = fastann::nn_obj_build_kdtree(dict_fl, DICTIONARY_SIZE , 128, 8, 768);
int count=0;
searchDir_create_bundles(dir, dict_fl, count, sketch_folder, nnobj_kdt);
return 0;
}