ViSP  2.10.0
vpConvert.cpp
1 /****************************************************************************
2  *
3  * $Id: vpIoTools.cpp 5214 2015-01-27 18:33:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Directory management.
36  *
37  * Authors:
38  * Fabien Spindler
39  * Souriya Trinh
40  *
41  *****************************************************************************/
42 
48 #include <vector> // std::vector
49 #include <algorithm> // std::transform
50 
51 #include <visp/vpConvert.h>
52 
53 
54 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
55 
61  vpImagePoint vpConvert::keyPointToVpImagePoint(const cv::KeyPoint &keypoint) {
62  return vpImagePoint(keypoint.pt.y, keypoint.pt.x);
63  }
64 
71  vpImagePoint vpConvert::point2fToVpImagePoint(const cv::Point2f &point) {
72  return vpImagePoint(point.y, point.x);
73  }
74 
81  vpImagePoint vpConvert::point2dToVpImagePoint(const cv::Point2d &point) {
82  return vpImagePoint(point.y, point.x);
83  }
84 
90  vpPoint vpConvert::point3fToVpObjectPoint(const cv::Point3f &point3f) {
91  vpPoint pt;
92  pt.set_oX(point3f.x);
93  pt.set_oY(point3f.y);
94  pt.set_oZ(point3f.z);
95  pt.set_oW(1.0);
96  return pt;
97  }
98 
104  vpPoint vpConvert::point3fToVpCamPoint(const cv::Point3f &point3f) {
105  vpPoint pt;
106  pt.set_X(point3f.x);
107  pt.set_Y(point3f.y);
108  pt.set_Z(point3f.z);
109  pt.set_W(1.0);
110  return pt;
111  }
112 
118  vpPoint vpConvert::point3dToVpObjectPoint(const cv::Point3d &point3d) {
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 
132  vpPoint vpConvert::point3dToVpCamPoint(const cv::Point3d &point3d) {
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 
147  cv::Point2f vpConvert::vpImagePointToPoint2f(const vpImagePoint &point) {
148  return cv::Point2f((float) point.get_u(), (float) point.get_v());
149  }
150 
157  cv::Point2d vpConvert::vpImagePointToPoint2d(const vpImagePoint &point) {
158  return cv::Point2d(point.get_u(), point.get_v());
159  }
160 
167  cv::Point3f vpConvert::vpCamPointToPoint3f(const vpPoint &point) {
168  return cv::Point3f((float) point.get_X(), (float) point.get_Y(), (float) point.get_Z());
169  }
170 
177  cv::Point3d vpConvert::vpCamPointToPoint3d(const vpPoint &point) {
178  return cv::Point3d(point.get_X(), point.get_Y(), point.get_Z());
179  }
180 
187  cv::Point3f vpConvert::vpObjectPointToPoint3f(const vpPoint &point) {
188  return cv::Point3f((float) point.get_oX(), (float) point.get_oY(), (float) point.get_oZ());
189  }
190 
197  cv::Point3d vpConvert::vpObjectPointToPoint3d(const vpPoint &point) {
198  return cv::Point3d(point.get_oX(), point.get_oY(), point.get_oZ());
199  }
200 
207  int vpConvert::dMatchToTrainIndex(const cv::DMatch &match) {
208  return match.trainIdx;
209  }
210 
211 
217  void vpConvert::convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to) {
218  to = keyPointToVpImagePoint(from);
219  }
220 
226  void vpConvert::convertFromOpenCV(const cv::Point2f &from, vpImagePoint &to) {
227  to = point2fToVpImagePoint(from);
228  }
229 
235  void vpConvert::convertFromOpenCV(const cv::Point2d &from, vpImagePoint &to) {
236  to = point2dToVpImagePoint(from);
237  }
238 
245  void vpConvert::convertFromOpenCV(const cv::Point3f &from, vpPoint &to, const bool cameraFrame) {
246  if(cameraFrame) {
247  to = point3fToVpCamPoint(from);
248  } else {
249  to = point3fToVpObjectPoint(from);
250  }
251  }
252 
259  void vpConvert::convertFromOpenCV(const cv::Point3d &from, vpPoint &to, const bool cameraFrame) {
260  if(cameraFrame) {
261  to = point3dToVpCamPoint(from);
262  } else {
263  to = point3dToVpObjectPoint(from);
264  }
265  }
266 
272  void vpConvert::convertFromOpenCV(const std::vector<cv::KeyPoint> &from, std::vector<vpImagePoint> &to) {
273  to.resize(from.size());
274  std::transform(from.begin(), from.end(), to.begin(), keyPointToVpImagePoint);
275  }
276 
282  void vpConvert::convertFromOpenCV(const std::vector<cv::Point2f> &from, std::vector<vpImagePoint> &to) {
283  to.resize(from.size());
284  std::transform(from.begin(), from.end(), to.begin(), point2fToVpImagePoint);
285  }
286 
292  void vpConvert::convertFromOpenCV(const std::vector<cv::Point2d> &from, std::vector<vpImagePoint> &to) {
293  to.resize(from.size());
294  std::transform(from.begin(), from.end(), to.begin(), point2dToVpImagePoint);
295  }
296 
303  void vpConvert::convertFromOpenCV(const std::vector<cv::Point3f> &from, std::vector<vpPoint> &to, const bool cameraFrame) {
304  to.resize(from.size());
305  if(cameraFrame) {
306  std::transform(from.begin(), from.end(), to.begin(), point3fToVpCamPoint);
307  } else {
308  std::transform(from.begin(), from.end(), to.begin(), point3fToVpObjectPoint);
309  }
310  }
311 
318  void vpConvert::convertFromOpenCV(const std::vector<cv::Point3d> &from, std::vector<vpPoint> &to, const bool cameraFrame) {
319  to.resize(from.size());
320  if(cameraFrame) {
321  std::transform(from.begin(), from.end(), to.begin(), point3dToVpCamPoint);
322  } else {
323  std::transform(from.begin(), from.end(), to.begin(), point3dToVpObjectPoint);
324  }
325  }
326 
335  void vpConvert::convertFromOpenCV(const std::vector<cv::DMatch> &from, std::vector<unsigned int> &to) {
336  to.resize(from.size());
337  std::transform(from.begin(), from.end(), to.begin(), dMatchToTrainIndex);
338  }
339 
345  void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2f &to) {
346  to = vpImagePointToPoint2f(from);
347  }
348 
354  void vpConvert::convertToOpenCV(const vpImagePoint &from, cv::Point2d &to) {
355  to = vpImagePointToPoint2d(from);
356  }
357 
364  void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3f &to, const bool cameraFrame) {
365  if(cameraFrame) {
366  to = vpCamPointToPoint3f(from);
367  } else {
368  to = vpObjectPointToPoint3f(from);
369  }
370  }
371 
378  void vpConvert::convertToOpenCV(const vpPoint &from, cv::Point3d &to, const bool cameraFrame) {
379  if(cameraFrame) {
380  to = vpCamPointToPoint3d(from);
381  } else {
382  to = vpObjectPointToPoint3d(from);
383  }
384  }
385 
391  void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2f> &to) {
392  to.resize(from.size());
393  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2f);
394  }
395 
401  void vpConvert::convertToOpenCV(const std::vector<vpImagePoint> &from, std::vector<cv::Point2d> &to) {
402  to.resize(from.size());
403  std::transform(from.begin(), from.end(), to.begin(), vpImagePointToPoint2d);
404  }
405 
412  void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3f> &to, const bool cameraFrame) {
413  to.resize(from.size());
414  if(cameraFrame) {
415  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3f);
416  } else {
417  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3f);
418  }
419  }
420 
427  void vpConvert::convertToOpenCV(const std::vector<vpPoint> &from, std::vector<cv::Point3d> &to, const bool cameraFrame) {
428  to.resize(from.size());
429  if(cameraFrame) {
430  std::transform(from.begin(), from.end(), to.begin(), vpCamPointToPoint3d);
431  } else {
432  std::transform(from.begin(), from.end(), to.begin(), vpObjectPointToPoint3d);
433  }
434  }
435 #endif
double get_v() const
Definition: vpImagePoint.h:264
double get_u() const
Definition: vpImagePoint.h:253
double get_oY() const
Get the point Y coordinate in the object frame.
Definition: vpPoint.h:129
void set_X(const double X)
Set the point X coordinate in the camera frame.
Definition: vpPoint.h:176
void set_oX(const double X)
Set the point X coordinate in the object frame.
Definition: vpPoint.h:185
Class that defines what is a point.
Definition: vpPoint.h:65
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:345
void set_Z(const double Z)
Set the point Z coordinate in the camera frame.
Definition: vpPoint.h:180
void set_oZ(const double Z)
Set the point Z coordinate in the object frame.
Definition: vpPoint.h:189
void set_W(const double W)
Set the point W coordinate in the camera frame.
Definition: vpPoint.h:182
double get_oZ() const
Get the point Z coordinate in the object frame.
Definition: vpPoint.h:131
void set_oW(const double W)
Set the point W coordinate in the object frame.
Definition: vpPoint.h:191
void set_Y(const double Y)
Set the point Y coordinate in the camera frame.
Definition: vpPoint.h:178
double get_Y() const
Get the point Y coordinate in the camera frame.
Definition: vpPoint.h:120
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.h:122
double get_oX() const
Get the point X coordinate in the object frame.
Definition: vpPoint.h:127
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
double get_X() const
Get the point X coordinate in the camera frame.
Definition: vpPoint.h:118
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:217
void set_oY(const double Y)
Set the point Y coordinate in the object frame.
Definition: vpPoint.h:187