Visual Servoing Platform  version 3.5.1 under development (2022-07-05)
vpPixelMeterConversion.h
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  * Pixel to meter conversion.
33  *
34  * Authors:
35  * Eric Marchand
36  * Anthony Saunier
37  *
38  *****************************************************************************/
39 
40 #ifndef vpPixelMeterConversion_H
41 #define vpPixelMeterConversion_H
42 
48 #include <visp3/core/vpCameraParameters.h>
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpException.h>
51 #include <visp3/core/vpImagePoint.h>
52 #include <visp3/core/vpMath.h>
53 
54 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
55 #include <opencv2/calib3d/calib3d.hpp>
56 #include <opencv2/imgproc/imgproc.hpp>
57 #endif
58 
71 class VISP_EXPORT vpPixelMeterConversion
72 {
73 public:
76  static void convertEllipse(const vpCameraParameters &cam, const vpImagePoint &center_p, double n20_p, double n11_p,
77  double n02_p, double &xc_m, double &yc_m, double &n20_m, double &n11_m, double &n02_m);
78  static void convertLine(const vpCameraParameters &cam, const double &rho_p, const double &theta_p, double &rho_m,
79  double &theta_m);
80 
81  static void convertMoment(const vpCameraParameters &cam, unsigned int order, const vpMatrix &moment_pixel,
82  vpMatrix &moment_meter);
108  inline static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
109  {
110  switch (cam.projModel) {
112  convertPointWithoutDistortion(cam, u, v, x, y);
113  break;
115  convertPointWithDistortion(cam, u, v, x, y);
116  break;
118  convertPointWithKannalaBrandtDistortion(cam, u, v, x, y);
119  break;
120  }
121  }
122 
150  inline static void convertPoint(const vpCameraParameters &cam, const vpImagePoint &iP, double &x, double &y)
151  {
152  switch (cam.projModel) {
154  convertPointWithoutDistortion(cam, iP, x, y);
155  break;
157  convertPointWithDistortion(cam, iP, x, y);
158  break;
160  convertPointWithKannalaBrandtDistortion(cam, iP, x, y);
161  break;
162  }
163  }
164 
165 #ifndef DOXYGEN_SHOULD_SKIP_THIS
178  inline static void convertPointWithoutDistortion(const vpCameraParameters &cam, const double &u, const double &v,
179  double &x, double &y)
180  {
181  x = (u - cam.u0) * cam.inv_px;
182  y = (v - cam.v0) * cam.inv_py;
183  }
184 
200  inline static void convertPointWithoutDistortion(const vpCameraParameters &cam, const vpImagePoint &iP, double &x,
201  double &y)
202  {
203  x = (iP.get_u() - cam.u0) * cam.inv_px;
204  y = (iP.get_v() - cam.v0) * cam.inv_py;
205  }
206 
221  inline static void convertPointWithDistortion(const vpCameraParameters &cam, const double &u, const double &v,
222  double &x, double &y)
223  {
224  double r2 = 1. + cam.kdu * (vpMath::sqr((u - cam.u0) * cam.inv_px) + vpMath::sqr((v - cam.v0) * cam.inv_py));
225  x = (u - cam.u0) * r2 * cam.inv_px;
226  y = (v - cam.v0) * r2 * cam.inv_py;
227  }
228 
245  inline static void convertPointWithDistortion(const vpCameraParameters &cam, const vpImagePoint &iP, double &x,
246  double &y)
247  {
248  double r2 = 1. + cam.kdu * (vpMath::sqr((iP.get_u() - cam.u0) * cam.inv_px) +
249  vpMath::sqr((iP.get_v() - cam.v0) * cam.inv_py));
250  x = (iP.get_u() - cam.u0) * r2 * cam.inv_px;
251  y = (iP.get_v() - cam.v0) * r2 * cam.inv_py;
252  }
253 
274  inline static void convertPointWithKannalaBrandtDistortion(const vpCameraParameters &cam, const double &u,
275  const double &v, double &x, double &y)
276  {
277  double x_d = (u - cam.u0) / cam.px, y_d = (v - cam.v0) / cam.py;
278  double scale = 1.0;
279  double r_d = sqrt(vpMath::sqr(x_d) + vpMath::sqr(y_d));
280 
281  r_d = std::min(std::max(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.
282 
283  std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();
284 
285  const double EPS = 1e-8;
286  // Use Newton-Raphson method to solve for the angle theta
287  if (r_d > EPS) {
288  // compensate distortion iteratively
289  double theta = r_d;
290 
291  for (int j = 0; j < 10; j++) {
292  double theta2 = theta * theta, theta4 = theta2 * theta2, theta6 = theta4 * theta2, theta8 = theta6 * theta2;
293  double k0_theta2 = k[0] * theta2, k1_theta4 = k[1] * theta4, k2_theta6 = k[2] * theta6,
294  k3_theta8 = k[3] * theta8;
295  /* new_theta = theta - theta_fix, theta_fix = f0(theta) / f0'(theta) */
296  double theta_fix = (theta * (1 + k0_theta2 + k1_theta4 + k2_theta6 + k3_theta8) - r_d) /
297  (1 + 3 * k0_theta2 + 5 * k1_theta4 + 7 * k2_theta6 + 9 * k3_theta8);
298  theta = theta - theta_fix;
299  if (fabs(theta_fix) < EPS)
300  break;
301  }
302 
303  scale = std::tan(theta) / r_d; // Scale of norm of (x,y) and (x_d, y_d)
304  }
305 
306  x = x_d * scale;
307  y = y_d * scale;
308  }
309 
329  inline static void convertPointWithKannalaBrandtDistortion(const vpCameraParameters &cam, const vpImagePoint &iP,
330  double &x, double &y)
331  {
332  double x_d = (iP.get_u() - cam.u0) / cam.px, y_d = (iP.get_v() - cam.v0) / cam.py;
333  double scale = 1.0;
334  double r_d = sqrt(vpMath::sqr(x_d) + vpMath::sqr(y_d));
335 
336  r_d = std::min(std::max(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.
337 
338  std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();
339 
340  const double EPS = 1e-8;
341  // Use Newton-Raphson method to solve for the angle theta
342  if (r_d > EPS) {
343  // compensate distortion iteratively
344  double theta = r_d;
345 
346  for (int j = 0; j < 10; j++) {
347  double theta2 = theta * theta, theta4 = theta2 * theta2, theta6 = theta4 * theta2, theta8 = theta6 * theta2;
348  double k0_theta2 = k[0] * theta2, k1_theta4 = k[1] * theta4, k2_theta6 = k[2] * theta6,
349  k3_theta8 = k[3] * theta8;
350  /* new_theta = theta - theta_fix, theta_fix = f0(theta) / f0'(theta) */
351  double theta_fix = (theta * (1 + k0_theta2 + k1_theta4 + k2_theta6 + k3_theta8) - r_d) /
352  (1 + 3 * k0_theta2 + 5 * k1_theta4 + 7 * k2_theta6 + 9 * k3_theta8);
353  theta = theta - theta_fix;
354  if (fabs(theta_fix) < EPS)
355  break;
356  }
357 
358  scale = std::tan(theta) / r_d; // Scale of norm of (x,y) and (x_d, y_d)
359  }
360 
361  x = x_d * scale;
362  y = y_d * scale;
363  }
364 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
366 
367 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
370  static void convertEllipse(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, const vpImagePoint &center_p,
371  double n20_p, double n11_p, double n02_p, double &xc_m, double &yc_m, double &n20_m,
372  double &n11_m, double &n02_m);
373  static void convertLine(const cv::Mat &cameraMatrix, const double &rho_p, const double &theta_p, double &rho_m,
374  double &theta_m);
375  static void convertMoment(const cv::Mat &cameraMatrix, unsigned int order, const vpMatrix &moment_pixel,
376  vpMatrix &moment_meter);
377  static void convertPoint(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, const double &u, const double &v,
378  double &x, double &y);
379  static void convertPoint(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, const vpImagePoint &iP, double &x,
380  double &y);
382 #endif
383 };
384 
385 #endif
Generic class defining intrinsic camera parameters.
std::vector< double > getKannalaBrandtDistortionCoefficients() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
double get_u() const
Definition: vpImagePoint.h:143
double get_v() const
Definition: vpImagePoint.h:154
static double sqr(double x)
Definition: vpMath.h:123
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
static void convertPoint(const vpCameraParameters &cam, const vpImagePoint &iP, double &x, double &y)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)