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
|
#include "match.h"
#include <cmath>
#include <algorithm>
#include "meowpp/oo/ObjSelector.h"
#include <vector>
#include "meowpp/math/methods.h"
#include <utility>
using namespace meow;
using namespace std;
inline Vector<double> calc8(vector<pair<Vector2D<double>, Vector2D<double> > > const& p) {
Matrix<double> m(p.size() * 2, 8, 0.0), b(8, 1, 0.0);
for (size_t i = 0; i < p.size(); ++i) {
m.entry(i * 2 + 0, 0, p[i].first.x());
m.entry(i * 2 + 0, 1, p[i].first.y());
m.entry(i * 2 + 0, 2, 1);
m.entry(i * 2 + 0, 6, -p[i].first.x() * p[i].second.x());
m.entry(i * 2 + 0, 7, -p[i].first.y() * p[i].second.x());
m.entry(i * 2 + 1, 3, p[i].first.x());
m.entry(i * 2 + 1, 4, p[i].first.y());
m.entry(i * 2 + 1, 5, 1);
m.entry(i * 2 + 1, 6, -p[i].first.x() * p[i].second.y());
m.entry(i * 2 + 1, 7, -p[i].first.y() * p[i].second.y());
b.entry(i * 2 + 0, 0, p[i].second.x());
b.entry(i * 2 + 1, 0, p[i].second.y());
}
if (p.size() == 4) {
/*
Vector<double> x = m.inverse() * b;
if (x.dimension() == 8) {
bool ok = true;
printf("size = %d\n", (int)x.dimension());
for (size_t i = 0; i < p.size(); ++i) {
double a = x(0) * p[i].first.x() + x(1) * p[i].first.y() + x(2) - x(6) * p[i].first.x() * p[i].second.x() - x(7) * p[i].first.y() * p[i].second.x() - p[i].second.x();
double b = x(3) * p[i].first.x() + x(4) * p[i].first.y() + x(5) - x(6) * p[i].first.x() * p[i].second.y() - x(7) * p[i].first.y() * p[i].second.y() - p[i].second.y();
printf("ab = %f, %f\n", a, b);
if (fabs(a) > 10 || fabs(b) > 10) ok = false;
}
if (!ok) {
for (size_t i = 0; i < p.size(); ++i) {
printf("<%10.3f, %10.3f> ---> <%10.3f, %10.3f>\n", p[i].first.x(), p[i].first.y(), p[i].second.x(), p[i].second.y());
}
getchar();
}
}
// */
return Vector<double>(m.inverse() * b);
}
else {
return Vector<double>((m.transpose() * m).inverse() * m.transpose() * b);
}
}
inline vector<Pair> goodPair(vector<Vector2D<double> > const& from,
vector<Vector2D<double> > const& to,
vector<Pair> const& pairs,
Vector<double> const& v,
double t) {
vector<Pair> ret;
for (size_t i = 0; i < pairs.size(); ++i) {
Vector2D<double> v1 = from[pairs[i].first ];
Vector2D<double> v2 = to [pairs[i].second];
Vector2D<double> v12(
(v1.x() * v(0) + v1.y() * v(1) + v(2)) / (v1.x() * v(6) + v1.y() * v(7) + 1),
(v1.x() * v(3) + v1.y() * v(4) + v(5)) / (v1.x() * v(6) + v1.y() * v(7) + 1)
);
if ((v12 - v2).length() <= t) {
ret.push_back(pairs[i]);
}
}
return ret;
}
class MatchOne_RANSAC: public MatchOne {
private:
double P_, p0_, t_;
double rat_, ang_;
class Controller {
vector<Vector2D<double> > v1_;
vector<Vector2D<double> > v2_;
double t_, rat_, ang_;
double x_max_, y_max_;
bool check(Vector<double> const& v) const {
Vector2D<double> vx((double)v(0), v(1));
Vector2D<double> vy((double)v(3), v(4));
Vector2D<double> d ((double)v(6), v(7));
double l1 = vx.length() * rat_, l2 = vx.length() / rat_;
if (vy.length() < min(l1, l2) ) return false;
if ( max(l1, l2) < vy.length()) return false;
double ang = acos(vx.dot(vy) / vx.length() / vy.length());
if (ang < PI * 0.5 - ang_ || PI * 0.5 + ang_ < ang) return false;
if (d.x() * 0 + d.y() * 0 + 1 <= 0) return false;
if (d.x() * x_max_ + d.y() * 0 + 1 <= 0) return false;
if (d.x() * 0 + d.y() * y_max_ + 1 <= 0) return false;
if (d.x() * x_max_ + d.y() * y_max_ + 1 <= 0) return false;
return true;
}
public:
Controller(vector<Vector2D<double> > const& fps1,
vector<Vector2D<double> > const& fps2,
double threshold,
double rat, double ang, double xmax, double ymax):
v1_(fps1), v2_(fps2), t_(threshold), rat_(rat), ang_(fabs(ang)), x_max_(xmax), y_max_(ymax) {
}
double operator()(vector<Pair> const& p, vector<Pair> const& pairs) const {
vector<pair<Vector2D<double>, Vector2D<double> > > ps;
for (size_t i = 0; i < p.size(); ++i)
for (size_t j = 0; j < p.size(); ++j)
if (i != j && (v1_[p[i].first ] == v1_[p[j].first ] ||
v2_[p[i].second] == v2_[p[j].second]))
return -1.0;
for (size_t i = 0; i < p.size(); ++i) {
ps.push_back(pair<Vector2D<double>, Vector2D<double> >(v1_[p[i].first], v2_[p[i].second]));
}
Vector<double> v = calc8(ps);
if (v.valid() == false || check(v) == false) return -1;
return 0.1 + (double)goodPair(v1_, v2_, pairs, v, t_).size();
}
};
public:
MatchOne_RANSAC(): P_(0.99), p0_(0.05), t_(5), rat_(0.8), ang_(PI / 2 / 8) {
}
MatchInfo match(vector<Vector2D<double> > const& fps1,
vector<Vector2D<double> > const& fps2,
vector<Pair> const& pairs,
size_t width, size_t height) const {
MatchInfo ret;
if ((int)pairs.size() < minNumber()) {
ret.ok = false;
}
else {
ret.pairs = ransac(pairs, Controller(fps1, fps2, t_, rat_, ang_, width, height), minNumber(), p0_, P_);
if ((int)ret.pairs.size() < minNumber()) {
ret.ok = false;
}
else {
vector<pair<Vector2D<double>, Vector2D<double> > > ps;
for (size_t i = 0; i <ret.pairs.size(); ++i) {
ps.push_back(pair<Vector2D<double>, Vector2D<double> >(fps1[ret.pairs[i].first], fps2[ret.pairs[i].second]));
}
Vector<double> v = calc8(ps);
ret.ok = true;
ret.pairs = goodPair(fps1, fps2, pairs, v, t_);
ret.x_axis.x(v(0));
ret.x_axis.y(v(1));
ret.x_offset = v(2);
ret.y_axis.x(v(3));
ret.y_axis.y(v(4));
ret.y_offset = v(5);
ret.depth.x(v(6));
ret.depth.y(v(7));
}
}
return ret;
}
string description() const {
return string("ransac with eight parameter");
}
Usage usage() const {
Usage tmp;
tmp.optionAdd("ransac-P",
"prob what I want",
"floating number",
stringPrintf("%.2f", P_),
false);
tmp.optionAdd("ransac-p0",
"prob each time",
"floating number",
stringPrintf("%.2f", p0_),
false);
tmp.optionAdd("ransac-t",
"threshold t",
"floating number",
stringPrintf("%.2f", t_),
false);
tmp.optionAdd("ransac-ratio",
"ratio",
"floating number",
stringPrintf("%.2f", rat_),
false);
tmp.optionAdd("ransac-angle",
"angle",
"floating number",
stringPrintf("%.2f", ang_),
false);
return tmp;
}
bool usage(Usage const& usg) {
P_ = inRange(0.00001, 0.99999, atof(usg.optionValue("ransac-P" , 0).c_str()));
p0_ = inRange(0.00001, 0.99999, atof(usg.optionValue("ransac-p0", 0).c_str()));
t_ = inRange(0.00001, 0.99999, atof(usg.optionValue("ransac-t" , 0).c_str()));
rat_ = inRange(0.00001, 0.99999, atof(usg.optionValue("ransac-ratio", 0).c_str()));
ang_ = inRange(0.00001, 0.99999, atof(usg.optionValue("ransac-angle", 0).c_str()));
return true;
}
int minNumber() const {
return 4;
}
ObjBase* create() const {
return new MatchOne_RANSAC;
}
};
static ObjSelector<kMatchOne_ID> __("ransac8", new MatchOne_RANSAC, true);
|