Calibration.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Calibration.cpp
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 // Changes: 17.11.2006, Stefanie Speidel
41 // * improved calculations for radial lens distortion
42 // * added calculations for tangential lens distortion
43 // ****************************************************************************
44 
45 
46 // ****************************************************************************
47 // Includes
48 // ****************************************************************************
49 
50 #include <new> // for explicitly using correct new/delete operators on VC DSPs
51 
52 #include "Calibration.h"
53 #include "Math/Matd.h"
54 #include "Math/Vecd.h"
55 #include "Image/ByteImage.h"
56 
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <math.h>
60 
61 
62 
63 
64 // ****************************************************************************
65 // Constructor / Destructor
66 // ****************************************************************************
67 
69 {
72 
77 
78  for (int i = 0; i < 4; i++)
80 }
81 
83 {
84 }
85 
86 
87 // ****************************************************************************
88 // Methods
89 // ****************************************************************************
90 
92 {
96  0, 0, 1);
97 }
98 
100 {
101  Mat3d K;
103 
106 }
107 
108 
110 {
114 }
115 
116 
117 void CCalibration::SetCameraParameters(float fx, float fy, float cx, float cy,
118  float d1, float d2, float d3, float d4,
119  const Mat3d &R, const Vec3d &t, int width, int height)
120 {
123 
126 
129 
134 
136 }
137 
138 void CCalibration::Set(const CCalibration &calibration)
139 {
140  const CCameraParameters &cameraParameters = calibration.GetCameraParameters();
141 
142  m_cameraParameters.width = cameraParameters.width;
143  m_cameraParameters.height = cameraParameters.height;
144 
147 
150 
151  m_cameraParameters.distortion[0] = cameraParameters.distortion[0];
152  m_cameraParameters.distortion[1] = cameraParameters.distortion[1];
153  m_cameraParameters.distortion[2] = cameraParameters.distortion[2];
154  m_cameraParameters.distortion[3] = cameraParameters.distortion[3];
155 
157 }
158 
159 void CCalibration::SetRotation(const Mat3d &rotation)
160 {
163 }
164 
165 void CCalibration::SetTranslation(const Vec3d &translation)
166 {
169 }
170 
171 void CCalibration::SetIntrinsicBase(float cx, float cy, float fx, float fy)
172 {
175 }
176 
177 void CCalibration::SetDistortion(float d1, float d2, float d3, float d4)
178 {
183 }
184 
185 
186 void CCalibration::WorldToImageCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters) const
187 {
188  Vec3d cameraPoint;
189  WorldToCameraCoordinates(worldPoint, cameraPoint);
190  CameraToImageCoordinates(cameraPoint, imagePoint, bUseDistortionParameters);
191 }
192 
193 void CCalibration::ImageToWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc, bool bUseDistortionParameters) const
194 {
195  Vec3d cameraPoint;
196  ImageToCameraCoordinates(imagePoint, cameraPoint, zc, bUseDistortionParameters);
197  CameraToWorldCoordinates(cameraPoint, worldPoint);
198 }
199 
200 void CCalibration::WorldToCameraCoordinates(const Vec3d &worldPoint, Vec3d &cameraPoint) const
201 {
203 }
204 
205 void CCalibration::CameraToWorldCoordinates(const Vec3d &cameraPoint, Vec3d &worldPoint) const
206 {
208 }
209 
210 void CCalibration::CameraToImageCoordinates(const Vec3d &cameraPoint, Vec2d &imagePoint, bool bUseDistortionParameters) const
211 {
212  imagePoint.x = m_cameraParameters.principalPoint.x + (cameraPoint.x * m_cameraParameters.focalLength.x) / cameraPoint.z;
213  imagePoint.y = m_cameraParameters.principalPoint.y + (cameraPoint.y * m_cameraParameters.focalLength.y) / cameraPoint.z;
214 
215  if (bUseDistortionParameters)
216  DistortImageCoordinates(imagePoint, imagePoint);
217 }
218 
219 void CCalibration::ImageToCameraCoordinates(const Vec2d &imagePoint_, Vec3d &cameraPoint, float zc, bool bUseDistortionParameters) const
220 {
221  Vec2d imagePoint;
222 
223  if (bUseDistortionParameters)
224  UndistortImageCoordinates(imagePoint_, imagePoint);
225  else
226  Math2d::SetVec(imagePoint, imagePoint_);
227 
228  cameraPoint.x = (imagePoint.x - m_cameraParameters.principalPoint.x) / m_cameraParameters.focalLength.x * zc;
229  cameraPoint.y = (imagePoint.y - m_cameraParameters.principalPoint.y) / m_cameraParameters.focalLength.y * zc;
230  cameraPoint.z = zc;
231 }
232 
233 void CCalibration::UndistortImageCoordinates(const Vec2d &distortedImagePoint, Vec2d &undistortedImagePoint) const
234 {
235  // distortion parameters
236  const float d1 = m_cameraParameters.distortion[0];
237  const float d2 = m_cameraParameters.distortion[1];
238  const float d3 = m_cameraParameters.distortion[2];
239  const float d4 = m_cameraParameters.distortion[3];
240 
241  // undistort
242  const float x_const = (distortedImagePoint.x - m_cameraParameters.principalPoint.x) / m_cameraParameters.focalLength.x;
243  const float y_const = (distortedImagePoint.y - m_cameraParameters.principalPoint.y) / m_cameraParameters.focalLength.y;
244 
245  float x = x_const;
246  float y = y_const;
247 
248  for (int i = 0; i < 10; i++)
249  {
250  // radial distortion
251  const float rr = x * x + y * y;
252  const float factor = 1 + d1 * rr + d2 * rr * rr;
253 
254  // tangential distortion
255  const float dx = d3 * (2 * x * y) + d4 * (rr + 2 * x * x);
256  const float dy = d3 * (rr + 2 * y * y) + d4 * (2 * x * y);
257 
258  x = (x_const - dx) / factor;
259  y = (y_const - dy) / factor;
260  }
261 
262  undistortedImagePoint.x = x * m_cameraParameters.focalLength.x + m_cameraParameters.principalPoint.x;
263  undistortedImagePoint.y = y * m_cameraParameters.focalLength.y + m_cameraParameters.principalPoint.y;
264 }
265 
266 void CCalibration::DistortImageCoordinates(const Vec2d &undistortedImagePoint, Vec2d &distortedImagePoint) const
267 {
268  const float x = (undistortedImagePoint.x - m_cameraParameters.principalPoint.x) / m_cameraParameters.focalLength.x;
269  const float y = (undistortedImagePoint.y - m_cameraParameters.principalPoint.y) / m_cameraParameters.focalLength.y;
270 
271  // distortion parameters
272  const float d1 = m_cameraParameters.distortion[0];
273  const float d2 = m_cameraParameters.distortion[1];
274  const float d3 = m_cameraParameters.distortion[2];
275  const float d4 = m_cameraParameters.distortion[3];
276 
277  // radial distortion
278  const float rr = x * x + y * y;
279  const float factor = 1 + d1 * rr + d2 * rr * rr;
280 
281  // tangential distortion
282  const float dx = d3 * (2 * x * y) + d4 * (rr + 2 * x * x);
283  const float dy = d3 * (rr + 2 * y * y) + d4 * (2 * x * y);
284 
285  distortedImagePoint.x = m_cameraParameters.focalLength.x * (factor * x + dx) + m_cameraParameters.principalPoint.x;
286  distortedImagePoint.y = m_cameraParameters.focalLength.y * (factor * y + dy) + m_cameraParameters.principalPoint.y;
287 }
288 
289 // <obsolete names>
290 void CCalibration::GetCameraCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters) const
291 {
292  WorldToImageCoordinates(worldPoint, imagePoint, bUseDistortionParameters);
293 }
294 
295 void CCalibration::GetWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc, bool bUseDistortionParameters) const
296 {
297  ImageToWorldCoordinates(imagePoint, worldPoint, zc, bUseDistortionParameters);
298 }
299 
300 void CCalibration::UndistortCameraCoordinates(const Vec2d &distortedImagePoint, Vec2d &undistortedImagePoint) const
301 {
302  UndistortImageCoordinates(distortedImagePoint, undistortedImagePoint);
303 }
304 
305 void CCalibration::DistortCameraCoordinates(const Vec2d &undistortedImagePoint, Vec2d &distortedImagePoint) const
306 {
307  DistortImageCoordinates(undistortedImagePoint, distortedImagePoint);
308 }
309 // </obsolete names>
310 
311 
312 bool CCalibration::LoadCameraParameters(const char *pCameraParameterFileName, int nCamera, bool bSetExtrinsicToIdentity)
313 {
314  int i, nCameraCount = 0;
315 
316  FILE* f = fopen(pCameraParameterFileName, "r");
317  if(!f)
318  return false;
319 
320  if (fscanf(f, "%d", &nCameraCount) != 1 || nCameraCount < nCamera + 1)
321  {
322  fclose(f);
323  return false;
324  }
325 
326  float skip_value, w, h;
327 
328  for (i = 0; i < nCamera; i++)
329  for (int j = 0; j < 27; j++)
330  if (fscanf(f, "%f", &skip_value) != 1)
331  {
332  fclose(f);
333  return false;
334  }
335 
336  if (fscanf(f, "%f", &w) != 1) { fclose(f); return false; }
337  if (fscanf(f, "%f", &h) != 1) { fclose(f); return false; }
338  if (fscanf(f, "%f", &m_cameraParameters.focalLength.x) != 1) { fclose(f); return false; }
339  if (fscanf(f, "%f", &skip_value) != 1) { fclose(f); return false; }
340  if (fscanf(f, "%f", &m_cameraParameters.principalPoint.x) != 1) { fclose(f); return false; }
341  if (fscanf(f, "%f", &skip_value) != 1) { fclose(f); return false; }
342  if (fscanf(f, "%f", &m_cameraParameters.focalLength.y) != 1) { fclose(f); return false; }
343  if (fscanf(f, "%f", &m_cameraParameters.principalPoint.y) != 1) { fclose(f); return false; }
344  if (fscanf(f, "%f", &skip_value) != 1) { fclose(f); return false; }
345  if (fscanf(f, "%f", &skip_value) != 1) { fclose(f); return false; }
346  if (fscanf(f, "%f", &skip_value) != 1) { fclose(f); return false; }
347  for (i = 0; i < 4; i++)
348  if (fscanf(f, "%f", &m_cameraParameters.distortion[i]) != 1) { fclose(f); return false; }
349  if (fscanf(f, "%f", &m_cameraParameters.rotation.r1) != 1) { fclose(f); return false; }
350  if (fscanf(f, "%f", &m_cameraParameters.rotation.r2) != 1) { fclose(f); return false; }
351  if (fscanf(f, "%f", &m_cameraParameters.rotation.r3) != 1) { fclose(f); return false; }
352  if (fscanf(f, "%f", &m_cameraParameters.rotation.r4) != 1) { fclose(f); return false; }
353  if (fscanf(f, "%f", &m_cameraParameters.rotation.r5) != 1) { fclose(f); return false; }
354  if (fscanf(f, "%f", &m_cameraParameters.rotation.r6) != 1) { fclose(f); return false; }
355  if (fscanf(f, "%f", &m_cameraParameters.rotation.r7) != 1) { fclose(f); return false; }
356  if (fscanf(f, "%f", &m_cameraParameters.rotation.r8) != 1) { fclose(f); return false; }
357  if (fscanf(f, "%f", &m_cameraParameters.rotation.r9) != 1) { fclose(f); return false; }
358  if (fscanf(f, "%f", &m_cameraParameters.translation.x) != 1) { fclose(f); return false; }
359  if (fscanf(f, "%f", &m_cameraParameters.translation.y) != 1) { fclose(f); return false; }
360  if (fscanf(f, "%f", &m_cameraParameters.translation.z) != 1) { fclose(f); return false; }
361 
362  m_cameraParameters.width = int(w + 0.5f);
363  m_cameraParameters.height = int(h + 0.5f);
364 
365  fclose(f);
366 
367  if (bSetExtrinsicToIdentity)
368  {
371  }
372  else
373  {
375  }
376 
377  return true;
378 }
379 
380 bool CCalibration::SaveCameraParameters(const char *pFileName) const
381 {
382  const Mat3d &rotation = m_cameraParameters.rotation;
383  const Vec3d &translation = m_cameraParameters.translation;
384 
385  const float cx = m_cameraParameters.principalPoint.x;
386  const float cy = m_cameraParameters.principalPoint.y;
387  const float fx = m_cameraParameters.focalLength.x;
388  const float fy = m_cameraParameters.focalLength.y;
389 
390  const float d1 = m_cameraParameters.distortion[0];
391  const float d2 = m_cameraParameters.distortion[1];
392  const float d3 = m_cameraParameters.distortion[2];
393  const float d4 = m_cameraParameters.distortion[3];
394 
395  FILE *f = fopen(pFileName, "w");
396  if (!f)
397  return false;
398 
399  fprintf(f, "1\n\n");
400  fprintf(f, "%i %i %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ", m_cameraParameters.width, m_cameraParameters.height, fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
401  fprintf(f, "%.10f %.10f %.10f %.10f ", d1, d2, d3, d4);
402  fprintf(f, "%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ", rotation.r1, rotation.r2, rotation.r3, rotation.r4, rotation.r5, rotation.r6, rotation.r7, rotation.r8, rotation.r9);
403  fprintf(f, "%.10f %.10f %.10f\n", translation.x, translation.y, translation.z);
404 
405  fclose(f);
406 
407  return true;
408 }
409 
411 {
412  printf("cx/cy = %.10f / %.10f\nfx/fy = %.10f / %.10f\nt = %.10f %.10f %.10f\nR = %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f\nd1/d2/d3/d4 = %.10f %.10f %.10f %.10f\n\n",
433  );
434 }
float r4
Definition: Math3d.h:95
Struct containing all parameters of the camera model.
Definition: Calibration.h:133
Mat3d m_rotation_inverse
Rotation matrix of the inverted extrinsic transformation.
Definition: Calibration.h:443
GLdouble GLdouble t
Definition: glext.h:3219
void SetDistortion(float d1, float d2, float d3, float d4)
Sets the distortion parameters of the distortion model.
void ImageToCameraCoordinates(const Vec2d &imagePoint, Vec3d &cameraPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Transforms 2D image coordinates to 3D camera coordinates.
float r7
Definition: Math3d.h:95
CCameraParameters m_cameraParameters
Definition: Calibration.h:465
float y
Definition: Math2d.h:84
void SetCameraParameters(float fx, float fy, float cx, float cy, float d1, float d2, float d3, float d4, const Mat3d &R, const Vec3d &t, int width, int height)
Initializes the camera model, given a complete parameter set.
void UndistortImageCoordinates(const Vec2d &distortedImagePoint, Vec2d &undistortedImagePoint) const
Transforms 2D distorted image coordinates to 2D undistorted image coordinates.
float r1
Definition: Math3d.h:95
void CameraToWorldCoordinates(const Vec3d &cameraPoint, Vec3d &worldPoint) const
Transforms 3D camera coordinates to 3D world coordinates.
void Transpose(const Mat3d &matrix, Mat3d &result)
Definition: Math3d.cpp:635
bool LoadCameraParameters(const char *pCameraParameterFileName, int nCamera=0, bool bSetExtrinsicToIdentity=false)
Initializes the camera model, given a file path to a camera parameter file.
float r2
Definition: Math3d.h:95
float r3
Definition: Math3d.h:95
Data structure for the representation of a 3D vector.
Definition: Math3d.h:73
float x
Definition: Math3d.h:75
void CameraToImageCoordinates(const Vec3d &cameraPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Transforms 3D camera coordinates to 2D image coordinates.
void DistortImageCoordinates(const Vec2d &undistortedImagePoint, Vec2d &distortedImagePoint) const
Transforms 2D undistorted image coordinates to 2D distorted image coordinates.
float z
Definition: Math3d.h:75
void GetCalibrationMatrix(Mat3d &K) const
Sets up the calibration matrix K.
Definition: Calibration.cpp:91
void PrintCameraParameters() const
Prints all camera parameters in the console window (STDOUT).
GLenum GLint x
Definition: glext.h:3125
const CCameraParameters & GetCameraParameters() const
Gives access to the camera parameters.
Definition: Calibration.h:268
float x
Definition: Math2d.h:84
bool SaveCameraParameters(const char *pCameraParameterFileName) const
Writes the parameters of the camera model to camera parameter file.
const Vec3d zero_vec
Definition: Math3d.cpp:59
void SetVec(Vec3d &vec, float x, float y, float z)
Definition: Math3d.cpp:243
void GetCameraCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Deprecated.
void ImageToWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Transforms 2D image coordinates to 3D world coordinates.
float y
Definition: Math3d.h:75
float r6
Definition: Math3d.h:95
void MulVecScalar(const Vec3d &vec, float scalar, Vec3d &result)
Definition: Math3d.cpp:502
void GetProjectionMatrix(Mat3d &P1, Vec3d &p2) const
Sets up the projection matrix P.
Definition: Calibration.cpp:99
void SetIntrinsicBase(float cx, float cy, float fx, float fy)
Sets the principal point and the focal lengths.
void GetWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Deprecated.
void MulMatMat(const Mat3d &matrix1, const Mat3d &matrix2, Mat3d &result)
Definition: Math3d.cpp:444
void MulMatVec(const Mat3d &matrix, const Vec3d &vec, Vec3d &result)
Definition: Math3d.cpp:422
~CCalibration()
The destructor.
Definition: Calibration.cpp:82
void UndistortCameraCoordinates(const Vec2d &distortedCameraPoint, Vec2d &undistortedCameraPoint) const
Deprecated.
void DistortCameraCoordinates(const Vec2d &undistortedCameraPoint, Vec2d &distortedCameraPoint) const
Deprecated.
float r8
Definition: Math3d.h:95
float r9
Definition: Math3d.h:95
CCalibration()
The default constructor.
Definition: Calibration.cpp:68
Vec3d m_translation_inverse
Translation vector of the inverted extrinsic transformation.
Definition: Calibration.h:454
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
void Set(const CCalibration &calibration)
Initializes the camera model, given an instance of CCalibration.
float r5
Definition: Math3d.h:95
const Mat3d unit_mat
Definition: Math3d.cpp:60
GLenum GLint GLint y
Definition: glext.h:3125
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
void SetTranslation(const Vec3d &t)
Sets the extrinsic parameter t (translation vector).
void CalculateInverseTransformation()
void SetVec(Vec2d &vec, float x, float y)
Definition: Math2d.cpp:68
void WorldToCameraCoordinates(const Vec3d &worldPoint, Vec3d &cameraPoint) const
Transforms 3D world coordinates to 3D camera coordinates.
void SetRotation(const Mat3d &R)
Sets the extrinsic parameter R (rotation matrix).
GLfloat GLfloat p
Definition: glext.h:5178
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
Definition: Math3d.cpp:257
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3571
Camera model parameters and functions for a single camera.
Definition: Calibration.h:125
void WorldToImageCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Transforms 3D world coordinates to 2D image coordinates.


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27