1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Camera calibration.
33  *
34  * Authors:
35  * Eric Marchand
36  * Francois Chaumette
37  * Anthony Saunier
38  *
39  *****************************************************************************/
40 
41 /*!
42   \file vpCalibration.h
43   \brief Tools for camera calibration.
44 
45   \author Eric Marchand (INRIA) using code from Francois Chaumette (INRIA)
46 
47   \sa the example in calibrate.cpp
48 */
49 #ifndef vpCalibration_h
50 #define vpCalibration_h
51 
52 #include <list>
53 #include <vector>
54 #include <visp3/core/vpCameraParameters.h>
55 #include <visp3/core/vpDisplay.h>
56 #include <visp3/core/vpExponentialMap.h>
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpImage.h>
59 #include <visp3/core/vpImagePoint.h>
60 #include <visp3/core/vpMath.h>
61 #include <visp3/core/vpMatrix.h>
62 #include <visp3/vision/vpCalibrationException.h>
63 /*!
64   \class vpCalibration
65 
66   \ingroup group_vision_calib
67 
68   \brief Tools for perspective camera calibration.
69 
70 */
71 class VISP_EXPORT vpCalibration
72 {
73 public:
74   /*!
75     Minimization algorithm use to estimate the camera parameters.
76   */
77   typedef enum {
78     CALIB_LAGRANGE,                 /*!< Lagrange approach without estimation of the
79                                        distortion. */
80     CALIB_VIRTUAL_VS,               /*!< Virtual visual servoing approach without estimation
81                                        of the distortion (results are similar to Lowe
82                                        approach). */
83     CALIB_VIRTUAL_VS_DIST,          /*!< Virtual visual servoing approach with
84                                        estimation of the distortion. */
85     CALIB_LAGRANGE_VIRTUAL_VS,      /*!< Lagrange approach first, than virtual
86                                        visual servoing approach,  without
87                                        estimation of the distortion. */
88     CALIB_LAGRANGE_VIRTUAL_VS_DIST, /*!< Lagrange approach first, than virtual
89                                        visual servoing approach, with
90                                        estimation of the distortion. */
91   } vpCalibrationMethodType;
92 
93   vpHomogeneousMatrix cMo; //!< Pose computed using camera parameters without distorsion
94   //!< (as a 3x4 matrix [R T])
95   vpHomogeneousMatrix cMo_dist; //!< Pose computed using camera parameters with distorsion
96   //!< with distortion model
97   //!< (as a 3x4 matrix [R T])
98   vpCameraParameters cam; //!< Camera intrinsic parameters for perspective
99   //!< projection model without distortion
100   vpCameraParameters cam_dist; //!< Camera intrinsic parameters for perspective
101   //!< projection model with distortion
102 
103   vpHomogeneousMatrix rMe; //!< Position of the effector in relation to the
104   //!< reference coordinates (manipulator base coordinates)
105   vpHomogeneousMatrix eMc; //!< Position of the camera in end-effector frame using camera parameters without distorsion
106   vpHomogeneousMatrix eMc_dist; //!< Position of the camera in end-effector frame using camera parameters with distorsion
107 
108 public:
109   // Constructor
110   vpCalibration();
111   vpCalibration(const vpCalibration &c);
112 
113   // Destructor
114   virtual ~vpCalibration();
115 
116   // Add a new point in this array
117   int addPoint(double X, double Y, double Z, vpImagePoint &ip);
118 
119   // = operator
120   vpCalibration &operator=(const vpCalibration &twinCalibration);
121 
122 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
123   /*!
124     @name Deprecated functions
125   */
126   //@{
127   vp_deprecated static void calibrationTsai(const std::vector<vpHomogeneousMatrix> &cMo,
128                                             const std::vector<vpHomogeneousMatrix> &rMe,
129                                             vpHomogeneousMatrix &eMc);
130   vp_deprecated static int computeCalibrationTsai(const std::vector<vpCalibration> &table_cal, vpHomogeneousMatrix &eMc,
131                                                   vpHomogeneousMatrix &eMc_dist);
132   //@}
133   #endif
134 
135   //! Suppress all the point in the array of point
136   int clearPoint();
137 
138   void computeStdDeviation(double &deviation, double &deviation_dist);
139   int computeCalibration(vpCalibrationMethodType method, vpHomogeneousMatrix &cMo_est, vpCameraParameters &cam_est,
140                          bool verbose = false);
141   static int computeCalibrationMulti(vpCalibrationMethodType method, std::vector<vpCalibration> &table_cal,
142                                      vpCameraParameters &cam, double &globalReprojectionError, bool verbose = false);
143 
144   double computeStdDeviation(const vpHomogeneousMatrix &cMo_est, const vpCameraParameters &camera);
145   double computeStdDeviation_dist(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam);
146   int displayData(vpImage<unsigned char> &I, vpColor color = vpColor::red, unsigned int thickness = 1,
147                   int subsampling_factor = 1);
148   int displayGrid(vpImage<unsigned char> &I, vpColor color = vpColor::yellow, unsigned int thickness = 1,
149                   int subsampling_factor = 1);
150 
151   //! Set the gain for the virtual visual servoing algorithm.
getLambda()152   static double getLambda() { return gain; }
153 
154   //! get the residual in pixels
getResidual(void)155   double getResidual(void) const { return residual; }
156   //! get the residual for perspective projection with distortion (in pixels)
getResidual_dist(void)157   double getResidual_dist(void) const { return residual_dist; }
158   //! get the number of points
get_npt()159   unsigned int get_npt() const { return npt; }
160 
161   int init();
162 
163   int readData(const char *filename);
164   static int readGrid(const char *filename, unsigned int &n, std::list<double> &oX, std::list<double> &oY,
165                       std::list<double> &oZ, bool verbose = false);
166 
167   //! set the gain for the virtual visual servoing algorithm
setLambda(const double & lambda)168   static void setLambda(const double &lambda) { gain = lambda; }
169   int writeData(const char *filename);
170 
171 private:
172   void computePose(const vpCameraParameters &cam, vpHomogeneousMatrix &cMo);
173   void calibLagrange(vpCameraParameters &cam, vpHomogeneousMatrix &cMo);
174 
175   //! Compute the calibration using virtual visual servoing approach
176   void calibVVS(vpCameraParameters &cam, vpHomogeneousMatrix &cMo, bool verbose = false);
177 
178   static void calibVVSMulti(unsigned int nbPose, vpCalibration table_cal[], vpCameraParameters &cam,
179                             bool verbose = false);
180   static void calibVVSMulti(std::vector<vpCalibration> &table_cal, vpCameraParameters &cam,
181                             double &globalReprojectionError, bool verbose = false);
182   void calibVVSWithDistortion(vpCameraParameters &cam, vpHomogeneousMatrix &cMo, bool verbose = false);
183   static void calibVVSWithDistortionMulti(unsigned int nbPose, vpCalibration table_cal[], vpCameraParameters &cam,
184                                           bool verbose = false);
185   static void calibVVSWithDistortionMulti(std::vector<vpCalibration> &table_cal, vpCameraParameters &cam,
186                                           double &globalReprojectionError, bool verbose = false);
187 
188 private:
189   unsigned int npt; //!< number of points used in calibration computation
190   std::list<double> LoX, LoY,
191       LoZ;                     //!< list of points coordinates (3D in meters)
192   std::list<vpImagePoint> Lip; //!< list of points coordinates (2D in pixels)
193 
194   double residual;      //!< residual in pixel for camera model without distortion
195   double residual_dist; //!< residual in pixel for perspective projection with
196                         //!< distortion model
197 
198   static double threshold;
199   static unsigned int nbIterMax;
200   static double gain;
201 };
202 
203 #endif
204 
205 /*
206  * Local variables:
207  * c-basic-offset: 2
208  * End:
209  */
210