-
Notifications
You must be signed in to change notification settings - Fork 22
/
Distortion.h
340 lines (277 loc) · 9.34 KB
/
Distortion.h
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Copyright (c) 2021 Darius Rückert
* Licensed under the MIT License.
* See LICENSE file for more information.
*/
#pragma once
#include "saiga/core/math/math.h"
#include "saiga/vision/cameraModel/Intrinsics4.h"
namespace Saiga
{
/**
* The 8 parameter Rad-Tan Distortion model.
* https://docs.opencv.org/4.5.4/d9/d0c/group__calib3d.html
*
* Note: The order of coefficients is different to OpenCV
*/
template <typename T>
struct DistortionBase
{
T k1 = 0;
T k2 = 0;
T k3 = 0;
T k4 = 0;
T k5 = 0;
T k6 = 0;
T p1 = 0;
T p2 = 0;
HD inline DistortionBase() {}
HD inline DistortionBase(const Eigen::Matrix<T, 8, 1>& c)
{
k1 = c(0);
k2 = c(1);
k3 = c(2);
k4 = c(3);
k5 = c(4);
k6 = c(5);
p1 = c(6);
p2 = c(7);
}
HD inline Eigen::Matrix<T, 8, 1> Coeffs() const
{
Eigen::Matrix<T, 8, 1> result;
result(0) = k1;
result(1) = k2;
result(2) = k3;
result(3) = k4;
result(4) = k5;
result(5) = k6;
result(6) = p1;
result(7) = p2;
return result;
}
HD inline Eigen::Matrix<T, 8, 1> OpenCVOrder() const
{
Eigen::Matrix<T, 8, 1> result;
result << k1, k2, p1, p2, k3, k4, k5, k6;
return result;
}
template <typename G>
HD inline DistortionBase<G> cast() const
{
return Coeffs().template cast<G>().eval();
}
HD inline T RadialFactor(const Eigen::Matrix<T, 2, 1>& p)
{
T r2 = p.dot(p);
T r4 = r2 * r2;
T r6 = r2 * r4;
T radial_u = T(1) + k1 * r2 + k2 * r4 + k3 * r6;
T radial_v = T(1) + k4 * r2 + k5 * r4 + k6 * r6;
T radial_iv = T(1) / radial_v;
T radial = radial_u * radial_iv;
return radial;
}
// Due to the higher oder polynomial the radial distortion is not monotonic.
// This results in a non-bijective mapping, where multiple world points are mapped to the same image points.
// To prevent this case, this function computes a threshold which should be passed as 'max_r' to
// distortNormalizedPoint.
HD inline T MonotonicThreshold(int steps = 100, T max_r = 2)
{
float last = 0;
float th = max_r;
for (int i = 0; i < steps; ++i)
{
float a = (float(i) / steps) * max_r;
float c = RadialFactor(vec2(a, 0));
float shift = c * a;
if (shift - last < 0)
{
th = a;
break;
}
last = shift;
}
return th;
}
};
// Use double by default here
using Distortion = DistortionBase<double>;
using Distortionf = DistortionBase<float>;
template <typename T>
std::ostream& operator<<(std::ostream& strm, const DistortionBase<T> dist)
{
strm << dist.Coeffs().transpose();
return strm;
}
/**
* The OpenCV distortion model applied to a point in normalized image coordinates.
* You can find a glsl implementation in shader/vision/distortion.h
*/
template <typename T>
HD inline Eigen::Matrix<T, 2, 1> distortNormalizedPoint(const Eigen::Matrix<T, 2, 1>& point,
const DistortionBase<T>& distortion,
Matrix<T, 2, 2>* J_point = nullptr,
Matrix<T, 2, 8>* J_distortion = nullptr, T max_r = T(100000))
{
T x = point.x();
T y = point.y();
T k1 = distortion.k1;
T k2 = distortion.k2;
T k3 = distortion.k3;
T k4 = distortion.k4;
T k5 = distortion.k5;
T k6 = distortion.k6;
T p1 = distortion.p1;
T p2 = distortion.p2;
T x2 = x * x, y2 = y * y;
T r2 = x2 + y2, _2xy = T(2) * x * y;
T r4 = r2 * r2;
T r6 = r4 * r2;
T radial_u = T(1) + k1 * r2 + k2 * r4 + k3 * r6;
T radial_v = T(1) + k4 * r2 + k5 * r4 + k6 * r6;
T radial_iv = T(1) / radial_v;
T radial = radial_u * radial_iv;
T tangentialX = p1 * _2xy + p2 * (r2 + T(2) * x2);
T tangentialY = p1 * (r2 + T(2) * y2) + p2 * _2xy;
T xd = x * radial + tangentialX;
T yd = y * radial + tangentialY;
if (r2 > max_r * max_r)
{
xd = 100000;
yd = 100000;
}
if (J_point)
{
Matrix<T, 2, 2> J_rad_u;
J_rad_u(0, 0) = k1 * (2 * x);
J_rad_u(0, 1) = k1 * (2 * y);
J_rad_u(0, 0) += k2 * (4 * x * x * x + 4 * x * y * y);
J_rad_u(0, 1) += k2 * (4 * y * y * y + 4 * y * x * x);
J_rad_u(0, 0) += k3 * (6 * x2 * x2 * x + 12 * x2 * x * y2 + 6 * x * y2 * y2);
J_rad_u(0, 1) += k3 * (6 * y2 * y2 * y + 12 * y2 * y * x2 + 6 * y * x2 * x2);
J_rad_u(1, 0) = J_rad_u(0, 0);
J_rad_u(1, 1) = J_rad_u(0, 1);
Matrix<T, 2, 2> J_rad_v;
J_rad_v(0, 0) = k4 * (2 * x);
J_rad_v(0, 1) = k4 * (2 * y);
J_rad_v(0, 0) += k5 * (4 * x * x * x + 4 * x * y * y);
J_rad_v(0, 1) += k5 * (4 * y * y * y + 4 * y * x * x);
J_rad_v(0, 0) += k6 * (6 * x2 * x2 * x + 12 * x2 * x * y2 + 6 * x * y2 * y2);
J_rad_v(0, 1) += k6 * (6 * y2 * y2 * y + 12 * y2 * y * x2 + 6 * y * x2 * x2);
J_rad_v(1, 0) = J_rad_v(0, 0);
J_rad_v(1, 1) = J_rad_v(0, 1);
Matrix<T, 2, 2> J_rad;
J_rad = (J_rad_u * radial_v - J_rad_v * radial_u) * radial_iv * radial_iv;
Matrix<T, 2, 2> J_rad_mul_xy;
J_rad_mul_xy(0, 0) = x * J_rad(0, 0) + radial;
J_rad_mul_xy(0, 1) = x * J_rad(0, 1);
J_rad_mul_xy(1, 0) = y * J_rad(1, 0);
J_rad_mul_xy(1, 1) = y * J_rad(1, 1) + radial;
Matrix<T, 2, 2> J_tan;
J_tan(0, 0) = 2 * p1 * y + 6 * p2 * x;
J_tan(0, 1) = 2 * p1 * x + 2 * p2 * y;
J_tan(1, 0) = 2 * p2 * y + 2 * p1 * x;
J_tan(1, 1) = 2 * p2 * x + 6 * p1 * y;
*J_point = J_rad_mul_xy + J_tan;
}
if (J_distortion)
{
auto& J = *J_distortion;
J(0, 0) = r2 * x * radial_iv;
J(0, 1) = r4 * x * radial_iv;
J(0, 2) = r6 * x * radial_iv;
J(1, 0) = r2 * y * radial_iv;
J(1, 1) = r4 * y * radial_iv;
J(1, 2) = r6 * y * radial_iv;
J(0, 3) = -r2 * radial_u * radial_iv * radial_iv * x;
J(0, 4) = -r4 * radial_u * radial_iv * radial_iv * x;
J(0, 5) = -r6 * radial_u * radial_iv * radial_iv * x;
J(1, 3) = -r2 * radial_u * radial_iv * radial_iv * y;
J(1, 4) = -r4 * radial_u * radial_iv * radial_iv * y;
J(1, 5) = -r6 * radial_u * radial_iv * radial_iv * y;
J(0, 6) = _2xy;
J(1, 7) = _2xy;
J(0, 7) = (r2 + T(2) * x2);
J(1, 6) = (r2 + T(2) * y2);
}
return {xd, yd};
}
template <typename T>
Eigen::Matrix<T, 2, 1> undistortPointGN(const Eigen::Matrix<T, 2, 1>& point, const Eigen::Matrix<T, 2, 1>& guess,
const DistortionBase<T>& d, int iterations = 5)
{
Eigen::Matrix<T, 2, 1> x = guess;
Eigen::Matrix<T, 2, 1> last_point = guess;
T last_chi2 = std::numeric_limits<T>::infinity();
for (int it = 0; it < iterations; ++it)
{
Eigen::Matrix<T, 2, 2> J;
Eigen::Matrix<T, 2, 1> res = distortNormalizedPoint(x, d, &J) - point;
T chi2 = res.squaredNorm();
Eigen::Matrix<T, 2, 2> JtJ = J.transpose() * J;
Eigen::Matrix<T, 2, 1> Jtb = -J.transpose() * res;
if (chi2 > last_chi2)
{
x = last_point;
continue;
}
last_point = x;
last_chi2 = chi2;
Eigen::Matrix<T, 2, 1> delta = JtJ.ldlt().solve(Jtb);
x += delta;
}
// Final check
T chi2 = (distortNormalizedPoint(x, d) - point).squaredNorm();
if (chi2 > last_chi2)
{
x = last_point;
}
return x;
}
/**
* The inverse OpenCV distortion model with 5 parameters.
*/
template <typename T>
HD inline Eigen::Matrix<T, 2, 1> undistortNormalizedPointSimple(const Eigen::Matrix<T, 2, 1>& point,
const DistortionBase<T>& distortion, int iterations = 5)
{
T x = point.x();
T y = point.y();
T k1 = distortion.k1;
T k2 = distortion.k2;
T k3 = distortion.k3;
T p1 = distortion.p1;
T p2 = distortion.p2;
T x0 = x;
T y0 = y;
// compensate distortion iteratively
for (int j = 0; j < iterations; j++)
{
T x2 = x * x, y2 = y * y;
T r2 = x2 + y2, _2xy = T(2) * x * y;
T radial = T(1) / (T(1) + ((k3 * r2 + k2) * r2 + k1) * r2);
T tangentialX = p1 * _2xy + p2 * (r2 + T(2) * x2);
T tangentialY = p1 * (r2 + T(2) * y2) + p2 * _2xy;
x = (x0 - tangentialX) * radial;
y = (y0 - tangentialY) * radial;
}
return {x, y};
}
/**
* Undistorts all points from begin to end and writes them to output.
*/
template <typename _InputIterator1, typename _InputIterator2, typename _T>
inline void undistortAll(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __output,
const IntrinsicsPinhole<_T>& intr, const DistortionBase<_T>& dis)
{
for (; __first1 != __last1; ++__first1, (void)++__output)
{
auto tmp = *__first1; // make sure it works inplace
tmp = intr.unproject2(tmp);
tmp = undistortPointGN(tmp, tmp, dis);
tmp = intr.normalizedToImage(tmp);
*__output = tmp;
}
}
} // namespace Saiga