File: testConvert.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (420 lines) | stat: -rw-r--r-- 11,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Test functions in vpIoTools.
 *
*****************************************************************************/

/*!

  \example testConvert.cpp

  \brief Test functions in Convert.

*/

#include <iostream> // std::cout
#include <limits>   // std::numeric_limits
#include <math.h>   // fabs

#include <visp3/core/vpConfig.h>
#include <visp3/core/vpConvert.h>

bool areSame(double a, double b) { return fabs(a - b) < std::numeric_limits<double>::epsilon(); }

void testConvertFromImagePointToPoint2f()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  vpImagePoint imPt1(12.5f, .85f);
  vpImagePoint imPt2(-44.26f, 125.11f);
  vpImagePoint imPt3(0.0f, -1.756e-10f);

  cv::Point2f pt1, pt2, pt3;
  vpConvert::convertToOpenCV(imPt1, pt1);
  vpConvert::convertToOpenCV(imPt2, pt2);
  vpConvert::convertToOpenCV(imPt3, pt3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
    nbOk++;
  else
    nbNOk++;

  std::vector<vpImagePoint> listOfImPts(3);
  listOfImPts[0] = imPt1;
  listOfImPts[1] = imPt2;
  listOfImPts[2] = imPt3;

  std::vector<cv::Point2f> listOfPts;
  vpConvert::convertToOpenCV(listOfImPts, listOfPts);

  if (listOfImPts.size() == listOfPts.size()) {
    for (size_t i = 0; i < 3; i++) {
      if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
        nbOk++;
      else
        nbNOk++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromImagePointToPoint2f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

void testConvertFromPoint2fToImagePoint()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  vpImagePoint imPt1, imPt2, imPt3;

  cv::Point2f pt1(12.5f, .85f), pt2(-44.26f, 125.11f), pt3(0.0f, -1.756e-10f);
  vpConvert::convertFromOpenCV(pt1, imPt1);
  vpConvert::convertFromOpenCV(pt2, imPt2);
  vpConvert::convertFromOpenCV(pt3, imPt3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
    nbOk++;
  else
    nbNOk++;

  std::vector<vpImagePoint> listOfImPts;

  std::vector<cv::Point2f> listOfPts(3);
  listOfPts[0] = pt1;
  listOfPts[1] = pt2;
  listOfPts[2] = pt3;

  vpConvert::convertFromOpenCV(listOfPts, listOfImPts);

  if (listOfImPts.size() == listOfPts.size()) {
    for (size_t i = 0; i < 3; i++) {
      if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
        nbOk++;
      else
        nbNOk++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromPoint2fToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

void testConvertFromImagePointToPoint2d()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  vpImagePoint imPt1(12.5, .85);
  vpImagePoint imPt2(-44.26, 125.11);
  vpImagePoint imPt3(0, -1.756e-10);

  cv::Point2d pt1, pt2, pt3;
  vpConvert::convertToOpenCV(imPt1, pt1);
  vpConvert::convertToOpenCV(imPt2, pt2);
  vpConvert::convertToOpenCV(imPt3, pt3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
    nbOk++;
  else
    nbNOk++;

  std::vector<vpImagePoint> listOfImPts(3);
  listOfImPts[0] = imPt1;
  listOfImPts[1] = imPt2;
  listOfImPts[2] = imPt3;

  std::vector<cv::Point2d> listOfPts;
  vpConvert::convertToOpenCV(listOfImPts, listOfPts);

  if (listOfImPts.size() == listOfPts.size()) {
    for (size_t i = 0; i < 3; i++) {
      if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
        nbOk++;
      else
        nbNOk++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromImagePointToPoint2d=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

void testConvertFromPoint2dToImagePoint()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  vpImagePoint imPt1, imPt2, imPt3;

  cv::Point2d pt1(12.5, .85), pt2(-44.26, 125.11), pt3(0, -1.756e-10);
  vpConvert::convertFromOpenCV(pt1, imPt1);
  vpConvert::convertFromOpenCV(pt2, imPt2);
  vpConvert::convertFromOpenCV(pt3, imPt3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
    nbOk++;
  else
    nbNOk++;

  std::vector<vpImagePoint> listOfImPts;

  std::vector<cv::Point2d> listOfPts(3);
  listOfPts[0] = pt1;
  listOfPts[1] = pt2;
  listOfPts[2] = pt3;

  vpConvert::convertFromOpenCV(listOfPts, listOfImPts);

  if (listOfImPts.size() == listOfPts.size()) {
    for (size_t i = 0; i < 3; i++) {
      if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
        nbOk++;
      else
        nbNOk++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromPoint2dToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

void testConvertFromKeyPointToImagePoint()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  cv::KeyPoint kp1(12.5f, .85f, 0), kp2(-44.26f, 125.11f, 0), kp3(0.0f, -1.756e-10f, 0);
  vpImagePoint imPt1, imPt2, imPt3;

  vpConvert::convertFromOpenCV(kp1, imPt1);
  vpConvert::convertFromOpenCV(kp2, imPt2);
  vpConvert::convertFromOpenCV(kp3, imPt3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(imPt1.get_u(), kp1.pt.x) && areSame(imPt1.get_v(), kp1.pt.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt2.get_u(), kp2.pt.x) && areSame(imPt2.get_v(), kp2.pt.y))
    nbOk++;
  else
    nbNOk++;
  if (areSame(imPt3.get_u(), kp3.pt.x) && areSame(imPt3.get_v(), kp3.pt.y))
    nbOk++;
  else
    nbNOk++;

  std::vector<cv::KeyPoint> listOfKeyPoints(3);
  listOfKeyPoints[0] = kp1;
  listOfKeyPoints[1] = kp2;
  listOfKeyPoints[2] = kp3;

  std::vector<vpImagePoint> listOfImPts;
  vpConvert::convertFromOpenCV(listOfKeyPoints, listOfImPts);

  if (listOfImPts.size() == listOfKeyPoints.size()) {
    for (size_t i = 0; i < 3; i++) {
      if (areSame(listOfImPts[i].get_u(), listOfKeyPoints[i].pt.x) &&
        areSame(listOfImPts[i].get_v(), listOfKeyPoints[i].pt.y))
        nbOk++;
      else
        nbNOk++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromKeyPointToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

void testConvertFromPoint3fToPoint()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  cv::Point3f pt1(12.5f, .85f, 110.0f), pt2(-44.26f, 125.11f, -98e2f), pt3(0.0f, -1.756e-10f, 0.00015f);
  vpPoint point1, point2, point3;

  vpConvert::convertFromOpenCV(pt1, point1);
  vpConvert::convertFromOpenCV(pt2, point2);
  vpConvert::convertFromOpenCV(pt3, point3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
    nbOk++;
  else
    nbNOk++;
  if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
    nbOk++;
  else
    nbNOk++;
  if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
    nbOk++;
  else
    nbNOk++;

  std::vector<cv::Point3f> listOfPoints3f(3);
  listOfPoints3f[0] = pt1;
  listOfPoints3f[1] = pt2;
  listOfPoints3f[2] = pt3;

  std::vector<vpPoint> listOfPts;
  vpConvert::convertFromOpenCV(listOfPoints3f, listOfPts);

  if (listOfPoints3f.size() == listOfPts.size()) {
    for (size_t i = 0; i < 3; i++) {
      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++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromPoint3fToPoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

void testConvertFromPointToPoint3f()
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
  cv::Point3f pt1, pt2, pt3;
  vpPoint point1, point2, point3;
  point1.set_oX(12.5f);
  point1.set_oY(.85f);
  point1.set_oZ(110.0f);

  point2.set_oX(-44.26f);
  point2.set_oY(125.11f);
  point2.set_oZ(-98e2f);

  point3.set_oX(0.0f);
  point3.set_oY(-1.756e-10f);
  point3.set_oZ(0.00015f);

  vpConvert::convertToOpenCV(point1, pt1);
  vpConvert::convertToOpenCV(point2, pt2);
  vpConvert::convertToOpenCV(point3, pt3);

  int nbOk = 0, nbNOk = 0;
  if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
    nbOk++;
  else
    nbNOk++;
  if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
    nbOk++;
  else
    nbNOk++;
  if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
    nbOk++;
  else
    nbNOk++;

  std::vector<cv::Point3f> listOfPoints3f;
  std::vector<vpPoint> listOfPts(3);
  listOfPts[0] = point1;
  listOfPts[1] = point2;
  listOfPts[2] = point3;

  vpConvert::convertToOpenCV(listOfPts, listOfPoints3f);

  if (listOfPoints3f.size() == listOfPts.size()) {
    for (size_t i = 0; i < 3; i++) {
      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++;
    }
  }
  else {
    nbNOk += 3;
  }

  std::cout << "testConvertFromPointToPoint3f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
#endif
}

int main()
{
  testConvertFromImagePointToPoint2f();
  testConvertFromPoint2fToImagePoint();
  testConvertFromImagePointToPoint2d();
  testConvertFromPoint2dToImagePoint();

  testConvertFromKeyPointToImagePoint();
  testConvertFromPoint3fToPoint();
  testConvertFromPointToPoint3f();
  return EXIT_SUCCESS;
}