Visual Servoing Platform  version 3.5.1 under development (2022-12-01)
vpCameraParameters.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Camera intrinsic parameters.
33  *
34  * Authors:
35  * Eric Marchand
36  * Anthony Saunier
37  *
38  *****************************************************************************/
39 
47 #include <cmath>
48 #include <iomanip>
49 #include <iostream>
50 #include <limits>
51 #include <sstream>
52 #include <visp3/core/vpCameraParameters.h>
53 #include <visp3/core/vpDebug.h>
54 #include <visp3/core/vpException.h>
55 #include <visp3/core/vpRotationMatrix.h>
56 
57 const double vpCameraParameters::DEFAULT_PX_PARAMETER = 600.0;
58 const double vpCameraParameters::DEFAULT_PY_PARAMETER = 600.0;
59 const double vpCameraParameters::DEFAULT_U0_PARAMETER = 192.0;
60 const double vpCameraParameters::DEFAULT_V0_PARAMETER = 144.0;
61 const double vpCameraParameters::DEFAULT_KUD_PARAMETER = 0.0;
62 const double vpCameraParameters::DEFAULT_KDU_PARAMETER = 0.0;
63 const vpCameraParameters::vpCameraParametersProjType vpCameraParameters::DEFAULT_PROJ_TYPE =
65 
73  : px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER), u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
74  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER), m_dist_coefs(), width(0), height(0), isFov(false),
75  m_hFovAngle(0), m_vFovAngle(0), fovNormals(), inv_px(1. / DEFAULT_PX_PARAMETER), inv_py(1. / DEFAULT_PY_PARAMETER),
76  projModel(DEFAULT_PROJ_TYPE)
77 {
78  init();
79 }
80 
85  : px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER), u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
86  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER), m_dist_coefs(), width(0), height(0), isFov(false),
87  m_hFovAngle(0), m_vFovAngle(0), fovNormals(), inv_px(1. / DEFAULT_PX_PARAMETER), inv_py(1. / DEFAULT_PY_PARAMETER),
88  projModel(DEFAULT_PROJ_TYPE)
89 {
90  init(c);
91 }
92 
100 vpCameraParameters::vpCameraParameters(double cam_px, double cam_py, double cam_u0, double cam_v0)
101  : px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER), u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
102  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER), m_dist_coefs(), width(0), height(0), isFov(false),
103  m_hFovAngle(0), m_vFovAngle(0), fovNormals(), inv_px(1. / DEFAULT_PX_PARAMETER), inv_py(1. / DEFAULT_PY_PARAMETER),
104  projModel(DEFAULT_PROJ_TYPE)
105 {
106  initPersProjWithoutDistortion(cam_px, cam_py, cam_u0, cam_v0);
107 }
108 
118 vpCameraParameters::vpCameraParameters(double cam_px, double cam_py, double cam_u0, double cam_v0, double cam_kud,
119  double cam_kdu)
120  : px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER), u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
121  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER), m_dist_coefs(), width(0), height(0), isFov(false),
122  m_hFovAngle(0), m_vFovAngle(0), fovNormals(), inv_px(1. / DEFAULT_PX_PARAMETER), inv_py(1. / DEFAULT_PY_PARAMETER),
123  projModel(DEFAULT_PROJ_TYPE)
124 {
125  initPersProjWithDistortion(cam_px, cam_py, cam_u0, cam_v0, cam_kud, cam_kdu);
126 }
127 
136 vpCameraParameters::vpCameraParameters(double cam_px, double cam_py, double cam_u0, double cam_v0,
137  const std::vector<double> &coefficients)
138  : px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER), u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
139  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER), m_dist_coefs(), width(0), height(0), isFov(false),
140  m_hFovAngle(0), m_vFovAngle(0), fovNormals(), inv_px(1. / DEFAULT_PX_PARAMETER), inv_py(1. / DEFAULT_PY_PARAMETER),
141  projModel(DEFAULT_PROJ_TYPE)
142 {
143  initProjWithKannalaBrandtDistortion(cam_px, cam_py, cam_u0, cam_v0, coefficients);
144 }
145 
150 {
151  if (fabs(this->px) < 1e-6) {
152  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
153  }
154  if (fabs(this->py) < 1e-6) {
155  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
156  }
157  this->inv_px = 1. / this->px;
158  this->inv_py = 1. / this->py;
159 }
160 
198 void vpCameraParameters::initPersProjWithoutDistortion(double cam_px, double cam_py, double cam_u0, double cam_v0)
199 {
201 
202  this->px = cam_px;
203  this->py = cam_py;
204  this->u0 = cam_u0;
205  this->v0 = cam_v0;
206  this->kud = 0;
207  this->kdu = 0;
208 
209  if (fabs(px) < 1e-6) {
210  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
211  }
212  if (fabs(py) < 1e-6) {
213  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
214  }
215  this->inv_px = 1. / px;
216  this->inv_py = 1. / py;
217 }
218 
261 void vpCameraParameters::initPersProjWithDistortion(double cam_px, double cam_py, double cam_u0, double cam_v0,
262  double cam_kud, double cam_kdu)
263 {
265 
266  this->px = cam_px;
267  this->py = cam_py;
268  this->u0 = cam_u0;
269  this->v0 = cam_v0;
270  this->kud = cam_kud;
271  this->kdu = cam_kdu;
272 
273  if (fabs(px) < 1e-6) {
274  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
275  }
276  if (fabs(py) < 1e-6) {
277  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
278  }
279  this->inv_px = 1. / px;
280  this->inv_py = 1. / py;
281 }
282 
289 void vpCameraParameters::initProjWithKannalaBrandtDistortion(double cam_px, double cam_py, double cam_u0, double cam_v0,
290  const std::vector<double> &coefficients)
291 {
293 
294  this->px = cam_px;
295  this->py = cam_py;
296  this->u0 = cam_u0;
297  this->v0 = cam_v0;
298 
299  if (fabs(px) < 1e-6) {
300  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
301  }
302  if (fabs(py) < 1e-6) {
303  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
304  }
305  this->inv_px = 1. / px;
306  this->inv_py = 1. / py;
307 
308  this->m_dist_coefs = coefficients;
309 }
310 
317 
321 void vpCameraParameters::init(const vpCameraParameters &c) { *this = c; }
322 
338 {
339  if (_K.getRows() != 3 || _K.getCols() != 3) {
340  throw vpException(vpException::dimensionError, "bad size for calibration matrix");
341  }
342  if (std::fabs(_K[2][2] - 1.0) > std::numeric_limits<double>::epsilon()) {
343  throw vpException(vpException::badValue, "bad value: K[2][2] must be equal to 1");
344  }
345  initPersProjWithoutDistortion(_K[0][0], _K[1][1], _K[0][2], _K[1][2]);
346 }
347 
381 void vpCameraParameters::initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov,
382  const double &vfov)
383 {
385  u0 = (double)w / 2.;
386  v0 = (double)h / 2.;
387  px = u0 / tan(hfov / 2);
388  py = v0 / tan(vfov / 2);
389  kud = 0;
390  kdu = 0;
391  inv_px = 1. / px;
392  inv_py = 1. / py;
393  computeFov(w, h);
394 }
395 
400 {
401  projModel = cam.projModel;
402  px = cam.px;
403  py = cam.py;
404  u0 = cam.u0;
405  v0 = cam.v0;
406  kud = cam.kud;
407  kdu = cam.kdu;
408  m_dist_coefs = cam.m_dist_coefs;
409 
410  inv_px = cam.inv_px;
411  inv_py = cam.inv_py;
412 
413  isFov = cam.isFov;
414  m_hFovAngle = cam.m_hFovAngle;
415  m_vFovAngle = cam.m_vFovAngle;
416  width = cam.width;
417  height = cam.height;
418  fovNormals = cam.fovNormals;
419 
420  return *this;
421 }
422 
427 {
428  if (projModel != c.projModel)
429  return false;
430 
431  if (!vpMath::equal(px, c.px, std::numeric_limits<double>::epsilon()) ||
432  !vpMath::equal(py, c.py, std::numeric_limits<double>::epsilon()) ||
433  !vpMath::equal(u0, c.u0, std::numeric_limits<double>::epsilon()) ||
434  !vpMath::equal(v0, c.v0, std::numeric_limits<double>::epsilon()) ||
435  !vpMath::equal(kud, c.kud, std::numeric_limits<double>::epsilon()) ||
436  !vpMath::equal(kdu, c.kdu, std::numeric_limits<double>::epsilon()) ||
437  !vpMath::equal(inv_px, c.inv_px, std::numeric_limits<double>::epsilon()) ||
438  !vpMath::equal(inv_py, c.inv_py, std::numeric_limits<double>::epsilon()))
439  return false;
440 
441  for (unsigned int i = 0; i < m_dist_coefs.size(); i++)
442  if (!vpMath::equal(m_dist_coefs[i], c.m_dist_coefs[i], std::numeric_limits<double>::epsilon()))
443  return false;
444 
445  if (isFov != c.isFov || !vpMath::equal(m_hFovAngle, c.m_hFovAngle, std::numeric_limits<double>::epsilon()) ||
446  !vpMath::equal(m_vFovAngle, c.m_vFovAngle, std::numeric_limits<double>::epsilon()) || width != c.width ||
447  height != c.height)
448  return false;
449 
450  if (fovNormals.size() != c.fovNormals.size())
451  return false;
452 
453  std::vector<vpColVector>::const_iterator it1 = fovNormals.begin();
454  std::vector<vpColVector>::const_iterator it2 = c.fovNormals.begin();
455  for (; it1 != fovNormals.end() && it2 != c.fovNormals.end(); ++it1, ++it2) {
456  if (*it1 != *it2)
457  return false;
458  }
459 
460  return true;
461 }
462 
466 bool vpCameraParameters::operator!=(const vpCameraParameters &c) const { return !(*this == c); }
467 
474 void vpCameraParameters::computeFov(const unsigned int &w, const unsigned int &h)
475 {
476  if ((!isFov || w != width || h != height) && w != 0 && h != 0) {
477  fovNormals = std::vector<vpColVector>(4);
478 
479  isFov = true;
480 
481  double hFovAngle = atan(((double)w - u0) * (1.0 / px));
482  double vFovAngle = atan((v0) * (1.0 / py));
483  double minushFovAngle = atan((u0) * (1.0 / px));
484  double minusvFovAngle = atan(((double)h - v0) * (1.0 / py));
485 
486  width = w;
487  height = h;
488 
489  vpColVector n(3);
490  n = 0;
491  n[0] = 1.0;
492 
493  vpRotationMatrix Rleft(0, -minushFovAngle, 0);
494  vpRotationMatrix Rright(0, hFovAngle, 0);
495 
496  vpColVector nLeft, nRight;
497 
498  nLeft = Rleft * (-n);
499  fovNormals[0] = nLeft.normalize();
500 
501  nRight = Rright * n;
502  fovNormals[1] = nRight.normalize();
503 
504  n = 0;
505  n[1] = 1.0;
506 
507  vpRotationMatrix Rup(vFovAngle, 0, 0);
508  vpRotationMatrix Rdown(-minusvFovAngle, 0, 0);
509 
510  vpColVector nUp, nDown;
511 
512  nUp = Rup * (-n);
513  fovNormals[2] = nUp.normalize();
514 
515  nDown = Rdown * n;
516  fovNormals[3] = nDown.normalize();
517 
518  m_hFovAngle = hFovAngle + minushFovAngle;
519  m_vFovAngle = vFovAngle + minusvFovAngle;
520  }
521 }
522 
535 {
536  vpMatrix K(3, 3, 0.);
537  K[0][0] = px;
538  K[1][1] = py;
539  K[0][2] = u0;
540  K[1][2] = v0;
541  K[2][2] = 1.0;
542 
543  return K;
544 }
557 {
558  vpMatrix K_inv(3, 3, 0.);
559  K_inv[0][0] = inv_px;
560  K_inv[1][1] = inv_py;
561  K_inv[0][2] = -u0 * inv_px;
562  K_inv[1][2] = -v0 * inv_py;
563  K_inv[2][2] = 1.0;
564 
565  return K_inv;
566 }
567 
574 {
575  std::ios::fmtflags original_flags(std::cout.flags());
576  switch (projModel) {
578  std::cout.precision(10);
579  std::cout << "Camera parameters for perspective projection without distortion:" << std::endl;
580  std::cout << " px = " << px << "\t py = " << py << std::endl;
581  std::cout << " u0 = " << u0 << "\t v0 = " << v0 << std::endl;
582  break;
584  std::cout.precision(10);
585  std::cout << "Camera parameters for perspective projection with distortion:" << std::endl;
586  std::cout << " px = " << px << "\t py = " << py << std::endl;
587  std::cout << " u0 = " << u0 << "\t v0 = " << v0 << std::endl;
588  std::cout << " kud = " << kud << std::endl;
589  std::cout << " kdu = " << kdu << std::endl;
590  break;
592  std::cout << " Coefficients: ";
593  for (unsigned int i = 0; i < m_dist_coefs.size(); i++)
594  std::cout << " " << m_dist_coefs[i];
595  std::cout << std::endl;
596  break;
597  }
598  // Restore ostream format
599  std::cout.flags(original_flags);
600 }
608 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpCameraParameters &cam)
609 {
610  switch (cam.get_projModel()) {
612  os << "Camera parameters for perspective projection without distortion:" << std::endl;
613  os << " px = " << cam.get_px() << "\t py = " << cam.get_py() << std::endl;
614  os << " u0 = " << cam.get_u0() << "\t v0 = " << cam.get_v0() << std::endl;
615  break;
617  std::ios_base::fmtflags original_flags = os.flags();
618  os.precision(10);
619  os << "Camera parameters for perspective projection with distortion:" << std::endl;
620  os << " px = " << cam.get_px() << "\t py = " << cam.get_py() << std::endl;
621  os << " u0 = " << cam.get_u0() << "\t v0 = " << cam.get_v0() << std::endl;
622  os << " kud = " << cam.get_kud() << std::endl;
623  os << " kdu = " << cam.get_kdu() << std::endl;
624  os.flags(original_flags); // restore os to standard state
625  } break;
627  os << "Camera parameters for projection with Kannala-Brandt distortion:" << std::endl;
628  os << " px = " << cam.get_px() << "\t py = " << cam.get_py() << std::endl;
629  os << " u0 = " << cam.get_u0() << "\t v0 = " << cam.get_v0() << std::endl;
630  os << " Coefficients: ";
631  std::vector<double> tmp_coefs = cam.getKannalaBrandtDistortionCoefficients();
632  for (unsigned int i = 0; i < tmp_coefs.size(); i++)
633  os << " " << tmp_coefs[i];
634  os << std::endl;
635  } break;
636  }
637  return os;
638 }
unsigned int getCols() const
Definition: vpArray2D.h:281
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:494
unsigned int getRows() const
Definition: vpArray2D.h:291
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
std::vector< double > getKannalaBrandtDistortionCoefficients() const
void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov)
vpMatrix get_K_inverse() const
vpMatrix get_K() const
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
vpCameraParameters & operator=(const vpCameraParameters &c)
void computeFov(const unsigned int &w, const unsigned int &h)
bool operator==(const vpCameraParameters &c) const
double get_kdu() const
void initFromCalibrationMatrix(const vpMatrix &_K)
bool operator!=(const vpCameraParameters &c) const
vpCameraParametersProjType get_projModel() const
void init()
basic initialization with the default parameters
void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0, const std::vector< double > &distortion_coefficients)
double get_kud() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
vpColVector & normalize()
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
@ dimensionError
Bad dimension.
Definition: vpException.h:95
@ divideByZeroError
Division by zero.
Definition: vpException.h:94
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:364
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
Implementation of a rotation matrix and operations on such kind of matrices.