forked from nsalminen/OpenCV-Features-Comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithmEstimation.cpp
230 lines (180 loc) · 8.26 KB
/
AlgorithmEstimation.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
#include "AlgorithmEstimation.hpp"
#include "Util.hpp"
bool computeMatchesDistanceStatistics(const Matches& matches, float& meanDistance, float& stdDev)
{
if (matches.empty())
return false;
std::vector<float> distances (matches.size());
for (size_t i = 0; i < matches.size(); i++)
distances[i] = matches[i].distance;
cv::Scalar mean, dev;
meanStdDev (distances, mean, dev);
meanDistance = static_cast<float> (mean.val[0]);
stdDev = static_cast<float> (dev.val[0]);
return false;
}
float distance (const cv::Point2f& a, const cv::Point2f& b)
{
return sqrt((a - b).dot(a - b));
}
cv::Scalar computeReprojectionError (const Keypoints& source, const Keypoints& query, const Matches& matches, const cv::Mat& homography);
bool performEstimation (const FeatureAlgorithm& alg, const ImageTransformation& transformation, const cv::Mat& srcImage, const Keypoints& srcKeypoints,
const Descriptors& srcDescriptors, std::vector<FrameMatchingStatistics>& stat)
{
std::vector<float> x = transformation.getX();
stat.resize (x.size());
const int count = x.size();
Keypoints resKeypoints;
Descriptors resDescriptors;
// To convert ticks to milliseconds
const double toMsMul = 1000. / cv::getTickFrequency();
#pragma omp parallel for private (resKpReal, resDesc, matches) schedule(dynamic, 10)
for (int i = 0; i < count; i++)
{
//std::cout << "Threads: " << omp_get_num_threads() << std::endl;
const float arg = x[i];
cv::Mat transformedImage;
transformation.transform (arg, srcImage, transformedImage);
const cv::Mat expectedHomography = transformation.getHomography (arg, srcImage);
if (SAVE_IMAGES)
imwrite (R"(C:\TransformedImages\)" + transformation.name + " (" + std::to_string(arg) + ").png", transformedImage);
int64 start, end;
const bool success = alg.extractFeatures (transformedImage, resKeypoints, resDescriptors, start, end);
if (!success || resKeypoints.empty())
{
std::cout << "Skipped for: " << alg.name << "\t" << transformation.name << "\t" << arg << std::endl;
continue;
}
Matches matches;
alg.matchFeatures (srcDescriptors, resDescriptors, matches);
// Calculate source points and source points in expected homography's frame.
std::vector<cv::Point2f> sourcePoints, sourcePointsInFrame;
cv::KeyPoint::convert (srcKeypoints, sourcePoints);
perspectiveTransform (sourcePoints, sourcePointsInFrame, expectedHomography);
// Count visible features and correct matches.
const int visibleFeatures = CountVisibleFeatures (sourcePoints, transformedImage.cols, transformedImage.rows);
const int correctMatches = CountCorrectMatches (matches, sourcePointsInFrame, resKeypoints);
FrameMatchingStatistics& s = stat[i];
// Initialize required fields
s.isValid = !resKeypoints.empty();
s.argumentValue = arg;
s.alg = alg.name;
s.trans = transformation.name;
// Fill in the remaining statistics.
s.totalKeypoints += resKeypoints.size();
s.consumedTimeMs += (end - start) * toMsMul;
s.precision += correctMatches / static_cast<float>(matches.size());
s.recall += correctMatches / static_cast<float>(visibleFeatures);
}
return true;
}
bool performEstimation (const FeatureAlgorithm& alg, const ImageTransformation& transformation, ImageData src, std::vector<FrameMatchingStatistics>& stat)
{
std::vector<float> x = transformation.getX();
stat.resize (x.size());
const int count = x.size();
ImageData res;
// To convert ticks to milliseconds
const double toMsMul = 1000. / cv::getTickFrequency();
#pragma omp parallel for private (resKpReal, resDesc, matches) schedule(dynamic, 10)
for (int i = 0; i < count; i++)
{
//std::cout << "Threads: " << omp_get_num_threads() << std::endl;
const float arg = x[i];
cv::Mat transformedImage;
transformation.transform (arg, src.imageGrey, transformedImage);
const cv::Mat expectedHomography = transformation.getHomography (arg, src.imageGrey);
if (SAVE_IMAGES)
imwrite (R"(C:\TransformedImages\)" + src.image + " with " + transformation.name + " (" + std::to_string(arg) + ").png", transformedImage);
int64 start, end;
const bool success = alg.extractFeatures (transformedImage, res.keypoints, res.descriptors, start, end);
if (!success || res.keypoints.empty())
{
std::cout << "Skipped for: " << alg.name << "\t" << transformation.name << "\t" << arg << std::endl;
continue;
}
Matches matches;
alg.matchFeatures (src.descriptors, res.descriptors, matches);
if (SAVE_IMAGES)
{
cv::Mat outPic;
drawMatches (src.imageOriginal, src.keypoints, transformedImage, res.keypoints, matches, outPic);
imwrite (R"(C:\TransformedImages\)" + src.image + " matches with " + transformation.name + " (" + std::to_string(arg) + ").png", transformedImage);
continue;
}
// Calculate source points and source points in expected homography's frame.
std::vector<cv::Point2f> sourcePoints, sourcePointsInFrame;
cv::KeyPoint::convert (src.keypoints, sourcePoints);
perspectiveTransform (sourcePoints, sourcePointsInFrame, expectedHomography);
// Count visible features and correct matches.
const int visibleFeatures = CountVisibleFeatures (sourcePoints, transformedImage.cols, transformedImage.rows);
const int correctMatches = CountCorrectMatches (matches, sourcePointsInFrame, res.keypoints);
FrameMatchingStatistics& s = stat[i];
// Initialize required fields
s.isValid = !res.keypoints.empty();
s.argumentValue = arg;
s.alg = alg.name;
s.trans = transformation.name;
// Fill in the remaining statistics.
s.totalKeypoints += res.keypoints.size();
s.consumedTimeMs += (end - start) * toMsMul;
s.precision += correctMatches / static_cast<float>(matches.size());
s.recall += correctMatches / static_cast<float>(visibleFeatures);
}
return true;
}
int CountVisibleFeatures (std::vector<cv::Point2f>& sourcePoints, int imageCols, int imageRows)
{
int visibleFeatures = 0;
for (const auto& point : sourcePoints)
{
if (point.x <= 0 ||
point.y <= 0 ||
point.x >= imageCols ||
point.y >= imageRows)
continue;
visibleFeatures++;
}
return visibleFeatures;
}
int CountCorrectMatches (Matches& matches, std::vector<cv::Point2f>& sourcePointsInFrame, Keypoints& resKpReal)
{
int correctMatches = 0;
const int matchesCount = matches.size();
for (auto& match : matches)
{
const cv::Point2f expected = sourcePointsInFrame[match.trainIdx];
const cv::Point2f actual = resKpReal[match.queryIdx].pt;
if (distance (expected, actual) < 3.0)
correctMatches++;
}
return correctMatches;
}
cv::Scalar computeReprojectionError (const Keypoints& source, const Keypoints& query, const Matches& matches, const cv::Mat& homography)
{
assert (!matches.empty());
const int pointsCount = matches.size();
std::vector<cv::Point2f> srcPoints, dstPoints;
std::vector<float> distances;
for (int i = 0; i < pointsCount; i++)
{
srcPoints.push_back(source[matches[i].trainIdx].pt);
dstPoints.push_back(query[matches[i].queryIdx].pt);
}
perspectiveTransform(dstPoints, dstPoints, homography.inv());
for (int i = 0; i < pointsCount; i++)
{
const cv::Point2f& src = srcPoints[i];
const cv::Point2f& dst = dstPoints[i];
cv::Point2f v = src - dst;
distances.push_back(sqrtf(v.dot(v)));
}
cv::Scalar mean, dev;
meanStdDev(distances, mean, dev);
cv::Scalar result;
result(0) = mean(0);
result(1) = dev(0);
result(2) = *std::max_element(distances.begin(), distances.end());
result(3) = *std::min_element(distances.begin(), distances.end());
return result;
}