Visual Servoing Platform version 3.5.0
testConvert.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Test functions in vpIoTools.
33 *
34 * Authors:
35 * Souriya Trinh
36 *
37 *****************************************************************************/
38
47#include <iostream> // std::cout
48#include <limits> // std::numeric_limits
49#include <visp3/core/vpConfig.h>
50#include <visp3/core/vpConvert.h>
51
52bool areSame(double a, double b) { return fabs(a - b) < std::numeric_limits<double>::epsilon(); }
53
54void testConvertFromImagePointToPoint2f()
55{
56#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
57 vpImagePoint imPt1(12.5f, .85f);
58 vpImagePoint imPt2(-44.26f, 125.11f);
59 vpImagePoint imPt3(0.0f, -1.756e-10f);
60
61 cv::Point2f pt1, pt2, pt3;
65
66 int nbOk = 0, nbNOk = 0;
67 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
68 nbOk++;
69 else
70 nbNOk++;
71 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
72 nbOk++;
73 else
74 nbNOk++;
75 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
76 nbOk++;
77 else
78 nbNOk++;
79
80 std::vector<vpImagePoint> listOfImPts(3);
81 listOfImPts[0] = imPt1;
82 listOfImPts[1] = imPt2;
83 listOfImPts[2] = imPt3;
84
85 std::vector<cv::Point2f> listOfPts;
86 vpConvert::convertToOpenCV(listOfImPts, listOfPts);
87
88 if (listOfImPts.size() == listOfPts.size()) {
89 for (size_t i = 0; i < 3; i++) {
90 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
91 nbOk++;
92 else
93 nbNOk++;
94 }
95 } else {
96 nbNOk += 3;
97 }
98
99 std::cout << "testConvertFromImagePointToPoint2f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
100#endif
101}
102
103void testConvertFromPoint2fToImagePoint()
104{
105#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
106 vpImagePoint imPt1, imPt2, imPt3;
107
108 cv::Point2f pt1(12.5f, .85f), pt2(-44.26f, 125.11f), pt3(0.0f, -1.756e-10f);
112
113 int nbOk = 0, nbNOk = 0;
114 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
115 nbOk++;
116 else
117 nbNOk++;
118 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
119 nbOk++;
120 else
121 nbNOk++;
122 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
123 nbOk++;
124 else
125 nbNOk++;
126
127 std::vector<vpImagePoint> listOfImPts;
128
129 std::vector<cv::Point2f> listOfPts(3);
130 listOfPts[0] = pt1;
131 listOfPts[1] = pt2;
132 listOfPts[2] = pt3;
133
134 vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
135
136 if (listOfImPts.size() == listOfPts.size()) {
137 for (size_t i = 0; i < 3; i++) {
138 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
139 nbOk++;
140 else
141 nbNOk++;
142 }
143 } else {
144 nbNOk += 3;
145 }
146
147 std::cout << "testConvertFromPoint2fToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
148#endif
149}
150
151void testConvertFromImagePointToPoint2d()
152{
153#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
154 vpImagePoint imPt1(12.5, .85);
155 vpImagePoint imPt2(-44.26, 125.11);
156 vpImagePoint imPt3(0, -1.756e-10);
157
158 cv::Point2d pt1, pt2, pt3;
159 vpConvert::convertToOpenCV(imPt1, pt1);
160 vpConvert::convertToOpenCV(imPt2, pt2);
161 vpConvert::convertToOpenCV(imPt3, pt3);
162
163 int nbOk = 0, nbNOk = 0;
164 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
165 nbOk++;
166 else
167 nbNOk++;
168 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
169 nbOk++;
170 else
171 nbNOk++;
172 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
173 nbOk++;
174 else
175 nbNOk++;
176
177 std::vector<vpImagePoint> listOfImPts(3);
178 listOfImPts[0] = imPt1;
179 listOfImPts[1] = imPt2;
180 listOfImPts[2] = imPt3;
181
182 std::vector<cv::Point2d> listOfPts;
183 vpConvert::convertToOpenCV(listOfImPts, listOfPts);
184
185 if (listOfImPts.size() == listOfPts.size()) {
186 for (size_t i = 0; i < 3; i++) {
187 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
188 nbOk++;
189 else
190 nbNOk++;
191 }
192 } else {
193 nbNOk += 3;
194 }
195
196 std::cout << "testConvertFromImagePointToPoint2d=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
197#endif
198}
199
200void testConvertFromPoint2dToImagePoint()
201{
202#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
203 vpImagePoint imPt1, imPt2, imPt3;
204
205 cv::Point2d pt1(12.5, .85), pt2(-44.26, 125.11), pt3(0, -1.756e-10);
209
210 int nbOk = 0, nbNOk = 0;
211 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
212 nbOk++;
213 else
214 nbNOk++;
215 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
216 nbOk++;
217 else
218 nbNOk++;
219 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
220 nbOk++;
221 else
222 nbNOk++;
223
224 std::vector<vpImagePoint> listOfImPts;
225
226 std::vector<cv::Point2d> listOfPts(3);
227 listOfPts[0] = pt1;
228 listOfPts[1] = pt2;
229 listOfPts[2] = pt3;
230
231 vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
232
233 if (listOfImPts.size() == listOfPts.size()) {
234 for (size_t i = 0; i < 3; i++) {
235 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
236 nbOk++;
237 else
238 nbNOk++;
239 }
240 } else {
241 nbNOk += 3;
242 }
243
244 std::cout << "testConvertFromPoint2dToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
245#endif
246}
247
248void testConvertFromKeyPointToImagePoint()
249{
250#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
251 cv::KeyPoint kp1(12.5f, .85f, 0), kp2(-44.26f, 125.11f, 0), kp3(0.0f, -1.756e-10f, 0);
252 vpImagePoint imPt1, imPt2, imPt3;
253
257
258 int nbOk = 0, nbNOk = 0;
259 if (areSame(imPt1.get_u(), kp1.pt.x) && areSame(imPt1.get_v(), kp1.pt.y))
260 nbOk++;
261 else
262 nbNOk++;
263 if (areSame(imPt2.get_u(), kp2.pt.x) && areSame(imPt2.get_v(), kp2.pt.y))
264 nbOk++;
265 else
266 nbNOk++;
267 if (areSame(imPt3.get_u(), kp3.pt.x) && areSame(imPt3.get_v(), kp3.pt.y))
268 nbOk++;
269 else
270 nbNOk++;
271
272 std::vector<cv::KeyPoint> listOfKeyPoints(3);
273 listOfKeyPoints[0] = kp1;
274 listOfKeyPoints[1] = kp2;
275 listOfKeyPoints[2] = kp3;
276
277 std::vector<vpImagePoint> listOfImPts;
278 vpConvert::convertFromOpenCV(listOfKeyPoints, listOfImPts);
279
280 if (listOfImPts.size() == listOfKeyPoints.size()) {
281 for (size_t i = 0; i < 3; i++) {
282 if (areSame(listOfImPts[i].get_u(), listOfKeyPoints[i].pt.x) &&
283 areSame(listOfImPts[i].get_v(), listOfKeyPoints[i].pt.y))
284 nbOk++;
285 else
286 nbNOk++;
287 }
288 } else {
289 nbNOk += 3;
290 }
291
292 std::cout << "testConvertFromKeyPointToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
293#endif
294}
295
296void testConvertFromPoint3fToPoint()
297{
298#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
299 cv::Point3f pt1(12.5f, .85f, 110.0f), pt2(-44.26f, 125.11f, -98e2f), pt3(0.0f, -1.756e-10f, 0.00015f);
300 vpPoint point1, point2, point3;
301
302 vpConvert::convertFromOpenCV(pt1, point1);
303 vpConvert::convertFromOpenCV(pt2, point2);
304 vpConvert::convertFromOpenCV(pt3, point3);
305
306 int nbOk = 0, nbNOk = 0;
307 if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
308 nbOk++;
309 else
310 nbNOk++;
311 if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
312 nbOk++;
313 else
314 nbNOk++;
315 if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
316 nbOk++;
317 else
318 nbNOk++;
319
320 std::vector<cv::Point3f> listOfPoints3f(3);
321 listOfPoints3f[0] = pt1;
322 listOfPoints3f[1] = pt2;
323 listOfPoints3f[2] = pt3;
324
325 std::vector<vpPoint> listOfPts;
326 vpConvert::convertFromOpenCV(listOfPoints3f, listOfPts);
327
328 if (listOfPoints3f.size() == listOfPts.size()) {
329 for (size_t i = 0; i < 3; i++) {
330 if (areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) &&
331 areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z))
332 nbOk++;
333 else
334 nbNOk++;
335 }
336 } else {
337 nbNOk += 3;
338 }
339
340 std::cout << "testConvertFromPoint3fToPoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
341#endif
342}
343
344void testConvertFromPointToPoint3f()
345{
346#if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
347 cv::Point3f pt1, pt2, pt3;
348 vpPoint point1, point2, point3;
349 point1.set_oX(12.5f);
350 point1.set_oY(.85f);
351 point1.set_oZ(110.0f);
352
353 point2.set_oX(-44.26f);
354 point2.set_oY(125.11f);
355 point2.set_oZ(-98e2f);
356
357 point3.set_oX(0.0f);
358 point3.set_oY(-1.756e-10f);
359 point3.set_oZ(0.00015f);
360
361 vpConvert::convertToOpenCV(point1, pt1);
362 vpConvert::convertToOpenCV(point2, pt2);
363 vpConvert::convertToOpenCV(point3, pt3);
364
365 int nbOk = 0, nbNOk = 0;
366 if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
367 nbOk++;
368 else
369 nbNOk++;
370 if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
371 nbOk++;
372 else
373 nbNOk++;
374 if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
375 nbOk++;
376 else
377 nbNOk++;
378
379 std::vector<cv::Point3f> listOfPoints3f;
380 std::vector<vpPoint> listOfPts(3);
381 listOfPts[0] = point1;
382 listOfPts[1] = point2;
383 listOfPts[2] = point3;
384
385 vpConvert::convertToOpenCV(listOfPts, listOfPoints3f);
386
387 if (listOfPoints3f.size() == listOfPts.size()) {
388 for (size_t i = 0; i < 3; i++) {
389 if (areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) &&
390 areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z))
391 nbOk++;
392 else
393 nbNOk++;
394 }
395 } else {
396 nbNOk += 3;
397 }
398
399 std::cout << "testConvertFromPointToPoint3f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
400#endif
401}
402
403int main()
404{
405 testConvertFromImagePointToPoint2f();
406 testConvertFromPoint2fToImagePoint();
407 testConvertFromImagePointToPoint2d();
408 testConvertFromPoint2dToImagePoint();
409
410 testConvertFromKeyPointToImagePoint();
411 testConvertFromPoint3fToPoint();
412 testConvertFromPointToPoint3f();
413 return 0;
414}
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:226
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:364
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_u() const
Definition: vpImagePoint.h:262
double get_v() const
Definition: vpImagePoint.h:273
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
double get_oX() const
Get the point oX coordinate in the object frame.
Definition: vpPoint.cpp:461
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:465
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition: vpPoint.cpp:504
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition: vpPoint.cpp:506
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition: vpPoint.cpp:502
double get_oY() const
Get the point oY coordinate in the object frame.
Definition: vpPoint.cpp:463