-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureComputer.cpp
553 lines (409 loc) · 12.5 KB
/
FeatureComputer.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include "FeatureComputer.hpp"
enum FeatureExtractorType{
FEAT_RGB,
FEAT_HSV,
FEAT_LAB,
FEAT_HOG,
FEAT_SIFT,
FEAT_SURF,
FEAT_BRIEF,
FEAT_ORB
};
void LcFeatureExtractor::set_extractor( string setting_string )
{
int bo[100]; memset(bo,0,sizeof(int)*100);
computers.clear();
for(int i = 0;i<(int) setting_string.length();i++)
{
switch(setting_string[i])
{
case 's':
bo[FEAT_SIFT] = 1;
break;
case 'h':
bo[FEAT_HOG] = 1;
break;
case 'l':
bo[FEAT_LAB] = 1;
break;
case 'v':
bo[FEAT_HSV] = 1;
break;
case 'b':
bo[FEAT_BRIEF] = 1;
break;
case 'o':
bo[FEAT_ORB] = 1;
break;
case 'r':
bo[FEAT_RGB] = 1;
break;
case 'u':
bo[FEAT_SURF] = 1;
break;
}
}
if(bo[FEAT_RGB])
{
computers.push_back( new LcColorComputer< LC_RGB,1> );
computers.push_back( new LcColorComputer< LC_RGB,3> );
computers.push_back( new LcColorComputer< LC_RGB,5> );
}
if(bo[FEAT_HSV])
{
computers.push_back( new LcColorComputer< LC_HSV,1> );
computers.push_back( new LcColorComputer< LC_HSV,3> );
computers.push_back( new LcColorComputer< LC_HSV,5> );
}
if(bo[FEAT_LAB])
{
computers.push_back( new LcColorComputer< LC_LAB,1> );
computers.push_back( new LcColorComputer< LC_LAB,3> );
computers.push_back( new LcColorComputer< LC_LAB,5> );
}
if(bo[FEAT_HOG])
{
computers.push_back( new LcHoGComputer );
}
if(bo[FEAT_SIFT])
{
computers.push_back( new LcSIFTComputer );
}
if(bo[FEAT_SURF])
{
computers.push_back( new LcSURFComputer );
}
if(bo[FEAT_BRIEF])
{
computers.push_back( new LcBRIEFComputer );
}
if(bo[FEAT_ORB])
{
computers.push_back( new LcOrbComputer );
}
for(int i = 0;i<(int)computers.size();i++) computers[i]->veb = veb;
}
LcFeatureExtractor::LcFeatureExtractor()
{
veb = 0;
bound_setting = 0;
computers.clear();
//set_extractor( "robvltdmchsug" );
set_extractor( "rl" );
for(int i = 0;i< (int)computers.size();i++) computers[i]->veb = veb;
}
void LcFeatureExtractor::work(Mat & img, Mat & desc, vector<KeyPoint> * p_keypoint)
{
Mat temp = Mat();
work( img, desc, temp , temp ,1, p_keypoint);
}
void LcFeatureExtractor::work(Mat & img, Mat & desc,int step_size, vector<KeyPoint> * p_keypoint)
{
Mat temp = Mat();
work( img, desc, temp , temp ,step_size, p_keypoint);
}
void LcFeatureExtractor::work(Mat & img, Mat & desc, Mat & img_gt, Mat & lab,vector<KeyPoint> * p_keypoint)
{
int step_size = 1;
work(img,desc,img_gt,lab,step_size,p_keypoint);
}
void LcFeatureExtractor::work(Mat & img, Mat & desc, Mat & img_gt, Mat & lab, int step_size, vector<KeyPoint> * p_keypoint)
{
//cout << "work" << endl;
vector<KeyPoint> * mp_keypts;
if( p_keypoint == NULL) mp_keypts = new vector<KeyPoint>;
else mp_keypts = p_keypoint;
vector<KeyPoint> & keypts = *mp_keypts;
vector<KeyPoint> keypts_ext;
Mat img_ext;
img2keypts(img, keypts, img_ext, keypts_ext, step_size); // set keypoints
int dims = get_dim();
allocate_memory(desc,dims, (int) keypts.size());
extract_feature( img, keypts, img_ext, keypts_ext, desc);
if(img_gt.data) Groundtruth2Label( img_gt, img.size(), keypts, lab);
}
void LcFeatureExtractor::Groundtruth2Label( Mat & img_gt, cv::Size _size , vector< KeyPoint> keypts , Mat & lab)
{
Mat im;
resize(img_gt ,im,_size ,0,0,INTER_NEAREST);
lab = Mat::zeros((int)keypts.size(),1,CV_32FC1);
for(int i=0;i<(int)keypts.size();i++)
{
cv::Point p ((int)floor(.5+keypts[i].pt.x),(int)floor(.5+keypts[i].pt.y) );
if((int)im.at<uchar>(p.y,p.x)>100) lab.at<float>(i,0) = 0.5; // don't care
if((int)im.at<uchar>(p.y,p.x)>200) lab.at<float>(i,0) = 1.0; // ground truth
}
if(veb) cout << " label size " << lab.rows << " by " << lab.cols << endl;
}
void LcFeatureExtractor::img2keypts(
Mat & img, vector<KeyPoint> & keypts,
Mat & img_ext, vector<KeyPoint> & keypts_ext,
int step_size)
{
int bound_max = get_maximal_bound();
if(bound_setting<0) bound_setting = bound_max;
DenseFeatureDetector dfd;
float initFeatureScale = 1.f; // inital size
int featureScaleLevels = 1; // one level
float featureScaleMul = 1.00f; // multiplier (ignored if only one level)
int train_initXyStep = step_size; // space between pixels for training (must be 1)
dfd = DenseFeatureDetector(initFeatureScale,featureScaleLevels,featureScaleMul,train_initXyStep,bound_setting,true,false);
dfd.detect(img,keypts);
if(veb)
{
cout << "image size " << img.rows << " by " << img.cols << " ( " << img.cols * img.rows << "p) " << endl;
cout << " keypts size " << keypts.size() << endl;
}
// bound_setting must be adjusted depending on even and odd ?
if( bound_max > bound_setting)
{
DenseFeatureDetector dfd_ext;
int diff = bound_max-bound_setting;
img_ext = Mat::zeros( img.rows + diff*2 , img.cols + diff*2, img.type());
img.copyTo( img_ext( Range(diff, diff+ img.rows),Range(diff, diff+ img.cols) ) );
// KK: use bordercopy function here!!!
//imshow("extended img debug",img_ext); cv::waitKey(1);
dfd = DenseFeatureDetector(initFeatureScale,featureScaleLevels,featureScaleMul,train_initXyStep,bound_max,true,false);
dfd.detect(img_ext,keypts_ext);
if(veb)
{
cout << "extended image size " << img.rows << " by " << img.cols << " ( " << img.cols * img.rows << "p) " << endl;
cout << "extended keypts size " << keypts_ext.size() << endl;
}
}
}
int LcFeatureExtractor::get_maximal_bound()
{
int ans = 0;
for(int i = 0;i< (int)computers.size();i++) ans = max(ans, computers[i]->bound);
return ans;
}
void LcFeatureExtractor::allocate_memory(Mat & desc,int dims,int data_n)
{
//double t = getTickCount();
desc = Mat::zeros(data_n,dims ,CV_32FC1);
//t = (getTickCount()-t)/getTickFrequency();
//if(veb) cout << " allocate memory:" << t << " secs." << endl;
//if(veb) cout << " allocate " << desc.rows << " by " << desc.cols << endl;
}
void LcFeatureExtractor::extract_feature(
Mat & img,vector<KeyPoint> & keypts,
Mat & img_ext, vector<KeyPoint> & keypts_ext,
Mat & desc)
{
double t = double(getTickCount());
int data_n = (int)keypts.size();
int d = 0;
for(int i = 0;i< (int)computers.size();i++)
{
int dim = computers[i]->dim;
Mat _desc = desc(cv::Rect(d,0, dim ,data_n));
if( computers[i]->bound < bound_setting ) computers[i]->compute( img, keypts, _desc);
else computers[i]->compute( img_ext , keypts_ext , _desc);
d+= dim;
}
t = (getTickCount()-t)/getTickFrequency();
if(veb) cout << " compute all features:" << t << " secs." << endl;
if(veb) cout << " feature size " << desc.rows << " by " << desc.cols << endl;
}
int LcFeatureExtractor::get_dim()
{
int ans = 0;
for(int i = 0;i< (int)computers.size();i++)
{
ans+= computers[i]->dim;
}
if(veb) cout << "feature dim = " << ans << endl;
return ans;
}
//void LcFeatureExtractor::img2keypts( Mat & img, vector<KeyPoint> & keypts)
//{
// DenseFeatureDetector dfd;
//
// float initFeatureScale = 1.f; // inital size
// int featureScaleLevels = 1; // one level
// float featureScaleMul = 1.00f; // multiplier (ignored if only one level)
// int train_initXyStep = 1; // space between pixels for training (must be 1)
//
// dfd = DenseFeatureDetector(initFeatureScale,featureScaleLevels,featureScaleMul,train_initXyStep,initImgBound,true,false);
//
// dfd.detect(img,keypts);
//
// if(veb)
// {
// cout << "image size " << img.rows << " by " << img.cols << " ( " << img.cols * img.rows << "p) " << endl;
// cout << " keypts size " << keypts.size() << endl;
// }
//}
//=============================
template< ColorSpaceType color_type, int win_size>
LcColorComputer<color_type, win_size>::LcColorComputer()
{
if(win_size ==1) dim = 3;
else
{
dim = win_size*win_size - (win_size-2)*(win_size-2);
dim = dim*3;
}
bound = (win_size-1)/2;
}
template< ColorSpaceType color_type, int win_size>
void LcColorComputer<color_type, win_size>::compute( Mat & src, vector<KeyPoint> & keypts, Mat & desc)
{
double t = double(getTickCount());
int code;
if(color_type==LC_RGB) code = CV_BGR2RGB;
else if(color_type==LC_HSV) code = CV_BGR2HSV_FULL;
else if(color_type==LC_LAB) code = CV_BGR2Lab;
Mat color;
cvtColor(src,color,code);
for(int k=0;k<(int)keypts.size();k++)
{
int r = int(floor(.5+keypts[k].pt.y) - floor(win_size*0.5)); // upper-left of patch
int c = int(floor(.5+keypts[k].pt.x) - floor(win_size*0.5));
int a = 0;
for(int i=0;i<win_size;i++)
{
for(int j=0;j<win_size;j++)
{
if(i==0 || j==0 || i==win_size-1 || j == win_size-1)
{
desc.at<float>(k,a+0) = color.at<Vec3b>(r+i,c+j)(0)/255.f;
desc.at<float>(k,a+1) = color.at<Vec3b>(r+i,c+j)(1)/255.f;
desc.at<float>(k,a+2) = color.at<Vec3b>(r+i,c+j)(2)/255.f;
a+=3;
}
}
}
}
if(0 && veb) {
t = (getTickCount()-t)/getTickFrequency();
if(color_type==LC_HSV) cout << " copy HSV features:" << t << " secs." << endl;
else if(color_type==LC_LAB) cout << " copy LAB features:" << t << " secs." << endl;
}
}
//==========================
//==========================
LcHoGComputer::LcHoGComputer()
{
dim = 36;
bound = 10;
}
void LcHoGComputer::compute( Mat & src, vector<KeyPoint> & keypts, Mat & desc)
{
double t = double(getTickCount());
HOGDescriptor hog(src.size(),cv::Size(16,16),cv::Size(1,1),cv::Size(8,8),9);
vector<float> hog_desp;
hog_desp.clear();
Mat try_src = Mat::zeros( src.size(),CV_8U);
//printf("%d %d\n",src.cols,src.rows);
//for(int i = 0;i<try_src.rows;i++) try_src.at<unsigned char>(i,0) = 255;
hog.compute(src,hog_desp);
int block_size = 16;
int shift_size = (block_size+1)/2;
int rows_step = src.rows-block_size+1;
int HOG_dim = 36;
for(int k=0;k<(int)keypts.size();k++)
{
int r = (int)floor(.5+keypts[k].pt.y);
int c = (int)floor(.5+keypts[k].pt.x);
int id_start = (rows_step * (c-shift_size) + r-shift_size)*HOG_dim;
for(int i = 0;i<HOG_dim;i++)
{
desc.at<float>(k,i) = hog_desp[id_start];
id_start++;
}
}
t = (getTickCount()-t)/getTickFrequency();
cout << " HoG Descriptor " << t << " secs." << endl;
}
//===============================
LcBRIEFComputer::LcBRIEFComputer()
{
dim = 16;
bound = 28;
}
void LcBRIEFComputer::compute( Mat & src, vector<KeyPoint> & keypts, Mat & desc)
{
double t = double(getTickCount());
Mat brief_desc;
BriefDescriptorExtractor bde(16);
bde.compute(src,keypts,brief_desc);
brief_desc.convertTo(brief_desc,CV_32FC1);
//lc::colorshow("brief debug",brief_desc);
for(int i=0;i<(int)brief_desc.rows;i++){
Mat row = brief_desc.row(i);
normalize(row,row,1.0,0,NORM_L1);
}
brief_desc.copyTo(desc);
t = (getTickCount()-t)/getTickFrequency();
if(veb) cout << " BRIEF features:" << t << " secs." << endl;
}
//===============================
LcSIFTComputer::LcSIFTComputer()
{
dim = 128;
bound = 0;
}
void LcSIFTComputer::compute( Mat & src, vector<KeyPoint> & keypts, Mat & desc)
{
double t = double(getTickCount());
//Ptr<DescriptorExtractor> siftde = DescriptorExtractor::create("SURF");
SIFT sift;
Mat sift_desc;
sift( src, cv::noArray() , keypts,sift_desc,true);
sift_desc.convertTo(sift_desc,5);
//lc::colorshow("sift debug",sift_desc); cv::waitKey(1);
for(int i=0;i<(int)sift_desc.rows;i++){
Mat row = sift_desc.row(i);
normalize(row,row,1.0,0,NORM_L1);
}
sift_desc.copyTo(desc);
t = (getTickCount()-t)/getTickFrequency();
if(veb) cout << " cSIFT features:" << t << " secs." << endl;
}
//===============================
LcSURFComputer::LcSURFComputer()
{
dim = 128;
bound = 0;
}
void LcSURFComputer::compute( Mat & src, vector<KeyPoint> & keypts, Mat & desc)
{
double t = double(getTickCount());
//Ptr<DescriptorExtractor> siftde = DescriptorExtractor::create("SURF");
SURF sift;
Mat sift_desc;
sift( src, Mat(), keypts,sift_desc,true);
sift_desc.convertTo(sift_desc,CV_32FC1);
for(int i=0;i<(int)sift_desc.rows;i++){
Mat row = sift_desc.row(i);
normalize(row,row,1.0,0,NORM_L1);
}
sift_desc.copyTo(desc);
t = (getTickCount()-t)/getTickFrequency();
if(veb) cout << " copy SURF features:" << t << " secs." << endl;
}
//===============================
//===============
LcOrbComputer::LcOrbComputer()
{
dim = 32;
bound = 31;
}
void LcOrbComputer::compute( Mat & src, vector<KeyPoint> & keypts, Mat & desc)
{
double t = (double)getTickCount();
Mat orb_desc;
OrbDescriptorExtractor ode;
ode.compute(src,keypts,orb_desc);
orb_desc.convertTo(orb_desc,CV_32FC1);
//cout << "orb_desc:" << orb_desc.cols << endl;
for(int i=0;i<(int)orb_desc.rows;i++){
Mat row = orb_desc.row(i);
normalize(row,row,1.0,0,NORM_L1);
}
orb_desc.copyTo(desc);
t = (getTickCount()-t)/getTickFrequency();
if(veb) cout << " compute ORB features:" << t << " secs." << endl;
}