-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.cpp
294 lines (247 loc) · 9.74 KB
/
simulation.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
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <boost/random.hpp>
#include <boost/format.hpp>
#include "KAZE.h"
#include "config.h"
#include "utils.h"
cv::Mat translate(const cv::Mat& img, cv::Mat& dst, double angle, double scale, int noise_power, double blur_sigma)
{
const int width_rot = scale * std::abs(img.rows * std::cos(angle) + img.cols * std::sin(angle));
const int height_rot = scale * std::abs(img.rows * std::sin(angle) + img.cols * std::cos(angle));
cv::Mat m = cv::getRotationMatrix2D(cv::Point2f(img.cols / 2.0, img.rows / 2.0), angle, scale);
m.at<double>(0, 2) += img.cols - img.cols / 2.0;
m.at<double>(1, 2) += img.rows - img.rows / 2.0;
cv::warpAffine(img, dst, m, cv::Size(img.cols * 2, img.rows * 2));
//white noise
if(noise_power)
{
boost::mt19937 rng;
for(int y = 0; y < dst.rows; ++y)
for(int x = 0; x < dst.cols; ++x)
{
const int c = dst.at<unsigned char>(y, x) +
static_cast<int>(rand() % (2 * noise_power) - noise_power);
dst.at<unsigned char>(y, x) = std::min(255, std::max(0, c));
}
}
if(0 < blur_sigma)
{
cv::GaussianBlur(dst, dst, cv::Size(0, 0), blur_sigma);
}
return m;
}
toptions get_default_toptions(int w, int h)
{
toptions options;
options.soffset = DEFAULT_SCALE_OFFSET;
options.omax = DEFAULT_OCTAVE_MAX;
options.nsublevels = DEFAULT_NSUBLEVELS;
options.dthreshold = DEFAULT_DETECTOR_THRESHOLD;
options.diffusivity = DEFAULT_DIFFUSIVITY_TYPE;
options.descriptor = DEFAULT_DESCRIPTOR_MODE;
options.upright = DEFAULT_UPRIGHT;
options.extended = DEFAULT_EXTENDED;
options.sderivatives = DEFAULT_SIGMA_SMOOTHING_DERIVATIVES;
options.save_scale_space = DEFAULT_SAVE_SCALE_SPACE;
options.show_results = DEFAULT_SHOW_RESULTS;
options.save_keypoints = DEFAULT_SAVE_KEYPOINTS;
options.verbosity = DEFAULT_VERBOSITY;
options.img_width = w;
options.img_height = h;
return options;
}
const float DRATIO = .6; // NNDR Matching value
float Compute_Descriptor_Distance(Ipoint &p1, Ipoint &p2, float best)
{
float dist = 0.0;
int dsize = p1.descriptor_size;
for(int i = 0; i < dsize; i++ )
{
dist += pow(p1.descriptor[i] - p2.descriptor[i],2);
if( dist > best )
break;
}
return dist;
}
unsigned int Matching_Descriptor(std::vector<Ipoint> &ipts1, std::vector<Ipoint> &ipts2, std::vector<int> &indexes )
{
float dist = 0.0, mind = 0.0, last_mind = 0.0;
int mindex = -1;
unsigned int correct_matches = 0;
bool first = false;
indexes.clear();
for( unsigned int i = 0; i < ipts1.size(); i++ )
{
mind = 10000.0;
last_mind = 10000.0;
mindex = -1;
first = false;
for( unsigned int j = 0; j < ipts2.size(); j++ )
{
dist = Compute_Descriptor_Distance(ipts1[i],ipts2[j],1000.0);
if( dist < mind )
{
if( first == false )
{ mind = dist;
mindex = j;
first = true;
}
else
{
last_mind = mind;
mind = dist;
mindex = j;
}
}
else if( dist < last_mind )
{
last_mind = dist;
}
}
if( mind < DRATIO*last_mind )
{
indexes.push_back(i);
indexes.push_back(mindex);
correct_matches++;
}
}
return correct_matches;
}
std::vector<cv::KeyPoint> convert_Ipoint_Keypoint(const std::vector<Ipoint>& kps)
{
std::vector<cv::KeyPoint> result;
for(std::vector<Ipoint>::const_iterator p = kps.begin(); p != kps.end(); ++p)
result.push_back(cv::KeyPoint(p->xf, p->yf, p->scale));
return result;
}
std::vector<cv::DMatch> convert_KAZEMatch_DMatch(const std::vector<int>& match)
{
std::vector<cv::DMatch> result;
for(int i = 0; i < match.size(); i += 2)
result.push_back(cv::DMatch(match[i], match[i + 1], 0)); //distanceはdummy
return result;
}
void evaluation(const std::vector<cv::KeyPoint>& kp1, const std::vector<cv::KeyPoint>& kp2, const std::vector<cv::DMatch>& match, const cv::Mat& trans_mat)
{
const double permit_err = 3.0;
int good_cnt = 0, bad_cnt = 0;
for(std::vector<cv::DMatch>::const_iterator p = match.begin(); p != match.end(); ++p)
{
const cv::KeyPoint src = kp1[p->queryIdx];
const cv::KeyPoint dst = kp2[p->trainIdx];
const double x = trans_mat.at<double>(0, 0) * src.pt.x + trans_mat.at<double>(0, 1) * src.pt.y + trans_mat.at<double>(0, 2);
const double y = trans_mat.at<double>(1, 0) * src.pt.x + trans_mat.at<double>(1, 1) * src.pt.y + trans_mat.at<double>(1, 2);
const double dx = x - dst.pt.x;
const double dy = y - dst.pt.y;
if(dx * dx + dy * dy < permit_err * permit_err)
++good_cnt;
else
++bad_cnt;
}
std::cout << "total: " << match.size();
std::cout << boost::format(", correct: %d (%d%%)") % good_cnt % (100 * good_cnt / match.size());
std::cout << boost::format(", miss: %d (%d%%)") % bad_cnt % (100 * bad_cnt / match.size());
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
if(argc == 1)
{
std::cerr << "usage:" << std::endl;
std::cerr << argv[0] << " imgfile" << std::endl;
return -1;
}
//load source image as grayscale
cv::Mat img_src = cv::imread(argv[1], 0);
cv::Mat img_src_32, img_dst, img_dst_32, img_match_sift, img_match_kaze;
//initialize
img_src.convertTo(img_src_32, CV_32F, 1.0/255.0,0);
//initialize SIFT feature detector descriptor
cv::SiftFeatureDetector sift_detector;
cv::SiftDescriptorExtractor sift_extractor;
cv::BruteForceMatcher<cv::L2<float> > matcher;
//SIFT feature extraction from source image
std::vector<cv::KeyPoint> sift_kp_src;
sift_detector.detect(img_src, sift_kp_src);
cv::Mat sift_src;
sift_extractor.compute(img_src, sift_kp_src, sift_src);
//initialize KAZE feature detector descriptor
toptions kazeopt_src = get_default_toptions(img_src.cols, img_src.rows);
KAZE kazeevol_src(kazeopt_src);
//KAZE feature extraction from source image
kazeevol_src.Create_Nonlinear_Scale_Space(img_src_32);
std::vector<Ipoint> kaze_kp_src;
kazeevol_src.Feature_Detection(kaze_kp_src);
kazeevol_src.Feature_Description(kaze_kp_src);
translate(img_src, img_dst, 0, 1.0, 0, 0);
toptions kazeopt_dst = get_default_toptions(img_dst.cols, img_dst.rows);
KAZE kazeevol_dst(kazeopt_dst);
double rot = 0.0;
double scale = 1.0;
int noise = 0;
double blur = 0;
int key;
while(key = cv::waitKey(10))
{
bool show = true;
if(key == 65362) scale = std::min(scale + 0.1, 1.5);
else if(key == 65364) scale = std::max(scale - 0.1, 0.5);
else if(key == 65361) rot += 5;
else if(key == 65363) rot -= 5;
else if(key == 'z') noise = std::min(noise + 5, 50);
else if(key == 'x') noise = std::max(noise - 5, 0);
else if(key == 'c') blur = std::min(blur + 0.5, 20.0);
else if(key == 'v') blur = std::max(blur - 0.5, 0.0);
else show = false;
if(show)
{
std::cout << "**********" << std::endl;
std::cout << "scale factor : " << scale << std::endl;
std::cout << "rotation : " << rot << " degree" << std::endl;
std::cout << "white noise max power : " << noise << std::endl;
std::cout << "gaussian blur sigma : " << blur << std::endl;
}
const cv::Mat m = translate(img_src, img_dst, rot, scale, noise, blur);
//feature extraction from destination image
std::vector<cv::KeyPoint> sift_kp_dst;
sift_detector.detect(img_dst, sift_kp_dst);
cv::Mat sift_dst;
sift_extractor.compute(img_dst, sift_kp_dst, sift_dst);
//match SIFT
cv::vector<cv::DMatch> corr;
matcher.match(sift_src, sift_dst, corr);
cv::drawMatches(img_src, sift_kp_src, img_dst, sift_kp_dst, corr, img_match_sift);
cv::putText(img_match_sift, "SIFT", cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 3);
//initialize KAZE feature detector descriptor
img_dst.convertTo(img_dst_32, CV_32F, 1.0/255.0,0);
//KAZE feature extraction from source image
kazeevol_dst.Create_Nonlinear_Scale_Space(img_dst_32);
std::vector<Ipoint> kaze_kp_dst;
kazeevol_dst.Feature_Detection(kaze_kp_dst);
kazeevol_dst.Feature_Description(kaze_kp_dst);
//match KAZE
std::vector<int> kaze_corr;
Matching_Descriptor(kaze_kp_src, kaze_kp_dst, kaze_corr);
//draw KAZEmatch
const std::vector<cv::DMatch> kaze_corr2 = convert_KAZEMatch_DMatch(kaze_corr);
const std::vector<cv::KeyPoint> kaze_kp1 = convert_Ipoint_Keypoint(kaze_kp_src);
const std::vector<cv::KeyPoint> kaze_kp2 = convert_Ipoint_Keypoint(kaze_kp_dst);
cv::drawMatches(img_src, kaze_kp1, img_dst, kaze_kp2, kaze_corr2, img_match_kaze);
cv::putText(img_match_kaze, "KAZE", cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 3);
cv::Mat result;
cv::Mat src[] = {img_match_sift, img_match_kaze};
cv::vconcat(src, 2, result);
cv::imshow("hoge", result);
if(show)
{
std::cout << "SIFT:";
evaluation(sift_kp_src, sift_kp_dst, corr, m);
std::cout << "KAZE:";
evaluation(kaze_kp1, kaze_kp2, kaze_corr2, m);
}
}
}