-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathPatchSIFT.h
155 lines (136 loc) · 6.19 KB
/
PatchSIFT.h
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
/**
* @copyright 2021 Xoan Iago Suarez Canosa. All rights reserved.
* Constact: [email protected]
* Software developed in the PhD: Low-level vision for resource-limited devices
*/
#ifndef EFFICIENT_DESCRIPTORS_PATCHSIFT_H_
#define EFFICIENT_DESCRIPTORS_PATCHSIFT_H_
#include <vector>
#include <opencv2/opencv.hpp>
namespace upm {
/**
* Implementation of the SIFT descriptor:
* Lowe, D. G. (2004). Distinctive image features from scale-invariant keypoints.
* International journal of computer vision, 60(2), 91-110.
*
* This code does not include the detection part. Instead, it is intended to be used with any
*/
class PatchSIFT : public cv::Feature2D {
public:
explicit PatchSIFT(float kp_scale, // Default 1 / 6.0
float cropping_scale,
double sigma = 1.6,
bool step1_pyramid = true,
bool step4_gaussian_weight = true,
bool step6_trilinear_interp = true,
bool step7_l2_normalization = true,
bool step8_trim_bigvals = true,
bool step9_uchar_scaling = true);
static cv::Ptr<cv::Feature2D> create(float kp_scale, // Default 1 / 6.0
float cropping_scale,
double sigma = 1.6,
bool step1_pyramid = true,
bool step4_gaussian_weight = true,
bool step6_trilinear_interp = true,
bool step7_l2_normalization = true,
bool step8_trim_bigvals = true,
bool step9_uchar_scaling = true);
//! returns the descriptor size in floats (128)
int descriptorSize() const override;
int descriptorType() const override { return CV_32FC1; }
int defaultNorm() const override { return cv::NORM_L2; }
void compute(cv::InputArray image,
CV_OUT CV_IN_OUT std::vector<cv::KeyPoint> &keypoints,
cv::OutputArray descriptors) override;
void describeKeypoint(const cv::Mat &image, cv::Mat &descriptor);
void calcSIFTDescriptor(const cv::Mat &img, cv::Mat descriptor);
inline const cv::Size &GetPatchSize() const { return patch_size; }
inline const float &GetCroppingScale() const { return cropping_scale; }
static void rectifyPatch(const cv::Mat &image,
const cv::KeyPoint &kp,
const cv::Size &patchSize,
cv::Mat &patch,
float scale_factor);
protected:
// The scale of the keypoint relative to the patch size. If will be used to modify the variance
// of the gaussian that give more weight to the pixels in the center
float kp_scale;
double sigma;
// default number of bins per histogram in descriptor array
int ori_bins = 8;
// default width of descriptor histogram array
int r_bins = 4;
// default width of descriptor histogram array
int c_bins = 4;
// threshold on magnitude of elements of descriptor vector
float magnitude_th = 0.2f;
// factor used to convert floating-point descriptor to unsigned char
float int_descr_factor = 512.f;
// Steps of the algorithm we can configure
bool step1_pyramid = true;
bool step4_gaussian_weight = true;
bool step6_trilinear_interp = true;
bool step7_l2_normalization = true;
bool step8_trim_bigvals = true;
bool step9_uchar_scaling = true;
cv::Size patch_size{32, 32};
float cropping_scale = 1.0f;
};
/******************************* Defs and macros *****************************/
// assumed gaussian blur for input image
static const float SIFT_INIT_SIGMA = 0.5f;
// determines the size of a single descriptor orientation histogram
static const float SIFT_DESCR_SCL_FCTR = 3.f;
class RootPatchSIFT : public PatchSIFT {
public:
RootPatchSIFT(float kp_scale, // Default 1 / 6.0
float cropping_scale,
double sigma = 1.6,
bool step1_pyramid = true,
bool step4_gaussian_weight = true,
bool step6_trilinear_interp = true,
bool step7_l2_normalization = true,
bool step8_trim_bigvals = true,
bool step9_uchar_scaling = true) :
PatchSIFT(kp_scale,
cropping_scale,
sigma,
step1_pyramid,
step4_gaussian_weight,
step6_trilinear_interp,
step7_l2_normalization,
step8_trim_bigvals,
step9_uchar_scaling) {
}
void compute(cv::InputArray image,
CV_OUT CV_IN_OUT std::vector<cv::KeyPoint> &keypoints,
cv::OutputArray _descriptors) override {
PatchSIFT::compute(image, keypoints, _descriptors);
cv::Mat descriptors = _descriptors.getMat();
for (int i = 0; i < descriptors.rows; i++) {
double norm = cv::norm(descriptors.row(i), cv::NORM_L1);
cv::sqrt(descriptors.row(i) / norm, descriptors.row(i));
}
}
static cv::Ptr<cv::Feature2D> create(float kp_scale, // Default 1 / 6.0
float cropping_scale,
double sigma = 1.6,
bool step1_pyramid = true,
bool step4_gaussian_weight = true,
bool step6_trilinear_interp = true,
bool step7_l2_normalization = true,
bool step8_trim_bigvals = true,
bool step9_uchar_scaling = true) {
return cv::makePtr<RootPatchSIFT>(kp_scale,
cropping_scale,
sigma,
step1_pyramid,
step4_gaussian_weight,
step6_trilinear_interp,
step7_l2_normalization,
step8_trim_bigvals,
step9_uchar_scaling);
}
};
}
#endif // EFFICIENT_DESCRIPTORS_PATCHSIFT_H_