Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
vpConvert.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Directory management.
33  *
34 *****************************************************************************/
35 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_OPENCV) && (defined(HAVE_OPENCV_FEATURES2D) || defined(HAVE_OPENCV_FEATURES))
44 
45 #include <algorithm> // std::transform
46 #include <vector> // std::vector
47 
48 #include <visp3/core/vpConvert.h>
49 
50 BEGIN_VISP_NAMESPACE
58  vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint)
59 {
60  return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
61 }
62 
69 vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) { return vpImagePoint(point.y, point.x); }
70 
77 vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) { return vpImagePoint(point.y, point.x); }
78 
85 vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f)
86 {
87  vpPoint pt;
88  pt.set_oX(point3f.x);
89  pt.set_oY(point3f.y);
90  pt.set_oZ(point3f.z);
91  pt.set_oW(1.0);
92  return pt;
93 }
94 
101 vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f)
102 {
103  vpPoint pt;
104  pt.set_X(point3f.x);
105  pt.set_Y(point3f.y);
106  pt.set_Z(point3f.z);
107  pt.set_W(1.0);
108  return pt;
109 }
110 
117 vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d)
118 {
119  vpPoint pt;
120  pt.set_oX(point3d.x);
121  pt.set_oY(point3d.y);
122  pt.set_oZ(point3d.z);
123  pt.set_oW(1.0);
124  return pt;
125 }
126 
133 vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d)
134 {
135  vpPoint pt;
136  pt.set_X(point3d.x);
137  pt.set_Y(point3d.y);
138  pt.set_Z(point3d.z);
139  pt.set_W(1.0);
140  return pt;
141 }
142 
149 cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point)
150 {
151  return cv::Point2f((float)point.get_u(), (float)point.get_v());
152 }
153 
160 cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point)
161 {
162  return cv::Point2d(point.get_u(), point.get_v());
163 }
164 
173 cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point)
174 {
175  return cv::Point3f((float)point.get_X(), (float)point.get_Y(), (float)point.get_Z());
176 }
177 
186 cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point)
187 {
188  return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
189 }
190 
198 cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point)
199 {
200  return cv::Point3f((float)point.get_oX(), (float)point.get_oY(), (float)point.get_oZ());
201 }
202 
210 cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point)
211 {
212  return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
213 }
214 
221 int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) { return match.trainIdx; }
222 
228 void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) { to = keyPointToVpImagePoint(from); }
229 
235 void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) { to = point2fToVpImagePoint(from); }
236 
242 void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) { to = point2dToVpImagePoint(from); }
243 
251 void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, bool cameraFrame)
252 {
253  if (cameraFrame) {
254  to = point3fToVpCamPoint(from);
255  }
256  else {
257  to = point3fToVpObjectPoint(from);
258  }
259 }
260 
268 void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, bool cameraFrame)
269 {
270  if (cameraFrame) {
271  to = point3dToVpCamPoint(from);
272  }
273  else {
274  to = point3dToVpObjectPoint(from);
275  }
276 }
277 
283 void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to)
284 {
285  to.resize(from.size());
286  std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
287 }
288 
294 void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to)
295 {
296  to.resize(from.size());
297  std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
298 }
299 
305 void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to)
306 {
307  to.resize(from.size());
308  std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
309 }
310 
318 void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, bool cameraFrame)
319 {
320  to.resize(from.size());
321  if (cameraFrame) {
322  std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
323  }
324  else {
325  std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
326  }
327 }
328 
336 void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, bool cameraFrame)
337 {
338  to.resize(from.size());
339  if (cameraFrame) {
340  std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
341  }
342  else {
343  std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
344  }
345 }
346 
357 void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to)
358 {
359  to.resize(from.size());
360  std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
361 }
362 
368 void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) { to = vpImagePointToPoint2f(from); }
369 
375 void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) { to = vpImagePointToPoint2d(from); }
376 
384 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, bool cameraFrame)
385 {
386  if (cameraFrame) {
387  to = vpCamPointToPoint3f(from);
388  }
389  else {
390  to = vpObjectPointToPoint3f(from);
391  }
392 }
393 
401 void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, bool cameraFrame)
402 {
403  if (cameraFrame) {
404  to = vpCamPointToPoint3d(from);
405  }
406  else {
407  to = vpObjectPointToPoint3d(from);
408  }
409 }
410 
416 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to)
417 {
418  to.resize(from.size());
419  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
420 }
421 
427 void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to)
428 {
429  to.resize(from.size());
430  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
431 }
432 
440 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, bool cameraFrame)
441 {
442  to.resize(from.size());
443  if (cameraFrame) {
444  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
445  }
446  else {
447  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
448  }
449 }
450 
458 void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, bool cameraFrame)
459 {
460  to.resize(from.size());
461  if (cameraFrame) {
462  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
463  }
464  else {
465  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
466  }
467 }
468 END_VISP_NAMESPACE
469 #elif !defined(VISP_BUILD_SHARED_LIBS)
470 // Work around to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no symbols
471 void dummy_vpConvert() { };
472 #endif
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:228
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:368
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
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:415
void set_W(double cW)
Set the point cW coordinate in the camera frame.
Definition: vpPoint.cpp:456
void set_oW(double oW)
Set the point oW coordinate in the object frame.
Definition: vpPoint.cpp:465
double get_Y() const
Get the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:408
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:419
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:461
void set_X(double cX)
Set the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:450
void set_Y(double cY)
Set the point cY coordinate in the camera frame.
Definition: vpPoint.cpp:452
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:410
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:463
void set_Z(double cZ)
Set the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:454
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:459
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:417
double get_X() const
Get the point cX coordinate in the camera frame.
Definition: vpPoint.cpp:406