ViSP  2.7.0
vpCameraParameters.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCameraParameters.cpp 4114 2013-02-07 13:26:03Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 <cmath>
55 #include <limits>
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;
64  vpCameraParameters::DEFAULT_PROJ_TYPE =
66 
74 {
75  init() ;
76 }
77 
82 {
83  init(c) ;
84 }
85 
93 vpCameraParameters::vpCameraParameters(const double px, const double py,
94  const double u0, const double v0)
95 {
96  initPersProjWithoutDistortion(px,py,u0,v0) ;
97 }
98 
108 vpCameraParameters::vpCameraParameters(const double px, const double py,
109  const double u0, const double v0,
110  const double kud, const double kdu)
111 {
112  initPersProjWithDistortion(px,py,u0,v0,kud,kdu) ;
113 }
114 
118 void
120 {
121  this->projModel = DEFAULT_PROJ_TYPE ;
122 
123  this->px = DEFAULT_PX_PARAMETER ;
124  this->py = DEFAULT_PY_PARAMETER ;
125  this->u0 = DEFAULT_U0_PARAMETER ;
126  this->v0 = DEFAULT_V0_PARAMETER ;
127  this->kud = DEFAULT_KUD_PARAMETER ;
128  this->kdu = DEFAULT_KDU_PARAMETER ;
129 
130  if (fabs(this->px)<1e-6)
131  {
132  vpERROR_TRACE("Camera parameter px = 0") ;
134  "Camera parameter px = 0")) ;
135  }
136  if (fabs(this->py)<1e-6)
137  {
138  vpERROR_TRACE("Camera parameter px = 0") ;
140  "Camera parameter px = 0")) ;
141  }
142  this->inv_px = 1./this->px;
143  this->inv_py = 1./this->py;
144 }
145 
152 void
154  const double py, const double u0, const double v0)
155 {
157 
158  this->px = px ;
159  this->py = py ;
160  this->u0 = u0 ;
161  this->v0 = v0 ;
162  this->kud = 0 ;
163  this->kdu = 0 ;
164 
165  if (fabs(px)<1e-6)
166  {
167  vpERROR_TRACE("Camera parameter px = 0") ;
169  "Camera parameter px = 0")) ;
170  }
171  if (fabs(py)<1e-6)
172  {
173  vpERROR_TRACE("Camera parameter px = 0") ;
175  "Camera parameter px = 0")) ;
176  }
177  this->inv_px = 1./px;
178  this->inv_py = 1./py;
179 }
180 
189 void
190 vpCameraParameters::initPersProjWithDistortion(const double px, const double py,
191  const double u0, const double v0,
192  const double kud, const double kdu)
193 {
195 
196  this->px = px ;
197  this->py = py ;
198  this->u0 = u0 ;
199  this->v0 = v0 ;
200  this->kud = kud ;
201  this->kdu = kdu ;
202 
203  if (fabs(px)<1e-6)
204  {
205  vpERROR_TRACE("Camera parameter px = 0") ;
207  "Camera parameter px = 0")) ;
208  }
209  if (fabs(py)<1e-6)
210  {
211  vpERROR_TRACE("Camera parameter px = 0") ;
213  "Camera parameter px = 0")) ;
214  }
215  this->inv_px = 1./px;
216  this->inv_py = 1./py;
217 }
218 
225 {
226 }
227 
231 void
233 {
234  *this = c ;
235 }
236 
237 
252 void
254 {
255  if(_K.getRows() != 3 || _K.getCols() != 3 ){
256  throw vpException(vpException::dimensionError, "bad size for calibration matrix");
257  }
258  if( std::fabs(_K[2][2] - 1.0) > std::numeric_limits<double>::epsilon()){
259  throw vpException(vpException::badValue, "bad value: K[2][2] must be equal to 1");
260  }
261  initPersProjWithoutDistortion (_K[0][0], _K[1][1], _K[0][2], _K[1][2]);
262 }
263 
264 
270 {
271  projModel = cam.projModel ;
272  px = cam.px ;
273  py = cam.py ;
274  u0 = cam.u0 ;
275  v0 = cam.v0 ;
276  kud = cam.kud ;
277  kdu = cam.kdu ;
278 
279  inv_px = cam.inv_px;
280  inv_py = cam.inv_py;
281  return *this ;
282 }
283 
300 vpMatrix
302 {
303  vpMatrix K;
304  switch(projModel){
306  K.resize(3,3) ;
307  K = 0.0 ;
308  K[0][0] = px ;
309  K[1][1] = py ;
310  K[0][2] = u0 ;
311  K[1][2] = v0 ;
312  K[2][2] = 1.0 ;
313  break;
315  default :
316  vpERROR_TRACE("\n\t getting K matrix in the case of projection \
317  with distortion has no sense");
319  "\n\t getting K matrix in the case of projection \
320  with distortion has no sense"));
321  }
322  return K;
323 }
340 vpMatrix
342 {
343  vpMatrix K_inv;
344  switch(projModel){
346  K_inv.resize(3,3) ;
347  K_inv = 0.0 ;
348  K_inv[0][0] = inv_px ;
349  K_inv[1][1] = inv_py ;
350  K_inv[0][2] = -u0*inv_px ;
351  K_inv[1][2] = -v0*inv_py ;
352  K_inv[2][2] = 1.0 ;
353  break;
355  default :
356  vpERROR_TRACE("\n\t getting K^-1 matrix in the case of projection \
357  with distortion has no sense");
359  "\n\t getting K matrix in the case of projection \
360  with distortion has no sense"));
361  }
362  return K_inv;
363 }
364 
365 
371 void
373 {
374  switch(projModel){
376  std::cout.precision(10);
377  std::cout << "Camera parameters for perspective projection without distortion:"
378  << std::endl ;
379  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
380  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
381  break;
383  std::cout.precision(10);
384  std::cout << "Camera parameters for perspective projection with distortion:"
385  << std::endl ;
386  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
387  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
388  std::cout << " kud = " << kud << std::endl ;
389  std::cout << " kdu = " << kdu << std::endl ;
390  break;
391  }
392 }
400 std::ostream & operator << (std::ostream & os,
401  const vpCameraParameters &cam)
402 {
403  switch(cam.get_projModel()){
405  os << "Camera parameters for perspective projection without distortion:"
406  << std::endl ;
407  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
408  << std::endl ;
409  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
410  << std::endl ;
411  break;
413  os.precision(10);
414  os << "Camera parameters for perspective projection with distortion:"
415  << std::endl ;
416  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
417  << std::endl ;
418  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
419  << std::endl ;
420  os << " kud = " << cam.get_kud() << std::endl ;
421  os << " kdu = " << cam.get_kdu() << std::endl ;
422  break;
423  }
424  return os;
425 }
426 
427 
428 /*
429  * Local variables:
430  * c-basic-offset: 2
431  * End:
432  */
433 
vpMatrix get_K_inverse() const
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
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:174
Perspective projection without distortion model.
#define vpERROR_TRACE
Definition: vpDebug.h:379
error that can be emited by ViSP classes.
Definition: vpException.h:75
double get_py() const
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
double get_v0() const
Generic class defining intrinsic camera parameters.
VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpImagePoint &ip)
Definition: vpImagePoint.h:529
Perspective projection with distortion model.
double get_px() const
double get_kud() const
vpMatrix get_K() const
vpCameraParametersProjType get_projModel() const
double get_kdu() const
unsigned int getCols() const
Return the number of columns of the matrix.
Definition: vpMatrix.h:159
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
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)