ViSP  2.10.0
vpCameraParameters.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCameraParameters.cpp 5215 2015-01-27 19:03:32Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Camera intrinsic parameters.
36  *
37  * Authors:
38  * Eric Marchand
39  * Anthony Saunier
40  *
41  *****************************************************************************/
42 
43 
51 #include <visp/vpCameraParameters.h>
52 #include <visp/vpDebug.h>
53 #include <visp/vpException.h>
54 #include <visp/vpRotationMatrix.h>
55 #include <cmath>
56 #include <limits>
57 #include <iostream>
58 #include <sstream>
59 #include <iomanip>
60 
61 const double vpCameraParameters::DEFAULT_PX_PARAMETER = 600.0;
62 const double vpCameraParameters::DEFAULT_PY_PARAMETER = 600.0;
63 const double vpCameraParameters::DEFAULT_U0_PARAMETER = 192.0;
64 const double vpCameraParameters::DEFAULT_V0_PARAMETER = 144.0;
65 const double vpCameraParameters::DEFAULT_KUD_PARAMETER = 0.0;
66 const double vpCameraParameters::DEFAULT_KDU_PARAMETER = 0.0;
68  vpCameraParameters::DEFAULT_PROJ_TYPE =
70 
78  :
79  px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER),
80  u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
81  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER),
82  width(0), height(0),
83  isFov(false), m_hFovAngle(0), m_vFovAngle(0), fovNormals(),
84  inv_px(1./DEFAULT_PX_PARAMETER), inv_py(1./DEFAULT_PY_PARAMETER),
85  projModel(DEFAULT_PROJ_TYPE)
86 {
87  init() ;
88 }
89 
94  :
95  px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER),
96  u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
97  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER),
98  width(0), height(0),
99  isFov(false), m_hFovAngle(0), m_vFovAngle(0), fovNormals(),
100  inv_px(1./DEFAULT_PX_PARAMETER), inv_py(1./DEFAULT_PY_PARAMETER),
101  projModel(DEFAULT_PROJ_TYPE)
102 {
103  init(c) ;
104 }
105 
113 vpCameraParameters::vpCameraParameters(const double cam_px, const double cam_py,
114  const double cam_u0, const double cam_v0)
115  :
116  px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER),
117  u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
118  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER),
119  width(0), height(0),
120  isFov(false), m_hFovAngle(0), m_vFovAngle(0), fovNormals(),
121  inv_px(1./DEFAULT_PX_PARAMETER), inv_py(1./DEFAULT_PY_PARAMETER),
122  projModel(DEFAULT_PROJ_TYPE)
123 {
124  initPersProjWithoutDistortion(cam_px,cam_py,cam_u0,cam_v0) ;
125 }
126 
136 vpCameraParameters::vpCameraParameters(const double cam_px, const double cam_py,
137  const double cam_u0, const double cam_v0,
138  const double cam_kud, const double cam_kdu)
139  :
140  px(DEFAULT_PX_PARAMETER), py(DEFAULT_PY_PARAMETER),
141  u0(DEFAULT_U0_PARAMETER), v0(DEFAULT_V0_PARAMETER),
142  kud(DEFAULT_KUD_PARAMETER), kdu(DEFAULT_KDU_PARAMETER),
143  width(0), height(0),
144  isFov(false), m_hFovAngle(0), m_vFovAngle(0), fovNormals(),
145  inv_px(1./DEFAULT_PX_PARAMETER), inv_py(1./DEFAULT_PY_PARAMETER),
146  projModel(DEFAULT_PROJ_TYPE)
147 {
148  initPersProjWithDistortion(cam_px,cam_py,cam_u0,cam_v0,cam_kud,cam_kdu) ;
149 }
150 
154 void
156 {
157  if (fabs(this->px)<1e-6)
158  {
159  vpERROR_TRACE("Camera parameter px = 0") ;
161  "Camera parameter px = 0")) ;
162  }
163  if (fabs(this->py)<1e-6)
164  {
165  vpERROR_TRACE("Camera parameter px = 0") ;
167  "Camera parameter px = 0")) ;
168  }
169  this->inv_px = 1./this->px;
170  this->inv_py = 1./this->py;
171 }
172 
209 void
210 vpCameraParameters::initPersProjWithoutDistortion(const double cam_px, const double cam_py,
211  const double cam_u0, const double cam_v0)
212 {
214 
215  this->px = cam_px ;
216  this->py = cam_py ;
217  this->u0 = cam_u0 ;
218  this->v0 = cam_v0 ;
219  this->kud = 0 ;
220  this->kdu = 0 ;
221 
222  if (fabs(px)<1e-6)
223  {
224  vpERROR_TRACE("Camera parameter px = 0") ;
226  "Camera parameter px = 0")) ;
227  }
228  if (fabs(py)<1e-6)
229  {
230  vpERROR_TRACE("Camera parameter px = 0") ;
232  "Camera parameter px = 0")) ;
233  }
234  this->inv_px = 1./px;
235  this->inv_py = 1./py;
236 }
237 
278 void
279 vpCameraParameters::initPersProjWithDistortion(const double cam_px, const double cam_py,
280  const double cam_u0, const double cam_v0,
281  const double cam_kud, const double cam_kdu)
282 {
284 
285  this->px = cam_px ;
286  this->py = cam_py ;
287  this->u0 = cam_u0 ;
288  this->v0 = cam_v0 ;
289  this->kud = cam_kud ;
290  this->kdu = cam_kdu ;
291 
292  if (fabs(px)<1e-6)
293  {
294  vpERROR_TRACE("Camera parameter px = 0") ;
296  "Camera parameter px = 0")) ;
297  }
298  if (fabs(py)<1e-6)
299  {
300  vpERROR_TRACE("Camera parameter px = 0") ;
302  "Camera parameter px = 0")) ;
303  }
304  this->inv_px = 1./px;
305  this->inv_py = 1./py;
306 }
307 
314 {
315 }
316 
320 void
322 {
323  *this = c ;
324 }
325 
326 
341 void
343 {
344  if(_K.getRows() != 3 || _K.getCols() != 3 ){
345  throw vpException(vpException::dimensionError, "bad size for calibration matrix");
346  }
347  if( std::fabs(_K[2][2] - 1.0) > std::numeric_limits<double>::epsilon()){
348  throw vpException(vpException::badValue, "bad value: K[2][2] must be equal to 1");
349  }
350  initPersProjWithoutDistortion (_K[0][0], _K[1][1], _K[0][2], _K[1][2]);
351 }
352 
387 void
388 vpCameraParameters::initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov)
389 {
391  u0 = (double)w/2.;
392  v0 = (double)h/2.;
393  px = u0 / tan(hfov/2);
394  py = v0 / tan(vfov/2);
395  kud = 0;
396  kdu = 0;
397  inv_px = 1./px;
398  inv_py = 1./py;
399  computeFov(w, h);
400 }
401 
407 {
408  projModel = cam.projModel ;
409  px = cam.px ;
410  py = cam.py ;
411  u0 = cam.u0 ;
412  v0 = cam.v0 ;
413  kud = cam.kud ;
414  kdu = cam.kdu ;
415 
416  inv_px = cam.inv_px;
417  inv_py = cam.inv_py;
418 
419  isFov = cam.isFov;
420  m_hFovAngle = cam.m_hFovAngle;
421  m_vFovAngle = cam.m_vFovAngle;
422  width = cam.width;
423  height = cam.height;
424  fovNormals = cam.fovNormals;
425 
426  return *this ;
427 }
428 
435 void
436 vpCameraParameters::computeFov(const unsigned int &w, const unsigned int &h)
437 {
438  if( !isFov && w != width && h != height && w != 0 && h != 0){
439  fovNormals = std::vector<vpColVector>(4);
440 
441  isFov = true;
442 
443  double half_hFovAngle = atan((double)w / ( 2.0 * px ));
444  double half_vFovAngle = atan((double)h / ( 2.0 * py ));
445 
446  width = w;
447  height = h;
448 
449  vpColVector n(3);
450  n = 0;
451  n[0] = 1.0;
452 
453  vpRotationMatrix Rleft (0,-half_hFovAngle,0);
454  vpRotationMatrix Rright(0, half_hFovAngle,0);
455 
456  vpColVector nLeft, nRight;
457 
458  nLeft = Rleft * (-n);
459  fovNormals[0] = nLeft.normalize();
460 
461  nRight = Rright * n;
462  fovNormals[1] = nRight.normalize();
463 
464  n = 0;
465  n[1] = 1.0;
466 
467  vpRotationMatrix Rup ( half_vFovAngle,0,0);
468  vpRotationMatrix Rdown(-half_vFovAngle,0,0);
469 
470  vpColVector nUp, nDown;
471 
472  nUp = Rup * (-n);
473  fovNormals[2] = nUp.normalize();
474 
475  nDown = Rdown * n;
476  fovNormals[3] = nDown.normalize();
477 
478  m_hFovAngle = 2 * half_hFovAngle;
479  m_vFovAngle = 2 * half_vFovAngle;
480  }
481 }
482 
483 
500 vpMatrix
502 {
503  vpMatrix K;
504  switch(projModel){
506  K.resize(3,3) ;
507  K = 0.0 ;
508  K[0][0] = px ;
509  K[1][1] = py ;
510  K[0][2] = u0 ;
511  K[1][2] = v0 ;
512  K[2][2] = 1.0 ;
513  break;
515  default :
516  vpERROR_TRACE("\n\t getting K matrix in the case of projection \
517  with distortion has no sense");
519  "\n\t getting K matrix in the case of projection \
520  with distortion has no sense"));
521  }
522  return K;
523 }
540 vpMatrix
542 {
543  vpMatrix K_inv;
544  switch(projModel){
546  K_inv.resize(3,3) ;
547  K_inv = 0.0 ;
548  K_inv[0][0] = inv_px ;
549  K_inv[1][1] = inv_py ;
550  K_inv[0][2] = -u0*inv_px ;
551  K_inv[1][2] = -v0*inv_py ;
552  K_inv[2][2] = 1.0 ;
553  break;
555  default :
556  vpERROR_TRACE("\n\t getting K^-1 matrix in the case of projection \
557  with distortion has no sense");
559  "\n\t getting K matrix in the case of projection \
560  with distortion has no sense"));
561  }
562  return K_inv;
563 }
564 
565 
571 void
573 {
574  std::ios::fmtflags original_flags( std::cout.flags() );
575  switch(projModel){
577  std::cout.precision(10);
578  std::cout << "Camera parameters for perspective projection without distortion:"
579  << 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:"
586  << std::endl ;
587  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
588  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
589  std::cout << " kud = " << kud << std::endl ;
590  std::cout << " kdu = " << kdu << std::endl ;
591  break;
592  }
593  // Restore ostream format
594  std::cout.flags(original_flags);
595 }
603 VISP_EXPORT std::ostream & operator << (std::ostream & os, const vpCameraParameters &cam)
604 {
605  switch(cam.get_projModel()){
607  os << "Camera parameters for perspective projection without distortion:"
608  << std::endl ;
609  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
610  << std::endl ;
611  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
612  << std::endl ;
613  break;
615  std::ios_base::fmtflags original_flags = os.flags();
616  os.precision(10);
617  os << "Camera parameters for perspective projection with distortion:"
618  << std::endl ;
619  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
620  << std::endl ;
621  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
622  << std::endl ;
623  os << " kud = " << cam.get_kud() << std::endl ;
624  os << " kdu = " << cam.get_kdu() << std::endl ;
625 
626  os.flags(original_flags); // restore os to standard state
627  break;
628  }
629  return os;
630 }
631 
632 
vpMatrix get_K_inverse() const
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void initFromCalibrationMatrix(const vpMatrix &_K)
double get_u0() const
void init()
basic initialization with the default parameters
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:199
Perspective projection without distortion model.
#define vpERROR_TRACE
Definition: vpDebug.h:395
error that can be emited by ViSP classes.
Definition: vpException.h:76
double get_py() const
The vpRotationMatrix considers the particular case of a rotation matrix.
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void initFromFov(const unsigned int &w, const unsigned int &h, const double &hfov, const double &vfov)
double get_v0() const
Generic class defining intrinsic camera parameters.
Perspective projection with distortion model.
double get_px() const
double get_kud() const
vpMatrix get_K() const
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpCameraParametersProjType get_projModel() const
double get_kdu() const
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:163
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:161
vpColVector & normalize()
Normalise the vector.
vpCameraParameters & operator=(const vpCameraParameters &c)
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)
void computeFov(const unsigned int &w, const unsigned int &h)