ViSP  2.6.2
vpCameraParameters.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCameraParameters.cpp 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 
298 vpMatrix
300 {
301  vpMatrix K;
302  switch(projModel){
304  K.resize(3,3) ;
305  K = 0.0 ;
306  K[0][0] = px ;
307  K[1][1] = py ;
308  K[0][2] = u0 ;
309  K[1][2] = v0 ;
310  K[2][2] = 1.0 ;
311  break;
313  default :
314  vpERROR_TRACE("\n\t getting K matrix in the case of projection \
315  with distortion has no sense");
317  "\n\t getting K matrix in the case of projection \
318  with distortion has no sense"));
319  }
320  return K;
321 }
322 
323 
329 void
331 {
332  switch(projModel){
334  std::cout.precision(10);
335  std::cout << "Camera parameters for perspective projection without distortion:"
336  << std::endl ;
337  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
338  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
339  break;
341  std::cout.precision(10);
342  std::cout << "Camera parameters for perspective projection with distortion:"
343  << std::endl ;
344  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
345  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
346  std::cout << " kud = " << kud << std::endl ;
347  std::cout << " kdu = " << kdu << std::endl ;
348  break;
349  }
350 }
358 std::ostream & operator << (std::ostream & os,
359  const vpCameraParameters &cam)
360 {
361  switch(cam.get_projModel()){
363  os << "Camera parameters for perspective projection without distortion:"
364  << std::endl ;
365  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
366  << std::endl ;
367  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
368  << std::endl ;
369  break;
371  os.precision(10);
372  os << "Camera parameters for perspective projection with distortion:"
373  << std::endl ;
374  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
375  << std::endl ;
376  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
377  << std::endl ;
378  os << " kud = " << cam.get_kud() << std::endl ;
379  os << " kdu = " << cam.get_kdu() << std::endl ;
380  break;
381  }
382  return os;
383 }
384 
385 
386 /*
387  * Local variables:
388  * c-basic-offset: 2
389  * End:
390  */
391 
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
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)