Blender V5.3
distortion_models.h
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
21#ifndef LIBMV_SIMPLE_PIPELINE_DISTORTION_MODELS_H_
22#define LIBMV_SIMPLE_PIPELINE_DISTORTION_MODELS_H_
23
24#include <algorithm>
25
26namespace libmv {
27
34
35// Invert camera intrinsics on the image point to get normalized coordinates.
36// This inverts the radial lens distortion to a point which is in image pixel
37// coordinates to get normalized coordinates.
38void InvertPolynomialDistortionModel(const double focal_length_x,
39 const double focal_length_y,
40 const double principal_point_x,
41 const double principal_point_y,
42 const double k1,
43 const double k2,
44 const double k3,
45 const double p1,
46 const double p2,
47 const double image_x,
48 const double image_y,
49 double* normalized_x,
50 double* normalized_y);
51
52// Apply camera intrinsics to the normalized point to get image coordinates.
53// This applies the radial lens distortion to a point which is in normalized
54// camera coordinates (i.e. the principal point is at (0, 0)) to get image
55// coordinates in pixels. Templated for use with autodifferentiation.
56template <typename T>
57inline void ApplyPolynomialDistortionModel(const T& focal_length_x,
58 const T& focal_length_y,
59 const T& principal_point_x,
60 const T& principal_point_y,
61 const T& k1,
62 const T& k2,
63 const T& k3,
64 const T& p1,
65 const T& p2,
66 const T& normalized_x,
67 const T& normalized_y,
68 T* image_x,
69 T* image_y) {
70 T x = normalized_x;
71 T y = normalized_y;
72
73 // Apply distortion to the normalized points to get (xd, yd).
74 T r2 = x * x + y * y;
75 T r4 = r2 * r2;
76 T r6 = r4 * r2;
77 T r_coeff = (T(1) + k1 * r2 + k2 * r4 + k3 * r6);
78 T xd = x * r_coeff + T(2) * p1 * x * y + p2 * (r2 + T(2) * x * x);
79 T yd = y * r_coeff + T(2) * p2 * x * y + p1 * (r2 + T(2) * y * y);
80
81 // Apply focal length and principal point to get the final image coordinates.
82 *image_x = focal_length_x * xd + principal_point_x;
83 *image_y = focal_length_y * yd + principal_point_y;
84}
85
86// Invert camera intrinsics on the image point to get normalized coordinates.
87// This inverts the radial lens distortion to a point which is in image pixel
88// coordinates to get normalized coordinates.
89//
90// Uses division distortion model.
91void InvertDivisionDistortionModel(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 double* normalized_x,
100 double* normalized_y);
101
102// Apply camera intrinsics to the normalized point to get image coordinates.
103// This applies the radial lens distortion to a point which is in normalized
104// camera coordinates (i.e. the principal point is at (0, 0)) to get image
105// coordinates in pixels. Templated for use with autodifferentiation.
106//
107// Uses division distortion model.
108template <typename T>
109inline void ApplyDivisionDistortionModel(const T& focal_length_x,
110 const T& focal_length_y,
111 const T& principal_point_x,
112 const T& principal_point_y,
113 const T& k1,
114 const T& k2,
115 const T& normalized_x,
116 const T& normalized_y,
117 T* image_x,
118 T* image_y) {
119 T x = normalized_x;
120 T y = normalized_y;
121 T r2 = x * x + y * y;
122 T r4 = r2 * r2;
123
124 T xd = x / (T(1) + k1 * r2 + k2 * r4);
125 T yd = y / (T(1) + k1 * r2 + k2 * r4);
126
127 // Apply focal length and principal point to get the final image coordinates.
128 *image_x = focal_length_x * xd + principal_point_x;
129 *image_y = focal_length_y * yd + principal_point_y;
130}
131
132// Invert camera intrinsics on the image point to get normalized coordinates.
133// This inverts the radial lens distortion to a point which is in image pixel
134// coordinates to get normalized coordinates.
135//
136// Uses Nuke distortion model.
137template <typename T>
138void InvertNukeDistortionModel(const T& focal_length_x,
139 const T& focal_length_y,
140 const T& principal_point_x,
141 const T& principal_point_y,
142 const int image_width,
143 const int image_height,
144 const T& k1,
145 const T& k2,
146 const T& p1,
147 const T& p2,
148 const T& image_x,
149 const T& image_y,
150 T* normalized_x,
151 T* normalized_y) {
152 // According to the documentation for the anamorphic model:
153 //
154 // xu = xd / (1 + k0 * rd^2 + k1 * rd^4 + k2 * yd^2)
155 // yu = yd / (1 + k0 * rd^2 + k1 * rd^4 + k3 * xd^2)
156 //
157 // Legend:
158 // (xd, yd) are the distorted cartesian coordinates,
159 // (rd, phid) are the distorted polar coordinates,
160 // (xu, yu) are the undistorted cartesian coordinates,
161 // (ru, phiu) are the undistorted polar coordinates,
162 // the k-values are the distortion coefficients.
163 //
164 // The coordinate systems are relative to the distortion centre.
165 // Nukes k0, k1, k2, k3 correspond to Blenders k1, k2, p1, p2.
166 // The spherical model is identical except p1 and p2 are both zero.
167
168 const int max_image_size = std::max(image_width, image_height);
169 const double max_half_image_size = max_image_size * 0.5;
170
171 if (max_half_image_size == 0.0) {
172 *normalized_x = image_x * max_half_image_size / focal_length_x;
173 *normalized_y = image_y * max_half_image_size / focal_length_y;
174 return;
175 }
176
177 const T xd = (image_x - principal_point_x) / max_half_image_size;
178 const T yd = (image_y - principal_point_y) / max_half_image_size;
179
180 T xd2 = xd * xd;
181 T yd2 = yd * yd;
182 T rd2 = xd2 + yd2;
183 T rd4 = rd2 * rd2;
184 T xu = xd / (T(1) + k1 * rd2 + k2 * rd4 + p1 * yd2);
185 T yu = yd / (T(1) + k1 * rd2 + k2 * rd4 + p2 * xd2);
186
187 *normalized_x = xu * max_half_image_size / focal_length_x;
188 *normalized_y = yu * max_half_image_size / focal_length_y;
189}
190
191// Apply camera intrinsics to the normalized point to get image coordinates.
192// This applies the radial lens distortion to a point which is in normalized
193// camera coordinates (i.e. the principal point is at (0, 0)) to get image
194// coordinates in pixels. Templated for use with autodifferentiation.
195//
196// Uses Nuke distortion model.
197void ApplyNukeDistortionModel(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 int image_width,
202 const int image_height,
203 const double k1,
204 const double k2,
205 const double p1,
206 const double p2,
207 const double normalized_x,
208 const double normalized_y,
209 double* image_x,
210 double* image_y);
211
212// Invert camera intrinsics on the image point to get normalized coordinates.
213// This inverts the radial lens distortion to a point which is in image pixel
214// coordinates to get normalized coordinates.
215void InvertBrownDistortionModel(const double focal_length_x,
216 const double focal_length_y,
217 const double principal_point_x,
218 const double principal_point_y,
219 const double k1,
220 const double k2,
221 const double k3,
222 const double k4,
223 const double p1,
224 const double p2,
225 const double image_x,
226 const double image_y,
227 double* normalized_x,
228 double* normalized_y);
229
230template <typename T>
231inline void ApplyBrownDistortionModel(const T& focal_length_x,
232 const T& focal_length_y,
233 const T& principal_point_x,
234 const T& principal_point_y,
235 const T& k1,
236 const T& k2,
237 const T& k3,
238 const T& k4,
239 const T& p1,
240 const T& p2,
241 const T& normalized_x,
242 const T& normalized_y,
243 T* image_x,
244 T* image_y) {
245 T x = normalized_x;
246 T y = normalized_y;
247
248 // Apply distortion to the normalized points to get (xd, yd).
249 T x2 = x * x;
250 T y2 = y * y;
251 T xy2 = T(2) * x * y;
252 T r2 = x2 + y2;
253 T r_coeff = T(1) + (((k4 * r2 + k3) * r2 + k2) * r2 + k1) * r2;
254 T tx = p1 * (r2 + T(2) * x2) + p2 * xy2;
255 T ty = p2 * (r2 + T(2) * y2) + p1 * xy2;
256 T xd = x * r_coeff + tx;
257 T yd = y * r_coeff + ty;
258
259 // Apply focal length and principal point to get the final image coordinates.
260 *image_x = focal_length_x * xd + principal_point_x;
261 *image_y = focal_length_y * yd + principal_point_y;
262} // namespace libmv
263
264} // namespace libmv
265
266#endif // LIBMV_SIMPLE_PIPELINE_DISTORTION_MODELS_H_
#define T
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)
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 p1, const double p2, 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)
@ DISTORTION_MODEL_POLYNOMIAL
@ DISTORTION_MODEL_DIVISION
@ DISTORTION_MODEL_BROWN
@ DISTORTION_MODEL_NUKE
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 &p1, const T &p2, const T &image_x, const T &image_y, T *normalized_x, T *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)