ViSP  2.9.0
vpCameraParameters.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCameraParameters.cpp 4649 2014-02-07 14:57:11Z 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), fovAngleX(0), fovAngleY(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), fovAngleX(0), fovAngleY(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), fovAngleX(0), fovAngleY(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), fovAngleX(0), fovAngleY(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 
179 void
180 vpCameraParameters::initPersProjWithoutDistortion(const double cam_px, const double cam_py,
181  const double cam_u0, const double cam_v0)
182 {
184 
185  this->px = cam_px ;
186  this->py = cam_py ;
187  this->u0 = cam_u0 ;
188  this->v0 = cam_v0 ;
189  this->kud = 0 ;
190  this->kdu = 0 ;
191 
192  if (fabs(px)<1e-6)
193  {
194  vpERROR_TRACE("Camera parameter px = 0") ;
196  "Camera parameter px = 0")) ;
197  }
198  if (fabs(py)<1e-6)
199  {
200  vpERROR_TRACE("Camera parameter px = 0") ;
202  "Camera parameter px = 0")) ;
203  }
204  this->inv_px = 1./px;
205  this->inv_py = 1./py;
206 }
207 
216 void
217 vpCameraParameters::initPersProjWithDistortion(const double cam_px, const double cam_py,
218  const double cam_u0, const double cam_v0,
219  const double cam_kud, const double cam_kdu)
220 {
222 
223  this->px = cam_px ;
224  this->py = cam_py ;
225  this->u0 = cam_u0 ;
226  this->v0 = cam_v0 ;
227  this->kud = cam_kud ;
228  this->kdu = cam_kdu ;
229 
230  if (fabs(px)<1e-6)
231  {
232  vpERROR_TRACE("Camera parameter px = 0") ;
234  "Camera parameter px = 0")) ;
235  }
236  if (fabs(py)<1e-6)
237  {
238  vpERROR_TRACE("Camera parameter px = 0") ;
240  "Camera parameter px = 0")) ;
241  }
242  this->inv_px = 1./px;
243  this->inv_py = 1./py;
244 }
245 
252 {
253 }
254 
258 void
260 {
261  *this = c ;
262 }
263 
264 
279 void
281 {
282  if(_K.getRows() != 3 || _K.getCols() != 3 ){
283  throw vpException(vpException::dimensionError, "bad size for calibration matrix");
284  }
285  if( std::fabs(_K[2][2] - 1.0) > std::numeric_limits<double>::epsilon()){
286  throw vpException(vpException::badValue, "bad value: K[2][2] must be equal to 1");
287  }
288  initPersProjWithoutDistortion (_K[0][0], _K[1][1], _K[0][2], _K[1][2]);
289 }
290 
291 
297 {
298  projModel = cam.projModel ;
299  px = cam.px ;
300  py = cam.py ;
301  u0 = cam.u0 ;
302  v0 = cam.v0 ;
303  kud = cam.kud ;
304  kdu = cam.kdu ;
305 
306  inv_px = cam.inv_px;
307  inv_py = cam.inv_py;
308 
309  isFov = cam.isFov;
310  fovAngleX = cam.fovAngleX;
311  fovAngleY = cam.fovAngleY;
312  fovNormals = cam.fovNormals;
313  width = cam.width;
314  height = cam.height;
315 
316  return *this ;
317 }
318 
325 void
326 vpCameraParameters::computeFov(const unsigned int &w, const unsigned int &h)
327 {
328  if( !isFov && w != width && h != height && w != 0 && h != 0){
329  fovNormals = std::vector<vpColVector>(4);
330 
331  isFov = true;
332 
333  fovAngleX = atan((double)w / ( 2.0 * px ));
334  fovAngleY = atan((double)h / ( 2.0 * py ));
335 
336  width = w;
337  height = h;
338 
339  vpColVector n(3);
340  n = 0;
341  n[0] = 1.0;
342 
343  vpRotationMatrix Rleft(0,-fovAngleX,0);
344  vpRotationMatrix Rright(0,fovAngleX,0);
345 
346  vpColVector nLeft, nRight;
347 
348  nLeft = Rleft * (-n);
349  fovNormals[0] = nLeft.normalize();
350 
351  nRight = Rright * n;
352  fovNormals[1] = nRight.normalize();
353 
354  n = 0;
355  n[1] = 1.0;
356 
357  vpRotationMatrix Rup(fovAngleY,0,0);
358  vpRotationMatrix Rdown(-fovAngleY,0,0);
359 
360  vpColVector nUp, nDown;
361 
362  nUp = Rup * (-n);
363  fovNormals[2] = nUp.normalize();
364 
365  nDown = Rdown * n;
366  fovNormals[3] = nDown.normalize();
367  }
368 }
369 
370 
387 vpMatrix
389 {
390  vpMatrix K;
391  switch(projModel){
393  K.resize(3,3) ;
394  K = 0.0 ;
395  K[0][0] = px ;
396  K[1][1] = py ;
397  K[0][2] = u0 ;
398  K[1][2] = v0 ;
399  K[2][2] = 1.0 ;
400  break;
402  default :
403  vpERROR_TRACE("\n\t getting K matrix in the case of projection \
404  with distortion has no sense");
406  "\n\t getting K matrix in the case of projection \
407  with distortion has no sense"));
408  }
409  return K;
410 }
427 vpMatrix
429 {
430  vpMatrix K_inv;
431  switch(projModel){
433  K_inv.resize(3,3) ;
434  K_inv = 0.0 ;
435  K_inv[0][0] = inv_px ;
436  K_inv[1][1] = inv_py ;
437  K_inv[0][2] = -u0*inv_px ;
438  K_inv[1][2] = -v0*inv_py ;
439  K_inv[2][2] = 1.0 ;
440  break;
442  default :
443  vpERROR_TRACE("\n\t getting K^-1 matrix in the case of projection \
444  with distortion has no sense");
446  "\n\t getting K matrix in the case of projection \
447  with distortion has no sense"));
448  }
449  return K_inv;
450 }
451 
452 
458 void
460 {
461  std::ios::fmtflags original_flags( std::cout.flags() );
462  switch(projModel){
464  std::cout.precision(10);
465  std::cout << "Camera parameters for perspective projection without distortion:"
466  << std::endl ;
467  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
468  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
469  break;
471  std::cout.precision(10);
472  std::cout << "Camera parameters for perspective projection with distortion:"
473  << std::endl ;
474  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
475  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
476  std::cout << " kud = " << kud << std::endl ;
477  std::cout << " kdu = " << kdu << std::endl ;
478  break;
479  }
480  // Restore ostream format
481  std::cout.flags(original_flags);
482 }
490 VISP_EXPORT std::ostream & operator << (std::ostream & os, const vpCameraParameters &cam)
491 {
492  switch(cam.get_projModel()){
494  os << "Camera parameters for perspective projection without distortion:"
495  << std::endl ;
496  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
497  << std::endl ;
498  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
499  << std::endl ;
500  break;
502  std::ios_base::fmtflags original_flags = os.flags();
503  os.precision(10);
504  os << "Camera parameters for perspective projection with distortion:"
505  << std::endl ;
506  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
507  << std::endl ;
508  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
509  << std::endl ;
510  os << " kud = " << cam.get_kud() << std::endl ;
511  os << " kdu = " << cam.get_kdu() << std::endl ;
512 
513  os.flags(original_flags); // restore os to standard state
514  break;
515  }
516  return os;
517 }
518 
519 
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:183
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)
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)