Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testConvert.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 * Test functions in vpIoTools.
33 *
34*****************************************************************************/
35
44#include <iostream> // std::cout
45#include <limits> // std::numeric_limits
46#include <math.h> // fabs
47
48#include <visp3/core/vpConfig.h>
49#include <visp3/core/vpConvert.h>
50
51bool areSame(double a, double b) { return fabs(a - b) < std::numeric_limits<double>::epsilon(); }
52
53void testConvertFromImagePointToPoint2f()
54{
55#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
56 vpImagePoint imPt1(12.5f, .85f);
57 vpImagePoint imPt2(-44.26f, 125.11f);
58 vpImagePoint imPt3(0.0f, -1.756e-10f);
59
60 cv::Point2f pt1, pt2, pt3;
64
65 int nbOk = 0, nbNOk = 0;
66 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
67 nbOk++;
68 else
69 nbNOk++;
70 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
71 nbOk++;
72 else
73 nbNOk++;
74 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
75 nbOk++;
76 else
77 nbNOk++;
78
79 std::vector<vpImagePoint> listOfImPts(3);
80 listOfImPts[0] = imPt1;
81 listOfImPts[1] = imPt2;
82 listOfImPts[2] = imPt3;
83
84 std::vector<cv::Point2f> listOfPts;
85 vpConvert::convertToOpenCV(listOfImPts, listOfPts);
86
87 if (listOfImPts.size() == listOfPts.size()) {
88 for (size_t i = 0; i < 3; i++) {
89 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
90 nbOk++;
91 else
92 nbNOk++;
93 }
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 defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
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 }
144 else {
145 nbNOk += 3;
146 }
147
148 std::cout << "testConvertFromPoint2fToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
149#endif
150}
151
152void testConvertFromImagePointToPoint2d()
153{
154#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
155 vpImagePoint imPt1(12.5, .85);
156 vpImagePoint imPt2(-44.26, 125.11);
157 vpImagePoint imPt3(0, -1.756e-10);
158
159 cv::Point2d pt1, pt2, pt3;
160 vpConvert::convertToOpenCV(imPt1, pt1);
161 vpConvert::convertToOpenCV(imPt2, pt2);
162 vpConvert::convertToOpenCV(imPt3, pt3);
163
164 int nbOk = 0, nbNOk = 0;
165 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
166 nbOk++;
167 else
168 nbNOk++;
169 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
170 nbOk++;
171 else
172 nbNOk++;
173 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
174 nbOk++;
175 else
176 nbNOk++;
177
178 std::vector<vpImagePoint> listOfImPts(3);
179 listOfImPts[0] = imPt1;
180 listOfImPts[1] = imPt2;
181 listOfImPts[2] = imPt3;
182
183 std::vector<cv::Point2d> listOfPts;
184 vpConvert::convertToOpenCV(listOfImPts, listOfPts);
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))
189 nbOk++;
190 else
191 nbNOk++;
192 }
193 }
194 else {
195 nbNOk += 3;
196 }
197
198 std::cout << "testConvertFromImagePointToPoint2d=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
199#endif
200}
201
202void testConvertFromPoint2dToImagePoint()
203{
204#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
205 vpImagePoint imPt1, imPt2, imPt3;
206
207 cv::Point2d pt1(12.5, .85), pt2(-44.26, 125.11), pt3(0, -1.756e-10);
211
212 int nbOk = 0, nbNOk = 0;
213 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
214 nbOk++;
215 else
216 nbNOk++;
217 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
218 nbOk++;
219 else
220 nbNOk++;
221 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
222 nbOk++;
223 else
224 nbNOk++;
225
226 std::vector<vpImagePoint> listOfImPts;
227
228 std::vector<cv::Point2d> listOfPts(3);
229 listOfPts[0] = pt1;
230 listOfPts[1] = pt2;
231 listOfPts[2] = pt3;
232
233 vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
234
235 if (listOfImPts.size() == listOfPts.size()) {
236 for (size_t i = 0; i < 3; i++) {
237 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
238 nbOk++;
239 else
240 nbNOk++;
241 }
242 }
243 else {
244 nbNOk += 3;
245 }
246
247 std::cout << "testConvertFromPoint2dToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
248#endif
249}
250
251void testConvertFromKeyPointToImagePoint()
252{
253#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
254 cv::KeyPoint kp1(12.5f, .85f, 0), kp2(-44.26f, 125.11f, 0), kp3(0.0f, -1.756e-10f, 0);
255 vpImagePoint imPt1, imPt2, imPt3;
256
260
261 int nbOk = 0, nbNOk = 0;
262 if (areSame(imPt1.get_u(), kp1.pt.x) && areSame(imPt1.get_v(), kp1.pt.y))
263 nbOk++;
264 else
265 nbNOk++;
266 if (areSame(imPt2.get_u(), kp2.pt.x) && areSame(imPt2.get_v(), kp2.pt.y))
267 nbOk++;
268 else
269 nbNOk++;
270 if (areSame(imPt3.get_u(), kp3.pt.x) && areSame(imPt3.get_v(), kp3.pt.y))
271 nbOk++;
272 else
273 nbNOk++;
274
275 std::vector<cv::KeyPoint> listOfKeyPoints(3);
276 listOfKeyPoints[0] = kp1;
277 listOfKeyPoints[1] = kp2;
278 listOfKeyPoints[2] = kp3;
279
280 std::vector<vpImagePoint> listOfImPts;
281 vpConvert::convertFromOpenCV(listOfKeyPoints, listOfImPts);
282
283 if (listOfImPts.size() == listOfKeyPoints.size()) {
284 for (size_t i = 0; i < 3; i++) {
285 if (areSame(listOfImPts[i].get_u(), listOfKeyPoints[i].pt.x) &&
286 areSame(listOfImPts[i].get_v(), listOfKeyPoints[i].pt.y))
287 nbOk++;
288 else
289 nbNOk++;
290 }
291 }
292 else {
293 nbNOk += 3;
294 }
295
296 std::cout << "testConvertFromKeyPointToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
297#endif
298}
299
300void testConvertFromPoint3fToPoint()
301{
302#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
303 cv::Point3f pt1(12.5f, .85f, 110.0f), pt2(-44.26f, 125.11f, -98e2f), pt3(0.0f, -1.756e-10f, 0.00015f);
304 vpPoint point1, point2, point3;
305
306 vpConvert::convertFromOpenCV(pt1, point1);
307 vpConvert::convertFromOpenCV(pt2, point2);
308 vpConvert::convertFromOpenCV(pt3, point3);
309
310 int nbOk = 0, nbNOk = 0;
311 if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
312 nbOk++;
313 else
314 nbNOk++;
315 if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
316 nbOk++;
317 else
318 nbNOk++;
319 if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
320 nbOk++;
321 else
322 nbNOk++;
323
324 std::vector<cv::Point3f> listOfPoints3f(3);
325 listOfPoints3f[0] = pt1;
326 listOfPoints3f[1] = pt2;
327 listOfPoints3f[2] = pt3;
328
329 std::vector<vpPoint> listOfPts;
330 vpConvert::convertFromOpenCV(listOfPoints3f, listOfPts);
331
332 if (listOfPoints3f.size() == listOfPts.size()) {
333 for (size_t i = 0; i < 3; i++) {
334 if (areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) &&
335 areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z))
336 nbOk++;
337 else
338 nbNOk++;
339 }
340 }
341 else {
342 nbNOk += 3;
343 }
344
345 std::cout << "testConvertFromPoint3fToPoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
346#endif
347}
348
349void testConvertFromPointToPoint3f()
350{
351#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
352 cv::Point3f pt1, pt2, pt3;
353 vpPoint point1, point2, point3;
354 point1.set_oX(12.5f);
355 point1.set_oY(.85f);
356 point1.set_oZ(110.0f);
357
358 point2.set_oX(-44.26f);
359 point2.set_oY(125.11f);
360 point2.set_oZ(-98e2f);
361
362 point3.set_oX(0.0f);
363 point3.set_oY(-1.756e-10f);
364 point3.set_oZ(0.00015f);
365
366 vpConvert::convertToOpenCV(point1, pt1);
367 vpConvert::convertToOpenCV(point2, pt2);
368 vpConvert::convertToOpenCV(point3, pt3);
369
370 int nbOk = 0, nbNOk = 0;
371 if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
372 nbOk++;
373 else
374 nbNOk++;
375 if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
376 nbOk++;
377 else
378 nbNOk++;
379 if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
380 nbOk++;
381 else
382 nbNOk++;
383
384 std::vector<cv::Point3f> listOfPoints3f;
385 std::vector<vpPoint> listOfPts(3);
386 listOfPts[0] = point1;
387 listOfPts[1] = point2;
388 listOfPts[2] = point3;
389
390 vpConvert::convertToOpenCV(listOfPts, listOfPoints3f);
391
392 if (listOfPoints3f.size() == listOfPts.size()) {
393 for (size_t i = 0; i < 3; i++) {
394 if (areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) &&
395 areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z))
396 nbOk++;
397 else
398 nbNOk++;
399 }
400 }
401 else {
402 nbNOk += 3;
403 }
404
405 std::cout << "testConvertFromPointToPoint3f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
406#endif
407}
408
409int main()
410{
411 testConvertFromImagePointToPoint2f();
412 testConvertFromPoint2fToImagePoint();
413 testConvertFromImagePointToPoint2d();
414 testConvertFromPoint2dToImagePoint();
415
416 testConvertFromKeyPointToImagePoint();
417 testConvertFromPoint3fToPoint();
418 testConvertFromPointToPoint3f();
419 return EXIT_SUCCESS;
420}
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
double get_u() const
double get_v() const
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:77
double get_oX() const
Get the point oX coordinate in the object frame.
Definition vpPoint.cpp:458
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition vpPoint.cpp:462
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition vpPoint.cpp:501
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition vpPoint.cpp:503
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition vpPoint.cpp:499
double get_oY() const
Get the point oY coordinate in the object frame.
Definition vpPoint.cpp:460