Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testConvert.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
41
42#include <iostream> // std::cout
43#include <limits> // std::numeric_limits
44#include <math.h> // fabs
45
46#include <visp3/core/vpConfig.h>
47#include <visp3/core/vpConvert.h>
48
49#ifdef ENABLE_VISP_NAMESPACE
50using namespace VISP_NAMESPACE_NAME;
51#endif
52
53bool areSame(double a, double b) { return fabs(a - b) < std::numeric_limits<double>::epsilon(); }
54
55void testConvertFromImagePointToPoint2f()
56{
57#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
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;
66
67 int nbOk = 0, nbNOk = 0;
68 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
69 nbOk++;
70 else
71 nbNOk++;
72 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
73 nbOk++;
74 else
75 nbNOk++;
76 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
77 nbOk++;
78 else
79 nbNOk++;
80
81 std::vector<vpImagePoint> listOfImPts(3);
82 listOfImPts[0] = imPt1;
83 listOfImPts[1] = imPt2;
84 listOfImPts[2] = imPt3;
85
86 std::vector<cv::Point2f> listOfPts;
87 vpConvert::convertToOpenCV(listOfImPts, listOfPts);
88
89 if (listOfImPts.size() == listOfPts.size()) {
90 for (size_t i = 0; i < 3; i++) {
91 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
92 nbOk++;
93 else
94 nbNOk++;
95 }
96 }
97 else {
98 nbNOk += 3;
99 }
100
101 std::cout << "testConvertFromImagePointToPoint2f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
102#endif
103}
104
105void testConvertFromPoint2fToImagePoint()
106{
107#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
108 vpImagePoint imPt1, imPt2, imPt3;
109
110 cv::Point2f pt1(12.5f, .85f), pt2(-44.26f, 125.11f), pt3(0.0f, -1.756e-10f);
114
115 int nbOk = 0, nbNOk = 0;
116 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
117 nbOk++;
118 else
119 nbNOk++;
120 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
121 nbOk++;
122 else
123 nbNOk++;
124 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
125 nbOk++;
126 else
127 nbNOk++;
128
129 std::vector<vpImagePoint> listOfImPts;
130
131 std::vector<cv::Point2f> listOfPts(3);
132 listOfPts[0] = pt1;
133 listOfPts[1] = pt2;
134 listOfPts[2] = pt3;
135
136 vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
137
138 if (listOfImPts.size() == listOfPts.size()) {
139 for (size_t i = 0; i < 3; i++) {
140 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
141 nbOk++;
142 else
143 nbNOk++;
144 }
145 }
146 else {
147 nbNOk += 3;
148 }
149
150 std::cout << "testConvertFromPoint2fToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
151#endif
152}
153
154void testConvertFromImagePointToPoint2d()
155{
156#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
157 vpImagePoint imPt1(12.5, .85);
158 vpImagePoint imPt2(-44.26, 125.11);
159 vpImagePoint imPt3(0, -1.756e-10);
160
161 cv::Point2d pt1, pt2, pt3;
162 vpConvert::convertToOpenCV(imPt1, pt1);
163 vpConvert::convertToOpenCV(imPt2, pt2);
164 vpConvert::convertToOpenCV(imPt3, pt3);
165
166 int nbOk = 0, nbNOk = 0;
167 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
168 nbOk++;
169 else
170 nbNOk++;
171 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
172 nbOk++;
173 else
174 nbNOk++;
175 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
176 nbOk++;
177 else
178 nbNOk++;
179
180 std::vector<vpImagePoint> listOfImPts(3);
181 listOfImPts[0] = imPt1;
182 listOfImPts[1] = imPt2;
183 listOfImPts[2] = imPt3;
184
185 std::vector<cv::Point2d> listOfPts;
186 vpConvert::convertToOpenCV(listOfImPts, listOfPts);
187
188 if (listOfImPts.size() == listOfPts.size()) {
189 for (size_t i = 0; i < 3; i++) {
190 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
191 nbOk++;
192 else
193 nbNOk++;
194 }
195 }
196 else {
197 nbNOk += 3;
198 }
199
200 std::cout << "testConvertFromImagePointToPoint2d=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
201#endif
202}
203
204void testConvertFromPoint2dToImagePoint()
205{
206#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
207 vpImagePoint imPt1, imPt2, imPt3;
208
209 cv::Point2d pt1(12.5, .85), pt2(-44.26, 125.11), pt3(0, -1.756e-10);
213
214 int nbOk = 0, nbNOk = 0;
215 if (areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y))
216 nbOk++;
217 else
218 nbNOk++;
219 if (areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y))
220 nbOk++;
221 else
222 nbNOk++;
223 if (areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y))
224 nbOk++;
225 else
226 nbNOk++;
227
228 std::vector<vpImagePoint> listOfImPts;
229
230 std::vector<cv::Point2d> listOfPts(3);
231 listOfPts[0] = pt1;
232 listOfPts[1] = pt2;
233 listOfPts[2] = pt3;
234
235 vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
236
237 if (listOfImPts.size() == listOfPts.size()) {
238 for (size_t i = 0; i < 3; i++) {
239 if (areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y))
240 nbOk++;
241 else
242 nbNOk++;
243 }
244 }
245 else {
246 nbNOk += 3;
247 }
248
249 std::cout << "testConvertFromPoint2dToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
250#endif
251}
252
253void testConvertFromKeyPointToImagePoint()
254{
255#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
256 cv::KeyPoint kp1(12.5f, .85f, 0), kp2(-44.26f, 125.11f, 0), kp3(0.0f, -1.756e-10f, 0);
257 vpImagePoint imPt1, imPt2, imPt3;
258
262
263 int nbOk = 0, nbNOk = 0;
264 if (areSame(imPt1.get_u(), kp1.pt.x) && areSame(imPt1.get_v(), kp1.pt.y))
265 nbOk++;
266 else
267 nbNOk++;
268 if (areSame(imPt2.get_u(), kp2.pt.x) && areSame(imPt2.get_v(), kp2.pt.y))
269 nbOk++;
270 else
271 nbNOk++;
272 if (areSame(imPt3.get_u(), kp3.pt.x) && areSame(imPt3.get_v(), kp3.pt.y))
273 nbOk++;
274 else
275 nbNOk++;
276
277 std::vector<cv::KeyPoint> listOfKeyPoints(3);
278 listOfKeyPoints[0] = kp1;
279 listOfKeyPoints[1] = kp2;
280 listOfKeyPoints[2] = kp3;
281
282 std::vector<vpImagePoint> listOfImPts;
283 vpConvert::convertFromOpenCV(listOfKeyPoints, listOfImPts);
284
285 if (listOfImPts.size() == listOfKeyPoints.size()) {
286 for (size_t i = 0; i < 3; i++) {
287 if (areSame(listOfImPts[i].get_u(), listOfKeyPoints[i].pt.x) &&
288 areSame(listOfImPts[i].get_v(), listOfKeyPoints[i].pt.y))
289 nbOk++;
290 else
291 nbNOk++;
292 }
293 }
294 else {
295 nbNOk += 3;
296 }
297
298 std::cout << "testConvertFromKeyPointToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
299#endif
300}
301
302void testConvertFromPoint3fToPoint()
303{
304#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
305 cv::Point3f pt1(12.5f, .85f, 110.0f), pt2(-44.26f, 125.11f, -98e2f), pt3(0.0f, -1.756e-10f, 0.00015f);
306 vpPoint point1, point2, point3;
307
308 vpConvert::convertFromOpenCV(pt1, point1);
309 vpConvert::convertFromOpenCV(pt2, point2);
310 vpConvert::convertFromOpenCV(pt3, point3);
311
312 int nbOk = 0, nbNOk = 0;
313 if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
314 nbOk++;
315 else
316 nbNOk++;
317 if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
318 nbOk++;
319 else
320 nbNOk++;
321 if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
322 nbOk++;
323 else
324 nbNOk++;
325
326 std::vector<cv::Point3f> listOfPoints3f(3);
327 listOfPoints3f[0] = pt1;
328 listOfPoints3f[1] = pt2;
329 listOfPoints3f[2] = pt3;
330
331 std::vector<vpPoint> listOfPts;
332 vpConvert::convertFromOpenCV(listOfPoints3f, listOfPts);
333
334 if (listOfPoints3f.size() == listOfPts.size()) {
335 for (size_t i = 0; i < 3; i++) {
336 if (areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) &&
337 areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z))
338 nbOk++;
339 else
340 nbNOk++;
341 }
342 }
343 else {
344 nbNOk += 3;
345 }
346
347 std::cout << "testConvertFromPoint3fToPoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
348#endif
349}
350
351void testConvertFromPointToPoint3f()
352{
353#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_FEATURES2D)
354 cv::Point3f pt1, pt2, pt3;
355 vpPoint point1, point2, point3;
356 point1.set_oX(12.5f);
357 point1.set_oY(.85f);
358 point1.set_oZ(110.0f);
359
360 point2.set_oX(-44.26f);
361 point2.set_oY(125.11f);
362 point2.set_oZ(-98e2f);
363
364 point3.set_oX(0.0f);
365 point3.set_oY(-1.756e-10f);
366 point3.set_oZ(0.00015f);
367
368 vpConvert::convertToOpenCV(point1, pt1);
369 vpConvert::convertToOpenCV(point2, pt2);
370 vpConvert::convertToOpenCV(point3, pt3);
371
372 int nbOk = 0, nbNOk = 0;
373 if (areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ()))
374 nbOk++;
375 else
376 nbNOk++;
377 if (areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ()))
378 nbOk++;
379 else
380 nbNOk++;
381 if (areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ()))
382 nbOk++;
383 else
384 nbNOk++;
385
386 std::vector<cv::Point3f> listOfPoints3f;
387 std::vector<vpPoint> listOfPts(3);
388 listOfPts[0] = point1;
389 listOfPts[1] = point2;
390 listOfPts[2] = point3;
391
392 vpConvert::convertToOpenCV(listOfPts, listOfPoints3f);
393
394 if (listOfPoints3f.size() == listOfPts.size()) {
395 for (size_t i = 0; i < 3; i++) {
396 if (areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) &&
397 areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z))
398 nbOk++;
399 else
400 nbNOk++;
401 }
402 }
403 else {
404 nbNOk += 3;
405 }
406
407 std::cout << "testConvertFromPointToPoint3f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
408#endif
409}
410
411int main()
412{
413 testConvertFromImagePointToPoint2f();
414 testConvertFromPoint2fToImagePoint();
415 testConvertFromImagePointToPoint2d();
416 testConvertFromPoint2dToImagePoint();
417
418 testConvertFromKeyPointToImagePoint();
419 testConvertFromPoint3fToPoint();
420 testConvertFromPointToPoint3f();
421 return EXIT_SUCCESS;
422}
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:79
double get_oX() const
Get the point oX coordinate in the object frame.
Definition vpPoint.cpp:418
double get_oZ() const
Get the point oZ coordinate in the object frame.
Definition vpPoint.cpp:422
void set_oY(double oY)
Set the point oY coordinate in the object frame.
Definition vpPoint.cpp:464
void set_oZ(double oZ)
Set the point oZ coordinate in the object frame.
Definition vpPoint.cpp:466
void set_oX(double oX)
Set the point oX coordinate in the object frame.
Definition vpPoint.cpp:462
double get_oY() const
Get the point oY coordinate in the object frame.
Definition vpPoint.cpp:420