Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpMeterPixelConversion.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Meter to pixel conversion.
32 */
33
38
39#ifndef VP_METER_PIXEL_CONVERSION_H
40#define VP_METER_PIXEL_CONVERSION_H
41
42#include <visp3/core/vpCameraParameters.h>
43#include <visp3/core/vpCircle.h>
44#include <visp3/core/vpException.h>
45#include <visp3/core/vpImagePoint.h>
46#include <visp3/core/vpMath.h>
47#include <visp3/core/vpSphere.h>
48
49#if defined(VISP_HAVE_OPENCV)
50#include <opencv2/core/core.hpp>
51#endif
52
66class VISP_EXPORT vpMeterPixelConversion
67{
68public:
71 static void convertEllipse(const vpCameraParameters &cam, const vpSphere &sphere, vpImagePoint &center_p,
72 double &n20_p, double &n11_p, double &n02_p);
73 static void convertEllipse(const vpCameraParameters &cam, const vpCircle &circle, vpImagePoint &center_p,
74 double &n20_p, double &n11_p, double &n02_p);
75 static void convertEllipse(const vpCameraParameters &cam, double xc_m, double yc_m, double n20_m, double n11_m,
76 double n02_m, vpImagePoint &center_p, double &n20_p, double &n11_p, double &n02_p);
77 static void convertLine(const vpCameraParameters &cam, const double &rho_m, const double &theta_m, double &rho_p,
78 double &theta_p);
79
105 inline static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
106 {
107 switch (cam.m_projModel) {
109 convertPointWithoutDistortion(cam, x, y, u, v);
110 break;
112 convertPointWithDistortion(cam, x, y, u, v);
113 break;
115 convertPointWithKannalaBrandtDistortion(cam, x, y, u, v);
116 break;
117 default: {
118 throw(vpException(vpException::fatalError, "Unsupported camera projection model in vpMeterPixelConversion::convertPoint()"));
119 }
120 }
121 }
122
149
150 inline static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, vpImagePoint &iP)
151 {
152 switch (cam.m_projModel) {
154 convertPointWithoutDistortion(cam, x, y, iP);
155 break;
157 convertPointWithDistortion(cam, x, y, iP);
158 break;
160 convertPointWithKannalaBrandtDistortion(cam, x, y, iP);
161 break;
162 default: {
163 throw(vpException(vpException::fatalError, "Unsupported camera projection model in vpMeterPixelConversion::convertPoint()"));
164 }
165 }
166 }
167
168#ifndef DOXYGEN_SHOULD_SKIP_THIS
177
178 inline static void convertPointWithoutDistortion(const vpCameraParameters &cam, const double &x, const double &y,
179 double &u, double &v)
180 {
181 u = (x * cam.m_px) + cam.m_u0;
182 v = (y * cam.m_py) + cam.m_v0;
183 }
184
194
195 inline static void convertPointWithoutDistortion(const vpCameraParameters &cam, const double &x, const double &y,
196 vpImagePoint &iP)
197 {
198 iP.set_u((x * cam.m_px) + cam.m_u0);
199 iP.set_v((y * cam.m_py) + cam.m_v0);
200 }
201
218 inline static void convertPointWithDistortion(const vpCameraParameters &cam, const double &x, const double &y,
219 double &u, double &v)
220 {
221 double r2 = 1. + (cam.m_kud * ((x * x) + (y * y)));
222 u = cam.m_u0 + (cam.m_px * x * r2);
223 v = cam.m_v0 + (cam.m_py * y * r2);
224 }
225
242 inline static void convertPointWithDistortion(const vpCameraParameters &cam, const double &x, const double &y,
243 vpImagePoint &iP)
244 {
245 double r2 = 1. + (cam.m_kud * ((x * x) + (y * y)));
246 iP.set_u(cam.m_u0 + (cam.m_px * x * r2));
247 iP.set_v(cam.m_v0 + (cam.m_py * y * r2));
248 }
249
272 inline static void convertPointWithKannalaBrandtDistortion(const vpCameraParameters &cam, const double &x,
273 const double &y, double &u, double &v)
274 {
275 double r = sqrt(vpMath::sqr(x) + vpMath::sqr(y));
276 double theta = atan(r);
277 const unsigned int index_0 = 0;
278 const unsigned int index_1 = 1;
279 const unsigned int index_2 = 2;
280 const unsigned int index_3 = 3;
281
282 std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();
283
284 double theta2 = theta * theta, theta3 = theta2 * theta, theta4 = theta2 * theta2, theta5 = theta4 * theta,
285 theta6 = theta3 * theta3, theta7 = theta6 * theta, theta8 = theta4 * theta4, theta9 = theta8 * theta;
286
287 double r_d = theta + (k[index_0] * theta3) + (k[index_1] * theta5) + (k[index_2] * theta7) + (k[index_3] * theta9);
288
289 double scale = (std::fabs(r) < std::numeric_limits<double>::epsilon()) ? 1.0 : (r_d / r);
290
291 double x_d = x * scale;
292 double y_d = y * scale;
293
294 u = (cam.m_px * x_d) + cam.m_u0;
295 v = (cam.m_py * y_d) + cam.m_v0;
296 }
297
319 inline static void convertPointWithKannalaBrandtDistortion(const vpCameraParameters &cam, const double &x,
320 const double &y, vpImagePoint &iP)
321 {
322 double r = sqrt(vpMath::sqr(x) + vpMath::sqr(y));
323 double theta = atan(r);
324 const unsigned int index_0 = 0;
325 const unsigned int index_1 = 1;
326 const unsigned int index_2 = 2;
327 const unsigned int index_3 = 3;
328
329 std::vector<double> k = cam.getKannalaBrandtDistortionCoefficients();
330
331 double theta2 = theta * theta, theta3 = theta2 * theta, theta4 = theta2 * theta2, theta5 = theta4 * theta,
332 theta6 = theta3 * theta3, theta7 = theta6 * theta, theta8 = theta4 * theta4, theta9 = theta8 * theta;
333
334 double r_d = theta + (k[index_0] * theta3) + (k[index_1] * theta5) + (k[index_2] * theta7) + (k[index_3] * theta9);
335
336 double scale = (std::fabs(r) < std::numeric_limits<double>::epsilon()) ? 1.0 : (r_d / r);
337
338 double x_d = x * scale;
339 double y_d = y * scale;
340
341 iP.set_u((cam.m_px * x_d) + cam.m_u0);
342 iP.set_v((cam.m_py * y_d) + cam.m_v0);
343 }
344
345#endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
347
348#if defined(VISP_HAVE_OPENCV) && \
349 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D)) || \
350 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_CALIB) && defined(HAVE_OPENCV_3D)))
353 static void convertEllipse(const cv::Mat &cameraMatrix, const vpCircle &circle, vpImagePoint &center, double &n20_p,
354 double &n11_p, double &n02_p);
355 static void convertEllipse(const cv::Mat &cameraMatrix, const vpSphere &sphere, vpImagePoint &center, double &n20_p,
356 double &n11_p, double &n02_p);
357 static void convertEllipse(const cv::Mat &cameraMatrix, double xc_m, double yc_m, double n20_m, double n11_m,
358 double n02_m, vpImagePoint &center_p, double &n20_p, double &n11_p, double &n02_p);
359 static void convertLine(const cv::Mat &cameraMatrix, const double &rho_m, const double &theta_m, double &rho_p,
360 double &theta_p);
361 static void convertPoint(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, const double &x, const double &y,
362 double &u, double &v);
363 static void convertPoint(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, const double &x, const double &y,
364 vpImagePoint &iP);
366#endif
367};
368END_VISP_NAMESPACE
369#endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition vpCircle.h:87
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_u(double u)
void set_v(double v)
static double sqr(double x)
Definition vpMath.h:203
static void convertLine(const vpCameraParameters &cam, const double &rho_m, const double &theta_m, double &rho_p, double &theta_p)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, vpImagePoint &iP)
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
static void convertEllipse(const vpCameraParameters &cam, const vpSphere &sphere, vpImagePoint &center_p, double &n20_p, double &n11_p, double &n02_p)
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition vpSphere.h:80