Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpConvert.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Directory management.
32 */
33
38
39#include <visp3/core/vpConfig.h>
40
41#if defined(VISP_HAVE_OPENCV) && (defined(HAVE_OPENCV_FEATURES2D) || defined(HAVE_OPENCV_FEATURES))
42
43#include <algorithm> // std::transform
44#include <vector> // std::vector
45
46#include <visp3/core/vpConvert.h>
47
56 vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint)
57{
58 return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
59}
60
67vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) { return vpImagePoint(point.y, point.x); }
68
75vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) { return vpImagePoint(point.y, point.x); }
76
83vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f)
84{
85 vpPoint pt;
86 pt.set_oX(point3f.x);
87 pt.set_oY(point3f.y);
88 pt.set_oZ(point3f.z);
89 pt.set_oW(1.0);
90 return pt;
91}
92
99vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f)
100{
101 vpPoint pt;
102 pt.set_X(point3f.x);
103 pt.set_Y(point3f.y);
104 pt.set_Z(point3f.z);
105 pt.set_W(1.0);
106 return pt;
107}
108
115vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d)
116{
117 vpPoint pt;
118 pt.set_oX(point3d.x);
119 pt.set_oY(point3d.y);
120 pt.set_oZ(point3d.z);
121 pt.set_oW(1.0);
122 return pt;
123}
124
131vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d)
132{
133 vpPoint pt;
134 pt.set_X(point3d.x);
135 pt.set_Y(point3d.y);
136 pt.set_Z(point3d.z);
137 pt.set_W(1.0);
138 return pt;
139}
140
147cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point)
148{
149 return cv::Point2f(static_cast<float>(point.get_u()), static_cast<float>(point.get_v()));
150}
151
158cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point)
159{
160 return cv::Point2d(point.get_u(), point.get_v());
161}
162
171cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point)
172{
173 return cv::Point3f(static_cast<float>(point.get_X()), static_cast<float>(point.get_Y()), static_cast<float>(point.get_Z()));
174}
175
184cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point)
185{
186 return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
187}
188
196cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point)
197{
198 return cv::Point3f(static_cast<float>(point.get_oX()), static_cast<float>(point.get_oY()), static_cast<float>(point.get_oZ()));
199}
200
208cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point)
209{
210 return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
211}
212
219int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) { return match.trainIdx; }
220
226void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) { to = keyPointToVpImagePoint(from); }
227
233void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) { to = point2fToVpImagePoint(from); }
234
240void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) { to = point2dToVpImagePoint(from); }
241
249void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, bool cameraFrame)
250{
251 if (cameraFrame) {
252 to = point3fToVpCamPoint(from);
253 }
254 else {
255 to = point3fToVpObjectPoint(from);
256 }
257}
258
266void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, bool cameraFrame)
267{
268 if (cameraFrame) {
269 to = point3dToVpCamPoint(from);
270 }
271 else {
272 to = point3dToVpObjectPoint(from);
273 }
274}
275
281void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to)
282{
283 to.resize(from.size());
284 std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
285}
286
292void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to)
293{
294 to.resize(from.size());
295 std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
296}
297
303void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to)
304{
305 to.resize(from.size());
306 std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
307}
308
316void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, bool cameraFrame)
317{
318 to.resize(from.size());
319 if (cameraFrame) {
320 std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
321 }
322 else {
323 std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
324 }
325}
326
334void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, bool cameraFrame)
335{
336 to.resize(from.size());
337 if (cameraFrame) {
338 std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
339 }
340 else {
341 std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
342 }
343}
344
355void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to)
356{
357 to.resize(from.size());
358 std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
359}
360
366void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) { to = vpImagePointToPoint2f(from); }
367
373void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) { to = vpImagePointToPoint2d(from); }
374
382void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, bool cameraFrame)
383{
384 if (cameraFrame) {
385 to = vpCamPointToPoint3f(from);
386 }
387 else {
388 to = vpObjectPointToPoint3f(from);
389 }
390}
391
399void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, bool cameraFrame)
400{
401 if (cameraFrame) {
402 to = vpCamPointToPoint3d(from);
403 }
404 else {
405 to = vpObjectPointToPoint3d(from);
406 }
407}
408
414void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to)
415{
416 to.resize(from.size());
417 std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
418}
419
425void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to)
426{
427 to.resize(from.size());
428 std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
429}
430
438void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, bool cameraFrame)
439{
440 to.resize(from.size());
441 if (cameraFrame) {
442 std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
443 }
444 else {
445 std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
446 }
447}
448
456void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, bool cameraFrame)
457{
458 to.resize(from.size());
459 if (cameraFrame) {
460 std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
461 }
462 else {
463 std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
464 }
465}
466END_VISP_NAMESPACE
467#elif !defined(VISP_BUILD_SHARED_LIBS)
468// Work around to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no symbols
469void dummy_vpConvert() { }
470#endif
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
double get_u() const
double get_v() const
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
double get_oX() const
Get the point oX coordinate in the object frame.
Definition vpPoint.cpp:418
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition vpPoint.cpp:459
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition vpPoint.cpp:468
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition vpPoint.cpp:411
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition vpPoint.cpp:422
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition vpPoint.cpp:464
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition vpPoint.cpp:453
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition vpPoint.cpp:455
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:413
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition vpPoint.cpp:466
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition vpPoint.cpp:457
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition vpPoint.cpp:462
double get_oY() const
Get the point oY coordinate in the object frame.
Definition vpPoint.cpp:420
double get_X() const
Get the point cX coordinate in the camera frame.
Definition vpPoint.cpp:409