Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpConvert.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Fabien Spindler
35  * Souriya Trinh
36  *
37  *****************************************************************************/
38 
44 #include <vector> // std::vector
45 #include <algorithm> // std::transform
46 
47 #include <visp3/core/vpConvert.h>
48 
49 
50 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
51 
57  vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint) {
58  return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
59  }
60 
67  vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) {
68  return vpImagePoint(point.y, point.x);
69  }
70 
77  vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) {
78  return vpImagePoint(point.y, point.x);
79  }
80 
86  vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f) {
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 
100  vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f) {
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 
114  vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d) {
115  vpPoint pt;
116  pt.set_oX(point3d.x);
117  pt.set_oY(point3d.y);
118  pt.set_oZ(point3d.z);
119  pt.set_oW(1.0);
120  return pt;
121  }
122 
128  vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d) {
129  vpPoint pt;
130  pt.set_X(point3d.x);
131  pt.set_Y(point3d.y);
132  pt.set_Z(point3d.z);
133  pt.set_W(1.0);
134  return pt;
135  }
136 
143  cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point) {
144  return cv::Point2f((float) point.get_u(), (float) point.get_v());
145  }
146 
153  cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point) {
154  return cv::Point2d(point.get_u(), point.get_v());
155  }
156 
163  cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point) {
164  return cv::Point3f((float) point.get_X(), (float) point.get_Y(), (float) point.get_Z());
165  }
166 
173  cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point) {
174  return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
175  }
176 
183  cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point) {
184  return cv::Point3f((float) point.get_oX(), (float) point.get_oY(), (float) point.get_oZ());
185  }
186 
193  cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point) {
194  return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
195  }
196 
203  int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) {
204  return match.trainIdx;
205  }
206 
207 
213  void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) {
214  to = keyPointToVpImagePoint(from);
215  }
216 
222  void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) {
223  to = point2fToVpImagePoint(from);
224  }
225 
231  void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) {
232  to = point2dToVpImagePoint(from);
233  }
234 
241  void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, const bool cameraFrame) {
242  if(cameraFrame) {
243  to = point3fToVpCamPoint(from);
244  } else {
245  to = point3fToVpObjectPoint(from);
246  }
247  }
248 
255  void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, const bool cameraFrame) {
256  if(cameraFrame) {
257  to = point3dToVpCamPoint(from);
258  } else {
259  to = point3dToVpObjectPoint(from);
260  }
261  }
262 
268  void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to) {
269  to.resize(from.size());
270  std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
271  }
272 
278  void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to) {
279  to.resize(from.size());
280  std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
281  }
282 
288  void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to) {
289  to.resize(from.size());
290  std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
291  }
292 
299  void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, const bool cameraFrame) {
300  to.resize(from.size());
301  if(cameraFrame) {
302  std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
303  } else {
304  std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
305  }
306  }
307 
314  void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, const bool cameraFrame) {
315  to.resize(from.size());
316  if(cameraFrame) {
317  std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
318  } else {
319  std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
320  }
321  }
322 
331  void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to) {
332  to.resize(from.size());
333  std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
334  }
335 
341  void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) {
342  to = vpImagePointToPoint2f(from);
343  }
344 
350  void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) {
351  to = vpImagePointToPoint2d(from);
352  }
353 
360  void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, const bool cameraFrame) {
361  if(cameraFrame) {
362  to = vpCamPointToPoint3f(from);
363  } else {
364  to = vpObjectPointToPoint3f(from);
365  }
366  }
367 
374  void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, const bool cameraFrame) {
375  if(cameraFrame) {
376  to = vpCamPointToPoint3d(from);
377  } else {
378  to = vpObjectPointToPoint3d(from);
379  }
380  }
381 
387  void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to) {
388  to.resize(from.size());
389  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
390  }
391 
397  void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to) {
398  to.resize(from.size());
399  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
400  }
401 
408  void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, const bool cameraFrame) {
409  to.resize(from.size());
410  if(cameraFrame) {
411  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
412  } else {
413  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
414  }
415  }
416 
423  void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, const bool cameraFrame) {
424  to.resize(from.size());
425  if(cameraFrame) {
426  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
427  } else {
428  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
429  }
430  }
431 #elif !defined(VISP_BUILD_SHARED_LIBS)
432 // Work arround to avoid warning: libvisp_core.a(vpConvert.cpp.o) has no symbols
433 void dummy_vpConvert() {};
434 #endif
double get_v() const
Definition: vpImagePoint.h:268
void set_oZ(const double oZ)
Set the point Z coordinate in the object frame.
Definition: vpPoint.cpp:491
double get_u() const
Definition: vpImagePoint.h:257
double get_oY() const
Get the point Y coordinate in the object frame.
Definition: vpPoint.cpp:449
void set_X(const double X)
Set the point X coordinate in the camera frame.
Definition: vpPoint.cpp:478
Class that defines what is a point.
Definition: vpPoint.h:59
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:341
void set_oW(const double oW)
Set the point W coordinate in the object frame.
Definition: vpPoint.cpp:493
void set_Z(const double Z)
Set the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:482
void set_W(const double W)
Set the point W coordinate in the camera frame.
Definition: vpPoint.cpp:484
double get_oZ() const
Get the point Z coordinate in the object frame.
Definition: vpPoint.cpp:451
void set_oX(const double oX)
Set the point X coordinate in the object frame.
Definition: vpPoint.cpp:487
void set_Y(const double Y)
Set the point Y coordinate in the camera frame.
Definition: vpPoint.cpp:480
double get_Y() const
Get the point Y coordinate in the camera frame.
Definition: vpPoint.cpp:440
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:442
double get_oX() const
Get the point X coordinate in the object frame.
Definition: vpPoint.cpp:447
void set_oY(const double oY)
Set the point Y coordinate in the object frame.
Definition: vpPoint.cpp:489
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
double get_X() const
Get the point X coordinate in the camera frame.
Definition: vpPoint.cpp:438
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:213