Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testConvert.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  * Test functions in vpIoTools.
32  *
33  * Authors:
34  * Souriya Trinh
35  *
36  *****************************************************************************/
37 
46 #include <iostream> // std::cout
47 #include <limits> // std::numeric_limits
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpConvert.h>
50 
51 
52 bool areSame(double a, double b) {
53  return fabs(a - b) < std::numeric_limits<double>::epsilon();
54 }
55 
56 void testConvertFromImagePointToPoint2f() {
57 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
58  vpImagePoint imPt1(12.5f, .85f);
59  vpImagePoint imPt2(-44.26f, 125.11f);
60  vpImagePoint imPt3(0.0f, -1.756e-10f);
61 
62  cv::Point2f pt1, pt2, pt3;
63  vpConvert::convertToOpenCV(imPt1, pt1);
64  vpConvert::convertToOpenCV(imPt2, pt2);
65  vpConvert::convertToOpenCV(imPt3, pt3);
66 
67  int nbOk = 0, nbNOk = 0;
68  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
69  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
70  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
71 
72  std::vector<vpImagePoint> listOfImPts(3);
73  listOfImPts[0] = imPt1;
74  listOfImPts[1] = imPt2;
75  listOfImPts[2] = imPt3;
76 
77  std::vector<cv::Point2f> listOfPts;
78  vpConvert::convertToOpenCV(listOfImPts, listOfPts);
79 
80  if(listOfImPts.size() == listOfPts.size()) {
81  for(size_t i = 0; i < 3; i++) {
82  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
83  }
84  } else {
85  nbNOk += 3;
86  }
87 
88  std::cout << "testConvertFromImagePointToPoint2f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
89 #endif
90 }
91 
92 void testConvertFromPoint2fToImagePoint() {
93 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
94  vpImagePoint imPt1, imPt2, imPt3;
95 
96  cv::Point2f pt1(12.5f, .85f), pt2(-44.26f, 125.11f), pt3(0.0f, -1.756e-10f);
97  vpConvert::convertFromOpenCV(pt1, imPt1);
98  vpConvert::convertFromOpenCV(pt2, imPt2);
99  vpConvert::convertFromOpenCV(pt3, imPt3);
100 
101  int nbOk = 0, nbNOk = 0;
102  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
103  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
104  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
105 
106  std::vector<vpImagePoint> listOfImPts;
107 
108  std::vector<cv::Point2f> listOfPts(3);
109  listOfPts[0] = pt1;
110  listOfPts[1] = pt2;
111  listOfPts[2] = pt3;
112 
113  vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
114 
115  if(listOfImPts.size() == listOfPts.size()) {
116  for(size_t i = 0; i < 3; i++) {
117  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
118  }
119  } else {
120  nbNOk += 3;
121  }
122 
123  std::cout << "testConvertFromPoint2fToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
124 #endif
125 }
126 
127 void testConvertFromImagePointToPoint2d() {
128 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
129  vpImagePoint imPt1(12.5, .85);
130  vpImagePoint imPt2(-44.26, 125.11);
131  vpImagePoint imPt3(0, -1.756e-10);
132 
133  cv::Point2d pt1, pt2, pt3;
134  vpConvert::convertToOpenCV(imPt1, pt1);
135  vpConvert::convertToOpenCV(imPt2, pt2);
136  vpConvert::convertToOpenCV(imPt3, pt3);
137 
138  int nbOk = 0, nbNOk = 0;
139  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
140  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
141  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
142 
143  std::vector<vpImagePoint> listOfImPts(3);
144  listOfImPts[0] = imPt1;
145  listOfImPts[1] = imPt2;
146  listOfImPts[2] = imPt3;
147 
148  std::vector<cv::Point2d> listOfPts;
149  vpConvert::convertToOpenCV(listOfImPts, listOfPts);
150 
151  if(listOfImPts.size() == listOfPts.size()) {
152  for(size_t i = 0; i < 3; i++) {
153  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
154  }
155  } else {
156  nbNOk += 3;
157  }
158 
159  std::cout << "testConvertFromImagePointToPoint2d=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
160 #endif
161 }
162 
163 void testConvertFromPoint2dToImagePoint() {
164 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
165  vpImagePoint imPt1, imPt2, imPt3;
166 
167  cv::Point2d pt1(12.5, .85), pt2(-44.26, 125.11), pt3(0, -1.756e-10);
168  vpConvert::convertFromOpenCV(pt1, imPt1);
169  vpConvert::convertFromOpenCV(pt2, imPt2);
170  vpConvert::convertFromOpenCV(pt3, imPt3);
171 
172  int nbOk = 0, nbNOk = 0;
173  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
174  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
175  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
176 
177  std::vector<vpImagePoint> listOfImPts;
178 
179  std::vector<cv::Point2d> listOfPts(3);
180  listOfPts[0] = pt1;
181  listOfPts[1] = pt2;
182  listOfPts[2] = pt3;
183 
184  vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
185 
186  if(listOfImPts.size() == listOfPts.size()) {
187  for(size_t i = 0; i < 3; i++) {
188  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
189  }
190  } else {
191  nbNOk += 3;
192  }
193 
194  std::cout << "testConvertFromPoint2dToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
195 #endif
196 }
197 
198 void testConvertFromKeyPointToImagePoint() {
199 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
200  cv::KeyPoint kp1(12.5f, .85f, 0), kp2(-44.26f, 125.11f, 0), kp3(0.0f, -1.756e-10f, 0);
201  vpImagePoint imPt1, imPt2, imPt3;
202 
203  vpConvert::convertFromOpenCV(kp1, imPt1);
204  vpConvert::convertFromOpenCV(kp2, imPt2);
205  vpConvert::convertFromOpenCV(kp3, imPt3);
206 
207  int nbOk = 0, nbNOk = 0;
208  if(areSame(imPt1.get_u(), kp1.pt.x) && areSame(imPt1.get_v(), kp1.pt.y)) nbOk++; else nbNOk++;
209  if(areSame(imPt2.get_u(), kp2.pt.x) && areSame(imPt2.get_v(), kp2.pt.y)) nbOk++; else nbNOk++;
210  if(areSame(imPt3.get_u(), kp3.pt.x) && areSame(imPt3.get_v(), kp3.pt.y)) nbOk++; else nbNOk++;
211 
212  std::vector<cv::KeyPoint> listOfKeyPoints(3);
213  listOfKeyPoints[0] = kp1;
214  listOfKeyPoints[1] = kp2;
215  listOfKeyPoints[2] = kp3;
216 
217  std::vector<vpImagePoint> listOfImPts;
218  vpConvert::convertFromOpenCV(listOfKeyPoints, listOfImPts);
219 
220  if(listOfImPts.size() == listOfKeyPoints.size()) {
221  for(size_t i = 0; i < 3; i++) {
222  if(areSame(listOfImPts[i].get_u(), listOfKeyPoints[i].pt.x) && areSame(listOfImPts[i].get_v(), listOfKeyPoints[i].pt.y)) nbOk++; else nbNOk++;
223  }
224  } else {
225  nbNOk += 3;
226  }
227 
228  std::cout << "testConvertFromKeyPointToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
229 #endif
230 }
231 
232 void testConvertFromPoint3fToPoint() {
233 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
234  cv::Point3f pt1(12.5f, .85f, 110.0f), pt2(-44.26f, 125.11f, -98e2f), pt3(0.0f, -1.756e-10f, 0.00015f);
235  vpPoint point1, point2, point3;
236 
237  vpConvert::convertFromOpenCV(pt1, point1);
238  vpConvert::convertFromOpenCV(pt2, point2);
239  vpConvert::convertFromOpenCV(pt3, point3);
240 
241  int nbOk = 0, nbNOk = 0;
242  if(areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ())) nbOk++; else nbNOk++;
243  if(areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ())) nbOk++; else nbNOk++;
244  if(areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ())) nbOk++; else nbNOk++;
245 
246  std::vector<cv::Point3f> listOfPoints3f(3);
247  listOfPoints3f[0] = pt1;
248  listOfPoints3f[1] = pt2;
249  listOfPoints3f[2] = pt3;
250 
251  std::vector<vpPoint> listOfPts;
252  vpConvert::convertFromOpenCV(listOfPoints3f, listOfPts);
253 
254  if(listOfPoints3f.size() == listOfPts.size()) {
255  for(size_t i = 0; i < 3; i++) {
256  if(areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) && areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z)) nbOk++; else nbNOk++;
257  }
258  } else {
259  nbNOk += 3;
260  }
261 
262  std::cout << "testConvertFromPoint3fToPoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
263 #endif
264 }
265 
266 void testConvertFromPointToPoint3f() {
267 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
268  cv::Point3f pt1, pt2, pt3;
269  vpPoint point1, point2, point3;
270  point1.set_oX(12.5f);
271  point1.set_oY(.85f);
272  point1.set_oZ(110.0f);
273 
274  point2.set_oX(-44.26f);
275  point2.set_oY(125.11f);
276  point2.set_oZ(-98e2f);
277 
278  point3.set_oX(0.0f);
279  point3.set_oY(-1.756e-10f);
280  point3.set_oZ(0.00015f);
281 
282  vpConvert::convertToOpenCV(point1, pt1);
283  vpConvert::convertToOpenCV(point2, pt2);
284  vpConvert::convertToOpenCV(point3, pt3);
285 
286  int nbOk = 0, nbNOk = 0;
287  if(areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ())) nbOk++; else nbNOk++;
288  if(areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ())) nbOk++; else nbNOk++;
289  if(areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ())) nbOk++; else nbNOk++;
290 
291  std::vector<cv::Point3f> listOfPoints3f;
292  std::vector<vpPoint> listOfPts(3);
293  listOfPts[0] = point1;
294  listOfPts[1] = point2;
295  listOfPts[2] = point3;
296 
297  vpConvert::convertToOpenCV(listOfPts, listOfPoints3f);
298 
299  if(listOfPoints3f.size() == listOfPts.size()) {
300  for(size_t i = 0; i < 3; i++) {
301  if(areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) && areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z)) nbOk++; else nbNOk++;
302  }
303  } else {
304  nbNOk += 3;
305  }
306 
307  std::cout << "testConvertFromPointToPoint3f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
308 #endif
309 }
310 
311 int main() {
312  testConvertFromImagePointToPoint2f();
313  testConvertFromPoint2fToImagePoint();
314  testConvertFromImagePointToPoint2d();
315  testConvertFromPoint2dToImagePoint();
316 
317  testConvertFromKeyPointToImagePoint();
318  testConvertFromPoint3fToPoint();
319  testConvertFromPointToPoint3f();
320  return 0;
321 }
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
Class that defines what is a point.
Definition: vpPoint.h:59
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:341
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
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
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:213