-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFAST_Laplace.cpp
238 lines (204 loc) · 7.21 KB
/
FAST_Laplace.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
#include "FAST_Laplace.hpp"
namespace pk
{
FAST_Laplace::FAST_Laplace(float thresh, int oct, int inter, double _sigma)
{
threshold = thresh;
octaves = oct;
intervals = inter;
sigma.resize(intervals + 3);
border = 5;
recursion = 0;
/*
precompute Gaussian sigmas using the following formula:
\sigma_{total}^2 = \sigma_{i}^2 + \sigma_{i-1}^2
*/
sigma[0] = _sigma;
double p = pow( 2.0, 1.0 / intervals );
for (int inter = 1; inter < intervals + 3; ++inter )
{
double sig_prev = pow( p, inter - 1 ) * _sigma;
double sig_total = sig_prev * p;
sigma[inter] = sqrt( sig_total * sig_total - sig_prev * sig_prev );
}
}
FAST_Laplace::FAST_Laplace(const cv::Mat& img, float thresh, int oct, int inter, double _sigma)
{
threshold = thresh;
octaves = oct;
intervals = inter;
sigma.resize(intervals + 3);
border = 5;
recursion= 0;
/*
precompute Gaussian sigmas using the following formula:
\sigma_{total}^2 = \sigma_{i}^2 + \sigma_{i-1}^2
*/
sigma[0] = _sigma;
double p = pow( 2.0, 1.0 / intervals );
for (int inter = 1; inter < intervals + 3; ++inter )
{
double sig_prev = pow( p, inter - 1 ) * _sigma;
double sig_total = sig_prev * p;
sigma[inter] = sqrt( sig_total * sig_total - sig_prev * sig_prev );
}
buildGaussPyr(img);
}
void FAST_Laplace::initialise(const cv::Mat& img)
{
buildGaussPyr(img);
}
void FAST_Laplace::detector(std::vector<cv::KeyPoint>& keypoints)
{
std::vector< std::vector< std::vector<cv::KeyPoint> > > fastPoints;
fastPoints.resize(octaves);
for (int oct = 0; oct < octaves; ++oct )
fastPoints[oct].resize(intervals + 1);
for (int oct = 0; oct < octaves; ++oct )
for (int inter = 0; inter < intervals + 1; ++inter )
{
cv::Mat img8bit, temp;
temp = gaussPyramid[oct][inter] * 255;
temp.convertTo(img8bit, CV_8UC1);
double size = sigma[0] * pow(2.0, oct + (double)inter / intervals);
cv::FAST(img8bit, fastPoints[oct][inter], this->threshold);
for (auto& point : fastPoints[oct][inter])
if (isScaleExtremum(point.pt.y, point.pt.x, oct, inter))
{
point.pt.x *= pow( 2.0, oct-1 );
point.pt.y *= pow( 2.0, oct-1 );
point.size = size * 2;
point.octave = oct;
point.class_id = inter;
keypoints.push_back(point);
}
}
return;
}
void FAST_Laplace::detector(cv::Mat& img,
std::vector<cv::KeyPoint>& keypoints)
{
buildGaussPyr(img);
std::vector< std::vector< std::vector<cv::KeyPoint> > > fastPoints;
fastPoints.resize(octaves);
for (int oct = 0; oct < octaves; ++oct )
fastPoints[oct].resize(intervals + 1);
for (int oct = 0; oct < octaves; ++oct )
for (int inter = 0; inter < intervals + 1; ++inter )
{
cv::Mat img8bit, temp;
temp = gaussPyramid[oct][inter] * 255;
temp.convertTo(img8bit, CV_8UC1);
double size = sigma[0] * pow(2.0, oct + (double)inter / intervals);
cv::FAST(img8bit, fastPoints[oct][inter], this->threshold);
for (auto& point : fastPoints[oct][inter])
if (isScaleExtremum(point.pt.y, point.pt.x, oct, inter))
{
point.pt.x *= pow( 2.0, oct-1 );
point.pt.y *= pow( 2.0, oct-1 );
point.size = size * 2;
point.octave = oct;
point.class_id = inter;
keypoints.push_back(point);
}
}
return;
}
/*
Converts an image to 8-bit grayscale and Gaussian-smooths it. The image is
optionally doubled in size prior to smoothing.
@param img input image
@param img_dbl if true, image is doubled in size prior to smoothing
@param sig total std of Gaussian smoothing
*/
void FAST_Laplace::createBaseImage(const cv::Mat& src, cv::Mat& dst,
const bool img_dbl, const double sig)
{
cv::Mat grey, dbl;
double sig_diff;
cv::Mat fimg;
src.convertTo( fimg, CV_64FC1 );
if (src.channels() == 3)
cv::cvtColor(fimg * 1.0 / 255, grey, CV_BGR2GRAY);
else if (src.channels() == 1)
grey = fimg * 1.0 / 255;
else
{
std::cout << "not an rgb or greyscale image\n";
exit (EXIT_FAILURE);
}
if (img_dbl)
{
sig_diff = sqrt(sig * sig - 0.5 * 0.5 * 4);
cv::resize(grey, dbl, cv::Size(), 2, 2, CV_INTER_CUBIC);
cv::GaussianBlur(dbl, dbl, cv::Size(), sig_diff);
dbl.convertTo(dst, CV_32FC1);
return;
}
else
{
sig_diff = sqrt(sig * sig - 0.5 * 0.5);
cv::GaussianBlur(grey, grey, cv::Size(), sig_diff);
grey.convertTo(dst, CV_32FC1);
return;
}
}
/*
Builds Gaussian scale space pyramid from an image
@param img base image of the pyramid
*/
void FAST_Laplace::buildGaussPyr(const cv::Mat& img)
{
gaussPyramid.resize(octaves);
for (int oct = 0; oct < octaves; ++oct )
gaussPyramid[oct].resize(intervals + 3);
for (int oct = 0; oct < octaves; ++oct )
for (int inter = 0; inter < intervals + 3; ++inter )
{
if ( oct == 0 && inter == 0 )
createBaseImage(img, gaussPyramid[oct][inter], true, sigma[0]);
/* base of new octave is halved image from end of previous octave */
else if ( inter == 0 )
cv::resize(gaussPyramid[oct-1][intervals], gaussPyramid[oct][inter], cv::Size(), 0.5, 0.5, CV_INTER_NN);
/* blur the current octave's last image to create the next one */
else{std::cout << sigma[inter] << std::endl;
cv::GaussianBlur(gaussPyramid[oct][inter-1], gaussPyramid[oct][inter], cv::Size(), sigma[inter]);
}
}
return;
}
bool FAST_Laplace::isScaleExtremum(int row, int col, int octave, int interval)
{
double size = sigma[0] * pow(2.0, octave + (double)interval / intervals);
//double size = sigma[interval];// * pow(2.0, octave);
double val = getPixelLaplacian(gaussPyramid[octave][interval + 1], row, col) * size * size;
if (std::abs(val) < 1e-1) return false;
if (val > 0)
for (int scale = -1; scale <= 1; ++ scale)
{
double size = sigma[0] * pow(2.0, octave + (double)(interval + scale) / intervals);
// double size = sigma[interval];
double neighbour = getPixelLaplacian(gaussPyramid[octave][interval + scale + 1], row, col) * size * size;
if (val < neighbour)
return false;
}
else
for (int scale = -1; scale <= 1; ++ scale)
{
double size = sigma[0] * pow(2.0, octave + (double)(interval + scale) / intervals);
// double size = sigma[interval];
double neighbour = getPixelLaplacian(gaussPyramid[octave][interval + scale + 1], row, col) * size * size;
if (val > neighbour)
return false;
}
return true;
}
double FAST_Laplace::getPixelLaplacian(const cv::Mat& img, int row, int col)
{
return ( img.at<float>(row - 1, col) +
img.at<float>(row, col - 1) +
img.at<float>(row + 1, col) +
img.at<float>(row, col + 1) -
img.at<float>(row, col) * 4 );
}
} /* End Namespace pk */