This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfeatures.cpp
More file actions
172 lines (151 loc) · 5.04 KB
/
features.cpp
File metadata and controls
172 lines (151 loc) · 5.04 KB
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
#include "ht-api.h"
#include "ht-internal.h"
#include <algorithm>
using namespace std;
using namespace cv;
void ht_draw_features(headtracker_t& ctx) {
int j = 0;
float mult = ctx.color.cols / (float)ctx.grayscale.cols;
for (int i = 0; i < ctx.config.max_keypoints; i++) {
if (ctx.keypoints[i].idx != -1) {
circle(ctx.color,
Point(ctx.keypoints[i].position.x * mult, ctx.keypoints[i].position.y * mult),
2,
Scalar(255, 255, 0),
-1);
j++;
}
}
}
static bool sort_keypoint_response(const KeyPoint& p1, const KeyPoint& p2)
{
return p2.response > p1.response;
}
void ht_track_features(headtracker_t& ctx) {
if (ctx.restarted) {
buildOpticalFlowPyramid(ctx.grayscale,
*ctx.pyr_a,
Size(ctx.config.pyrlk_win_size_w, ctx.config.pyrlk_win_size_h),
ctx.config.pyrlk_pyramids);
}
buildOpticalFlowPyramid(ctx.grayscale,
*ctx.pyr_b,
Size(ctx.config.pyrlk_win_size_w, ctx.config.pyrlk_win_size_h),
ctx.config.pyrlk_pyramids);
int k = 0;
vector<Point2f> old_features;
for (int i = 0; i < ctx.config.max_keypoints; i++) {
if (ctx.keypoints[i].idx == -1)
continue;
old_features.push_back(ctx.keypoints[i].position);
k++;
}
vector<Point2f> new_features = vector<Point2f>(k);
Mat features_found(1, k, CV_8UC1);
if (k > 0) {
calcOpticalFlowPyrLK(*ctx.pyr_a,
*ctx.pyr_b,
old_features,
new_features,
features_found,
noArray(),
Size(ctx.config.pyrlk_win_size_w, ctx.config.pyrlk_win_size_h),
ctx.config.pyrlk_pyramids,
TermCriteria(TermCriteria::COUNT | TermCriteria::EPS, 30, 0.01));
for (int i = 0, j = 0; i < k; i++, j++) {
for (; j < ctx.config.max_keypoints && ctx.keypoints[j].idx == -1; j++)
;;
if (j == ctx.config.max_keypoints)
break;
if (!features_found.at<char>(i)) {
ctx.keypoints[j].idx = -1;
} else {
ctx.keypoints[j].position = new_features[i];
}
}
}
std::swap(ctx.pyr_a, ctx.pyr_b);
}
void ht_get_features(headtracker_t& ctx, model_t& model) {
if (!model.projection)
return;
Rect roi = ht_get_bounds(ctx, model);
if (!(roi.width > 20 && roi.height > 40))
return;
float max_dist = ctx.config.keypoint_distance * ctx.zoom_ratio;
max_dist *= max_dist;
Mat img = ctx.grayscale(roi);
//ORB foo(2000, 1.2, 8, 2, 0, 2, ORB::HARRIS_SCORE, 2);
//foo.detect(img, corners);
//Ptr<FeatureDetector> fast = FeatureDetector::create("FAST");
//fast->detect(img, corners);
//GridAdaptedFeatureDetector detector(fast, ctx.config.max_keypoints, 4, 2);
//detector.detect(img, corners);
start:
vector<KeyPoint> corners;
FAST(img, corners, ctx.fast_state, true);
if (corners.size() < ctx.config.max_keypoints*0.5 && ctx.fast_state > 5)
{
corners.clear();
ctx.fast_state--;
goto start;
}
if (corners.size() > ctx.config.max_keypoints*2.5 && ctx.fast_state < 60)
{
corners.clear();
ctx.fast_state++;
goto start;
}
std::sort(corners.begin(), corners.end(), sort_keypoint_response);
//ctx.detector->detect(img, corners);
int cnt = corners.size();
int no_triangle = 0, overlapped = 0;
int kpidx = 0;
for (int i = 0; i < cnt; i++) {
Point2f kp = corners[i].pt;
kp.x += roi.x;
kp.y += roi.y;
bool overlap = false;
for (int j = 0; j < ctx.config.max_keypoints; j++) {
if (ctx.keypoints[j].idx != -1) {
float dist = ht_distance2d_squared(kp, ctx.keypoints[j].position);
if (dist < max_dist) {
overlap = true;
break;
}
}
}
if (overlap)
{
overlapped++;
continue;
}
triangle_t t;
int idx;
Point2f uv;
if (!ht_triangle_at(kp, &t, &idx, model, uv))
{
no_triangle++;
continue;
}
for (; kpidx < ctx.config.max_keypoints; kpidx++) {
if (ctx.keypoints[kpidx].idx == -1) {
ctx.keypoints[kpidx].idx = idx;
ctx.keypoints[kpidx].position = kp;
ctx.keypoint_uv[kpidx] = ht_get_triangle_pos(uv, t);
break;
}
}
if (kpidx == ctx.config.max_keypoints)
break;
}
#if 0
if (ctx.config.debug)
{
fprintf(stderr,
"no-triangle=%d, overlapped=%d, good=%d, FAST=%d\n",
no_triangle, overlapped, cnt - no_triangle - overlapped, ctx.fast_state);
fflush(stderr);
}
#endif
}