Blender V4.3
distortion_models.cc
Go to the documentation of this file.
1// Copyright (c) 2014 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
23
24namespace libmv {
25
26namespace {
27
28struct InvertPolynomialIntrinsicsCostFunction {
29 public:
30 typedef Vec2 FMatrixType;
31 typedef Vec2 XMatrixType;
32
33 InvertPolynomialIntrinsicsCostFunction(const double focal_length_x,
34 const double focal_length_y,
35 const double principal_point_x,
36 const double principal_point_y,
37 const double k1,
38 const double k2,
39 const double k3,
40 const double p1,
41 const double p2,
42 const double image_x,
43 const double image_y)
44 : focal_length_x_(focal_length_x),
45 focal_length_y_(focal_length_y),
46 principal_point_x_(principal_point_x),
47 principal_point_y_(principal_point_y),
48 k1_(k1),
49 k2_(k2),
50 k3_(k3),
51 p1_(p1),
52 p2_(p2),
53 x_(image_x),
54 y_(image_y) {}
55
56 Vec2 operator()(const Vec2& u) const {
57 double xx, yy;
58
59 ApplyPolynomialDistortionModel(focal_length_x_,
60 focal_length_y_,
61 principal_point_x_,
62 principal_point_y_,
63 k1_,
64 k2_,
65 k3_,
66 p1_,
67 p2_,
68 u(0),
69 u(1),
70 &xx,
71 &yy);
72
73 Vec2 fx;
74 fx << (xx - x_), (yy - y_);
75 return fx;
76 }
81 double k1_, k2_, k3_;
82 double p1_, p2_;
83 double x_, y_;
84};
85
86struct InvertDivisionIntrinsicsCostFunction {
87 public:
88 typedef Vec2 FMatrixType;
89 typedef Vec2 XMatrixType;
90
91 InvertDivisionIntrinsicsCostFunction(const double focal_length_x,
92 const double focal_length_y,
93 const double principal_point_x,
94 const double principal_point_y,
95 const double k1,
96 const double k2,
97 const double image_x,
98 const double image_y)
99 : focal_length_x_(focal_length_x),
100 focal_length_y_(focal_length_y),
101 principal_point_x_(principal_point_x),
102 principal_point_y_(principal_point_y),
103 k1_(k1),
104 k2_(k2),
105 x_(image_x),
106 y_(image_y) {}
107
108 Vec2 operator()(const Vec2& u) const {
109 double xx, yy;
110
111 ApplyDivisionDistortionModel(focal_length_x_,
112 focal_length_y_,
113 principal_point_x_,
114 principal_point_y_,
115 k1_,
116 k2_,
117 u(0),
118 u(1),
119 &xx,
120 &yy);
121
122 Vec2 fx;
123 fx << (xx - x_), (yy - y_);
124 return fx;
125 }
126 double focal_length_x_;
127 double focal_length_y_;
128 double principal_point_x_;
129 double principal_point_y_;
130 double k1_, k2_;
131 double x_, y_;
132};
133
134struct InvertBrownIntrinsicsCostFunction {
135 public:
136 typedef Vec2 FMatrixType;
137 typedef Vec2 XMatrixType;
138
139 InvertBrownIntrinsicsCostFunction(const double focal_length_x,
140 const double focal_length_y,
141 const double principal_point_x,
142 const double principal_point_y,
143 const double k1,
144 const double k2,
145 const double k3,
146 const double k4,
147 const double p1,
148 const double p2,
149 const double image_x,
150 const double image_y)
151 : focal_length_x_(focal_length_x),
152 focal_length_y_(focal_length_y),
153 principal_point_x_(principal_point_x),
154 principal_point_y_(principal_point_y),
155 k1_(k1),
156 k2_(k2),
157 k3_(k3),
158 k4_(k4),
159 p1_(p1),
160 p2_(p2),
161 x_(image_x),
162 y_(image_y) {}
163
164 Vec2 operator()(const Vec2& u) const {
165 double xx, yy;
166
167 ApplyBrownDistortionModel(focal_length_x_,
168 focal_length_y_,
169 principal_point_x_,
170 principal_point_y_,
171 k1_,
172 k2_,
173 k3_,
174 k4_,
175 p1_,
176 p2_,
177 u(0),
178 u(1),
179 &xx,
180 &yy);
181
182 Vec2 fx;
183 fx << (xx - x_), (yy - y_);
184 return fx;
185 }
186 double focal_length_x_;
187 double focal_length_y_;
188 double principal_point_x_;
189 double principal_point_y_;
190 double k1_, k2_, k3_, k4_;
191 double p1_, p2_;
192 double x_, y_;
193};
194
195} // namespace
196
197void InvertPolynomialDistortionModel(const double focal_length_x,
198 const double focal_length_y,
199 const double principal_point_x,
200 const double principal_point_y,
201 const double k1,
202 const double k2,
203 const double k3,
204 const double p1,
205 const double p2,
206 const double image_x,
207 const double image_y,
208 double* normalized_x,
209 double* normalized_y) {
210 // Compute the initial guess. For a camera with no distortion, this will also
211 // be the final answer; the LM iteration will terminate immediately.
213 normalized(0) = (image_x - principal_point_x) / focal_length_x;
214 normalized(1) = (image_y - principal_point_y) / focal_length_y;
215
217
218 InvertPolynomialIntrinsicsCostFunction intrinsics_cost(focal_length_x,
219 focal_length_y,
220 principal_point_x,
221 principal_point_y,
222 k1,
223 k2,
224 k3,
225 p1,
226 p2,
227 image_x,
228 image_y);
229 Solver::SolverParameters params;
230 Solver solver(intrinsics_cost);
231
232 /*Solver::Results results =*/solver.minimize(params, &normalized);
233
234 // TODO(keir): Better error handling.
235
236 *normalized_x = normalized(0);
237 *normalized_y = normalized(1);
238}
239
240void InvertDivisionDistortionModel(const double focal_length_x,
241 const double focal_length_y,
242 const double principal_point_x,
243 const double principal_point_y,
244 const double k1,
245 const double k2,
246 const double image_x,
247 const double image_y,
248 double* normalized_x,
249 double* normalized_y) {
250 // Compute the initial guess. For a camera with no distortion, this will also
251 // be the final answer; the LM iteration will terminate immediately.
253 normalized(0) = (image_x - principal_point_x) / focal_length_x;
254 normalized(1) = (image_y - principal_point_y) / focal_length_y;
255
256 // TODO(sergey): Use Ceres minimizer instead.
258
259 InvertDivisionIntrinsicsCostFunction intrinsics_cost(focal_length_x,
260 focal_length_y,
261 principal_point_x,
262 principal_point_y,
263 k1,
264 k2,
265 image_x,
266 image_y);
267 Solver::SolverParameters params;
268 Solver solver(intrinsics_cost);
269
270 /*Solver::Results results =*/solver.minimize(params, &normalized);
271
272 // TODO(keir): Better error handling.
273
274 *normalized_x = normalized(0);
275 *normalized_y = normalized(1);
276}
277
278void InvertBrownDistortionModel(const double focal_length_x,
279 const double focal_length_y,
280 const double principal_point_x,
281 const double principal_point_y,
282 const double k1,
283 const double k2,
284 const double k3,
285 const double k4,
286 const double p1,
287 const double p2,
288 const double image_x,
289 const double image_y,
290 double* normalized_x,
291 double* normalized_y) {
292 // Compute the initial guess. For a camera with no distortion, this will also
293 // be the final answer; the LM iteration will terminate immediately.
295 normalized(0) = (image_x - principal_point_x) / focal_length_x;
296 normalized(1) = (image_y - principal_point_y) / focal_length_y;
297
299
300 InvertBrownIntrinsicsCostFunction intrinsics_cost(focal_length_x,
301 focal_length_y,
302 principal_point_x,
303 principal_point_y,
304 k1,
305 k2,
306 k3,
307 k4,
308 p1,
309 p2,
310 image_x,
311 image_y);
312 Solver::SolverParameters params;
313 Solver solver(intrinsics_cost);
314
315 /*Solver::Results results =*/solver.minimize(params, &normalized);
316
317 // TODO(keir): Better error handling.
318
319 *normalized_x = normalized(0);
320 *normalized_y = normalized(1);
321}
322
324 public:
327
328 ApplyNukeIntrinsicsCostFunction(const double focal_length_x,
329 const double focal_length_y,
330 const double principal_point_x,
331 const double principal_point_y,
332 const int image_width,
333 const int image_height,
334 const double k1,
335 const double k2,
336 const double expected_normalized_x,
337 const double expected_normalized_y)
338 : focal_length_x_(focal_length_x),
339 focal_length_y_(focal_length_y),
340 principal_point_x_(principal_point_x),
341 principal_point_y_(principal_point_y),
344 k1_(k1),
345 k2_(k2),
346 expected_normalized_x_(expected_normalized_x),
347 expected_normalized_y_(expected_normalized_y) {}
348
349 Vec2 operator()(const Vec2& image_coordinate) const {
350 double actual_normalized_x, actual_normalized_y;
351
358 k1_,
359 k2_,
360 image_coordinate(0),
361 image_coordinate(1),
362 &actual_normalized_x,
363 &actual_normalized_y);
364
365 Vec2 fx;
366 fx << (actual_normalized_x - expected_normalized_x_),
367 (actual_normalized_y - expected_normalized_y_);
368 return fx;
369 }
376 double k1_, k2_;
378};
379
380void ApplyNukeDistortionModel(const double focal_length_x,
381 const double focal_length_y,
382 const double principal_point_x,
383 const double principal_point_y,
384 const int image_width,
385 const int image_height,
386 const double k1,
387 const double k2,
388 const double normalized_x,
389 const double normalized_y,
390 double* image_x,
391 double* image_y) {
392 // Compute the initial guess. For a camera with no distortion, this will also
393 // be the final answer; the LM iteration will terminate immediately.
394 Vec2 image;
395 image(0) = normalized_x * focal_length_x + principal_point_x;
396 image(1) = normalized_y * focal_length_y + principal_point_y;
397
398 // TODO(sergey): Use Ceres minimizer instead.
400
401 ApplyNukeIntrinsicsCostFunction intrinsics_cost(focal_length_x,
402 focal_length_y,
403 principal_point_x,
404 principal_point_y,
407 k1,
408 k2,
409 normalized_x,
410 normalized_y);
411 Solver::SolverParameters params;
412 Solver solver(intrinsics_cost);
413
414 /*Solver::Results results =*/solver.minimize(params, &image);
415
416 // TODO(keir): Better error handling.
417
418 *image_x = image(0);
419 *image_y = image(1);
420}
421
422} // namespace libmv
static constexpr int image_width
static constexpr int image_height
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
SIMD_FORCE_INLINE btVector3 normalized() const
Return a normalized version of this vector.
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
double p1_
double principal_point_x_
double principal_point_y_
double focal_length_y_
double p2_
double k4_
double k3_
double k2_
double focal_length_x_
double k1_
const Mat y_
const Mat x_
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void InvertNukeDistortionModel(const T &focal_length_x, const T &focal_length_y, const T &principal_point_x, const T &principal_point_y, const int image_width, const int image_height, const T &k1, const T &k2, const T &image_x, const T &image_y, T *normalized_x, T *normalized_y)
void InvertPolynomialDistortionModel(const double focal_length_x, const double focal_length_y, const double principal_point_x, const double principal_point_y, const double k1, const double k2, const double k3, const double p1, const double p2, const double image_x, const double image_y, double *normalized_x, double *normalized_y)
void ApplyPolynomialDistortionModel(const T &focal_length_x, const T &focal_length_y, const T &principal_point_x, const T &principal_point_y, const T &k1, const T &k2, const T &k3, const T &p1, const T &p2, const T &normalized_x, const T &normalized_y, T *image_x, T *image_y)
Eigen::Vector2d Vec2
Definition numeric.h:105
void ApplyDivisionDistortionModel(const T &focal_length_x, const T &focal_length_y, const T &principal_point_x, const T &principal_point_y, const T &k1, const T &k2, const T &normalized_x, const T &normalized_y, T *image_x, T *image_y)
void ApplyNukeDistortionModel(const double focal_length_x, const double focal_length_y, const double principal_point_x, const double principal_point_y, const int image_width, const int image_height, const double k1, const double k2, const double normalized_x, const double normalized_y, double *image_x, double *image_y)
void InvertBrownDistortionModel(const double focal_length_x, const double focal_length_y, const double principal_point_x, const double principal_point_y, const double k1, const double k2, const double k3, const double k4, const double p1, const double p2, const double image_x, const double image_y, double *normalized_x, double *normalized_y)
void InvertDivisionDistortionModel(const double focal_length_x, const double focal_length_y, const double principal_point_x, const double principal_point_y, const double k1, const double k2, const double image_x, const double image_y, double *normalized_x, double *normalized_y)
void ApplyBrownDistortionModel(const T &focal_length_x, const T &focal_length_y, const T &principal_point_x, const T &principal_point_y, const T &k1, const T &k2, const T &k3, const T &k4, const T &p1, const T &p2, const T &normalized_x, const T &normalized_y, T *image_x, T *image_y)
Vec2 operator()(const Vec2 &image_coordinate) const
ApplyNukeIntrinsicsCostFunction(const double focal_length_x, const double focal_length_y, const double principal_point_x, const double principal_point_y, const int image_width, const int image_height, const double k1, const double k2, const double expected_normalized_x, const double expected_normalized_y)