forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAKAZEFeatures.cpp
More file actions
2317 lines (1957 loc) · 72.6 KB
/
AKAZEFeatures.cpp
File metadata and controls
2317 lines (1957 loc) · 72.6 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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file AKAZEFeatures.cpp
* @brief Main class for detecting and describing binary features in an
* accelerated nonlinear scale space
* @date Sep 15, 2013
* @author Pablo F. Alcantarilla, Jesus Nuevo
*/
#include "../precomp.hpp"
#include "AKAZEFeatures.h"
#include "fed.h"
#include "nldiffusion_functions.h"
#include "utils.h"
#include "opencl_kernels_features2d.hpp"
#include <iostream>
// Namespaces
namespace cv
{
using namespace std;
/* ************************************************************************* */
/**
* @brief AKAZEFeatures constructor with input options
* @param options AKAZEFeatures configuration options
* @note This constructor allocates memory for the nonlinear scale space
*/
AKAZEFeatures::AKAZEFeatures(const AKAZEOptions& options) : options_(options) {
ncycles_ = 0;
reordering_ = true;
if (options_.descriptor_size > 0 && options_.descriptor >= AKAZE::DESCRIPTOR_MLDB_UPRIGHT) {
generateDescriptorSubsample(descriptorSamples_, descriptorBits_, options_.descriptor_size,
options_.descriptor_pattern_size, options_.descriptor_channels);
}
Allocate_Memory_Evolution();
}
/* ************************************************************************* */
/**
* @brief This method allocates the memory for the nonlinear diffusion evolution
*/
void AKAZEFeatures::Allocate_Memory_Evolution(void) {
CV_INSTRUMENT_REGION()
float rfactor = 0.0f;
int level_height = 0, level_width = 0;
// maximum size of the area for the descriptor computation
float smax = 0.0;
if (options_.descriptor == AKAZE::DESCRIPTOR_MLDB_UPRIGHT || options_.descriptor == AKAZE::DESCRIPTOR_MLDB) {
smax = 10.0f*sqrtf(2.0f);
}
else if (options_.descriptor == AKAZE::DESCRIPTOR_KAZE_UPRIGHT || options_.descriptor == AKAZE::DESCRIPTOR_KAZE) {
smax = 12.0f*sqrtf(2.0f);
}
// Allocate the dimension of the matrices for the evolution
for (int i = 0, power = 1; i <= options_.omax - 1; i++, power *= 2) {
rfactor = 1.0f / power;
level_height = (int)(options_.img_height*rfactor);
level_width = (int)(options_.img_width*rfactor);
// Smallest possible octave and allow one scale if the image is small
if ((level_width < 80 || level_height < 40) && i != 0) {
options_.omax = i;
break;
}
for (int j = 0; j < options_.nsublevels; j++) {
MEvolution step;
step.size = Size(level_width, level_height);
step.esigma = options_.soffset*pow(2.f, (float)(j) / (float)(options_.nsublevels) + i);
step.sigma_size = cvRound(step.esigma * options_.derivative_factor / power); // In fact sigma_size only depends on j
step.etime = 0.5f * (step.esigma * step.esigma);
step.octave = i;
step.sublevel = j;
step.octave_ratio = (float)power;
step.border = cvRound(smax * step.sigma_size) + 1;
evolution_.push_back(step);
}
}
// Allocate memory for the number of cycles and time steps
for (size_t i = 1; i < evolution_.size(); i++) {
int naux = 0;
vector<float> tau;
float ttime = 0.0f;
ttime = evolution_[i].etime - evolution_[i - 1].etime;
naux = fed_tau_by_process_time(ttime, 1, 0.25f, reordering_, tau);
nsteps_.push_back(naux);
tsteps_.push_back(tau);
ncycles_++;
}
}
/* ************************************************************************* */
/**
* @brief Computes kernel size for Gaussian smoothing if the image
* @param sigma Kernel standard deviation
* @returns kernel size
*/
static inline int getGaussianKernelSize(float sigma) {
// Compute an appropriate kernel size according to the specified sigma
int ksize = (int)cvCeil(2.0f*(1.0f + (sigma - 0.8f) / (0.3f)));
ksize |= 1; // kernel should be odd
return ksize;
}
/* ************************************************************************* */
/**
* @brief This function computes a scalar non-linear diffusion step
* @param Lt Base image in the evolution
* @param Lf Conductivity image
* @param Lstep Output image that gives the difference between the current
* Ld and the next Ld being evolved
* @param row_begin row where to start
* @param row_end last row to fill exclusive. the range is [row_begin, row_end).
* @note Forward Euler Scheme 3x3 stencil
* The function c is a scalar value that depends on the gradient norm
* dL_by_ds = d(c dL_by_dx)_by_dx + d(c dL_by_dy)_by_dy
*/
static inline void
nld_step_scalar_one_lane(const Mat& Lt, const Mat& Lf, Mat& Lstep, float step_size, int row_begin, int row_end)
{
CV_INSTRUMENT_REGION()
/* The labeling scheme for this five star stencil:
[ a ]
[ -1 c +1 ]
[ b ]
*/
Lstep.create(Lt.size(), Lt.type());
const int cols = Lt.cols - 2;
int row = row_begin;
const float *lt_a, *lt_c, *lt_b;
const float *lf_a, *lf_c, *lf_b;
float *dst;
float step_r = 0.f;
// Process the top row
if (row == 0) {
lt_c = Lt.ptr<float>(0) + 1; /* Skip the left-most column by +1 */
lf_c = Lf.ptr<float>(0) + 1;
lt_b = Lt.ptr<float>(1) + 1;
lf_b = Lf.ptr<float>(1) + 1;
// fill the corner to prevent uninitialized values
dst = Lstep.ptr<float>(0);
dst[0] = 0.0f;
++dst;
for (int j = 0; j < cols; j++) {
step_r = (lf_c[j] + lf_c[j + 1])*(lt_c[j + 1] - lt_c[j]) +
(lf_c[j] + lf_c[j - 1])*(lt_c[j - 1] - lt_c[j]) +
(lf_c[j] + lf_b[j ])*(lt_b[j ] - lt_c[j]);
dst[j] = step_r * step_size;
}
// fill the corner to prevent uninitialized values
dst[cols] = 0.0f;
++row;
}
// Process the middle rows
int middle_end = std::min(Lt.rows - 1, row_end);
for (; row < middle_end; ++row)
{
lt_a = Lt.ptr<float>(row - 1);
lf_a = Lf.ptr<float>(row - 1);
lt_c = Lt.ptr<float>(row );
lf_c = Lf.ptr<float>(row );
lt_b = Lt.ptr<float>(row + 1);
lf_b = Lf.ptr<float>(row + 1);
dst = Lstep.ptr<float>(row);
// The left-most column
step_r = (lf_c[0] + lf_c[1])*(lt_c[1] - lt_c[0]) +
(lf_c[0] + lf_b[0])*(lt_b[0] - lt_c[0]) +
(lf_c[0] + lf_a[0])*(lt_a[0] - lt_c[0]);
dst[0] = step_r * step_size;
lt_a++; lt_c++; lt_b++;
lf_a++; lf_c++; lf_b++;
dst++;
// The middle columns
for (int j = 0; j < cols; j++)
{
step_r = (lf_c[j] + lf_c[j + 1])*(lt_c[j + 1] - lt_c[j]) +
(lf_c[j] + lf_c[j - 1])*(lt_c[j - 1] - lt_c[j]) +
(lf_c[j] + lf_b[j ])*(lt_b[j ] - lt_c[j]) +
(lf_c[j] + lf_a[j ])*(lt_a[j ] - lt_c[j]);
dst[j] = step_r * step_size;
}
// The right-most column
step_r = (lf_c[cols] + lf_c[cols - 1])*(lt_c[cols - 1] - lt_c[cols]) +
(lf_c[cols] + lf_b[cols ])*(lt_b[cols ] - lt_c[cols]) +
(lf_c[cols] + lf_a[cols ])*(lt_a[cols ] - lt_c[cols]);
dst[cols] = step_r * step_size;
}
// Process the bottom row (row == Lt.rows - 1)
if (row_end == Lt.rows) {
lt_a = Lt.ptr<float>(row - 1) + 1; /* Skip the left-most column by +1 */
lf_a = Lf.ptr<float>(row - 1) + 1;
lt_c = Lt.ptr<float>(row ) + 1;
lf_c = Lf.ptr<float>(row ) + 1;
// fill the corner to prevent uninitialized values
dst = Lstep.ptr<float>(row);
dst[0] = 0.0f;
++dst;
for (int j = 0; j < cols; j++) {
step_r = (lf_c[j] + lf_c[j + 1])*(lt_c[j + 1] - lt_c[j]) +
(lf_c[j] + lf_c[j - 1])*(lt_c[j - 1] - lt_c[j]) +
(lf_c[j] + lf_a[j ])*(lt_a[j ] - lt_c[j]);
dst[j] = step_r * step_size;
}
// fill the corner to prevent uninitialized values
dst[cols] = 0.0f;
}
}
class NonLinearScalarDiffusionStep : public ParallelLoopBody
{
public:
NonLinearScalarDiffusionStep(const Mat& Lt, const Mat& Lf, Mat& Lstep, float step_size)
: Lt_(&Lt), Lf_(&Lf), Lstep_(&Lstep), step_size_(step_size)
{}
void operator()(const Range& range) const
{
nld_step_scalar_one_lane(*Lt_, *Lf_, *Lstep_, step_size_, range.start, range.end);
}
private:
const Mat* Lt_;
const Mat* Lf_;
Mat* Lstep_;
float step_size_;
};
#ifdef HAVE_OPENCL
static inline bool
ocl_non_linear_diffusion_step(InputArray Lt_, InputArray Lf_, OutputArray Lstep_, float step_size)
{
if(!Lt_.isContinuous())
return false;
UMat Lt = Lt_.getUMat();
UMat Lf = Lf_.getUMat();
UMat Lstep = Lstep_.getUMat();
size_t globalSize[] = {(size_t)Lt.cols, (size_t)Lt.rows};
ocl::Kernel ker("AKAZE_nld_step_scalar", ocl::features2d::akaze_oclsrc);
if( ker.empty() )
return false;
return ker.args(
ocl::KernelArg::ReadOnly(Lt),
ocl::KernelArg::PtrReadOnly(Lf),
ocl::KernelArg::PtrWriteOnly(Lstep),
step_size).run(2, globalSize, 0, true);
}
#endif // HAVE_OPENCL
static inline void
non_linear_diffusion_step(InputArray Lt_, InputArray Lf_, OutputArray Lstep_, float step_size)
{
CV_INSTRUMENT_REGION()
Lstep_.create(Lt_.size(), Lt_.type());
CV_OCL_RUN(Lt_.isUMat() && Lf_.isUMat() && Lstep_.isUMat(),
ocl_non_linear_diffusion_step(Lt_, Lf_, Lstep_, step_size));
Mat Lt = Lt_.getMat();
Mat Lf = Lf_.getMat();
Mat Lstep = Lstep_.getMat();
parallel_for_(Range(0, Lt.rows), NonLinearScalarDiffusionStep(Lt, Lf, Lstep, step_size));
}
/**
* @brief This function computes a good empirical value for the k contrast factor
* given two gradient images, the percentile (0-1), the temporal storage to hold
* gradient norms and the histogram bins
* @param Lx Horizontal gradient of the input image
* @param Ly Vertical gradient of the input image
* @param nbins Number of histogram bins
* @return k contrast factor
*/
static inline float
compute_kcontrast(InputArray Lx_, InputArray Ly_, float perc, int nbins)
{
CV_INSTRUMENT_REGION()
CV_Assert(nbins > 2);
CV_Assert(!Lx_.empty());
Mat Lx = Lx_.getMat();
Mat Ly = Ly_.getMat();
// temporary square roots of dot product
Mat modgs (Lx.rows - 2, Lx.cols - 2, CV_32F);
const int total = modgs.cols * modgs.rows;
float *modg = modgs.ptr<float>();
float hmax = 0.0f;
for (int i = 1; i < Lx.rows - 1; i++) {
const float *lx = Lx.ptr<float>(i) + 1;
const float *ly = Ly.ptr<float>(i) + 1;
const int cols = Lx.cols - 2;
for (int j = 0; j < cols; j++) {
float dist = sqrtf(lx[j] * lx[j] + ly[j] * ly[j]);
*modg++ = dist;
hmax = std::max(hmax, dist);
}
}
modg = modgs.ptr<float>();
if (hmax == 0.0f)
return 0.03f; // e.g. a blank image
// Compute the bin numbers: the value range [0, hmax] -> [0, nbins-1]
modgs *= (nbins - 1) / hmax;
// Count up histogram
std::vector<int> hist(nbins, 0);
for (int i = 0; i < total; i++)
hist[(int)modg[i]]++;
// Now find the perc of the histogram percentile
const int nthreshold = (int)((total - hist[0]) * perc); // Exclude hist[0] as background
int nelements = 0;
for (int k = 1; k < nbins; k++) {
if (nelements >= nthreshold)
return (float)hmax * k / nbins;
nelements += hist[k];
}
return 0.03f;
}
#ifdef HAVE_OPENCL
static inline bool
ocl_pm_g2(InputArray Lx_, InputArray Ly_, OutputArray Lflow_, float kcontrast)
{
UMat Lx = Lx_.getUMat();
UMat Ly = Ly_.getUMat();
UMat Lflow = Lflow_.getUMat();
int total = Lx.rows * Lx.cols;
size_t globalSize[] = {(size_t)total};
ocl::Kernel ker("AKAZE_pm_g2", ocl::features2d::akaze_oclsrc);
if( ker.empty() )
return false;
return ker.args(
ocl::KernelArg::PtrReadOnly(Lx),
ocl::KernelArg::PtrReadOnly(Ly),
ocl::KernelArg::PtrWriteOnly(Lflow),
kcontrast, total).run(1, globalSize, 0, true);
}
#endif // HAVE_OPENCL
static inline void
compute_diffusivity(InputArray Lx, InputArray Ly, OutputArray Lflow, float kcontrast, int diffusivity)
{
CV_INSTRUMENT_REGION()
Lflow.create(Lx.size(), Lx.type());
switch (diffusivity) {
case KAZE::DIFF_PM_G1:
pm_g1(Lx, Ly, Lflow, kcontrast);
break;
case KAZE::DIFF_PM_G2:
CV_OCL_RUN(Lx.isUMat() && Ly.isUMat() && Lflow.isUMat(), ocl_pm_g2(Lx, Ly, Lflow, kcontrast));
pm_g2(Lx, Ly, Lflow, kcontrast);
break;
case KAZE::DIFF_WEICKERT:
weickert_diffusivity(Lx, Ly, Lflow, kcontrast);
break;
case KAZE::DIFF_CHARBONNIER:
charbonnier_diffusivity(Lx, Ly, Lflow, kcontrast);
break;
default:
CV_Error(diffusivity, "Diffusivity is not supported");
break;
}
}
/**
* @brief Converts input image to grayscale float image
*
* @param image any image
* @param dst grayscale float image
*/
static inline void prepareInputImage(InputArray image, OutputArray dst)
{
Mat img = image.getMat();
if (img.channels() > 1)
cvtColor(image, img, COLOR_BGR2GRAY);
if ( img.depth() == CV_32F )
dst.assign(img);
else if ( img.depth() == CV_8U )
img.convertTo(dst, CV_32F, 1.0 / 255.0, 0);
else if ( img.depth() == CV_16U )
img.convertTo(dst, CV_32F, 1.0 / 65535.0, 0);
}
/**
* @brief This method creates the nonlinear scale space for a given image
* @param image Input image for which the nonlinear scale space needs to be created
*/
template<typename MatType>
static inline void
create_nonlinear_scale_space(InputArray image, const AKAZEOptions &options,
const std::vector<std::vector<float > > &tsteps_evolution, std::vector<Evolution<MatType> > &evolution)
{
CV_INSTRUMENT_REGION()
CV_Assert(evolution.size() > 0);
// convert input to grayscale float image if needed
MatType img;
prepareInputImage(image, img);
// create first level of the evolution
int ksize = getGaussianKernelSize(options.soffset);
GaussianBlur(img, evolution[0].Lsmooth, Size(ksize, ksize), options.soffset, options.soffset, BORDER_REPLICATE);
evolution[0].Lsmooth.copyTo(evolution[0].Lt);
if (evolution.size() == 1) {
// we don't need to compute kcontrast factor
Compute_Determinant_Hessian_Response(evolution);
return;
}
// derivatives, flow and diffusion step
MatType Lx, Ly, Lsmooth, Lflow, Lstep;
// compute derivatives for computing k contrast
GaussianBlur(img, Lsmooth, Size(5, 5), 1.0f, 1.0f, BORDER_REPLICATE);
Scharr(Lsmooth, Lx, CV_32F, 1, 0, 1, 0, BORDER_DEFAULT);
Scharr(Lsmooth, Ly, CV_32F, 0, 1, 1, 0, BORDER_DEFAULT);
Lsmooth.release();
// compute the kcontrast factor
float kcontrast = compute_kcontrast(Lx, Ly, options.kcontrast_percentile, options.kcontrast_nbins);
// Now generate the rest of evolution levels
for (size_t i = 1; i < evolution.size(); i++) {
Evolution<MatType> &e = evolution[i];
if (e.octave > evolution[i - 1].octave) {
// new octave will be half the size
resize(evolution[i - 1].Lt, e.Lt, e.size, 0, 0, INTER_AREA);
kcontrast *= 0.75f;
}
else {
evolution[i - 1].Lt.copyTo(e.Lt);
}
GaussianBlur(e.Lt, e.Lsmooth, Size(5, 5), 1.0f, 1.0f, BORDER_REPLICATE);
// Compute the Gaussian derivatives Lx and Ly
Scharr(e.Lsmooth, Lx, CV_32F, 1, 0, 1.0, 0, BORDER_DEFAULT);
Scharr(e.Lsmooth, Ly, CV_32F, 0, 1, 1.0, 0, BORDER_DEFAULT);
// Compute the conductivity equation
compute_diffusivity(Lx, Ly, Lflow, kcontrast, options.diffusivity);
// Perform Fast Explicit Diffusion on Lt
const std::vector<float> &tsteps = tsteps_evolution[i - 1];
for (size_t j = 0; j < tsteps.size(); j++) {
const float step_size = tsteps[j] * 0.5f;
non_linear_diffusion_step(e.Lt, Lflow, Lstep, step_size);
add(e.Lt, Lstep, e.Lt);
}
}
Compute_Determinant_Hessian_Response(evolution);
return;
}
/**
* @brief Converts between UMatPyramid and Pyramid and vice versa
* @details Matrices in evolution levels will be copied
*
* @param src source pyramid
* @param dst destination pyramid
*/
template<typename MatTypeSrc, typename MatTypeDst>
static inline void
convertScalePyramid(const std::vector<Evolution<MatTypeSrc> >& src, std::vector<Evolution<MatTypeDst> > &dst)
{
dst.resize(src.size());
for (size_t i = 0; i < src.size(); ++i) {
dst[i] = Evolution<MatTypeDst>(src[i]);
}
}
/**
* @brief This method creates the nonlinear scale space for a given image
* @param image Input image for which the nonlinear scale space needs to be created
*/
void AKAZEFeatures::Create_Nonlinear_Scale_Space(InputArray image)
{
if (ocl::isOpenCLActivated() && image.isUMat()) {
// will run OCL version of scale space pyramid
UMatPyramid uPyr;
// init UMat pyramid with sizes
convertScalePyramid(evolution_, uPyr);
create_nonlinear_scale_space(image, options_, tsteps_, uPyr);
// download pyramid from GPU
convertScalePyramid(uPyr, evolution_);
} else {
// CPU version
create_nonlinear_scale_space(image, options_, tsteps_, evolution_);
}
}
/* ************************************************************************* */
#ifdef HAVE_OPENCL
static inline bool
ocl_compute_determinant(InputArray Lxx_, InputArray Lxy_, InputArray Lyy_,
OutputArray Ldet_, float sigma)
{
UMat Lxx = Lxx_.getUMat();
UMat Lxy = Lxy_.getUMat();
UMat Lyy = Lyy_.getUMat();
UMat Ldet = Ldet_.getUMat();
const int total = Lxx.rows * Lxx.cols;
size_t globalSize[] = {(size_t)total};
ocl::Kernel ker("AKAZE_compute_determinant", ocl::features2d::akaze_oclsrc);
if( ker.empty() )
return false;
return ker.args(
ocl::KernelArg::PtrReadOnly(Lxx),
ocl::KernelArg::PtrReadOnly(Lxy),
ocl::KernelArg::PtrReadOnly(Lyy),
ocl::KernelArg::PtrWriteOnly(Ldet),
sigma, total).run(1, globalSize, 0, true);
}
#endif // HAVE_OPENCL
/**
* @brief Compute determinant from hessians
* @details Compute Ldet by (Lxx.mul(Lyy) - Lxy.mul(Lxy)) * sigma
*
* @param Lxx spatial derivates
* @param Lxy spatial derivates
* @param Lyy spatial derivates
* @param Ldet output determinant
* @param sigma determinant will be scaled by this sigma
*/
static inline void compute_determinant(InputArray Lxx_, InputArray Lxy_, InputArray Lyy_,
OutputArray Ldet_, float sigma)
{
CV_INSTRUMENT_REGION()
Ldet_.create(Lxx_.size(), Lxx_.type());
CV_OCL_RUN(Lxx_.isUMat() && Ldet_.isUMat(), ocl_compute_determinant(Lxx_, Lxy_, Lyy_, Ldet_, sigma));
// output determinant
Mat Lxx = Lxx_.getMat(), Lxy = Lxy_.getMat(), Lyy = Lyy_.getMat(), Ldet = Ldet_.getMat();
float *lxx = Lxx.ptr<float>();
float *lxy = Lxy.ptr<float>();
float *lyy = Lyy.ptr<float>();
float *ldet = Ldet.ptr<float>();
const int total = Lxx.cols * Lxx.rows;
for (int j = 0; j < total; j++) {
ldet[j] = (lxx[j] * lyy[j] - lxy[j] * lxy[j]) * sigma;
}
}
template <typename MatType>
class DeterminantHessianResponse : public ParallelLoopBody
{
public:
explicit DeterminantHessianResponse(std::vector<Evolution<MatType> >& ev)
: evolution_(&ev)
{
}
void operator()(const Range& range) const
{
MatType Lxx, Lxy, Lyy;
for (int i = range.start; i < range.end; i++)
{
Evolution<MatType> &e = (*evolution_)[i];
// we cannot use cv:Scharr here, because we need to handle also
// kernel sizes other than 3, by default we are using 9x9, 5x5 and 7x7
// compute kernels
Mat DxKx, DxKy, DyKx, DyKy;
compute_derivative_kernels(DxKx, DxKy, 1, 0, e.sigma_size);
compute_derivative_kernels(DyKx, DyKy, 0, 1, e.sigma_size);
// compute the multiscale derivatives
sepFilter2D(e.Lsmooth, e.Lx, CV_32F, DxKx, DxKy);
sepFilter2D(e.Lx, Lxx, CV_32F, DxKx, DxKy);
sepFilter2D(e.Lx, Lxy, CV_32F, DyKx, DyKy);
sepFilter2D(e.Lsmooth, e.Ly, CV_32F, DyKx, DyKy);
sepFilter2D(e.Ly, Lyy, CV_32F, DyKx, DyKy);
// free Lsmooth to same some space in the pyramid, it is not needed anymore
e.Lsmooth.release();
// compute determinant scaled by sigma
float sigma_size_quat = (float)(e.sigma_size * e.sigma_size * e.sigma_size * e.sigma_size);
compute_determinant(Lxx, Lxy, Lyy, e.Ldet, sigma_size_quat);
}
}
private:
std::vector<Evolution<MatType> >* evolution_;
};
/**
* @brief This method computes the feature detector response for the nonlinear scale space
* @details OCL version
* @note We use the Hessian determinant as the feature detector response
*/
static inline void
Compute_Determinant_Hessian_Response(UMatPyramid &evolution) {
CV_INSTRUMENT_REGION()
DeterminantHessianResponse<UMat> body (evolution);
body(Range(0, (int)evolution.size()));
}
/**
* @brief This method computes the feature detector response for the nonlinear scale space
* @details CPU version
* @note We use the Hessian determinant as the feature detector response
*/
static inline void
Compute_Determinant_Hessian_Response(Pyramid &evolution) {
CV_INSTRUMENT_REGION()
parallel_for_(Range(0, (int)evolution.size()), DeterminantHessianResponse<Mat>(evolution));
}
/* ************************************************************************* */
/**
* @brief This method selects interesting keypoints through the nonlinear scale space
* @param kpts Vector of detected keypoints
*/
void AKAZEFeatures::Feature_Detection(std::vector<KeyPoint>& kpts)
{
CV_INSTRUMENT_REGION()
kpts.clear();
std::vector<Mat> keypoints_by_layers;
Find_Scale_Space_Extrema(keypoints_by_layers);
Do_Subpixel_Refinement(keypoints_by_layers, kpts);
Compute_Keypoints_Orientation(kpts);
}
/**
* @brief This method searches v for a neighbor point of the point candidate p
* @param x Coordinates of the keypoint candidate to search a neighbor
* @param y Coordinates of the keypoint candidate to search a neighbor
* @param mask Matrix holding keypoints positions
* @param search_radius neighbour radius for searching keypoints
* @param idx The index to mask, pointing to keypoint found.
* @return true if a neighbor point is found; false otherwise
*/
static inline bool
find_neighbor_point(const int x, const int y, const Mat &mask, const int search_radius, int &idx)
{
// search neighborhood for keypoints
for (int i = y - search_radius; i < y + search_radius; ++i) {
const uchar *curr = mask.ptr<uchar>(i);
for (int j = x - search_radius; j < x + search_radius; ++j) {
if (curr[j] == 0) {
continue; // skip non-keypoint
}
// fine-compare with L2 metric (L2 is smaller than our search window)
int dx = j - x;
int dy = i - y;
if (dx * dx + dy * dy <= search_radius * search_radius) {
idx = i * mask.cols + j;
return true;
}
}
}
return false;
}
/**
* @brief Find keypoints in parallel for each pyramid layer
*/
class FindKeypointsSameScale : public ParallelLoopBody
{
public:
explicit FindKeypointsSameScale(const Pyramid& ev,
std::vector<Mat>& kpts, float dthreshold)
: evolution_(&ev), keypoints_by_layers_(&kpts), dthreshold_(dthreshold)
{}
void operator()(const Range& range) const
{
for (int i = range.start; i < range.end; i++)
{
const MEvolution &e = (*evolution_)[i];
Mat &kpts = (*keypoints_by_layers_)[i];
// this mask will hold positions of keypoints in this level
kpts = Mat::zeros(e.Ldet.size(), CV_8UC1);
// if border is too big we shouldn't search any keypoints
if (e.border + 1 >= e.Ldet.rows)
continue;
const float * prev = e.Ldet.ptr<float>(e.border - 1);
const float * curr = e.Ldet.ptr<float>(e.border );
const float * next = e.Ldet.ptr<float>(e.border + 1);
const float * ldet = e.Ldet.ptr<float>();
uchar *mask = kpts.ptr<uchar>();
const int search_radius = e.sigma_size; // size of keypoint in this level
for (int y = e.border; y < e.Ldet.rows - e.border; y++) {
for (int x = e.border; x < e.Ldet.cols - e.border; x++) {
const float value = curr[x];
// Filter the points with the detector threshold
if (value <= dthreshold_)
continue;
if (value <= curr[x-1] || value <= curr[x+1])
continue;
if (value <= prev[x-1] || value <= prev[x ] || value <= prev[x+1])
continue;
if (value <= next[x-1] || value <= next[x ] || value <= next[x+1])
continue;
int idx = 0;
// Compare response with the same scale
if (find_neighbor_point(x, y, kpts, search_radius, idx)) {
if (value > ldet[idx]) {
mask[idx] = 0; // clear old point - we have better candidate now
} else {
continue; // there already is a better keypoint
}
}
kpts.at<uchar>(y, x) = 1; // we have a new keypoint
}
prev = curr;
curr = next;
next += e.Ldet.cols;
}
}
}
private:
const Pyramid* evolution_;
std::vector<Mat>* keypoints_by_layers_;
float dthreshold_; ///< Detector response threshold to accept point
};
/**
* @brief This method finds extrema in the nonlinear scale space
* @param keypoints_by_layers Output vectors of detected keypoints; one vector for each evolution level
*/
void AKAZEFeatures::Find_Scale_Space_Extrema(std::vector<Mat>& keypoints_by_layers)
{
CV_INSTRUMENT_REGION()
keypoints_by_layers.resize(evolution_.size());
// find points in the same level
parallel_for_(Range(0, (int)evolution_.size()),
FindKeypointsSameScale(evolution_, keypoints_by_layers, options_.dthreshold));
// Filter points with the lower scale level
for (size_t i = 1; i < keypoints_by_layers.size(); i++) {
// constants for this level
const Mat &keypoints = keypoints_by_layers[i];
const uchar *const kpts = keypoints_by_layers[i].ptr<uchar>();
uchar *const kpts_prev = keypoints_by_layers[i-1].ptr<uchar>();
const float *const ldet = evolution_[i].Ldet.ptr<float>();
const float *const ldet_prev = evolution_[i-1].Ldet.ptr<float>();
// ratios are just powers of 2
const int diff_ratio = (int)evolution_[i].octave_ratio / (int)evolution_[i-1].octave_ratio;
const int search_radius = evolution_[i].sigma_size * diff_ratio; // size of keypoint in this level
size_t j = 0;
for (int y = 0; y < keypoints.rows; y++) {
for (int x = 0; x < keypoints.cols; x++, j++) {
if (kpts[j] == 0) {
continue; // skip non-keypoints
}
int idx = 0;
// project point to lower scale layer
const int p_x = x * diff_ratio;
const int p_y = y * diff_ratio;
if (find_neighbor_point(p_x, p_y, keypoints_by_layers[i-1], search_radius, idx)) {
if (ldet[j] > ldet_prev[idx]) {
kpts_prev[idx] = 0; // clear keypoint in lower layer
}
// else this pt may be pruned by the upper scale
}
}
}
}
// Now filter points with the upper scale level (the other direction)
for (int i = (int)keypoints_by_layers.size() - 2; i >= 0; i--) {
// constants for this level
const Mat &keypoints = keypoints_by_layers[i];
const uchar *const kpts = keypoints_by_layers[i].ptr<uchar>();
uchar *const kpts_next = keypoints_by_layers[i+1].ptr<uchar>();
const float *const ldet = evolution_[i].Ldet.ptr<float>();
const float *const ldet_next = evolution_[i+1].Ldet.ptr<float>();
// ratios are just powers of 2, i+1 ratio is always greater or equal to i
const int diff_ratio = (int)evolution_[i+1].octave_ratio / (int)evolution_[i].octave_ratio;
const int search_radius = evolution_[i+1].sigma_size; // size of keypoints in upper level
size_t j = 0;
for (int y = 0; y < keypoints.rows; y++) {
for (int x = 0; x < keypoints.cols; x++, j++) {
if (kpts[j] == 0) {
continue; // skip non-keypoints
}
int idx = 0;
// project point to upper scale layer
const int p_x = x / diff_ratio;
const int p_y = y / diff_ratio;
if (find_neighbor_point(p_x, p_y, keypoints_by_layers[i+1], search_radius, idx)) {
if (ldet[j] > ldet_next[idx]) {
kpts_next[idx] = 0; // clear keypoint in upper layer
}
}
}
}
}
}
/* ************************************************************************* */
/**
* @brief This method performs subpixel refinement of the detected keypoints
* @param keypoints_by_layers Input vectors of detected keypoints, sorted by evolution levels
* @param kpts Output vector of the final refined keypoints
*/
void AKAZEFeatures::Do_Subpixel_Refinement(
std::vector<Mat>& keypoints_by_layers, std::vector<KeyPoint>& output_keypoints)
{
CV_INSTRUMENT_REGION()
for (size_t i = 0; i < keypoints_by_layers.size(); i++) {
const MEvolution &e = evolution_[i];
const float * const ldet = e.Ldet.ptr<float>();
const float ratio = e.octave_ratio;
const int cols = e.Ldet.cols;
const Mat& keypoints = keypoints_by_layers[i];
const uchar *const kpts = keypoints.ptr<uchar>();
size_t j = 0;
for (int y = 0; y < keypoints.rows; y++) {
for (int x = 0; x < keypoints.cols; x++, j++) {
if (kpts[j] == 0) {
continue; // skip non-keypoints
}
// create a new keypoint
KeyPoint kp;
kp.pt.x = x * e.octave_ratio;
kp.pt.y = y * e.octave_ratio;
kp.size = e.esigma * options_.derivative_factor;
kp.angle = -1;
kp.response = ldet[j];
kp.octave = e.octave;
kp.class_id = static_cast<int>(i);
// Compute the gradient
float Dx = 0.5f * (ldet[ y *cols + x + 1] - ldet[ y *cols + x - 1]);
float Dy = 0.5f * (ldet[(y + 1)*cols + x ] - ldet[(y - 1)*cols + x ]);
// Compute the Hessian
float Dxx = ldet[ y *cols + x + 1] + ldet[ y *cols + x - 1] - 2.0f * ldet[y*cols + x];
float Dyy = ldet[(y + 1)*cols + x ] + ldet[(y - 1)*cols + x ] - 2.0f * ldet[y*cols + x];
float Dxy = 0.25f * (ldet[(y + 1)*cols + x + 1] + ldet[(y - 1)*cols + x - 1] -
ldet[(y - 1)*cols + x + 1] - ldet[(y + 1)*cols + x - 1]);
// Solve the linear system
Matx22f A( Dxx, Dxy,
Dxy, Dyy );
Vec2f b( -Dx, -Dy );
Vec2f dst( 0.0f, 0.0f );
solve(A, b, dst, DECOMP_LU);
float dx = dst(0);
float dy = dst(1);
if (fabs(dx) > 1.0f || fabs(dy) > 1.0f)
continue; // Ignore the point that is not stable
// Refine the coordinates
kp.pt.x += dx * ratio + .5f*(ratio-1.f);
kp.pt.y += dy * ratio + .5f*(ratio-1.f);
kp.angle = 0.0;
kp.size *= 2.0f; // In OpenCV the size of a keypoint is the diameter
// Push the refined keypoint to the final storage
output_keypoints.push_back(kp);
}
}
}
}
/* ************************************************************************* */
class SURF_Descriptor_Upright_64_Invoker : public ParallelLoopBody
{
public:
SURF_Descriptor_Upright_64_Invoker(std::vector<KeyPoint>& kpts, Mat& desc, const Pyramid& evolution)
: keypoints_(&kpts)
, descriptors_(&desc)
, evolution_(&evolution)
{
}
void operator() (const Range& range) const
{
for (int i = range.start; i < range.end; i++)
{
Get_SURF_Descriptor_Upright_64((*keypoints_)[i], descriptors_->ptr<float>(i), descriptors_->cols);
}
}
void Get_SURF_Descriptor_Upright_64(const KeyPoint& kpt, float* desc, int desc_size) const;
private:
std::vector<KeyPoint>* keypoints_;
Mat* descriptors_;
const Pyramid* evolution_;
};
class SURF_Descriptor_64_Invoker : public ParallelLoopBody
{
public:
SURF_Descriptor_64_Invoker(std::vector<KeyPoint>& kpts, Mat& desc, Pyramid& evolution)
: keypoints_(&kpts)
, descriptors_(&desc)
, evolution_(&evolution)
{
}
void operator()(const Range& range) const
{
for (int i = range.start; i < range.end; i++)
{
Get_SURF_Descriptor_64((*keypoints_)[i], descriptors_->ptr<float>(i), descriptors_->cols);
}
}
void Get_SURF_Descriptor_64(const KeyPoint& kpt, float* desc, int desc_size) const;
private:
std::vector<KeyPoint>* keypoints_;
Mat* descriptors_;
Pyramid* evolution_;
};
class MSURF_Upright_Descriptor_64_Invoker : public ParallelLoopBody
{
public:
MSURF_Upright_Descriptor_64_Invoker(std::vector<KeyPoint>& kpts, Mat& desc, Pyramid& evolution)
: keypoints_(&kpts)
, descriptors_(&desc)
, evolution_(&evolution)
{