Visual Servoing Platform  version 3.3.1 under development (2020-08-10)
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/imgproc/imgproc.hpp>
56 # include <opencv2/calib3d/calib3d.hpp>
57 #endif
58 
71 class VISP_EXPORT vpPixelMeterConversion
72 {
73 public:
76  static void convertLine(const vpCameraParameters &cam, const double &rho_p, const double &theta_p, double &rho_m,
77  double &theta_m);
78 
79  static void convertMoment(const vpCameraParameters &cam, unsigned int order, const vpMatrix &moment_pixel,
80  vpMatrix &moment_meter);
106  inline static void convertPoint(const vpCameraParameters &cam,
107  const double &u, const double &v, double &x, double &y)
108  {
109  switch (cam.projModel) {
111  convertPointWithoutDistortion(cam, u, v, x, y);
112  break;
114  convertPointWithDistortion(cam, u, v, x, y);
115  break;
117  convertPointWithKannalaBrandtDistortion(cam, u, v, x, y);
118  break;
119  }
120  }
121 
149  inline static void convertPoint(const vpCameraParameters &cam,
150  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
166 
178  inline static void convertPointWithoutDistortion(const vpCameraParameters &cam,
179  const double &u, const double &v, 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,
201  const vpImagePoint &iP, double &x, 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,
222  const double &u, const double &v, 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,
246  const vpImagePoint &iP, double &x, 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  {
289  // compensate distortion iteratively
290  double theta = r_d;
291 
292  for (int j = 0; j < 10; j++)
293  {
294  double theta2 = theta*theta, theta4 = theta2*theta2, theta6 = theta4*theta2, theta8 = theta6*theta2;
295  double k0_theta2 = k[0] * theta2, k1_theta4 = k[1] * theta4, k2_theta6 = k[2] * theta6, k3_theta8 = k[3] * theta8;
296  /* new_theta = theta - theta_fix, theta_fix = f0(theta) / f0'(theta) */
297  double theta_fix = (theta * (1 + k0_theta2 + k1_theta4 + k2_theta6 + k3_theta8) - r_d) /
298  (1 + 3*k0_theta2 + 5*k1_theta4 + 7*k2_theta6 + 9*k3_theta8);
299  theta = theta - theta_fix;
300  if (fabs(theta_fix) < EPS)
301  break;
302  }
303 
304  scale = std::tan(theta) / r_d; // Scale of norm of (x,y) and (x_d, y_d)
305  }
306 
307  x = x_d * scale;
308  y = y_d * scale;
309  }
310 
330  inline static void convertPointWithKannalaBrandtDistortion(const vpCameraParameters &cam, const vpImagePoint &iP,
331  double &x, double &y)
332  {
333  double x_d = (iP.get_u() - cam.u0) / cam.px, y_d = (iP.get_v() - cam.v0) / cam.py;
334  double scale = 1.0;
335  double r_d = sqrt(vpMath::sqr(x_d) + vpMath::sqr(y_d));
336 
337  r_d = std::min(std::max(-M_PI, r_d), M_PI); // FOV restricted to 180degrees.
338 
339  std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();
340 
341  const double EPS = 1e-8;
342  // Use Newton-Raphson method to solve for the angle theta
343  if (r_d > EPS)
344  {
345  // compensate distortion iteratively
346  double theta = r_d;
347 
348  for (int j = 0; j < 10; j++)
349  {
350  double theta2 = theta*theta, theta4 = theta2*theta2, theta6 = theta4*theta2, theta8 = theta6*theta2;
351  double k0_theta2 = k[0] * theta2, k1_theta4 = k[1] * theta4, k2_theta6 = k[2] * theta6, k3_theta8 = k[3] * theta8;
352  /* new_theta = theta - theta_fix, theta_fix = f0(theta) / f0'(theta) */
353  double theta_fix = (theta * (1 + k0_theta2 + k1_theta4 + k2_theta6 + k3_theta8) - r_d) /
354  (1 + 3*k0_theta2 + 5*k1_theta4 + 7*k2_theta6 + 9*k3_theta8);
355  theta = theta - theta_fix;
356  if (fabs(theta_fix) < EPS)
357  break;
358  }
359 
360  scale = std::tan(theta) / r_d; // Scale of norm of (x,y) and (x_d, y_d)
361  }
362 
363  x = x_d * scale;
364  y = y_d * scale;
365  }
366 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
367 
368 
369 #if VISP_HAVE_OPENCV_VERSION >= 0x020300
370 
372  static void convertLine(const cv::Mat &cameraMatrix,
373  const double &rho_p, const double &theta_p,
374  double &rho_m, double &theta_m);
375  static void convertMoment(const cv::Mat &cameraMatrix,
376  unsigned int order, const vpMatrix &moment_pixel,
377  vpMatrix &moment_meter);
378  static void convertPoint(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs,
379  const double &u, const double &v, double &x, double &y);
380  static void convertPoint(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs,
381  const vpImagePoint &iP, double &x, double &y);
383 #endif
384 };
385 
386 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:156
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
double get_u() const
Definition: vpImagePoint.h:262
static double sqr(double x)
Definition: vpMath.h:116
Generic class defining intrinsic camera parameters.
std::vector< double > getKannalaBrandtDistortionCoefficients() const
static void convertPoint(const vpCameraParameters &cam, const vpImagePoint &iP, double &x, double &y)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
double get_v() const
Definition: vpImagePoint.h:273