Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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), width(0), height(0), isFov(false), m_hFovAngle(0),
75  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), width(0), height(0), isFov(false), m_hFovAngle(0),
87  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), width(0), height(0), isFov(false), m_hFovAngle(0),
103  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,
119  double cam_v0, double cam_kud, 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), width(0), height(0), isFov(false), m_hFovAngle(0),
122  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 
132 {
133  if (fabs(this->px) < 1e-6) {
134  vpERROR_TRACE("Camera parameter px = 0");
135  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
136  }
137  if (fabs(this->py) < 1e-6) {
138  vpERROR_TRACE("Camera parameter px = 0");
139  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
140  }
141  this->inv_px = 1. / this->px;
142  this->inv_py = 1. / this->py;
143 }
144 
182 void vpCameraParameters::initPersProjWithoutDistortion(double cam_px, double cam_py, double cam_u0,
183  double cam_v0)
184 {
186 
187  this->px = cam_px;
188  this->py = cam_py;
189  this->u0 = cam_u0;
190  this->v0 = cam_v0;
191  this->kud = 0;
192  this->kdu = 0;
193 
194  if (fabs(px) < 1e-6) {
195  vpERROR_TRACE("Camera parameter px = 0");
196  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
197  }
198  if (fabs(py) < 1e-6) {
199  vpERROR_TRACE("Camera parameter px = 0");
200  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
201  }
202  this->inv_px = 1. / px;
203  this->inv_py = 1. / py;
204 }
205 
248 void vpCameraParameters::initPersProjWithDistortion(double cam_px, double cam_py, double cam_u0,
249  double cam_v0, double cam_kud, double cam_kdu)
250 {
252 
253  this->px = cam_px;
254  this->py = cam_py;
255  this->u0 = cam_u0;
256  this->v0 = cam_v0;
257  this->kud = cam_kud;
258  this->kdu = cam_kdu;
259 
260  if (fabs(px) < 1e-6) {
261  vpERROR_TRACE("Camera parameter px = 0");
262  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
263  }
264  if (fabs(py) < 1e-6) {
265  vpERROR_TRACE("Camera parameter px = 0");
266  throw(vpException(vpException::divideByZeroError, "Camera parameter px = 0"));
267  }
268  this->inv_px = 1. / px;
269  this->inv_py = 1. / py;
270 }
271 
278 
282 void vpCameraParameters::init(const vpCameraParameters &c) { *this = c; }
283 
299 {
300  if (_K.getRows() != 3 || _K.getCols() != 3) {
301  throw vpException(vpException::dimensionError, "bad size for calibration matrix");
302  }
303  if (std::fabs(_K[2][2] - 1.0) > std::numeric_limits<double>::epsilon()) {
304  throw vpException(vpException::badValue, "bad value: K[2][2] must be equal to 1");
305  }
306  initPersProjWithoutDistortion(_K[0][0], _K[1][1], _K[0][2], _K[1][2]);
307 }
308 
342 void vpCameraParameters::initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov,
343  const double &vfov)
344 {
346  u0 = (double)w / 2.;
347  v0 = (double)h / 2.;
348  px = u0 / tan(hfov / 2);
349  py = v0 / tan(vfov / 2);
350  kud = 0;
351  kdu = 0;
352  inv_px = 1. / px;
353  inv_py = 1. / py;
354  computeFov(w, h);
355 }
356 
361 {
362  projModel = cam.projModel;
363  px = cam.px;
364  py = cam.py;
365  u0 = cam.u0;
366  v0 = cam.v0;
367  kud = cam.kud;
368  kdu = cam.kdu;
369 
370  inv_px = cam.inv_px;
371  inv_py = cam.inv_py;
372 
373  isFov = cam.isFov;
374  m_hFovAngle = cam.m_hFovAngle;
375  m_vFovAngle = cam.m_vFovAngle;
376  width = cam.width;
377  height = cam.height;
378  fovNormals = cam.fovNormals;
379 
380  return *this;
381 }
382 
387  if (projModel != c.projModel)
388  return false;
389 
390  if (!vpMath::equal(px, c.px, std::numeric_limits<double>::epsilon()) ||
391  !vpMath::equal(py, c.py, std::numeric_limits<double>::epsilon()) ||
392  !vpMath::equal(u0, c.u0, std::numeric_limits<double>::epsilon()) ||
393  !vpMath::equal(v0, c.v0, std::numeric_limits<double>::epsilon()) ||
394  !vpMath::equal(kud, c.kud, std::numeric_limits<double>::epsilon()) ||
395  !vpMath::equal(kdu, c.kdu, std::numeric_limits<double>::epsilon()) ||
396  !vpMath::equal(inv_px, c.inv_px, std::numeric_limits<double>::epsilon()) ||
397  !vpMath::equal(inv_py, c.inv_py, std::numeric_limits<double>::epsilon()))
398  return false;
399 
400  if (isFov != c.isFov ||
401  !vpMath::equal(m_hFovAngle, c.m_hFovAngle, std::numeric_limits<double>::epsilon()) ||
402  !vpMath::equal(m_vFovAngle, c.m_vFovAngle, std::numeric_limits<double>::epsilon()) ||
403  width != c.width || height != c.height)
404  return false;
405 
406  if (fovNormals.size() != c.fovNormals.size())
407  return false;
408 
409  std::vector<vpColVector>::const_iterator it1 = fovNormals.begin();
410  std::vector<vpColVector>::const_iterator it2 = c.fovNormals.begin();
411  for (; it1 != fovNormals.end() && it2 != c.fovNormals.end(); ++it1, ++it2) {
412  if (*it1 != *it2)
413  return false;
414  }
415 
416  return true;
417 }
418 
423  return !(*this == c);
424 }
425 
432 void vpCameraParameters::computeFov(const unsigned int &w, const unsigned int &h)
433 {
434  if ((!isFov || w != width || h != height) && w != 0 && h != 0) {
435  fovNormals = std::vector<vpColVector>(4);
436 
437  isFov = true;
438 
439  double hFovAngle = atan(((double)w - u0) * (1.0 / px));
440  double vFovAngle = atan((v0) * (1.0 / py));
441  double minushFovAngle = atan((u0) * (1.0 / px));
442  double minusvFovAngle = atan(((double)h - v0) * (1.0 / py));
443 
444  width = w;
445  height = h;
446 
447  vpColVector n(3);
448  n = 0;
449  n[0] = 1.0;
450 
451  vpRotationMatrix Rleft(0, -minushFovAngle, 0);
452  vpRotationMatrix Rright(0, hFovAngle, 0);
453 
454  vpColVector nLeft, nRight;
455 
456  nLeft = Rleft * (-n);
457  fovNormals[0] = nLeft.normalize();
458 
459  nRight = Rright * n;
460  fovNormals[1] = nRight.normalize();
461 
462  n = 0;
463  n[1] = 1.0;
464 
465  vpRotationMatrix Rup(vFovAngle, 0, 0);
466  vpRotationMatrix Rdown(-minusvFovAngle, 0, 0);
467 
468  vpColVector nUp, nDown;
469 
470  nUp = Rup * (-n);
471  fovNormals[2] = nUp.normalize();
472 
473  nDown = Rdown * n;
474  fovNormals[3] = nDown.normalize();
475 
476  m_hFovAngle = hFovAngle + minushFovAngle;
477  m_vFovAngle = vFovAngle + minusvFovAngle;
478  }
479 }
480 
493 {
494  vpMatrix K(3, 3, 0.);
495  K[0][0] = px;
496  K[1][1] = py;
497  K[0][2] = u0;
498  K[1][2] = v0;
499  K[2][2] = 1.0;
500 
501  return K;
502 }
515 {
516  vpMatrix K_inv(3, 3, 0.);
517  K_inv[0][0] = inv_px;
518  K_inv[1][1] = inv_py;
519  K_inv[0][2] = -u0 * inv_px;
520  K_inv[1][2] = -v0 * inv_py;
521  K_inv[2][2] = 1.0;
522 
523  return K_inv;
524 }
525 
532 {
533  std::ios::fmtflags original_flags(std::cout.flags());
534  switch (projModel) {
536  std::cout.precision(10);
537  std::cout << "Camera parameters for perspective projection without distortion:" << std::endl;
538  std::cout << " px = " << px << "\t py = " << py << std::endl;
539  std::cout << " u0 = " << u0 << "\t v0 = " << v0 << std::endl;
540  break;
542  std::cout.precision(10);
543  std::cout << "Camera parameters for perspective projection with distortion:" << std::endl;
544  std::cout << " px = " << px << "\t py = " << py << std::endl;
545  std::cout << " u0 = " << u0 << "\t v0 = " << v0 << std::endl;
546  std::cout << " kud = " << kud << std::endl;
547  std::cout << " kdu = " << kdu << std::endl;
548  break;
549  }
550  // Restore ostream format
551  std::cout.flags(original_flags);
552 }
560 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpCameraParameters &cam)
561 {
562  switch (cam.get_projModel()) {
564  os << "Camera parameters for perspective projection without distortion:" << std::endl;
565  os << " px = " << cam.get_px() << "\t py = " << cam.get_py() << std::endl;
566  os << " u0 = " << cam.get_u0() << "\t v0 = " << cam.get_v0() << std::endl;
567  break;
569  std::ios_base::fmtflags original_flags = os.flags();
570  os.precision(10);
571  os << "Camera parameters for perspective projection with distortion:" << std::endl;
572  os << " px = " << cam.get_px() << "\t py = " << cam.get_py() << std::endl;
573  os << " u0 = " << cam.get_u0() << "\t v0 = " << cam.get_v0() << std::endl;
574  os << " kud = " << cam.get_kud() << std::endl;
575  os << " kdu = " << cam.get_kdu() << std::endl;
576 
577  os.flags(original_flags); // restore os to standard state
578  break;
579  }
580  return os;
581 }
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
void initFromCalibrationMatrix(const vpMatrix &_K)
void init()
basic initialization with the default parameters
double get_kdu() const
vpCameraParametersProjType get_projModel() const
#define vpERROR_TRACE
Definition: vpDebug.h:393
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:296
error that can be emited by ViSP classes.
Definition: vpException.h:71
unsigned int getRows() const
Definition: vpArray2D.h:289
vpMatrix get_K() const
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
bool operator!=(const vpCameraParameters &c) const
Implementation of a rotation matrix and operations on such kind of matrices.
unsigned int getCols() const
Definition: vpArray2D.h:279
vpColVector & normalize()
void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov)
bool operator==(const vpCameraParameters &c) const
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpCameraParameters &cam)
double get_kud() const
vpCameraParameters & operator=(const vpCameraParameters &c)
vpMatrix get_K_inverse() const
void computeFov(const unsigned int &w, const unsigned int &h)