ViSP  2.8.0
vpCameraParameters.cpp
1 /****************************************************************************
2  *
3  * $Id: vpCameraParameters.cpp 4317 2013-07-17 09:40:17Z 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 <visp/vpRotationMatrix.h>
55 #include <cmath>
56 #include <limits>
57 
58 const double vpCameraParameters::DEFAULT_PX_PARAMETER = 600.0;
59 const double vpCameraParameters::DEFAULT_PY_PARAMETER = 600.0;
60 const double vpCameraParameters::DEFAULT_U0_PARAMETER = 192.0;
61 const double vpCameraParameters::DEFAULT_V0_PARAMETER = 144.0;
62 const double vpCameraParameters::DEFAULT_KUD_PARAMETER = 0.0;
63 const double vpCameraParameters::DEFAULT_KDU_PARAMETER = 0.0;
65  vpCameraParameters::DEFAULT_PROJ_TYPE =
67 
75 {
76  isFov = false;
77  fovAngleX = 0;
78  fovAngleY = 0;
79  width = 0;
80  height = 0;
81  init() ;
82 }
83 
88 {
89  init(c) ;
90 }
91 
99 vpCameraParameters::vpCameraParameters(const double px, const double py,
100  const double u0, const double v0)
101 {
102  isFov = false;
103  fovAngleX = 0;
104  fovAngleY = 0;
105  width = 0;
106  height = 0;
107  initPersProjWithoutDistortion(px,py,u0,v0) ;
108 }
109 
119 vpCameraParameters::vpCameraParameters(const double px, const double py,
120  const double u0, const double v0,
121  const double kud, const double kdu)
122 {
123  isFov = false;
124  fovAngleX = 0;
125  fovAngleY = 0;
126  width = 0;
127  height = 0;
128  initPersProjWithDistortion(px,py,u0,v0,kud,kdu) ;
129 }
130 
134 void
136 {
137  this->projModel = DEFAULT_PROJ_TYPE ;
138 
139  this->px = DEFAULT_PX_PARAMETER ;
140  this->py = DEFAULT_PY_PARAMETER ;
141  this->u0 = DEFAULT_U0_PARAMETER ;
142  this->v0 = DEFAULT_V0_PARAMETER ;
143  this->kud = DEFAULT_KUD_PARAMETER ;
144  this->kdu = DEFAULT_KDU_PARAMETER ;
145 
146  if (fabs(this->px)<1e-6)
147  {
148  vpERROR_TRACE("Camera parameter px = 0") ;
150  "Camera parameter px = 0")) ;
151  }
152  if (fabs(this->py)<1e-6)
153  {
154  vpERROR_TRACE("Camera parameter px = 0") ;
156  "Camera parameter px = 0")) ;
157  }
158  this->inv_px = 1./this->px;
159  this->inv_py = 1./this->py;
160 }
161 
168 void
170  const double py, const double u0, const double v0)
171 {
173 
174  this->px = px ;
175  this->py = py ;
176  this->u0 = u0 ;
177  this->v0 = v0 ;
178  this->kud = 0 ;
179  this->kdu = 0 ;
180 
181  if (fabs(px)<1e-6)
182  {
183  vpERROR_TRACE("Camera parameter px = 0") ;
185  "Camera parameter px = 0")) ;
186  }
187  if (fabs(py)<1e-6)
188  {
189  vpERROR_TRACE("Camera parameter px = 0") ;
191  "Camera parameter px = 0")) ;
192  }
193  this->inv_px = 1./px;
194  this->inv_py = 1./py;
195 }
196 
205 void
206 vpCameraParameters::initPersProjWithDistortion(const double px, const double py,
207  const double u0, const double v0,
208  const double kud, const double kdu)
209 {
211 
212  this->px = px ;
213  this->py = py ;
214  this->u0 = u0 ;
215  this->v0 = v0 ;
216  this->kud = kud ;
217  this->kdu = kdu ;
218 
219  if (fabs(px)<1e-6)
220  {
221  vpERROR_TRACE("Camera parameter px = 0") ;
223  "Camera parameter px = 0")) ;
224  }
225  if (fabs(py)<1e-6)
226  {
227  vpERROR_TRACE("Camera parameter px = 0") ;
229  "Camera parameter px = 0")) ;
230  }
231  this->inv_px = 1./px;
232  this->inv_py = 1./py;
233 }
234 
241 {
242 }
243 
247 void
249 {
250  *this = c ;
251 }
252 
253 
268 void
270 {
271  if(_K.getRows() != 3 || _K.getCols() != 3 ){
272  throw vpException(vpException::dimensionError, "bad size for calibration matrix");
273  }
274  if( std::fabs(_K[2][2] - 1.0) > std::numeric_limits<double>::epsilon()){
275  throw vpException(vpException::badValue, "bad value: K[2][2] must be equal to 1");
276  }
277  initPersProjWithoutDistortion (_K[0][0], _K[1][1], _K[0][2], _K[1][2]);
278 }
279 
280 
286 {
287  projModel = cam.projModel ;
288  px = cam.px ;
289  py = cam.py ;
290  u0 = cam.u0 ;
291  v0 = cam.v0 ;
292  kud = cam.kud ;
293  kdu = cam.kdu ;
294 
295  inv_px = cam.inv_px;
296  inv_py = cam.inv_py;
297 
298  isFov = cam.isFov;
299  fovAngleX = cam.fovAngleX;
300  fovAngleY = cam.fovAngleY;
301  fovNormals = cam.fovNormals;
302  width = cam.width;
303  height = cam.height;
304 
305  return *this ;
306 }
307 
314 void
315 vpCameraParameters::computeFov(const unsigned int &w, const unsigned int &h)
316 {
317  if( !isFov && w != width && h != height && w != 0 && h != 0){
318  fovNormals = std::vector<vpColVector>(4);
319 
320  isFov = true;
321 
322  fovAngleX = atan((double)w / ( 2.0 * px ));
323  fovAngleY = atan((double)h / ( 2.0 * py ));
324 
325  width = w;
326  height = h;
327 
328  vpColVector n(3);
329  n = 0;
330  n[0] = 1.0;
331 
332  vpRotationMatrix Rleft(0,-fovAngleX,0);
333  vpRotationMatrix Rright(0,fovAngleX,0);
334 
335  vpColVector nLeft, nRight;
336 
337  nLeft = Rleft * (-n);
338  fovNormals[0] = nLeft.normalize();
339 
340  nRight = Rright * n;
341  fovNormals[1] = nRight.normalize();
342 
343  n = 0;
344  n[1] = 1.0;
345 
346  vpRotationMatrix Rup(fovAngleY,0,0);
347  vpRotationMatrix Rdown(-fovAngleY,0,0);
348 
349  vpColVector nUp, nDown;
350 
351  nUp = Rup * (-n);
352  fovNormals[2] = nUp.normalize();
353 
354  nDown = Rdown * n;
355  fovNormals[3] = nDown.normalize();
356  }
357 }
358 
359 
376 vpMatrix
378 {
379  vpMatrix K;
380  switch(projModel){
382  K.resize(3,3) ;
383  K = 0.0 ;
384  K[0][0] = px ;
385  K[1][1] = py ;
386  K[0][2] = u0 ;
387  K[1][2] = v0 ;
388  K[2][2] = 1.0 ;
389  break;
391  default :
392  vpERROR_TRACE("\n\t getting K matrix in the case of projection \
393  with distortion has no sense");
395  "\n\t getting K matrix in the case of projection \
396  with distortion has no sense"));
397  }
398  return K;
399 }
416 vpMatrix
418 {
419  vpMatrix K_inv;
420  switch(projModel){
422  K_inv.resize(3,3) ;
423  K_inv = 0.0 ;
424  K_inv[0][0] = inv_px ;
425  K_inv[1][1] = inv_py ;
426  K_inv[0][2] = -u0*inv_px ;
427  K_inv[1][2] = -v0*inv_py ;
428  K_inv[2][2] = 1.0 ;
429  break;
431  default :
432  vpERROR_TRACE("\n\t getting K^-1 matrix in the case of projection \
433  with distortion has no sense");
435  "\n\t getting K matrix in the case of projection \
436  with distortion has no sense"));
437  }
438  return K_inv;
439 }
440 
441 
447 void
449 {
450  switch(projModel){
452  std::cout.precision(10);
453  std::cout << "Camera parameters for perspective projection without distortion:"
454  << std::endl ;
455  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
456  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
457  break;
459  std::cout.precision(10);
460  std::cout << "Camera parameters for perspective projection with distortion:"
461  << std::endl ;
462  std::cout << " px = " << px <<"\t py = "<< py << std::endl ;
463  std::cout << " u0 = " << u0 <<"\t v0 = "<< v0 << std::endl ;
464  std::cout << " kud = " << kud << std::endl ;
465  std::cout << " kdu = " << kdu << std::endl ;
466  break;
467  }
468 }
476 std::ostream & operator << (std::ostream & os,
477  const vpCameraParameters &cam)
478 {
479  switch(cam.get_projModel()){
481  os << "Camera parameters for perspective projection without distortion:"
482  << std::endl ;
483  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
484  << std::endl ;
485  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
486  << std::endl ;
487  break;
489  os.precision(10);
490  os << "Camera parameters for perspective projection with distortion:"
491  << std::endl ;
492  os << " px = " << cam.get_px() <<"\t py = "<< cam.get_py()
493  << std::endl ;
494  os << " u0 = " << cam.get_u0() <<"\t v0 = "<< cam.get_v0()
495  << std::endl ;
496  os << " kud = " << cam.get_kud() << std::endl ;
497  os << " kdu = " << cam.get_kdu() << std::endl ;
498  break;
499  }
500  return os;
501 }
502 
503 
504 /*
505  * Local variables:
506  * c-basic-offset: 2
507  * End:
508  */
509 
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
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.
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
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:159
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
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)