-
Notifications
You must be signed in to change notification settings - Fork 184
/
CMT.cpp
201 lines (150 loc) · 5.92 KB
/
CMT.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
#include "CMT.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
namespace cmt {
void CMT::initialize(const Mat im_gray, const Rect rect)
{
FILE_LOG(logDEBUG) << "CMT::initialize() call";
//Remember initial size
size_initial = rect.size();
//Remember initial image
im_prev = im_gray;
//Compute center of rect
Point2f center = Point2f(rect.x + rect.width/2.0, rect.y + rect.height/2.0);
//Initialize rotated bounding box
bb_rot = RotatedRect(center, size_initial, 0.0);
//Initialize detector and descriptor
#if CV_MAJOR_VERSION > 2
detector = cv::FastFeatureDetector::create();
descriptor = cv::BRISK::create();
#else
detector = FeatureDetector::create(str_detector);
descriptor = DescriptorExtractor::create(str_descriptor);
#endif
//Get initial keypoints in whole image and compute their descriptors
vector<KeyPoint> keypoints;
detector->detect(im_gray, keypoints);
//Divide keypoints into foreground and background keypoints according to selection
vector<KeyPoint> keypoints_fg;
vector<KeyPoint> keypoints_bg;
for (size_t i = 0; i < keypoints.size(); i++)
{
KeyPoint k = keypoints[i];
Point2f pt = k.pt;
if (pt.x > rect.x && pt.y > rect.y && pt.x < rect.br().x && pt.y < rect.br().y)
{
keypoints_fg.push_back(k);
}
else
{
keypoints_bg.push_back(k);
}
}
//Create foreground classes
vector<int> classes_fg;
classes_fg.reserve(keypoints_fg.size());
for (size_t i = 0; i < keypoints_fg.size(); i++)
{
classes_fg.push_back(i);
}
//Compute foreground/background features
Mat descs_fg;
Mat descs_bg;
descriptor->compute(im_gray, keypoints_fg, descs_fg);
descriptor->compute(im_gray, keypoints_bg, descs_bg);
//Only now is the right time to convert keypoints to points, as compute() might remove some keypoints
vector<Point2f> points_fg;
vector<Point2f> points_bg;
for (size_t i = 0; i < keypoints_fg.size(); i++)
{
points_fg.push_back(keypoints_fg[i].pt);
}
FILE_LOG(logDEBUG) << points_fg.size() << " foreground points.";
for (size_t i = 0; i < keypoints_bg.size(); i++)
{
points_bg.push_back(keypoints_bg[i].pt);
}
//Create normalized points
vector<Point2f> points_normalized;
for (size_t i = 0; i < points_fg.size(); i++)
{
points_normalized.push_back(points_fg[i] - center);
}
//Initialize matcher
matcher.initialize(points_normalized, descs_fg, classes_fg, descs_bg, center);
//Initialize consensus
consensus.initialize(points_normalized);
//Create initial set of active keypoints
for (size_t i = 0; i < keypoints_fg.size(); i++)
{
points_active.push_back(keypoints_fg[i].pt);
classes_active = classes_fg;
}
FILE_LOG(logDEBUG) << "CMT::initialize() return";
}
void CMT::processFrame(Mat im_gray) {
FILE_LOG(logDEBUG) << "CMT::processFrame() call";
//Track keypoints
vector<Point2f> points_tracked;
vector<unsigned char> status;
tracker.track(im_prev, im_gray, points_active, points_tracked, status);
FILE_LOG(logDEBUG) << points_tracked.size() << " tracked points.";
//keep only successful classes
vector<int> classes_tracked;
for (size_t i = 0; i < classes_active.size(); i++)
{
if (status[i])
{
classes_tracked.push_back(classes_active[i]);
}
}
//Detect keypoints, compute descriptors
vector<KeyPoint> keypoints;
detector->detect(im_gray, keypoints);
FILE_LOG(logDEBUG) << keypoints.size() << " keypoints found.";
Mat descriptors;
descriptor->compute(im_gray, keypoints, descriptors);
//Match keypoints globally
vector<Point2f> points_matched_global;
vector<int> classes_matched_global;
matcher.matchGlobal(keypoints, descriptors, points_matched_global, classes_matched_global);
FILE_LOG(logDEBUG) << points_matched_global.size() << " points matched globally.";
//Fuse tracked and globally matched points
vector<Point2f> points_fused;
vector<int> classes_fused;
fusion.preferFirst(points_tracked, classes_tracked, points_matched_global, classes_matched_global,
points_fused, classes_fused);
FILE_LOG(logDEBUG) << points_fused.size() << " points fused.";
//Estimate scale and rotation from the fused points
float scale;
float rotation;
consensus.estimateScaleRotation(points_fused, classes_fused, scale, rotation);
FILE_LOG(logDEBUG) << "scale " << scale << ", " << "rotation " << rotation;
//Find inliers and the center of their votes
Point2f center;
vector<Point2f> points_inlier;
vector<int> classes_inlier;
consensus.findConsensus(points_fused, classes_fused, scale, rotation,
center, points_inlier, classes_inlier);
FILE_LOG(logDEBUG) << points_inlier.size() << " inlier points.";
FILE_LOG(logDEBUG) << "center " << center;
//Match keypoints locally
vector<Point2f> points_matched_local;
vector<int> classes_matched_local;
matcher.matchLocal(keypoints, descriptors, center, scale, rotation, points_matched_local, classes_matched_local);
FILE_LOG(logDEBUG) << points_matched_local.size() << " points matched locally.";
//Clear active points
points_active.clear();
classes_active.clear();
//Fuse locally matched points and inliers
fusion.preferFirst(points_matched_local, classes_matched_local, points_inlier, classes_inlier, points_active, classes_active);
// points_active = points_fused;
// classes_active = classes_fused;
FILE_LOG(logDEBUG) << points_active.size() << " final fused points.";
//TODO: Use theta to suppress result
bb_rot = RotatedRect(center, size_initial * scale, rotation/CV_PI * 180);
//Remember current image
im_prev = im_gray;
FILE_LOG(logDEBUG) << "CMT::processFrame() return";
}
} /* namespace CMT */