-
Notifications
You must be signed in to change notification settings - Fork 1
/
acl_ezsift.cpp
1694 lines (1421 loc) · 71.5 KB
/
acl_ezsift.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
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
/*
Copyright (c) 2013, Robert Wang, email: robertwgh (at) gmail.com
Copyright (c) 2017, Armin Zare Zadeh, email: ali.a.zarezadeh (at) gmail.com
All rights reserved. https://sourceforge.net/p/ezsift
Some algorithms used in this code referred to:
1. OpenCV: http://opencv.org/
2. VLFeat: http://www.vlfeat.org/
The SIFT algorithm was developed by David Lowe. More information can be found from:
David G. Lowe, "Distinctive image features from scale-invariant keypoints,"
International Journal of Computer Vision, 60, 2 (2004), pp. 91-110.
Pay attention that the SIFT algorithm is patented. It is your responsibility to use the code
in a legal way. Patent information:
Method and apparatus for identifying scale invariant features in an image
and use of same for locating an object in an image David G. Lowe, US Patent 6,711,293
(March 23, 2004). Provisional application filed March 8, 1999. Asignee: The University of
British Columbia.
Revision history:
September, 15, 2013: initial version.
July 8th, 2014: fixed a bug in sample_2x in image.h. The bug only happened for image with odd width or height.
May 18 2017: ported to run on ARM Neon/Mali Technology by using ARM Computation Library (ACL)
*/
#include "acl_ezsift.h"
#include "opencv2/opencv.hpp"
#include "arm_neon.h"
#include <string>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <fstream>
#include <iostream>
#include <cmath>
#include "arm_compute/runtime/NEON/NEFunctions.h"
#include "arm_compute/runtime/CL/CLFunctions.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/Error.h"
#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/NEON/kernels/NEConvolutionKernel.h"
#include "arm_compute/core/CL/kernels/CLConvolutionKernel.h"
#include "arm_compute/core/NEON/kernels/NEMagnitudePhaseKernel.h"
#include "arm_compute/core/CL/kernels/CLMagnitudePhaseKernel.h"
#include "arm_compute/core/PixelValue.h"
#include "arm_compute/runtime/NEON/NEScheduler.h"
#include "arm_compute/runtime/CL/CLScheduler.h"
#include "arm_compute/core/TensorInfo.h"
#include "arm_compute/core/TensorShape.h"
#include "test_helpers/Utils.h"
using namespace arm_compute;
void NEGradRot::configure(const ITensor *input1, const ITensor *input2, ITensor *outputMag, ITensor *outputPhase, bool use_fp16)
{
if(use_fp16){
auto k = arm_compute::cpp14::make_unique<NEMagnitudePhaseFP16Kernel<MagnitudeType::L2NORM, PhaseType::SIGNED>>();
k->configure(input1, input2, outputMag, outputPhase);
_kernel = std::move(k);
}
else{
auto k = arm_compute::cpp14::make_unique<NEMagnitudePhaseKernel<MagnitudeType::L2NORM, PhaseType::SIGNED>>();
k->configure(input1, input2, outputMag, outputPhase);
_kernel = std::move(k);
}
}
void CLGradRot::configure(const ICLTensor *input1, const ICLTensor *input2, ICLTensor *outputMag, ICLTensor *outputPhase, MagnitudeType mag_type)
{
auto k = arm_compute::cpp14::make_unique<CLMagnitudePhaseKernel>();
k->configure(input1, input2, outputMag, outputPhase, mag_type);
_kernel = std::move(k);
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::scratch_pad()
{
////////////////////////////////////////////////////////
// tensor _tst_in_img, _tst_out_img;
// _tst_in_img.allocator()->init(TensorInfo(5, 5, Format::U8));
// _tst_in_img.allocator()->allocate();
// _tst_out_img.allocator()->init(TensorInfo(5, 5, Format::U8));
// _tst_out_img.allocator()->allocate();
//
//
// std::cout << "Test:" << std::endl;
// Window tst_window;
// tst_window.use_tensor_dimensions(_tst_in_img.info());
// Iterator tst_it(&_tst_in_img, tst_window);
// execute_window_loop(tst_window, [&](const Coordinates & id)
// {
// *reinterpret_cast<unsigned char *>(tst_it.ptr()) = 'a' + id.x() + id.y()*5;
// std::cout << "(" << id.y() << "," << id.x() << ")=" << *reinterpret_cast<unsigned char *>(tst_it.ptr()) << "," ;
// if (id.x() == 4) std::cout << std::endl;
// },
// tst_it);
// std::cout << std::endl;
//
//// Iterator input(_input, win);
// const unsigned char *_tst_lft_top_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(-1, -1));
// const unsigned char *_tst_lft_mid_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(-1, 0));
// const unsigned char *_tst_lft_low_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(-1, 1));
//
// const unsigned char *_tst_mid_top_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(0, -1));
// const unsigned char *_tst_mid_mid_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(0, 0));
// const unsigned char *_tst_mid_low_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(0, 1));
//
// const unsigned char *_tst_rht_top_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(1, -1));
// const unsigned char *_tst_rht_mid_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(1, 0));
// const unsigned char *_tst_rht_low_ptr = _tst_in_img.buffer() + _tst_in_img.info()->offset_element_in_bytes(Coordinates(1, 1));
//
// // Configure kernel window
// constexpr unsigned int num_elems_processed_per_iteration = 1;
// constexpr unsigned int num_elems_read_per_iteration = 1;
// constexpr unsigned int num_elems_written_per_iteration = 1;
//#define BRD 1
//#define BRD_DEF true
// Window win = calculate_max_window(*_tst_in_img.info(), Steps(num_elems_processed_per_iteration), BRD_DEF/*border_undefined*/, BorderSize(BRD));
// AccessWindowHorizontal output_access(_tst_out_img.info(), 0, num_elems_written_per_iteration);
//
// update_window_and_padding(win,
// AccessWindowRectangle(_tst_in_img.info(), -BorderSize(BRD).left, -BorderSize(BRD).top, num_elems_read_per_iteration, 1),
// output_access);
//
// output_access.set_valid_region(win, _tst_in_img.info()->valid_region(), BRD_DEF/*border_undefined*/, BorderSize(BRD));
//
// Iterator innn(&_tst_in_img, win);
// Iterator outtt(&_tst_out_img, win);
// execute_window_loop(win, [&](const Coordinates & id)
// {
// std::cout << "lft_top:" << *reinterpret_cast<const unsigned char *>(_tst_lft_top_ptr + innn.offset()) << " mid_top:" << *reinterpret_cast<const unsigned char *>(_tst_mid_top_ptr + innn.offset()) << " rht_top:" << *reinterpret_cast<const unsigned char *>(_tst_rht_top_ptr + innn.offset()) << std::endl;
// std::cout << "lft_mid:" << *reinterpret_cast<const unsigned char *>(_tst_lft_mid_ptr + innn.offset()) << " mid_mid:" << *reinterpret_cast<const unsigned char *>(_tst_mid_mid_ptr + innn.offset()) << " rht_mid:" << *reinterpret_cast<const unsigned char *>(_tst_rht_mid_ptr + innn.offset()) << std::endl;
// std::cout << "lft_low:" << *reinterpret_cast<const unsigned char *>(_tst_lft_low_ptr + innn.offset()) << " mid_low:" << *reinterpret_cast<const unsigned char *>(_tst_mid_low_ptr + innn.offset()) << " rht_low:" << *reinterpret_cast<const unsigned char *>(_tst_rht_low_ptr + innn.offset()) << std::endl;
// std::cout << std::endl;
// },
// innn,outtt);
#ifdef ARM_COMPUTE_CL
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
CLKernelLibrary::get().init("/home/odroid/acl/ComputeLibrary-master/src/core/CL/cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault());
CLScheduler::get().init(cl::Context::getDefault(), cl::CommandQueue::getDefault());
// CLScheduler::get().default_init();
}
#endif
scale _scale_octave;
tensor _tst_in_img;
_tst_in_img.allocator()->init(TensorInfo(640, 480, Format::U8));
_tst_in_img.allocator()->allocate();
tensor _tst_out_img;
_tst_out_img.allocator()->init(TensorInfo(640*2, 480*2, Format::U8));
_tst_out_img.allocator()->allocate();
_scale_octave.configure(&_tst_in_img, &_tst_out_img, InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED);
_scale_octave.run();
CLScheduler::get().sync();
const std::string output_filename = "test.ppm";
// arm_compute::write_ppm<tensor>(*(_octaves.get() + 0), output_filename);
arm_compute::write_ppm<tensor>(_tst_out_img, output_filename);
return;
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::init(const cv::Mat & inimg)
{
cv::Size s = inimg.size();
unsigned int srcW = s.width, srcH = s.height;
// Index of the first octave.
_firstOctave = (SIFT_IMG_DBL)? -1 : 0;
// Number of layers in one octave; same as s in the paper.
_nLayers = SIFT_INTVLS;
// Number of Gaussian images in one octave.
_nGpyrLayers = _nLayers + 3;
// Number of DoG images in one octave.
_nDogLayers = _nLayers + 2;
// Number of octaves according to the size of image.
_nOctaves = (int) my_log2((float)std::min(srcW, srcH)) - 3 - _firstOctave -1; // 2 or 3, need further research
////////////////////////////////////////////////////////////////////////
// Init OpenCL
////////////////////////////////////////////////////////////////////////
// Init OpenCL if creating a CLTensor
#ifdef ARM_COMPUTE_CL
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
CLKernelLibrary::get().init("/home/odroid/acl/ComputeLibrary-master/src/core/CL/cl_kernels/", cl::Context::getDefault(), cl::Device::getDefault());
CLScheduler::get().init(cl::Context::getDefault(), cl::CommandQueue::getDefault());
}
#endif
////////////////////////////////////////////////////////////////////////
// Construct and allocate the input image tensor
////////////////////////////////////////////////////////////////////////
// Initialize the tensor dimensions and type:
_input_img.allocator()->init(TensorInfo(srcW, srcH, Format::U8));
// Allocate the input tensor:
_input_img.allocator()->allocate();
#ifdef ARM_COMPUTE_CL
// Map buffer if creating a CLTensor
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
_input_img.map();
}
#endif
// Fill the input tensor:
// Simplest way: create an iterator to iterate through each element of the input tensor:
Window input_window;
input_window.use_tensor_dimensions(_input_img.info());
// Create an iterator for the input image:
Iterator input_it(&_input_img, input_window);
// Iterate through the elements of src_data and copy them one by one to the input tensor:
uchar* src_data = inimg.data;
execute_window_loop(input_window, [&](const Coordinates & id)
{
*reinterpret_cast<unsigned char *>(input_it.ptr()) = src_data[id.y() * srcW + id.x()];
},
input_it);
#ifdef ARM_COMPUTE_CL
// Unmap buffer if creating a CLTensor
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
_input_img.unmap();
}
#endif
////////////////////////////////////////////////////////////////////////
// Construct and allocate the octave tensors
////////////////////////////////////////////////////////////////////////
// Octave tensors
_octaves = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves);
// Initialize the first octave width and height (up-sample x2):
unsigned int dstW = srcW << 1, dstH = srcH << 1;
// Initialize the tensor dimensions and type:
for(size_t i = 0; i < _nOctaves; i++){
// Initialize the tensor dimensions and type for this octave:
get_octave(i)->allocator()->init(TensorInfo(dstW, dstH, Format::U8));
// Allocate the octave tensor:
get_octave(i)->allocator()->allocate();
// Initialize the next octave width and height (down-sample x2):
dstW = dstW >> 1;
dstH = dstH >> 1;
}
////////////////////////////////////////////////////////////////////////
// Construct and allocate the Gaussian pyramid tensors
////////////////////////////////////////////////////////////////////////
_gaussian_coefs = arm_compute::cpp14::make_unique<int16_t[]>(conv_matrix_size * conv_matrix_size * _nGpyrLayers);
compute_gaussian_coefs();
// dump_gaussian_coefs();
_gpyr = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves * _nGpyrLayers);
// Initialize the first octave width and height (up-sample x2):
dstW = srcW << 1, dstH = srcH << 1;
// Initialize the tensor dimensions and type:
for (int i = 0; i < _nOctaves; i++){
for (int j = 0; j < _nGpyrLayers; j++){
// Initialize the tensor dimensions and type for this octave:
get_gaussian_pyramid(i,j)->allocator()->init(TensorInfo(dstW, dstH, Format::U8));
// Allocate the Gaussian pyramid tensor:
get_gaussian_pyramid(i,j)->allocator()->allocate();
}
// Initialize the next octave width and height (down-sample x2):
dstW = dstW >> 1;
dstH = dstH >> 1;
}
////////////////////////////////////////////////////////////////////////
// Construct and allocate the Difference of Gaussian pyramid tensors
////////////////////////////////////////////////////////////////////////
_dogPyr = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves * _nDogLayers);
// Initialize the first octave width and height (up-sample x2):
dstW = srcW << 1, dstH = srcH << 1;
// Initialize the tensor dimensions and type:
for (int i = 0; i < _nOctaves; i++){
for (int j = 0; j < _nDogLayers; j++){
// Initialize the tensor dimensions and type for this octave:
get_dog_pyramid(i,j)->allocator()->init(TensorInfo(dstW, dstH, Format::U8));
// Allocate the Difference of Gaussian pyramid tensor:
get_dog_pyramid(i,j)->allocator()->allocate();
}
// Initialize the next octave width and height (down-sample x2):
dstW = dstW >> 1;
dstH = dstH >> 1;
}
////////////////////////////////////////////////////////////////////////
// Construct and allocate the gradient and rotation pyramid tensors
////////////////////////////////////////////////////////////////////////
_grdPyr = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves * _nGpyrLayers);
_rotPyr = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves * _nGpyrLayers);
// Initialize the first octave width and height (up-sample x2):
dstW = srcW << 1, dstH = srcH << 1;
// Initialize the tensor dimensions and type:
for (int i = 0; i < _nOctaves; i++){
for (int j = 0; j < _nGpyrLayers; j++){
// Initialize the tensor dimensions and type for this octave:
get_grd_pyramid(i,j)->allocator()->init(TensorInfo(dstW, dstH, Format::S16));
// Allocate the Gaussian pyramid tensor:
get_grd_pyramid(i,j)->allocator()->allocate();
// Initialize the tensor dimensions and type for this octave:
get_rot_pyramid(i,j)->allocator()->init(TensorInfo(dstW, dstH, Format::U8));
// Allocate the Gaussian pyramid tensor:
get_rot_pyramid(i,j)->allocator()->allocate();
}
// Initialize the next octave width and height (down-sample x2):
dstW = dstW >> 1;
dstH = dstH >> 1;
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
tensor *EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::get_octave(size_t index) const
{
ARM_COMPUTE_ERROR_ON(index >= _nOctaves);
return (_octaves.get() + index);
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::dump_octave_image() const
{
ARM_COMPUTE_ERROR_ON(_octaves == nullptr);
for(size_t i = 0; i < _nOctaves; ++i){
tensor *tempTensor = get_octave(i);
const std::string output_filename = "octave_Octave-" + std::to_string(i) + ".ppm";
arm_compute::write_ppm<tensor>(*tempTensor, output_filename);
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::build_octaves()
{
// Octaves functions
std::unique_ptr<scale[]> _scale_octave{ nullptr };
// Octave scale
_scale_octave = arm_compute::cpp14::make_unique<scale[]>(_nOctaves);
// Configure scale on all octaves:
for(unsigned int i = 0; i < _nOctaves; i++){
// Configure horizontal kernel
_scale_octave[i].configure(&_input_img, get_octave(i), InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED);
}
// Run scale on all octaves:
for(unsigned int i = 0; i < _nOctaves; i++){
(_scale_octave.get() + i)->run();
// NEScheduler::get().multithread(_scale_octave.get() + i);
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
tensor *EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::get_gaussian_pyramid(size_t index_octave, size_t index_gpyr) const
{
ARM_COMPUTE_ERROR_ON(index_octave >= _nOctaves);
ARM_COMPUTE_ERROR_ON(index_gpyr >= _nGpyrLayers);
return ((_gpyr.get() + index_octave*_nGpyrLayers+index_gpyr));
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::dump_gaussian_pyramid_image() const
{
ARM_COMPUTE_ERROR_ON(_gpyr == nullptr);
for (int i = 0; i < _nOctaves; i++){
for (int j = 0; j < _nGpyrLayers; j++){
tensor *tempTensor = get_gaussian_pyramid(i,j);
const std::string output_filename = "gpyr-" + std::to_string(i) + "-" + std::to_string(j) + ".ppm";
arm_compute::write_ppm<tensor>(*tempTensor, output_filename);
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::get_gaussian_coefs(size_t index_gpyr, int16_t *gaussianFun) const
{
ARM_COMPUTE_ERROR_ON(index_gpyr >= _nGpyrLayers);
std::copy_n((_gaussian_coefs.get() + index_gpyr*conv_matrix_size*conv_matrix_size), conv_matrix_size*conv_matrix_size, gaussianFun);
return;
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::dump_gaussian_coefs() const
{
ARM_COMPUTE_ERROR_ON(_gaussian_coefs == nullptr);
for (int i = 0; i < _nGpyrLayers; i++){
std::cout << "GpyrLayer:" << i << std::endl;
for (int j = 0; j < conv_matrix_size; j++){
for (int k = 0; k < conv_matrix_size; k++){
std::cout << *(_gaussian_coefs.get() + i*conv_matrix_size*conv_matrix_size + j*conv_matrix_size + k) << "," ;
}
std::cout << std::endl;
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::compute_gaussian_coefs()
{
// Compute all sigmas for different layers
int nLayers = _nGpyrLayers - 3;
float sigma, sigma_pre;
float sigma0 = SIFT_SIGMA;
float k = powf(2.0f, 1.0f / nLayers);
std::vector<float> sig(_nGpyrLayers);
sigma_pre = SIFT_IMG_DBL? 2.0f * SIFT_INIT_SIGMA : SIFT_INIT_SIGMA;
sig[0] = sqrtf(sigma0 * sigma0 - sigma_pre * sigma_pre);
for (int i = 1; i < _nGpyrLayers; i ++){
sigma_pre = powf(k, (float)(i - 1)) * sigma0;
sigma = sigma_pre * k;
sig[i] = sqrtf(sigma * sigma - sigma_pre * sigma_pre);
}
for (int i = 0; i < _nGpyrLayers; i++){
// Compute Gaussian filter coefficients
float factor = SIFT_GAUSSIAN_FILTER_RADIUS;
int gR = (sig[i] * factor > 1.0f)? (int)ceilf(sig[i] * factor): 1;
int gW = gR * 2 + 1;
int l = 0;
for(int j = 0; j < gW; j++){
if (j >= ((gW-1)/2-(conv_matrix_size/2)) && j <= ((gW-1)/2+(conv_matrix_size/2))){
for(int k = 0; k < gW; k ++){
float tmp1 = sqrtf(j * j + k * k);
float tmp = (float)(((j - gR) * (j - gR) + (k - gR) * (k - gR)) / (sig[i] * sig[i]));
float tmp2 = expf(-0.5f * tmp) * (1 + tmp1/1000.0f);
if (k >= ((gW-1)/2-(conv_matrix_size/2)) && k <= ((gW-1)/2+(conv_matrix_size/2))){
*(_gaussian_coefs.get() + i*conv_matrix_size*conv_matrix_size + l) = static_cast<int16_t>(std::floor(tmp2*10.));
l++;
}
}
}
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::build_gaussian_pyramid()
{
// Gaussian pyramid functions
std::unique_ptr<conv[]> _conv_gpyr{ nullptr };
std::unique_ptr<scale[]> _scale_gpyr{ nullptr };
// Gaussian pyramid conv
_conv_gpyr = arm_compute::cpp14::make_unique<conv[]>(_nOctaves * _nGpyrLayers);
_scale_gpyr = arm_compute::cpp14::make_unique<scale[]>(_nOctaves);
// Configure Gaussian Convolution kernels on all Gaussian pyramid:
for(unsigned int i = 0; i < _nOctaves; i++){
for (unsigned int j = 0; j < _nGpyrLayers; j++){
int16_t gaussianFun[conv_matrix_size*conv_matrix_size];
get_gaussian_coefs(j, gaussianFun);
// Configure convolution kernel
if (i == 0 && j == 0){
_conv_gpyr[i*_nGpyrLayers+j].configure(get_octave(i), get_gaussian_pyramid(i,j), gaussianFun, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
} else if (i > 0 && j == 0){
_scale_gpyr[i-1].configure(get_gaussian_pyramid(i-1,_nLayers), get_gaussian_pyramid(i,j), InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED); // BILINEAR NEAREST_NEIGHBOR
} else {
_conv_gpyr[i*_nGpyrLayers+j].configure(get_gaussian_pyramid(i,j-1), get_gaussian_pyramid(i,j), gaussianFun, 0 /* Let arm_compute calculate the scale */, BorderMode::UNDEFINED);
}
}
}
// Run Gaussian Convolution kernels on all Gaussian pyramid:
for(unsigned int i = 0; i < _nOctaves; i++){
for (unsigned int j = 0; j < _nGpyrLayers; j++){
if (i == 0 && j == 0){
(_conv_gpyr.get() + i*_nGpyrLayers+j)->run();
} else if (i > 0 && j == 0){
(_scale_gpyr.get() + (i-1))->run();
} else {
(_conv_gpyr.get() + i*_nGpyrLayers+j)->run();
}
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::dump_dog_pyramid_image() const
{
ARM_COMPUTE_ERROR_ON(_dogPyr == nullptr);
for (int i = 0; i < _nOctaves; i++){
for (int j = 0; j < _nDogLayers; j++){
tensor *tempTensor = get_dog_pyramid(i,j);
const std::string output_filename = "dog_Octave-" + std::to_string(i) + "_Layer-" + std::to_string(j) + ".ppm";
arm_compute::write_ppm<tensor>(*tempTensor, output_filename);
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
tensor *EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::get_dog_pyramid(size_t index_octave, size_t index_gpyr) const
{
ARM_COMPUTE_ERROR_ON(index_octave >= _nOctaves);
ARM_COMPUTE_ERROR_ON(index_gpyr >= _nDogLayers);
return ((_dogPyr.get() + index_octave*_nDogLayers+index_gpyr));
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::build_dog_pyr()
{
// Arithmetic subtract functions
std::unique_ptr<absdif[]> _arithsub_dog_pyr{ nullptr };
// DoG pyramid arithmetic subtract
_arithsub_dog_pyr = arm_compute::cpp14::make_unique<absdif[]>(_nOctaves * _nDogLayers);
// Configure Subtraction kernels on all DoG pyramid:
for(unsigned int i = 0; i < _nOctaves; i++){
for (unsigned int j = 0; j < _nDogLayers; j++){
_arithsub_dog_pyr[i*_nDogLayers+j].configure(get_gaussian_pyramid(i,j+1), get_gaussian_pyramid(i,j), get_dog_pyramid(i,j));
}
}
// Run Subtraction kernels on all DoG pyramid:
for(unsigned int i = 0; i < _nOctaves; i++){
for (unsigned int j = 0; j < _nDogLayers; j++){
(_arithsub_dog_pyr.get() + i*_nDogLayers+j)->run();
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
tensor *EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::get_grd_pyramid(size_t index_octave, size_t index_gpyr) const
{
ARM_COMPUTE_ERROR_ON(index_octave >= _nOctaves);
ARM_COMPUTE_ERROR_ON(index_gpyr >= _nGpyrLayers);
return ((_grdPyr.get() + index_octave*_nGpyrLayers+index_gpyr));
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
tensor *EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::get_rot_pyramid(size_t index_octave, size_t index_gpyr) const
{
ARM_COMPUTE_ERROR_ON(index_octave >= _nOctaves);
ARM_COMPUTE_ERROR_ON(index_gpyr >= _nGpyrLayers);
return ((_rotPyr.get() + index_octave*_nGpyrLayers+index_gpyr));
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
void EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::build_grd_rot_pyr()
{
std::unique_ptr<tensor[]> _dx{ nullptr };
std::unique_ptr<tensor[]> _dy{ nullptr };
_dx = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves * _nGpyrLayers);
_dy = arm_compute::cpp14::make_unique<tensor[]>(_nOctaves * _nGpyrLayers);
// Initialize the first octave width and height (up-sample x2):
int dstW = get_gaussian_pyramid(0,0)->info()->dimension(0), dstH = get_gaussian_pyramid(0,0)->info()->dimension(1);
// Initialize the tensor dimensions and type:
for (int i = 0; i < _nOctaves; i++){
for (int j = 0; j < _nGpyrLayers; j++){
// Initialize the tensor dimensions and type for this octave:
(_dx.get() + i*_nGpyrLayers+j)->allocator()->init(TensorInfo(dstW, dstH, Format::S16));
// Allocate the Gaussian pyramid tensor:
(_dx.get() + i*_nGpyrLayers+j)->allocator()->allocate();
// Initialize the tensor dimensions and type for this octave:
(_dy.get() + i*_nGpyrLayers+j)->allocator()->init(TensorInfo(dstW, dstH, Format::S16));
// Allocate the Gaussian pyramid tensor:
(_dy.get() + i*_nGpyrLayers+j)->allocator()->allocate();
}
// Initialize the next octave width and height (down-sample x2):
dstW = dstW >> 1;
dstH = dstH >> 1;
}
// Derivative functions
std::unique_ptr<deriv[]> _driv_pyr{ nullptr };
// Derivative, x and y pyramid
_driv_pyr = arm_compute::cpp14::make_unique<deriv[]>(_nOctaves * _nGpyrLayers);
// Magnitude/Phase functions
std::unique_ptr<gradrot[]> _mag_phase_pyr{ nullptr };
// Magnitude/Phase pyramid
_mag_phase_pyr = arm_compute::cpp14::make_unique<gradrot[]>(_nOctaves * _nGpyrLayers);
for(unsigned int i = 0; i < _nOctaves; i++){
for (unsigned int j = 0; j < _nGpyrLayers; j++){
// Configure derivative kernels:
_driv_pyr[i*_nGpyrLayers+j].configure(get_gaussian_pyramid(i,j), (_dx.get() + i*_nGpyrLayers+j), (_dy.get() + i*_nGpyrLayers+j), BorderMode::UNDEFINED, 0 /*border_value*/);
// Configure magnitude/phase kernels:
#ifdef ARM_COMPUTE_CL
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
_mag_phase_pyr[i*_nGpyrLayers+j].configure((_dx.get() + i*_nGpyrLayers+j), (_dy.get() + i*_nGpyrLayers+j), get_grd_pyramid(i,j), get_rot_pyramid(i,j));
}
#endif
#ifndef ARM_COMPUTE_CL
if(std::is_same<typename std::decay<tensor>::type, Tensor>::value)
{
_mag_phase_pyr[i*_nGpyrLayers+j].configure((_dx.get() + i*_nGpyrLayers+j), (_dy.get() + i*_nGpyrLayers+j), get_grd_pyramid(i,j), get_rot_pyramid(i,j), false);
}
#endif
}
}
for(unsigned int i = 0; i < _nOctaves; i++){
for (unsigned int j = 0; j < _nGpyrLayers; j++){
// Run derivative kernels:
(_driv_pyr.get() + i*_nGpyrLayers+j)->run();
// Run magnitude kernels:
(_mag_phase_pyr.get() + i*_nGpyrLayers+j)->run();
}
}
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
bool EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::refine_local_extrema(SiftKeypoint & kpt)
{
int nGpyrLayers = _nDogLayers + 1;
int w, h;
int octave = kpt.octave;
int layer = kpt.layer;
int r = (int)kpt.ri;
int c = (int)kpt.ci;
int xs_i = 0, xr_i = 0, xc_i = 0;
float tmp_r, tmp_c, tmp_layer;
float xr = 0.0f, xc = 0.0f, xs = 0.0f;
float x_hat[3] = {xc, xr, xs};
float dx, dy, ds;
float dxx, dyy, dss, dxs, dys, dxy;
tmp_r = (float)r;
tmp_c = (float)c;
tmp_layer = (float)layer;
// Interpolation (x,y,sigma) 3D space to find sub-pixel accurate
// location of keypoints.
int i = 0;
for (; i < SIFT_MAX_INTERP_STEPS; i ++){
c += xc_i;
r += xr_i;
w = get_dog_pyramid(octave,layer)->info()->dimension(0);
h = get_dog_pyramid(octave,layer)->info()->dimension(1);
////////////////////////////////////////////////////////////////
// Current data
////////////////////////////////////////////////////////////////
// Current data, the left-top side of the window
const unsigned char *_curr_lft_top_ptr = get_dog_pyramid(octave,layer)->buffer() + (r-1) * get_dog_pyramid(octave,layer)->info()->dimension(0) + c - 1;
// Current data, the left-mid side of the window
const unsigned char *_curr_lft_mid_ptr = get_dog_pyramid(octave,layer)->buffer() + r * get_dog_pyramid(octave,layer)->info()->dimension(0) + c - 1;
// Current data, the left-low side of the window
const unsigned char *_curr_lft_low_ptr = get_dog_pyramid(octave,layer)->buffer() + (r+1) * get_dog_pyramid(octave,layer)->info()->dimension(0) + c - 1;
// Current data, the right-top side of the window
const unsigned char *_curr_rht_top_ptr = get_dog_pyramid(octave,layer)->buffer() + (r-1) * get_dog_pyramid(octave,layer)->info()->dimension(0) + c + 1;
// Current data, the right-mid side of the window
const unsigned char *_curr_rht_mid_ptr = get_dog_pyramid(octave,layer)->buffer() + r * get_dog_pyramid(octave,layer)->info()->dimension(0) + c + 1;
// Current data, the right-low side of the window
const unsigned char *_curr_rht_low_ptr = get_dog_pyramid(octave,layer)->buffer() + (r+1) * get_dog_pyramid(octave,layer)->info()->dimension(0) + c + 1;
// Current data, the mid-top side of the window
const unsigned char *_curr_mid_top_ptr = get_dog_pyramid(octave,layer)->buffer() + (r-1) * get_dog_pyramid(octave,layer)->info()->dimension(0) + c;
// Current data, the mid-mid side of the window
const unsigned char *_curr_mid_mid_ptr = get_dog_pyramid(octave,layer)->buffer() + r * get_dog_pyramid(octave,layer)->info()->dimension(0) + c;
// Current data, the mid-low side of the window
const unsigned char *_curr_mid_low_ptr = get_dog_pyramid(octave,layer)->buffer() + (r+1) * get_dog_pyramid(octave,layer)->info()->dimension(0) + c;
////////////////////////////////////////////////////////////////
// Low data
////////////////////////////////////////////////////////////////
// Low data, the left-top side of the window
const unsigned char *_low_lft_top_ptr = get_dog_pyramid(octave,layer-1)->buffer() + (r-1) * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c - 1;
// Low data, the left-mid side of the window
const unsigned char *_low_lft_mid_ptr = get_dog_pyramid(octave,layer-1)->buffer() + r * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c - 1;
// Low data, the left-low side of the window
const unsigned char *_low_lft_low_ptr = get_dog_pyramid(octave,layer-1)->buffer() + (r+1) * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c - 1;
// Low data, the right-top side of the window
const unsigned char *_low_rht_top_ptr = get_dog_pyramid(octave,layer-1)->buffer() + (r-1) * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c + 1;
// Low data, the right-mid side of the window
const unsigned char *_low_rht_mid_ptr = get_dog_pyramid(octave,layer-1)->buffer() + r * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c + 1;
// Low data, the right-low side of the window
const unsigned char *_low_rht_low_ptr = get_dog_pyramid(octave,layer-1)->buffer() + (r+1) * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c + 1;
// Low data, the mid-top side of the window
const unsigned char *_low_mid_top_ptr = get_dog_pyramid(octave,layer-1)->buffer() + (r-1) * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c;
// Low data, the mid-mid side of the window
const unsigned char *_low_mid_mid_ptr = get_dog_pyramid(octave,layer-1)->buffer() + r * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c;
// Low data, the mid-low side of the window
const unsigned char *_low_mid_low_ptr = get_dog_pyramid(octave,layer-1)->buffer() + (r+1) * get_dog_pyramid(octave,layer-1)->info()->dimension(0) + c;
////////////////////////////////////////////////////////////////
// High data
////////////////////////////////////////////////////////////////
// High data, the left-top side of the window
const unsigned char *_high_lft_top_ptr = get_dog_pyramid(octave,layer+1)->buffer() + (r-1) * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c - 1;
// High data, the left-mid side of the window
const unsigned char *_high_lft_mid_ptr = get_dog_pyramid(octave,layer+1)->buffer() + r * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c - 1;
// High data, the left-low side of the window
const unsigned char *_high_lft_low_ptr = get_dog_pyramid(octave,layer+1)->buffer() + (r+1) * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c - 1;
// High data, the right-top side of the window
const unsigned char *_high_rht_top_ptr = get_dog_pyramid(octave,layer+1)->buffer() + (r-1) * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c + 1;
// High data, the right-mid side of the window
const unsigned char *_high_rht_mid_ptr = get_dog_pyramid(octave,layer+1)->buffer() + r * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c + 1;
// High data, the right-low side of the window
const unsigned char *_high_rht_low_ptr = get_dog_pyramid(octave,layer+1)->buffer() + (r+1) * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c + 1;
// High data, the mid-top side of the window
const unsigned char *_high_mid_top_ptr = get_dog_pyramid(octave,layer+1)->buffer() + (r-1) * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c;
// High data, the mid-mid side of the window
const unsigned char *_high_mid_mid_ptr = get_dog_pyramid(octave,layer+1)->buffer() + r * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c;
// High data, the mid-low side of the window
const unsigned char *_high_mid_low_ptr = get_dog_pyramid(octave,layer+1)->buffer() + (r+1) * get_dog_pyramid(octave,layer+1)->info()->dimension(0) + c;
dx = ((*_curr_rht_mid_ptr)*1.0 - (*_curr_lft_mid_ptr)) * 0.5f;
dy = ((*_curr_mid_low_ptr)*1.0 - (*_curr_mid_top_ptr)) * 0.5f;
ds = ((*_high_mid_mid_ptr)*1.0 - (*_low_mid_mid_ptr)) * 0.5f;
float dD[3] = {-dx, -dy, -ds};
float v2 = 2.0f * (*_curr_mid_mid_ptr);
dxx = ((*_curr_rht_mid_ptr)*1.0 + (*_curr_lft_mid_ptr) - v2);
dyy = ((*_curr_mid_low_ptr)*1.0 + (*_curr_mid_top_ptr) - v2);
dss = ((*_high_mid_mid_ptr)*1.0 + (*_low_mid_mid_ptr) - v2);
dxy = ((*_curr_rht_low_ptr)*1.0 - (*_curr_lft_low_ptr) -
(*_curr_rht_top_ptr)*1.0 + (*_curr_lft_top_ptr)) * 0.25f;
dxs = ((*_high_rht_mid_ptr)*1.0 - (*_high_lft_mid_ptr) -
(*_low_rht_mid_ptr)*1.0 + (*_low_lft_mid_ptr)) * 0.25f;
dys = ((*_high_mid_low_ptr)*1.0 - (*_high_mid_top_ptr) -
(*_low_mid_low_ptr)*1.0 + (*_low_mid_top_ptr)) * 0.25f;
// The scale in two sides of the equation should cancel each other.
float H[3][3] = {{dxx, dxy, dxs},
{dxy, dyy, dys},
{dxs, dys, dss}};
float Hinvert[3][3];
float det;
// Matrix inversion
// INVERT_3X3 = DETERMINANT_3X3, then SCALE_ADJOINT_3X3;
// Using INVERT_3X3(Hinvert, det, H) is more convenient;
// but using separate ones, we can check det==0 easily.
float tmp;
DETERMINANT_3X3 (det, H);
if (fabsf(det) < (std::numeric_limits<float>::min)()){
break;
}
tmp = 1.0f / (det);
//INVERT_3X3(Hinvert, det, H);
SCALE_ADJOINT_3X3 (Hinvert, tmp, H);
MAT_DOT_VEC_3X3(x_hat, Hinvert, dD);
xs = x_hat[2];
xr = x_hat[1];
xc = x_hat[0];
// Update tmp data for keypoint update.
tmp_r = r + xr;
tmp_c = c + xc;
tmp_layer = layer + xs;
// Make sure there is room to move for next iteration.
xc_i= ((xc >= SIFT_KEYPOINT_SUBPiXEL_THR && c < w - 2) ? 1 : 0)
+ ((xc <= -SIFT_KEYPOINT_SUBPiXEL_THR && c > 1 ) ? -1 : 0);
xr_i= ((xr >= SIFT_KEYPOINT_SUBPiXEL_THR && r < h - 2) ? 1 : 0)
+ ((xr <= -SIFT_KEYPOINT_SUBPiXEL_THR && r > 1 ) ? -1 : 0);
if (xc_i == 0 && xr_i == 0 && xs_i == 0){
break;
}
}
// We MIGHT be able to remove the following two checking conditions.
// Condition 1
if (i >= SIFT_MAX_INTERP_STEPS)
return false;
// Condition 2.
if (fabsf(xc) >= 1.5 || fabsf(xr) >= 1.5 || fabsf(xs) >= 1.5)
return false;
// If (r, c, layer) is out of range, return false.
if (tmp_layer < 0 || tmp_layer > (nGpyrLayers - 1)
|| tmp_r < 0 || tmp_r > h - 1
|| tmp_c < 0 || tmp_c > w - 1)
return false;
{
// Current data, the mid-mid side of the window
const unsigned char *_curr_mid_mid_ptr = get_dog_pyramid(octave,layer)->buffer() + r * get_dog_pyramid(octave,layer)->info()->dimension(0) + c;
float value = (*_curr_mid_mid_ptr) + 0.5f * (dx * xc + dy * xr + ds * xs);
if (fabsf(value) < SIFT_CONTR_THR)
return false;
float trH = dxx + dyy;
float detH = dxx * dyy - dxy * dxy;
float response = (SIFT_CURV_THR + 1) * (SIFT_CURV_THR + 1) / (SIFT_CURV_THR);
if(detH <= 0 || (trH * trH / detH) >= response)
return false;
}
// Coordinates in the current layer.
kpt.ci = tmp_c;
kpt.ri = tmp_r;
kpt.layer_scale = SIFT_SIGMA * powf(2.0f, tmp_layer/SIFT_INTVLS);
int firstOctave = SIFT_IMG_DBL ? -1 : 0;
float norm = powf(2.0f, (float) (octave + firstOctave));
// Coordinates in the normalized format (compared to the original image).
kpt.c = tmp_c * norm;
kpt.r = tmp_r * norm;
kpt.rlayer = tmp_layer;
kpt.layer = layer;
// Formula: Scale = sigma0 * 2^octave * 2^(layer/S);
kpt.scale = kpt.layer_scale * norm;
return true;
}
template <typename tensor,class scale,class conv,unsigned int conv_matrix_size,class absdif,class deriv,class gradrot>
float EZSIFT<tensor,scale,conv,conv_matrix_size,absdif,deriv,gradrot>::compute_orientation_hist_with_gradient(
SiftKeypoint & kpt,
float * & hist)
{
int nBins = SIFT_ORI_HIST_BINS;
int octave = kpt.octave;
int layer = kpt.layer;
float kptr = kpt.ri;
float kptc = kpt.ci;
float kpt_scale = kpt.layer_scale;
int kptr_i = (int) (kptr + 0.5f);
int kptc_i = (int) (kptc + 0.5f);
float d_kptr = kptr - kptr_i;
float d_kptc = kptc - kptc_i;
float sigma = SIFT_ORI_SIG_FCTR * kpt_scale;
int win_radius = (int) (SIFT_ORI_RADIUS * kpt_scale);
int win_width = win_radius * 2 + 1;
float exp_factor = -1.0f / (2.0f * sigma * sigma);
int w = get_grd_pyramid(octave,layer)->info()->dimension(0);
int h = get_grd_pyramid(octave,layer)->info()->dimension(1);
int r, c;
float magni, angle, weight;
int bin;
float fbin; // float point bin
float *tmpHist = new float[nBins];
memset(tmpHist, 0, nBins * sizeof(float));
#ifdef ARM_COMPUTE_CL
// Map buffer if creating a CLTensor
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
get_grd_pyramid(octave,layer)->map();
get_rot_pyramid(octave,layer)->map();
}
#endif
for (int i = -win_radius; i <= win_radius; i ++){ // rows
r = kptr_i + i;
if (r <= 0 || r >= h-1) // Cannot calculate dy
continue;
for (int j = -win_radius; j <= win_radius; j ++){ // columns
c = kptc_i + j;
if (c <= 0 || c >= w-1)
continue;
magni = *reinterpret_cast<signed short *>(get_grd_pyramid(octave,layer)->buffer() + (r * get_grd_pyramid(octave,layer)->info()->dimension(0) + c)*sizeof(signed short))*1.0f;
angle = *reinterpret_cast<unsigned char *>(get_rot_pyramid(octave,layer)->buffer() + (r * get_rot_pyramid(octave,layer)->info()->dimension(0) + c)*sizeof(unsigned char))*1.0f;
// fbin = angle * nBins / _2PI;
fbin = angle * nBins / 360.0f;
weight = expf(((i-d_kptr) * (i-d_kptr) + (j-d_kptc) * (j-d_kptc)) * exp_factor);
#define SIFT_ORI_BILINEAR
#ifdef SIFT_ORI_BILINEAR
bin = (int) (fbin - 0.5f);
float d_fbin = fbin - 0.5f - bin;
float mw = weight * magni;
float dmw = d_fbin * mw;
tmpHist[(bin + nBins) % nBins] += mw - dmw;
tmpHist[(bin + 1) % nBins] += dmw;
#else
bin = (int) (fbin);
tmpHist[bin] += magni * weight;
#endif
}
}
#ifdef ARM_COMPUTE_CL
// Unmap buffer if creating a CLTensor
if(std::is_same<typename std::decay<tensor>::type, CLTensor>::value)
{
get_grd_pyramid(octave,layer)->unmap();
get_rot_pyramid(octave,layer)->unmap();
}
#endif
#define TMPHIST(idx) (idx < 0? tmpHist[0] : (idx >= nBins ? tmpHist[nBins - 1] : tmpHist[idx]))
#define USE_SMOOTH1 1
#if USE_SMOOTH1
// Smooth the histogram. Algorithm comes from OpenCV.
hist[0] = (tmpHist[0] + tmpHist[2]) * 1.0f / 16.0f +
(tmpHist[0] + tmpHist[1]) * 4.0f / 16.0f +
tmpHist[0] * 6.0f / 16.0f;
hist[1] = (tmpHist[0] + tmpHist[3]) * 1.0f / 16.0f +
(tmpHist[0] + tmpHist[2]) * 4.0f / 16.0f +
tmpHist[1] * 6.0f / 16.0f;
hist[nBins - 2] = (tmpHist[nBins - 4] + tmpHist[nBins - 1]) * 1.0f / 16.0f +
(tmpHist[nBins - 3] + tmpHist[nBins - 1]) * 4.0f / 16.0f +
tmpHist[nBins - 2] * 6.0f / 16.0f;
hist[nBins - 1] = (tmpHist[nBins - 3] + tmpHist[nBins - 1]) * 1.0f / 16.0f +
(tmpHist[nBins - 2] + tmpHist[nBins - 1]) * 4.0f / 16.0f +
tmpHist[nBins - 1] * 6.0f / 16.0f;
for(int i = 2; i < nBins - 2; i ++){
hist[i] = (tmpHist[i - 2] + tmpHist[i + 2]) * 1.0f / 16.0f +
(tmpHist[i - 1] + tmpHist[i + 1]) * 4.0f / 16.0f +
tmpHist[i] * 6.0f / 16.0f;
}
#else
// Yet another smooth function
// Algorithm comes from the vl_feat implementation.
for (int iter = 0; iter < 6; iter ++){
float prev = TMPHIST(nBins - 1);
float first = TMPHIST(0);
int i;
for (i = 0; i < nBins - 1; i ++){
float newh = (prev + TMPHIST(i) + TMPHIST(i + 1)) / 3.0f ;
prev = hist[i];
hist[i] = newh;
}
hist[i] = (prev + hist[i] + first) / 3.0f;
}
#endif
// Find the maximum item of the histogram
float maxitem = hist[0];
int max_i = 0;